diff options
Diffstat (limited to 'discocaml/ast.ml')
-rw-r--r-- | discocaml/ast.ml | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/discocaml/ast.ml b/discocaml/ast.ml index 9436b69..025fa24 100644 --- a/discocaml/ast.ml +++ b/discocaml/ast.ml @@ -23,6 +23,7 @@ and expr = | Cons of expr index * expr index | Int of int | Lam of string * expr index + | Let of bool * string * expr index * expr index | Nil | Prim : 'a prim * 'a -> expr | Var of string @@ -85,6 +86,20 @@ let add_expr_to_ast (ast : 'a ast) : Parsetree.expression -> expr index = | Pexp_ident { txt = Lident "-"; _ } -> binop Sub | Pexp_ident { txt = Lident "*"; _ } -> binop Mul | Pexp_ident { txt = Lident name; _ } -> add (Var name) + | Pexp_let + ( recursive, + [ + { + pvb_pat = { ppat_desc = Ppat_var { txt = name; _ }; _ }; + pvb_expr; + _; + }; + ], + body ) -> + let recursive = + match recursive with Nonrecursive -> false | Recursive -> true + in + add (Let (recursive, name, loop pvb_expr, loop body)) | _ -> raise_unsupported_expr expr in loop @@ -154,6 +169,20 @@ let parsetree_of_subexpr (ast : 'a ast) : expr -> Parsetree.expression = None, Wrap.pattern (Ppat_var (Wrap.var x)), loop (subexpr b) )) + | Let (recursive, name, bound, body) -> + Wrap.expression + (Pexp_let + ( (if recursive then Recursive else Nonrecursive), + [ + { + pvb_pat = Wrap.pattern (Ppat_var (Wrap.var name)); + pvb_expr = loop (subexpr bound); + pvb_constraint = None; + pvb_attributes = []; + pvb_loc = Location.none; + }; + ], + loop (subexpr body) )) | Nil -> Wrap.expression (Pexp_construct (Wrap.ident "[]", None)) | Prim (Add, (l, r)) -> Wrap.expression |