File tree Expand file tree Collapse file tree 3 files changed +19
-3
lines changed
Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ $S = Prog$
3535
3636$P = \{$
3737
38+ $Prog \to // comment \dots$
39+
3840$Prog \to Prog; Prog$
3941
4042$Prog \to Var := Expr$
@@ -83,11 +85,17 @@ cat ./examples/multiplication-func.while
8385
8486#+RESULTS:
8587#+begin_example
88+ // init var state
8689rv := 0;
8790a := 420;
8891b := 69;
8992
93+ // loop will be exec b-times
9094while b != 0 do
95+
96+ // calculate rv += a (b-times)
97+ // -> a * b = a + a + ... b-times
98+
9199 temp := rv;
92100 loop a do temp := temp + 1 end;
93101 rv := temp;
Original file line number Diff line number Diff line change 1+ // init var state
12rv := 0;
23a := 420;
34b := 69;
45
6+ // loop will be exec b-times
57while b != 0 do
8+
9+ // calculate rv += a
10+ // -> a * b = a + a + ... (b-times)
11+
612 temp := rv;
713 loop a do temp := temp + 1 end;
814 rv := temp;
Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ module WhileParser
2020
2121import Control.Applicative
2222import Control.Monad
23- import Data.Char (isDigit , isAlpha )
24- import Data.List (stripPrefix )
23+ import Data.Char (isDigit , isAlpha , isSpace )
24+ import Data.List (stripPrefix , isPrefixOf )
2525
2626data Token = VarToken { getVarName :: String } -- e.g. x_1 (has to start with letter - can contain digit and '_')
2727 | ConstToken { getConstInt :: Int } -- e.g. 5 (positive int with 0 only)
@@ -116,7 +116,9 @@ tokenParser t = Parser $ \case
116116readFileContent :: FilePath -> IO [(LineNum , String )]
117117readFileContent filePath = putStrLn (" \ESC [92m[READING .WHILE FILE]\ESC [0m " ++ show filePath)
118118 >> readFile filePath
119- >>= \ c -> return $ zip [1 .. ] (lines c)
119+ >>= \ c -> return $ filterComments $ zip [1 .. ] (lines c)
120+ where
121+ filterComments = filter (not . isPrefixOf " //" . dropWhile isSpace . snd )
120122
121123-- begin parser expression ----------------------------------------------------
122124
You can’t perform that action at this time.
0 commit comments