summaryrefslogtreecommitdiff
path: root/src/parser.mly
blob: 98bebafbb3fa00ce0ce48c247c53c95443412654 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
%{
	open Ast

	module Sset = Set.Make(String)

	let type_names = ref Sset.empty
%}

%token <int> INTVAL
%token <string> STRVAL
%token <string> IDENT
%token <string> TIDENT

/* this is stupid */
%token INCLUDE_IOSTREAM

/* keywords */
%token CLASS ELSE FALSE FOR IF INT NEW NULL PUBLIC RETURN
%token THIS TRUE VIRTUAL VOID WHILE

/* operators */
%token ASSIGN LOR LAND EQ NE LT LE GT GE PLUS MINUS
%token TIMES DIV MOD NOT INCR DECR REF
%token LPAREN RPAREN RARROW DOT

/* other symbols */
%token SEMICLON COLON DOUBLECOLON LFLOW LBRACE RBRACE


/* operator priority */
%right ASSIGN
%left LOR
%left LAND
%left EQ NE
%left LT LE GT GE
%left PLUS MINUS
%left TIMES DIV MOD
/* opérateurs unaires associatifs à droite */
%left RARROW DOT LPAREN

%start prog

%type <unit> prog

%%

prog:
	INCLUDE_IOSTREAM?
	decls = declaration*
	EOF
		{ () }
;

declaration:
| 	d = decl_var
	{ d }
| 	d = decl_class
	{ d }
|	p = proto
	b = block
	{ () }
;

decl_vars:
|	t = ty
	vars = separated_nonempty_list(COMMA, var)
	SEMICOLON
;

decl_class:
|	CLASS i = IDENT
	s = supers?
	LBRACE
	PUBLIC COLON
	m = members*
	RBRACE SEMICOLON
	{ () }
;

supers:
|	COLON
	s = separated_nonempty_list(COMMA, super_id)
	{ s }
;

super_id:
|	PUBLIC i = TIDENT
	{ i }
;

member:
|	d = decl_vars
	{ () }
|	v = VIRTUAL?
	p = proto
	{ () }
;

proto:
|	t = ty
	qv = qvar
	LPAREN args = separated_list(COMMA, argument) RPAREN
	{ () }
|	qi = TIDENT
	LPAREN args = separated_list(COMMA, arg) RPAREN
	{ () }
|	qa = TIDENT DOUBLECOLON
	qb = TIDENT
	LPAREN args = separated_list(COMMA, arg) RPAREN
	{ () }
;

argument:
|	t = ty
	v = var
	{ () }
;

var:
|	i = IDENT
	{ () }
|	TIMES v = var
	{ () }
|	REF v = var
	{ () }
;

qvar:
|	qi = qident
	{ qi }
|	TIMES v = qvar
	{ () }
|	REF v = qvar
	{ () }
;

qident:
|	i = IDENT
	{ () }
|	i = IDENT DOUBLECOLON j = IDENT
	{ () }
;

expression:
|	i = INTVAL { EIntConst(i) }
|	THIS { EThis }
|	FALSE { EBoolConst(false) }
|	TRUE { EBoolConst(true) }
|	NULL { ENull }
|	q = qident { () }
|	TIMES expression { EUnary(Deref, e) }
|	e1 = expression DOT e2 = IDENT { () }
|	e1 = expression RARROW e2 = IDENT { () }
|	e1 = expression ASSIGN e2 = expression { () }
|	f = expression LPAREN
	a = separated_list(COLON, expression)
	{ () }
|	NEW c = IDENT LPAREN
	a = separated_list(COLON, expression)
	{ () }
|	INCR e = expression { EUnary(PreIncr, e) }
|	DECR e = expression { EUnary(PreDecr, e) }
|	e = expression INCR { EUnary(PostIncr, e) }
|	e = expression DECR { EUnary(PostDecr, e) }
|	REF e = expression { EUnary(Ref, e) }
|	NOT e = expression { EUnary(Not, e) }
|	MINUS e = expression { EUnary(Minus, e) }
|	PLUS e = expression { EUnary(Plus, e) }
|	e1 = expression
	o = operator
	e2 = expression
	{ EBinop(e1, o, e2) }
|	LPAREN e = expression RPAREN { e }
;

operator:
|	EQ { Equal }
|	NEQ { NotEqual }
|	LT	{ Lt }
|	LE	{ Le }
|	GT	{ Gt }
|	GE	{ Ge }
|	PLUS { Add }
|	MINUS { Sub }
|	TIMES { Mul }
|	DIV { Div }
|	MOD { Modulo }
|	LAND { Land }
|	LOR { Lor }
;