@@ -83,6 +83,20 @@ let rec compile_expr expr state =
83
83
| Cdr expr ->
84
84
let (tuple_expr, f) = compile_expr expr state in
85
85
(tuple_expr @ [CDR ]), f
86
+ | Letrec (bindings , body ) ->
87
+ let n = List. length bindings in
88
+ let names = List. map ~f: fst bindings in
89
+ let rec_env = push_vars state.env names in
90
+ let rec_state = {functions = state.functions; env = rec_env} in
91
+ let (vals_code, s1) = List. fold_left bindings
92
+ ~init: ([] , rec_state)
93
+ ~f: (fun (code , state ) (_name , e ) ->
94
+ let (c, s1) = compile_expr e state in
95
+ (code @ c, s1))
96
+ in
97
+ let id = Address. create () in
98
+ let s2 = compile_func id names body s1 in
99
+ DUM n :: vals_code @ [LDF id] @ [RAP n], s2
86
100
87
101
and compile_func id formals expr {functions; env} =
88
102
let e_env = push_vars env formals in
@@ -131,6 +145,8 @@ let assemble instructions =
131
145
| SEL (addr1 , addr2 ) -> sprintf " SEL %d %d" (resolve addr1) (resolve addr2)
132
146
| LDF addr -> sprintf " LDF %d" (resolve addr)
133
147
| AP n -> sprintf " AP %d" n
148
+ | DUM n -> sprintf " DUM %d" n
149
+ | RAP n -> sprintf " RAP %d" n
134
150
| _ ->
135
151
let sexp = Sexp. to_string (sexp_of_instruction instruction) in
136
152
failwithf " unsupported instruction %s" sexp ()
0 commit comments