-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrix.y
312 lines (277 loc) · 14 KB
/
rix.y
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
%{
#include <stdio.h>
#include <stdlib.h>
#include "ObjectTree.h"
#include "rixc.h"
#include "errors.h"
#define YYPARSE_PARAM scanner
#define YYLEX_PARAM scanner
%}
%glr-parser
%locations
%define api.pure //full
%define parse.error verbose
// Bison fundamentally works by asking flex to get the next token, which it
// returns as an object of type "yystype". But tokens could be of any
// arbitrary data type! So we deal with that in Bison by defining a C union
// holding each of the types of tokens that Flex could return, and have Bison
// use that union instead of "int" for the definition of "yystype":
%union {
int ival;
float fval;
char* sval;
Object* oval;
}
// define the constant-string tokens:
%token ENDOFLINE
%token ENDOFFILE 0 "end of file"
// define the "terminal symbol" token types I'm going to use (in CAPS
// by convention), and associate each with a field of the union:
%token <ival> INT
%token <sval> CHAR
%token <fval> FLOAT
%token <sval> STRING
%token <sval> IDENT
%token <sval> NEWIDENT
%token <sval> UNMARKEDNEWIDENT
%token <sval> FIELD
%token <sval> VERB
%token <sval> STATICVERB
%token <sval> TYPE
%token <sval> MATH_OP
%token <sval> ASSIGNMENT
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> LBRACKET
%token <sval> RBRACKET
%token <sval> RBRACKETASSIGN
%token <ival> INDENT
%token <sval> LBRACE
%token <sval> RBRACE
%token <ival> UNINDENT
%token <sval> CONDITIONLINK
%token <sval> UNARYNEGATE
%token <sval> MATHASSIGN
%token <sval> BITWISEOP
%token <sval> BOOLEANOP
%token <sval> COLON
%token <sval> CLASSDEC
%token <sval> CLASSDECPRIM
%token <sval> PARAMCOMMA
%token <sval> RETURN
%token <sval> SELFIDENT
%token <sval> SLCOMMENT
%token <sval> COMPARISON
%token <sval> LESSTHAN
%token <sval> GREATERTHAN
%token <ival> TERNARY
%token <sval> CODE_INSERT
%token <sval> CONDRETURN
%token <sval> ACCESSOR
%token <sval> DTV_EXTERNAL
%token <sval> DTV_ADDSOURCE
%type <oval> rix;
%type <oval> statements;
%type <oval> statement;
%type <oval> simple_statement;
%type <oval> class_statements;
%type <oval> class_statement;
%type <oval> parameters;
%type <oval> codeblock;
%type <oval> funcblock;
%type <oval> classblock;
%type <oval> function_definition;
%type <oval> class_definition;
%type <oval> ctor_definition;
%type <oval> expr;
%type <oval> object;
%type <oval> arguments;
%type <oval> arglist;
%type <oval> typeArgList;
%type <sval> parameterIdent;
%type <sval> anyIdent;
%type <sval> anyIdentOrVerb;
%{
void yyerror(YYLTYPE *locp, const char* msg);
%}
// Precedence for the following tokens.
// Higher tokens happen last
// Lower tokens happen first / take priority
// Multiple tokens means treat as the same precedence
// and rely on associativity.
// %right (as opposed to %left) means,
// given a compound expression,
// evaluate from right to left.
%right ASSIGNMENT MATHASSIGN
%left VERB TYPE STATICVERB CONDRETURN RBRACKETASSIGN
%left ENDOFLINE INDENT
%left PARAMCOMMA
%left BOOLEANOP
%left COMPARISON LESSTHAN GREATERTHAN TERNARY
%left MATH_OP
%left UNARYNEGATE
%left ACCESSOR
%left DESTRUCTOR
%left LBRACKET RBRACKET LBRACE RBRACE
//%right class_definition
%%
%start rix;
rix:
statements { compilerDebugPrintf("parser: rix-stmts\n"); $$ = $1; }
| ENDOFFILE { compilerDebugPrintf("parser: rix-EOF\n"); $$ = 0; handleEOF(); }
;
statements:
simple_statement { compilerDebugPrintf("parser: stmts-s_s\n"); $$ = $1; }
| statements simple_statement { compilerDebugPrintf("parser: stmts-stmt,s_s\n"); $$ = $1; }
;
simple_statement:
ENDOFLINE { compilerDebugPrintf("parser: s_s-eol\nempty EOL\n"); $$ = 0; }
| DTV_EXTERNAL STRING ENDOFLINE { compilerDebugPrintf("parser: dtv\n"); directive($1,$2); }
| DTV_ADDSOURCE STRING ENDOFLINE { compilerDebugPrintf("parser: dtv\n"); directive($1,$2); }
| statement ENDOFLINE { compilerDebugPrintf("parser: s_s-stmt\nstatement EOL\n"); $$ = $1; }
| statement ENDOFLINE codeblock { compilerDebugPrintf("parser: s_s-stCB\nstatement EOL\n"); closeBrace(); $$ = $1; }
| function_definition ASSIGNMENT statement ENDOFLINE {
compilerDebugPrintf("parser: s_s-func - Funliner Defined! %s\n", $1->fullname);
makeReturn($3); doneFunction($1); }
| function_definition ENDOFLINE funcblock {
compilerDebugPrintf("parser: s_s-func - Function Defined! %s\n", $1->fullname);
doneFunction($1); }
| CODE_INSERT ENDOFLINE { compilerDebugPrintf("parser: code-insert\n"); $$ = injectC($1); }
| class_definition ENDOFLINE classblock {
compilerDebugPrintf("parser: s_s-class - Class Defined! %s\n", $1->fullname);
doneClass($1); }
//Todo-Proper handling of codeblocks
//Verb calls with Codeblocks
// | VERB arguments ENDOFLINE codeblock { compilerDebugPrintf("parser: expr- vo-cb\n"); $$ = completeExpression(conjugate( 0, verbIdent($1), $2)); }
// | TYPE arguments ENDOFLINE codeblock { compilerDebugPrintf("parser: expr-sto-cb\n"); $$ = conjugate( 0, verbCtor($1,0), $2); }
// | expr ACCESSOR VERB arguments ENDOFLINE codeblock { compilerDebugPrintf("parser: expr-.vo-cb\n"); $$ = conjugate( $1, verbIdent($3), $4); }
// | TYPE LBRACE TYPE RBRACE arguments ENDOFLINE codeblock { compilerDebugPrintf("parser: expr-sto-cb\n"); $$ = conjugate( 0, verbCtor($1,$3), $5); }
////Verb calls without Codeblocks
// | VERB arguments ENDOFLINE { compilerDebugPrintf("parser: expr- vo-nocb\n"); $$ = completeExpression(conjugate( 0, verbIdent($1), $2)); }
// | TYPE arguments ENDOFLINE { compilerDebugPrintf("parser: expr-sto-noccb\n"); $$ = completeExpression(conjugate( 0, verbCtor($1,0), $2)); }
// | expr ACCESSOR VERB arguments ENDOFLINE { compilerDebugPrintf("parser: expr-.vo-noccb\n"); $$ = completeExpression(conjugate( $1, verbIdent($3), $4)); }
// | TYPE LBRACE TYPE RBRACE arguments ENDOFLINE { compilerDebugPrintf("parser: expr-sto-noccb\n"); $$ = completeExpression(conjugate( 0, verbCtor($1,$3), $5)); }
;
statement:
expr { compilerDebugPrintf("parser: stmt-expr\n"); $$ = completeExpression(finalize($1)); }
| RETURN expr { compilerDebugPrintf("parser: stmt-rtEx\n"); $$ = completeExpression(makeReturn($2)); }
| RETURN { compilerDebugPrintf("parser: stmt-rtEx\n"); $$ = completeExpression(makeReturn(0)); }
;
expr:
object { compilerDebugPrintf("parser: expr-obj\n"); $$ = $1; }
| expr ASSIGNMENT expr { compilerDebugPrintf("parser: expr-asn\n"); $$ = conjugate($1, verbAssignment($2), $3); }
| expr MATHASSIGN expr { compilerDebugPrintf("parser: expr-mas\n"); $$ = conjugate($1, verbAssignment($2), $3); }
| expr LESSTHAN expr { compilerDebugPrintf("parser: expr-cmp\n"); $$ = conjugate($1, verbComparison($2), $3); }
| expr GREATERTHAN expr { compilerDebugPrintf("parser: expr-cmp\n"); $$ = conjugate($1, verbComparison($2), $3); }
| expr COMPARISON expr { compilerDebugPrintf("parser: expr-cmp\n"); $$ = conjugate($1, verbComparison($2), $3); }
| expr BOOLEANOP expr { compilerDebugPrintf("parser: expr-cmp\n"); $$ = conjugate($1, verbComparison($2), $3); }
| expr TERNARY expr { compilerDebugPrintf("parser: expr-cmp\n"); $$ = conjugate($1, verbTernary(), $3); }
| expr CONDRETURN expr { compilerDebugPrintf("parser: expr-crt\n"); $$ = conjugate($1, verbCondReturn(), $3); }
| expr MATH_OP expr { compilerDebugPrintf("parser: expr-mth\n"); $$ = conjugate($1, verbMathOp($2), $3); }
| expr UNARYNEGATE expr { compilerDebugPrintf("parser: expr-mth\n"); $$ = conjugate($1, verbMathOp($2), $3); }
| UNARYNEGATE expr { compilerDebugPrintf("parser: expr-mth\n"); $$ = conjugate($2, verbMathOp("*"), objectInt(-1)); }
| VERB arguments { compilerDebugPrintf("parser: expr- vo\n"); $$ = conjugate( 0, verbIdent($1), $2); }
| TYPE arguments { compilerDebugPrintf("parser: expr-sto\n"); $$ = conjugate( 0, verbCtor($1,0), $2); }
| expr ACCESSOR VERB arguments { compilerDebugPrintf("parser: expr-.vo\n"); $$ = conjugate( $1, verbIdent($3), $4); }
| TYPE LBRACE TYPE RBRACE arguments { compilerDebugPrintf("parser: expr-sto\n"); $$ = conjugate( 0, verbCtor($1,$3), $5); }
| LPAREN expr RPAREN { compilerDebugPrintf("parser: expr-prn\n"); $$ = parenthesize($2); }
| expr LBRACKET expr RBRACKETASSIGN expr { compilerDebugPrintf("parser: expr-prn\n"); $$ = conjugate($1, verbPutObjAtIdx(), concatParams($3,$5)); }
| expr LBRACKET expr RBRACKET { compilerDebugPrintf("parser: expr-prn\n"); $$ = conjugate($1, verbGetObjAtIdx(), $3); }
| expr ACCESSOR anyIdent { compilerDebugPrintf("parser: exp-.i\n"); $$ = conjugateAccessorIdent( $1, $3); }
| IDENT DESTRUCTOR { compilerDebugPrintf("parser: expr-cmp\n"); $$ = conjugate(objectIdent($1), verbDestructor(), 0); }
;
arguments:
%empty { compilerDebugPrintf("parser: arg\n"); $$ = 0; } //0-ary
| LPAREN RPAREN { compilerDebugPrintf("parser: arg-()\n"); $$ = 0; } //0-ary
| LPAREN arglist RPAREN { compilerDebugPrintf("parser: arg(...)\n"); $$ = $2; } //n-ary
;
arglist:
expr { compilerDebugPrintf("parser: arg-(x)\n"); $$ = $1; } //1-ary
| expr PARAMCOMMA arglist { compilerDebugPrintf("parser: arg(x,y)\n"); $$ = concatParams($1,$3); } //n-ary
;
typeArgList:
UNMARKEDNEWIDENT { compilerDebugPrintf("parser: arg-(x) %s\n",$1); $$ = objectPlaceHolderType($1); } //1-ary
| UNMARKEDNEWIDENT PARAMCOMMA UNMARKEDNEWIDENT { compilerDebugPrintf("parser: arg(x,y) %s, %s\n",$1,$3); $$ = concatParams(objectPlaceHolderType($1),objectPlaceHolderType($3)); } //n-ary
;
object:
INT { compilerDebugPrintf("parser: object-int\n"); $$ = objectInt($1); }
| CHAR { compilerDebugPrintf("parser: object-int\n"); $$ = objectChar($1); }
| FLOAT { compilerDebugPrintf("parser: object-float\n"); $$ = objectfloat($1);}
| IDENT { compilerDebugPrintf("parser: object-identifer\n"); $$ = objectIdent($1); }
| NEWIDENT { compilerDebugPrintf("parser: object-new-identifer\n"); $$ = objectNewIdent($1); }
| UNMARKEDNEWIDENT { compilerDebugPrintf("parser: object-unmarked-new-identifer\n"); $$ = objectUnmarkedNewIdent($1); }
| STRING { compilerDebugPrintf("parser: object-string\n"); $$ = objectString($1); }
| SELFIDENT { compilerDebugPrintf("parser: object-self\n"); $$ = objectSelfIdent($1);}
| CONDITIONLINK { compilerDebugPrintf("parser: object-previous\n"); $$ = objectPrev(); }
;
anyIdent:
IDENT { compilerDebugPrintf("parser: IDENT\n"); $$ = $1; }
| NEWIDENT { compilerDebugPrintf("parser: NEWIDENT\n"); $$ = $1; }
| UNMARKEDNEWIDENT { compilerDebugPrintf("parser: UNMARKEDNEWIDENT\n"); $$ = $1; }
;
anyIdentOrVerb:
IDENT { compilerDebugPrintf("parser: IDENT\n"); $$ = $1; }
| NEWIDENT { compilerDebugPrintf("parser: NEWIDENT\n"); $$ = $1; }
| UNMARKEDNEWIDENT { compilerDebugPrintf("parser: UNMARKEDNEWIDENT\n"); $$ = $1; }
| VERB { compilerDebugPrintf("parser: UNMARKEDNEWIDENT\n"); $$ = $1; }
;
function_definition:
anyIdentOrVerb RETURN TYPE LPAREN parameters RPAREN { compilerDebugPrintf("parser: func-def\n"); $$ = beginFunction($1, $3, 0, $5); }
| anyIdentOrVerb RETURN LPAREN parameters RPAREN { compilerDebugPrintf("parser: func-void\n"); $$ = beginFunction($1, "void", 0, $4); }
| anyIdentOrVerb RETURN TYPE LBRACE TYPE RBRACE LPAREN parameters RPAREN { compilerDebugPrintf("parser: func-def-gen\n"); $$ = beginFunction($1, $3, $5, $8); }
;
parameters:
%empty { compilerDebugPrintf("parser: param0\n"); $$ = CreateObject(0, 0, 0, Expression, 0); }
| TYPE parameterIdent { compilerDebugPrintf("parser: param1\n"); $$ = funcParameters( 0, $1, $2); }
| parameters PARAMCOMMA TYPE parameterIdent { compilerDebugPrintf("parser: paramN\n"); $$ = funcParameters($1, $3, $4); }
;
parameterIdent:
IDENT { compilerDebugPrintf("parser: IDENT\n"); $$ = $1; }
|UNMARKEDNEWIDENT { compilerDebugPrintf("parser: UNMARKEDNEWIDENT\n"); $$ = $1; }
;
funcblock:
INDENT statements UNINDENT { compilerDebugPrintf("parser: codeblock\n"); $$ = $2; }
;
codeblock:
//Todo-Proper handling of codeblocks
// INDENT statements UNINDENT { compilerDebugPrintf("parser: codeblock\n"); $$ = createCodeBlock($2); }
INDENT statements UNINDENT { compilerDebugPrintf("parser: codeblock\n"); $$ = $2; }
;
class_definition:
UNMARKEDNEWIDENT CLASSDEC TYPE { compilerDebugPrintf("parser: class-def\n"); $$ = beginClass($1, $3, 0, false); }
| UNMARKEDNEWIDENT LBRACE typeArgList RBRACE CLASSDEC TYPE { compilerDebugPrintf("parser: gen-class-def\n"); $$ = beginClass($1, $6, $3, false); }
| UNMARKEDNEWIDENT CLASSDECPRIM TYPE { compilerDebugPrintf("parser: class-def\n"); $$ = beginClass($1, $3, 0, true); }
| UNMARKEDNEWIDENT LBRACE typeArgList RBRACE CLASSDECPRIM TYPE { compilerDebugPrintf("parser: gen-class-def\n"); $$ = beginClass($1, $6, $3, true); }
;
ctor_definition:
SELFIDENT LPAREN parameters RPAREN { compilerDebugPrintf("parser: class-def\n"); $$ = beginConstructor($3); }
;
classblock:
INDENT class_statements UNINDENT { $$ = $2; }
;
class_statements:
class_statement
| class_statements class_statement
;
class_statement:
ENDOFLINE { compilerDebugPrintf("parser: c_s-eol\nempty EOL\n"); $$ = 0; }
| UNMARKEDNEWIDENT ASSIGNMENT TYPE ENDOFLINE { compilerDebugPrintf("parser: c_s:varType\n"); $$ = declareVariable($1, $3); }
| function_definition ENDOFLINE codeblock {
compilerDebugPrintf("parser: c_s-func - Function Defined! %s\n", $1->fullname);
doneFunction($1); }
| function_definition ASSIGNMENT statement ENDOFLINE {
compilerDebugPrintf("parser: c_s-func - Function Defined! %s\n", $1->fullname);
makeReturn($3); doneFunction($1); }
| ctor_definition ENDOFLINE codeblock {
compilerDebugPrintf("parser: c_s-func - Constructor Defined! %s\n", $1->fullname);
doneConstructor($1); }
;
%%
//void yyerror(char const *msg) {
void yyerror(YYLTYPE *locp, const char* msg) {
char errorMsg[256];
snprintf(errorMsg, 256, "%s\n", msg);
criticalError(ERROR_ParseError, errorMsg);
// might as well halt now:
exit(-1);
}