aboutsummaryrefslogtreecommitdiff
path: root/discocaml/ast.ml
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-01-23 20:53:31 -0600
committerNathan Ringo <nathan@remexre.com>2024-01-23 20:53:31 -0600
commit6b1b1abafff15c5021d33689335e9b792c1873d4 (patch)
tree64c849f6f1b3149dcdd4e548620e4f66553a8c20 /discocaml/ast.ml
parent8b5dab508800c08a11a255280798bd4b245e0818 (diff)
let expressions
Diffstat (limited to 'discocaml/ast.ml')
-rw-r--r--discocaml/ast.ml29
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