summaryrefslogtreecommitdiff
path: root/paper/While/Simple.icl
diff options
context:
space:
mode:
authorCamil Staps2016-05-10 21:12:44 +0200
committerCamil Staps2016-05-10 21:12:44 +0200
commitd2b12fdf5681a71f1f6a295b0591e4d8213fb41d (patch)
treec242637b4491ce3d1a44b6f8b51f398f20fc0daf /paper/While/Simple.icl
parentMakefile: bibtex may fail (diff)
Straightforward While lexer, parser and interpreter
Diffstat (limited to 'paper/While/Simple.icl')
-rw-r--r--paper/While/Simple.icl22
1 files changed, 22 insertions, 0 deletions
diff --git a/paper/While/Simple.icl b/paper/While/Simple.icl
new file mode 100644
index 0000000..9138f1f
--- /dev/null
+++ b/paper/While/Simple.icl
@@ -0,0 +1,22 @@
+implementation module Simple
+
+import StdString
+
+import WhileCommon
+import Common
+
+instance toString Stm
+where
+ toString (Ass v a) = v <+ " := " <+ a
+ toString (If b s1 s2) = "if " <+ b <+ " then " <+ s1 <+ " else " <+ s2
+ toString (While b s) = "while " <+ b <+ " do " <+ s
+ toString Skip = "skip"
+ toString (Compose s1 s2) = s1 <+ "; " <+ s2
+
+instance run Stm
+where
+ run (Ass v e) st = eval e st >>= \r -> pure (\w -> if (w==v) (pure r) (st w))
+ run (If b s1 s2) st = eval b st >>= \r -> run (if r s1 s2) st
+ run w=:(While b s) st = eval b st >>= \r -> if r (run s st >>= \st` -> run w st`) (pure st)
+ run Skip st = pure st
+ run (Compose s1 s2) st = run s1 st >>= \st` -> run s2 st`