diff --git a/bin/comp.ml b/bin/comp.ml index 656ec1e..322f03c 100644 --- a/bin/comp.ml +++ b/bin/comp.ml @@ -20,7 +20,7 @@ and dbg_print_start = function let def = Parser.parse_str "(define (f) (let ((x 5)) - (cond ((+ x 1))))) + (if t (+ x 1)))) (define (f) (define (g y) (* y 2)) (or (g 5) (g 6))) diff --git a/lib/compiler/syntactic_ast.ml b/lib/compiler/syntactic_ast.ml index 677bb28..c7b7bd5 100644 --- a/lib/compiler/syntactic_ast.ml +++ b/lib/compiler/syntactic_ast.ml @@ -34,6 +34,8 @@ type _ t = | CondClause : expression t * expression t -> clause t | Cond : clause t list -> expression t + | If : expression t * expression t * expression t -> expression t + | Var : symbol -> expression t | Apply : expression t * expression t list -> expression t @@ -188,6 +190,18 @@ and builtin_cond cons = let* clauses = parse_clauses clauses in exp (Cond clauses) +and builtin_if cons = + let* cons = sexpr_cdr cons in + let* test = sexpr_car cons in + let* test = unwrap_exp (transform test) in + let* then_branch = sexpr_cadr cons in + let* then_branch = unwrap_exp (transform then_branch) in + let* else_branch = (match sexpr_caddr cons with + | Error _ -> Ok LNil + | Ok x -> Ok x) in + let* else_branch = unwrap_exp (transform else_branch) in + exp (If (test, then_branch, else_branch)) + and apply f args = let* args = list_of_sexpr args in let* args = Result.map_l (fun x -> unwrap_exp (transform x)) args in @@ -200,6 +214,7 @@ and builtin_symbol = function | "let" -> (make_builtin_let (fun x y -> Let (x,y))) | "letrec" -> (make_builtin_let (fun x y -> LetRec (x,y))) | "cond" -> builtin_cond + | "if" -> builtin_if | _ -> (function | LCons (f, args) -> apply f args | _ -> Error "Invalid function application!") @@ -288,6 +303,8 @@ and print_expr = function %s)" (print_clauses clauses) | Var s -> s + | If (e1, e2, e3) -> + pf "(if %s %s %s)" (print_expr e1) (print_expr e2) (print_expr e3) | Apply (f, exprs) -> pf "(apply %s %s)" (print_expr f) diff --git a/lib/compiler/syntactic_ast.mli b/lib/compiler/syntactic_ast.mli index 6db8cb3..d68b50f 100644 --- a/lib/compiler/syntactic_ast.mli +++ b/lib/compiler/syntactic_ast.mli @@ -28,6 +28,8 @@ type _ t = | CondClause : expression t * expression t -> clause t | Cond : clause t list -> expression t + | If : expression t * expression t * expression t -> expression t + | Var : symbol -> expression t | Apply : expression t * expression t list -> expression t