-
Notifications
You must be signed in to change notification settings - Fork 1
/
Grep.hs
29 lines (24 loc) · 899 Bytes
/
Grep.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- Demo: Generating LLVM code to match regular expressions
-- grep alike (last line)
import Control.Monad
import qualified Data.ByteString as BS
import Data.Char (ord)
import LLVM.Core
import LLVM.ExecutionEngine
import LLVM.Util.File
import System.Environment
import System.IO.Unsafe
import Text.RegExp.LLVM
import Text.RegExp.LLVM.Parser
main :: IO ()
main = do
args <- getArgs
when (null args) $ ioError (userError "Usage: ./RegExpLLVM regexp < file")
regex <- liftM (parse . head) getArgs
let matcherCode = regexMatcher regex
writeCodeGenModule "matcher.bc" matcherCode
initializeNativeTarget
matches <- liftM ((unsafePerformIO.) . runMatcher) (simpleFunction matcherCode)
input <- BS.getContents
forM_ (zip [1..] $ BS.split (fromIntegral (ord '\n')) input) $ \(ix, line) -> do
when (matches line) $ putStrLn $ "Line " ++ show ix ++ " matches"