Last active
December 22, 2015 14:48
-
-
Save arjunguha/6487770 to your computer and use it in GitHub Desktop.
Code from Friday, September 6th
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Arith_syntax | |
let rec subst (x : id) (v : int) (e : exp) : exp = | |
match e with | |
| Int _ -> e | |
| Add (e1, e2) -> Add (subst x v e1, subst x v e2) | |
| Mul (e1, e2) -> Mul (subst x v e1, subst x v e2) | |
| Let (y, e1, e2) -> | |
Let (y, subst x v e1, if x = y then e2 else subst x v e2) | |
| Id y -> if x = y then Int v else Id y | |
let rec eval (e : exp) : int = | |
match e with | |
| Int n -> n | |
| Add (e1, e2) -> eval e1 + eval e2 | |
| Mul (e1, e2) -> eval e1 * eval e2 | |
| Let (x, e1, e2) -> eval (subst x (eval e1) e2) | |
| Id x -> failwith ("unbound identifier " ^ x) | |
let rec repl () = | |
print_string "> "; | |
let str = read_line () in | |
match Arith_util.parse str with | |
| Arith_util.Exp exp -> | |
let _ = print_int (eval exp) in | |
let _ = print_newline () in | |
repl () | |
| Arith_util.ParseError msg -> | |
let _ = print_string msg in | |
let _ = print_newline () in | |
repl () | |
let _ = | |
match Array.to_list Sys.argv with | |
| [ exe; "repl" ] -> repl () | |
| _ -> () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment