vm and compiler: removed automatic currying, and several other modifications to the language
This commit is contained in:
+16
-30
@@ -17,8 +17,8 @@ type literal =
|
||||
type expression =
|
||||
| Literal of literal
|
||||
| Var of string
|
||||
| Apply of expression * expression
|
||||
| Lambda of string * expression
|
||||
| Apply of expression * expression list
|
||||
| Lambda of string list * string option * expression
|
||||
| If of expression * expression * expression
|
||||
| Set of string * expression
|
||||
| Begin of expression list
|
||||
@@ -35,42 +35,30 @@ let rec pair_of_def : Syntactic_ast.def -> string * expression =
|
||||
and pair_of_binding (s, e) = (s, of_expr e)
|
||||
and pair_of_clause (e1, e2) = (of_expr e1, of_expr e2)
|
||||
|
||||
and make_lambda args body =
|
||||
match args with
|
||||
(* TODO: gensym here instead of using _ directly *)
|
||||
| [] -> Lambda ("_", of_body body)
|
||||
| x :: [] -> Lambda (x, of_body body)
|
||||
| x :: xs -> Lambda (x, make_lambda xs body)
|
||||
|
||||
and make_apply f args =
|
||||
let rec aux f = function
|
||||
| [] -> Apply (f, Literal Nil)
|
||||
| arg :: [] -> Apply (f, arg)
|
||||
| arg :: args -> aux (Apply (f, arg)) args
|
||||
in aux f args
|
||||
and make_lambda (args, rest) body =
|
||||
Lambda (args, rest, body)
|
||||
|
||||
(* desugars this...
|
||||
(let ((x 5) (y 4)) (f x y))
|
||||
... into this...
|
||||
(((lambda (x) (lambda (y) ((f x) y))) 5) 4)
|
||||
((lambda (x y) (f x y)) 5 4)
|
||||
*)
|
||||
and make_let bs body =
|
||||
let bs = List.map pair_of_binding bs in
|
||||
let rec aux = function
|
||||
| (s, e) :: rest ->
|
||||
Apply (Lambda (s, aux rest), e)
|
||||
| [] -> of_body body in
|
||||
aux bs
|
||||
let args = List.map (fun (s, _) -> s) bs in
|
||||
let es = List.map (fun (_, e) -> e) bs in
|
||||
Apply (Lambda (args, None, body), es)
|
||||
|
||||
(* The Core AST does not feature a letrec node. Instead, we desugar letrecs further
|
||||
into a let that binds each symbol to nil, then `set!`s them to their real value
|
||||
before running the body.
|
||||
*)
|
||||
and make_letrec bs exprs =
|
||||
let tmp_bs = List.map (fun (s, _) -> (s, Literal Nil)) bs in
|
||||
let tmp_bs = List.map (fun (_, _) -> Literal Nil) bs in
|
||||
let setters = List.fold_right (fun (s, e) acc -> (Set (s, e)) :: acc) bs [] in
|
||||
let args = List.map (fun (s, _) -> s) bs in
|
||||
let body = Begin ((List.rev setters) @ exprs) in
|
||||
List.fold_right (fun (s, e) acc -> Apply (Lambda (s, acc), e)) tmp_bs body
|
||||
Apply (Lambda (args, None, body), tmp_bs)
|
||||
|
||||
(* We convert a body into a regular letrec form.
|
||||
A body is defined as a series of definitions followed by a series
|
||||
@@ -86,10 +74,8 @@ and of_body : Syntactic_ast.body -> expression = function
|
||||
let defs = List.map pair_of_def defs in
|
||||
make_letrec defs exprs
|
||||
|
||||
(* TODO: currently this ignores the "optional" part of the lambda list,
|
||||
fix this *)
|
||||
and of_ll : Syntactic_ast.lambda_list -> string list = function
|
||||
| (sl, _) -> sl
|
||||
and of_ll : Syntactic_ast.lambda_list -> string list * string option = function
|
||||
| (sl, rest) -> (sl, rest)
|
||||
|
||||
and of_literal : Syntactic_ast.literal -> literal = function
|
||||
| LitInt x -> Int x
|
||||
@@ -102,8 +88,8 @@ and of_literal : Syntactic_ast.literal -> literal = function
|
||||
and of_expr : Syntactic_ast.expr -> expression = function
|
||||
| Literal l -> Literal (of_literal l)
|
||||
| Var x -> Var x
|
||||
| Lambda (ll, b) -> make_lambda (of_ll ll) b
|
||||
| Let (bindings, b) -> make_let bindings b
|
||||
| Lambda ((args, rest), b) -> Lambda (args, rest, of_body b)
|
||||
| Let (bindings, b) -> make_let bindings (of_body b)
|
||||
| LetRec (bindings, b) -> make_letrec (List.map pair_of_binding bindings) [(of_body b)]
|
||||
| Cond (clauses) ->
|
||||
List.fold_right
|
||||
@@ -113,7 +99,7 @@ and of_expr : Syntactic_ast.expr -> expression = function
|
||||
| If (e1, e2, e3) ->
|
||||
If (of_expr e1, of_expr e2, of_expr e3)
|
||||
| Set (s, e) -> Set (s, of_expr e)
|
||||
| Apply (f, es) -> make_apply (of_expr f) (List.map of_expr es)
|
||||
| Apply (f, es) -> Apply (of_expr f, List.map of_expr es)
|
||||
|
||||
|
||||
and of_syntactic : Syntactic_ast.top_level -> top_level = function
|
||||
|
||||
Reference in New Issue
Block a user