Skip to content

Commit e60220c

Browse files
committed
added support for comments in .while files
1 parent 81c7151 commit e60220c

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.org

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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
8689
rv := 0;
8790
a := 420;
8891
b := 69;
8992

93+
// loop will be exec b-times
9094
while 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;

examples/multiplication-func.while

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
// init var state
12
rv := 0;
23
a := 420;
34
b := 69;
45

6+
// loop will be exec b-times
57
while 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;

src/WhileParser.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module WhileParser
2020

2121
import Control.Applicative
2222
import 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

2626
data 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
116116
readFileContent :: FilePath -> IO [(LineNum, String)]
117117
readFileContent 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

0 commit comments

Comments
 (0)