forked from artagnon/rhine-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
93 lines (83 loc) · 2.14 KB
/
parser.mly
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
%{ open Ast
let (--) i j =
let rec aux n acc =
if n < i then acc else aux (n-1) (n :: acc)
in aux j []
%}
%token LPAREN RPAREN LSQBR RSQBR NIL TRUE FALSE REST_ARGS EOF
%token <int> SQUOTE
%token <int> UNQUOTE
%token <int> INTEGER
%token <float> DOUBLE
%token <string> SYMBOL
%token <char> CHAR
%token <string> STRING
%start prog
%type <Ast.prog> prog
%type <Ast.atom> atom
%type <Ast.sexpr> sexpr
%%
prog:
sexprs EOF { Prog($1) }
atom:
NIL { Nil }
| TRUE { Bool(true) }
| FALSE { Bool(false) }
| INTEGER { Int($1) }
| DOUBLE { Double($1) }
| CHAR { Char($1) }
| STRING { String($1) }
| SYMBOL { Symbol($1) }
sexpr:
atom { Atom($1) }
| REST_ARGS SYMBOL { Atom(RestArgs($2)) }
| LPAREN sexprs RPAREN { List($2) }
vsexpr:
LSQBR sexprs RSQBR { Vector($2) }
| LSQBR RSQBR { Vector([]) }
tsexpr:
sexpr { $1 }
| vsexpr { $1 }
tsexprs:
tsexpr sexprs { $1::$2 }
sexprs:
tsexpr { [$1] }
| SQUOTE tsexpr
{
let towrap = $2 in
let w1 = List.fold_left (fun a b -> SQuote a) towrap (1--$1) in
[w1]
}
| UNQUOTE tsexpr
{
let towrap = $2 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$1) in
[w1]
}
| SQUOTE UNQUOTE tsexpr
{
let towrap = $3 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$2) in
let w2 = List.fold_left (fun a b -> SQuote a) w1 (1--$1) in
[w2]
}
| tsexprs { $1 }
| SQUOTE tsexprs
{
let towrap = List.hd $2 in
let w1 = List.fold_left (fun a b -> SQuote a) towrap (1--$1) in
w1::(List.tl $2)
}
| UNQUOTE tsexprs
{
let towrap = List.hd $2 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$1) in
w1::(List.tl $2)
}
| SQUOTE UNQUOTE tsexprs
{
let towrap = List.hd $3 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$2) in
let w2 = List.fold_left (fun a b -> SQuote a) w1 (1--$1) in
w2::(List.tl $3)
}