Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Sexplib.Sexp
- let rec parse_bindings bindings =
- match bindings with
- | [] -> []
- | first::rest ->
- match first with
- | List([Atom(name); e]) ->
- [(name, parse e)] @ (parse_bindings rest)
- let rec parse sexp : expr =
- match sexp with
- | Atom(s) ->
- (match int_of_string_opt s with
- | Some(num) -> ENumber(num)
- | None -> if mem s reserved_words then failwith "s is a reserved keyword" else EId(s))
- | List(sexps) ->
- match sexps with
- | [Atom("add1"); arg] -> EPrim1(Add1, parse arg)
- | [Atom("sub1"); arg] -> EPrim1(Sub1, parse arg)
- | [Atom("+"); arg1; arg2] -> EPrim2(Plus, parse arg1, parse arg2)
- | [Atom("-"); arg1; arg2] -> EPrim2(Minus, parse arg1, parse arg2)
- | [Atom("*"); arg1; arg2] -> EPrim2(Times, parse arg1, parse arg2)
- | [Atom("let"); List(bindings_sexp); e2] ->
- let binding_expr = parse_bindings bindings in
- ELet(binding_expr, parse e2)
- | _ -> failwith "foo"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement