/
parser.mly
157 lines (139 loc) · 3.38 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
%{
open Language
%}
%token <Language.info> LAMBDA
%token <Language.info> ALL
%token <Language.info> PI
%token <string Language.withinfo> UCID /* uppercase-initial */
%token <string Language.withinfo> LCID /* lowercase/symbolic-initial */
%token <int Language.withinfo> INTV
%token <float Language.withinfo> FLOATV
%token <string Language.withinfo> STRINGV
%token <Language.info> APOSTROPHE
%token <Language.info> DQUOTE
%token <Language.info> ARROW
%token <Language.info> BANG
%token <Language.info> BARGT
%token <Language.info> BARRCURLY
%token <Language.info> BARRSQUARE
%token <Language.info> COLON
%token <Language.info> COLONCOLON
%token <Language.info> COLONEQ
%token <Language.info> COLONHASH
%token <Language.info> COMMA
%token <Language.info> DARROW
%token <Language.info> DDARROW
%token <Language.info> DOT
%token <Language.info> EOF
%token <Language.info> EQ
%token <Language.info> EQEQ
%token <Language.info> EXISTS
%token <Language.info> GT
%token <Language.info> HASH
%token <Language.info> LCURLY
%token <Language.info> LCURLYBAR
%token <Language.info> LEFTARROW
%token <Language.info> LPAREN
%token <Language.info> LSQUARE
%token <Language.info> LSQUAREBAR
%token <Language.info> LT
%token <Language.info> RCURLY
%token <Language.info> RPAREN
%token <Language.info> RSQUARE
%token <Language.info> SEMI
%token <Language.info> SLASH
%token <Language.info> STAR
%token <Language.info> TRIANGLE
%token <Language.info> USCORE
%token <Language.info> VBAR
%start toplevel
%type <Language.context -> (Language.command list * Language.context)> toplevel
%%
toplevel :
EOF
{ fun ctx -> [],ctx }
| Command SEMI toplevel
{ fun ctx ->
let cmd,ctx = $1 ctx in
let cmds,ctx = $3 ctx in
cmd::cmds,ctx }
Command :
| Term
{ fun ctx -> (let t = $1 ctx in Eval(dummyinfo ,t)),ctx }
| UCID TyBinder
{ fun ctx -> ((Bind($1.i, $1.v, $2 ctx)), addname ctx $1.v) }
| LCID Binder
{ fun ctx -> ((Bind($1.i,$1.v,$2 ctx)), addname ctx $1.v) }
/**
** BINDINGS
**/
Binder :
COLON Type
{ fun ctx -> VarBind ($2 ctx)}
TyBinder :
/* empty */
{ fun _ -> TyVarBind(KnStar) }
| COLONCOLON Kind
{ fun ctx -> TyVarBind($2 ctx) }
/**
** KINDS
**/
Kind :
ArrowKind
{ $1 }
ArrowKind :
PI LCID COLON Type DOT Kind
{ fun ctx ->
let ctx1 = addname ctx $2.v in
KnPi($2.v, $4 ctx, $6 ctx1) }
| AKind
{ $1 }
AKind :
STAR { fun _ -> KnStar }
| LPAREN Kind RPAREN { $2 }
/**
** TYPES
**/
Type :
ArrowType
{ $1 }
| PI LCID COLON Type DOT Type
{ fun ctx ->
let ctx1 = addname ctx $2.v in
TyPi($2.v, $4 ctx, $6 ctx1) }
ArrowType :
AppType
{ $1 }
AppType :
AppType ATerm { fun ctx -> TyPiApp($1 ctx,$2 ctx) }
| AType { $1 }
AType :
LPAREN Type RPAREN
{ $2 }
| UCID
{ fun ctx ->
TyVar(name2index ctx $1.v, ctxlength ctx) }
/**
** TERMS
**/
Term :
AppTerm
{ $1 }
| LAMBDA LCID COLON Type DOT Term
{ fun ctx ->
let ctx1 = addname ctx $2.v in
TmAbs($1, $2.v, $4 ctx, $6 ctx1) }
AppTerm :
ATerm
{ $1 }
| AppTerm ATerm
{ fun ctx ->
let e1 = $1 ctx in
let e2 = $2 ctx in
TmApp(dummyinfo ,e1,e2) }
ATerm :
LPAREN Term RPAREN
{ $2 }
| LCID
{ fun ctx ->
TmVar($1.i, name2index ctx $1.v, ctxlength ctx) }