summaryrefslogtreecommitdiff
path: root/DSL/Parsing.hs
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-02-18 16:22:54 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-02-18 16:22:54 +1300
commitf8a928d18371e0b67741f5d75b8154d1c105327b (patch)
tree78853084e99e2a0cc7fbc0112ef9c7010ec53e1e /DSL/Parsing.hs
parent69276220df02d2c226021d79ee4a4fd173ae85ee (diff)
Introduce procs
Diffstat (limited to 'DSL/Parsing.hs')
-rw-r--r--DSL/Parsing.hs30
1 files changed, 25 insertions, 5 deletions
diff --git a/DSL/Parsing.hs b/DSL/Parsing.hs
index 76acacd..d255c35 100644
--- a/DSL/Parsing.hs
+++ b/DSL/Parsing.hs
@@ -20,7 +20,8 @@ wsL :: DSLLexer
wsL = buildDSLLexer (mult1 $ satisfy isSpace) T_WHITESPACE
keywordL :: DSLLexer
-keywordL = fromTableL [ ("IF", T_IF)
+keywordL = fromTableL [ ("PROC", T_PROC)
+ , ("IF", T_IF)
, ("ELSE", T_ELSE)
, ("WHILE", T_WHILE)
, ("DO", T_DO)
@@ -53,6 +54,11 @@ literalL = firstOf [ intLiteralL
, stringLiteralL
]
+identifierL :: DSLLexer
+identifierL = buildDSLLexer go T_IDENTIFIER
+ where
+ go = mult1 $ satisfy $ not . isSpace
+
intrinsicL :: DSLLexer
intrinsicL = fromTableL [ (".", T_INTRINSIC I_DUMP)
, ("DROP", T_INTRINSIC I_DROP)
@@ -77,6 +83,7 @@ lexemeL :: DSLLexer
lexemeL = firstOf [ keywordL
, literalL
, intrinsicL
+ , identifierL
]
tokenizer :: Parser Char [Token]
@@ -112,9 +119,13 @@ intrinsicP = do
T_INTRINSIC i -> result $ OpIntrinsic i
_ -> flunk
+callP :: DSLParser Operation
+callP = OpCall . tStr <$> tagP T_IDENTIFIER
+
operationP :: DSLParser Operation
operationP = firstOf [ pushDataP
, intrinsicP
+ , callP
]
linearP :: DSLParser Block
@@ -139,6 +150,12 @@ whileP = do
b <- mult blockP <* tagP T_END
return $ BWhile c b
+procP :: DSLParser ProcSpec
+procP = front `plus` back
+ where
+ front = tagP T_PROC >> tStr <$> tagP T_IDENTIFIER
+ back = mult blockP <* tagP T_END
+
blockP :: DSLParser Block
blockP = firstOf [ whileP
, ifElseP
@@ -146,9 +163,12 @@ blockP = firstOf [ whileP
, linearP
]
-programP :: DSLParser Program
-programP = phrase $ mult1 blockP
+programP :: DSLParser ([ProcSpec], Program)
+programP = phrase $ procs `plus` code
+ where
+ procs = mult procP
+ code = mult1 blockP
-stringToProgram :: String -> Maybe Program
-stringToProgram = fmap snd . parse (chain tokenizer programP)
+stringToProgram :: String -> Maybe ([ProcSpec], Program)
+stringToProgram = fmap snd . parse (tokenizer `chain` programP)