Skip to content

Commit 259f7ae

Browse files
committed
tagging release w.t.f.
1 parent e1142df commit 259f7ae

File tree

4 files changed

+63
-35
lines changed

4 files changed

+63
-35
lines changed

README.org

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,29 @@ or use
162162
stack ghci
163163
#+end_src
164164

165+
#+begin_src haskell
166+
-- ast tokens lines of code file name
167+
ast <- runASTParser . tokenize <$> readFileContent "./filename.wile"
168+
169+
-- print $ eval ast [] -- eval v1
170+
-- print $ evalT ast [] -- eval v2
171+
172+
let program = evalM ast -- Monad eval
173+
print $ getVarState program []
174+
175+
-- eg.
176+
--
177+
-- let program2 = evalM ast_1 >> evalM ast_2 >> ...
178+
-- print $ getVarState program2 []
179+
--
180+
-- where `ast_1` could set global vars and `ast_2` could
181+
-- contain a script using the global vars ...
182+
183+
184+
-- or use:
185+
-- print $ runEvalM ast []
186+
#+end_src
187+
165188

166189
*** compile
167190

app/Main.hs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ module Main (main) where
66
import System.Environment (getArgs)
77

88
import WhileParser (tokenize, readFileContent, runASTParser) -- WhileAST(..),
9-
import WhileEvaluation (eval, evalT, evalM, getVST, runEvalM)
10-
--, evalM, VarName, VarVal, VarState, VarStateWorld)
9+
import WhileEvaluation (eval, evalT, evalM, getVarState, runEvalM)
10+
-- VarName, VarVal, VarState, VarStateWorld)
1111

1212

1313
-- | Define a data type to represent the command-line options
14-
newtype Options = Options { getFileName :: FilePath }
14+
newtype Options = Options { getFileName :: FilePath } deriving Show
1515

1616

1717
-- | Parse command-line arguments manually
@@ -26,31 +26,21 @@ runInterpreter options = do
2626

2727
putStrLn $ "\ESC[92m[Interpreting file]\ESC[0m " ++ getFileName options
2828

29-
-- Read the content of the specified file
30-
whileLines <- readFileContent $ getFileName options
31-
let tokens = tokenize whileLines
32-
33-
putStrLn "\ESC[92m[DONE TOKENIZE]\ESC[0m"
34-
-- print tokens
35-
36-
let ast = runASTParser tokens
37-
putStrLn "\ESC[92m[DONE PARSING AST]\ESC[0m"
38-
-- print ast
39-
40-
putStrLn "\ESC[92m[RESULT OF EVALUATION]\ESC[0m"
29+
-- ast tokens lines of code file name
30+
ast <- runASTParser . tokenize <$> (readFileContent . getFileName) options
4131

4232
-- print $ eval ast [] -- eval v1
4333
-- print $ evalT ast [] -- eval v2
4434

4535
let program = evalM ast -- Monad eval
46-
print $ getVST program []
36+
print $ getVarState program []
4737

4838
-- eg.
49-
-- let program2 = evalM ast >> evalM ast
50-
-- print $ getVST program2 []
39+
-- let program2 = evalM ast_1 >> evalM ast_2 >> ...
40+
-- print $ getVarState program2 []
5141

5242
-- or use:
53-
-- print $ runEvalM ast []
43+
-- print $ runEvalM ast []
5444

5545
putStrLn $ "\n" ++ concat (replicate 40 "= ") ++ "=\n"
5646

src/WhileEvaluation.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ infixl 1 >>>=
3939
vst >>>= f = uncurry f . vst
4040

4141
-- | Variable State Monade
42-
newtype VarStateM a = VarStateM { getVST :: VarStateT a } deriving Functor
42+
newtype VarStateM a = VarStateM { getVarState :: VarStateT a } deriving Functor
4343

4444
instance Applicative VarStateM where
4545
pure x = VarStateM (x,)
4646

47-
vstf <*> vst = VarStateM (getVST vstf
48-
>>>= \f -> getVST vst
49-
>>>= \x -> getVST (pure (f x)))
47+
vstf <*> vst = VarStateM (getVarState vstf
48+
>>>= \f -> getVarState vst
49+
>>>= \x -> getVarState (pure (f x)))
5050

5151
instance Monad VarStateM where
52-
vst >>= f = VarStateM (getVST vst >>>= getVST . f)
52+
vst >>= f = VarStateM (getVarState vst >>>= getVarState . f)
5353

5454

5555
-- getState :: VarStateM VarStateWorld
@@ -129,4 +129,4 @@ evalM = VarStateM . evalT
129129

130130

131131
runEvalM :: WhileAST -> VarStateWorld -> ((), VarStateWorld)
132-
runEvalM ast = getVST $ evalM ast
132+
runEvalM ast = getVarState $ evalM ast

test/Spec.hs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66
import Test.Tasty
77
import Test.Tasty.HUnit
88

9-
import WhileParser (WhileAST(..), MetaToken(..), LineNum, Token(..), Expression(..), tokenize, readFileContent, runASTParser)
10-
import WhileEvaluation (evalM, VarName, VarVal, VarState, VarStateWorld)
9+
import WhileParser
10+
(WhileAST(..)
11+
,MetaToken(..)
12+
,LineNum
13+
,Token(..)
14+
,Expression(..)
15+
,tokenize
16+
,readFileContent
17+
,runASTParser
18+
)
1119

20+
import WhileEvaluation
21+
(evalM
22+
,VarName
23+
,VarVal
24+
,VarState
25+
,VarStateWorld
26+
)
1227

1328
main :: IO ()
1429
main = defaultMain $ testGroup "0. ./src/WhileParser.hs" [tokenizerTest0, parserTest0]
@@ -49,7 +64,7 @@ tokenizerTest0 = testGroup "tokenizer" [
4964
MetaToken 5 (ConstToken 1),
5065
MetaToken 5 EndToken]
5166

52-
, testCase "tokenize .while file" $ show (10 + 10) @?= "20"
67+
, testCase "tokenize .while file" $ (show (10 + 10) :: String) @?= "20"
5368
-- whileLines <- readFileContent "./examples/foo.while"
5469
-- let tokens = tokenize whileLines
5570
]
@@ -66,11 +81,11 @@ parserTest0 = testGroup "parser" [
6681
(5, "loop 8 do i := i + 1 end")])
6782
@?=
6883
Sequential
69-
(Assignment "x_1" (Constant 10))
70-
(Sequential
71-
(While
72-
(Neq (Variable "x") (Constant 0))
73-
(Assignment "x" (Subtract (Variable "x") (Constant 1))))
74-
(Loop
75-
(Constant 8) (Assignment "i" (Add (Variable "i") (Constant 1)))))
84+
(Assignment "x_1" (Constant 10))
85+
(Sequential
86+
(While
87+
(Neq (Variable "x") (Constant 0))
88+
(Assignment "x" (Subtract (Variable "x") (Constant 1))))
89+
(Loop
90+
(Constant 8) (Assignment "i" (Add (Variable "i") (Constant 1)))))
7691
]

0 commit comments

Comments
 (0)