This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
cookast.ml
53 lines (46 loc) · 2.04 KB
/
cookast.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
open Pretty
open Ast
exception Error of string
let named_macros:(string, Parsetree.macro) Hashtbl.t = Hashtbl.create 10
let extract_strings args =
let restarg, atoms =
if args == [] then Parsetree.RestNil, [] else
match List.hd (List.rev args) with
Parsetree.Atom(Parsetree.RestArgs s) -> Parsetree.RestVar s,
List.rev (List.tl (List.rev args))
| _ -> Parsetree.RestNil, args in
let args = List.map (fun i ->
match i with
Parsetree.Atom(Parsetree.Symbol s) -> s
| _ -> raise (Error "Bad argument in defn"))
atoms in
args, restarg
let parse_defn_form = function
Parsetree.Atom(Parsetree.Symbol(sym))::Parsetree.Array(v)::
Parsetree.Atom(Parsetree.String(_))::body |
Parsetree.Atom(Parsetree.Symbol(sym))::Parsetree.Array(v)::body ->
if body == [] then
raise (Error "Empty function definition");
let args, restarg = extract_strings v in
Parsetree.Defn(sym, args, restarg, body)
| se -> raise (Error ("Malformed defn form:" ^ ppsexprl se))
let parse_defmacro_form = function
Parsetree.Atom(Parsetree.Symbol(sym))::Parsetree.Array(v)::
Parsetree.Atom(Parsetree.String(_))::body |
Parsetree.Atom(Parsetree.Symbol(sym))::Parsetree.Array(v)::body ->
if body == [] then
raise (Error "Empty macro definition");
let args, restarg = extract_strings v in
Parsetree.Defmacro(sym, args, restarg, body)
| se -> raise (Error ("Malformed defmacro form:" ^ ppsexprl se))
let parse_def_form = function
[Parsetree.Atom(Parsetree.Symbol(sym)); expr] -> Parsetree.Def(sym, expr)
| se -> raise (Error ("Malformed def form:" ^ ppsexprl se))
let cook_toplevel sexpr = match sexpr with
Parsetree.List(Parsetree.Atom(Parsetree.Symbol(sym))::s2) ->
(match sym with
"defn" -> parse_defn_form s2
| "def" -> parse_def_form s2
| "defmacro" -> parse_defmacro_form s2
| _ -> Parsetree.AnonCall [sexpr])
| _ -> Parsetree.AnonCall [sexpr]