diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-02-18 17:35:13 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-02-18 17:35:13 +1300 |
| commit | 77d84c1539851f096b2c632cc6381ac72b2bfd1b (patch) | |
| tree | dcdb7e649e481f40a389c07c1d6664961d43d8ca | |
| parent | 28ab4a097fb73a138850a3fb7f6e480765c702ee (diff) | |
Implement multline comments
| -rw-r--r-- | DSL/Parsing.hs | 14 | ||||
| -rw-r--r-- | DSL/Types.hs | 1 | ||||
| -rw-r--r-- | examples/fizzbuzz.dumb | 6 |
3 files changed, 19 insertions, 2 deletions
diff --git a/DSL/Parsing.hs b/DSL/Parsing.hs index fad68f1..6dcc4bf 100644 --- a/DSL/Parsing.hs +++ b/DSL/Parsing.hs @@ -19,6 +19,12 @@ fromTableL table = firstOf $ map (uncurry fromStringL) table wsL :: DSLLexer wsL = buildDSLLexer (mult1 $ satisfy isSpace) T_WHITESPACE +commentL :: DSLLexer +commentL = buildDSLLexer go T_COMMENT + where + delim = list "'''" + go = delim *> (fmap fst $ stopAfter delim) + keywordL :: DSLLexer keywordL = fromTableL [ ("PROC", T_PROC) , ("IF", T_IF) @@ -81,14 +87,18 @@ intrinsicL = fromTableL [ (".", T_INTRINSIC I_DUMP) ] lexemeL :: DSLLexer -lexemeL = firstOf [ keywordL +lexemeL = firstOf [ commentL + , keywordL , literalL , intrinsicL , identifierL ] tokenizer :: Parser Char [Token] -tokenizer = optional wsL *> lexemeL `sepBy` wsL <* optional wsL +tokenizer = filter f <$> go + where + go = optional wsL *> lexemeL `sepBy` wsL <* optional wsL + f Token { tTag=t } = t /= T_COMMENT ------------------------------------------------------------------------------ -- Parsing diff --git a/DSL/Types.hs b/DSL/Types.hs index 7bd9f56..dbed203 100644 --- a/DSL/Types.hs +++ b/DSL/Types.hs @@ -75,6 +75,7 @@ data Machine = Machine { ok :: Bool data TokenTag = T_WHITESPACE + | T_COMMENT | T_PROC | T_IF | T_ELSE diff --git a/examples/fizzbuzz.dumb b/examples/fizzbuzz.dumb index 43b4d82..2ba6033 100644 --- a/examples/fizzbuzz.dumb +++ b/examples/fizzbuzz.dumb @@ -1,8 +1,14 @@ +''' bottom of the stack is the incrementing value, starts at 1 ''' 1 +''' while the incrementing value is <= 100 ''' WHILE 100 OVER <= DO + ''' push string "Buzz" if inc. is divisible by 5, else an empty string ''' 5 OVER % IF 0 == DO "Buzz" ELSE "" END + ''' push string "Fizz" if inc. is divisible by 3, else an empty string ''' OVER 3 SWAP % IF 0 == DO "Fizz" ELSE "" END + ''' concat the two strings and copy the inc., if the string is empty, swap them ''' ++ OVER SWAP IF DUP "" == DO SWAP END + ''' print the top of the stack, drop the next item, add 1 to inc. ''' . DROP 1 + END |
