Commits

Maxime Buquet  committed 340af60

Basic version of brainfuck interpreteur,
Type and Eval loop done

  • Participants
  • Parent commits ab4aad2

Comments (0)

Files changed (1)

+
+module Env = Zip.Make (struct type t = int let zero = 0 end)
+
+type t =
+    | Plus
+    | Minus
+    | Right
+    | Left
+    | Print
+    | Read
+    | Loop of t list
+
+let rec eval env = function
+    | Plus :: tl ->
+            let v = Env.pop env in
+            eval (Env.push (v + 1) env) tl
+    | Minus :: tl ->
+            let v = Env.pop env in
+            eval (Env.push (v - 1) env) tl
+    | Right :: tl ->
+            eval (Env.right env) tl
+    | Left :: tl ->
+            eval (Env.left env) tl
+    | Print :: tl ->
+            let () = Printf.printf "%c%!" (Char.chr (Env.pop env)) in
+                eval env tl
+    | Read :: tl ->
+            let push c = eval (Env.push (Char.code c) env) tl in
+                Scanf.scanf "\nEnter character: %c" push
+    | Loop loop :: tl ->
+            let env = eval env loop in
+                if Env.pop env = 0 then
+                    eval env tl
+                else
+                    eval env (Loop loop :: tl)
+    | [] -> env