-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyruby_parse.y
254 lines (222 loc) · 9.38 KB
/
tinyruby_parse.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "ast.h"
#include "types.h"
#include "tinyruby.h"
#include "eval.h"
#include "error.h"
#include "vtable.h"
extern FILE *yyin;
NODE * nodeprogram;
%}
%union {
NODE * node;
char * id;
long vinteger;
double vfloat;
char * vstring;
}
%start program
%error-verbose
%locations
%token <id> ID
%token <vinteger> NINTEGER
%token <vfloat> NFLOAT
%token <vstring> TSTRING
%token <node> DEF END RETURN PUTS DO IF ELSE ELSIF CASE WHEN THEN WHILE
%token <node> FALSE CLASS REQUIRE LOAD NIL B_AND B_OR B_NOT STMT_TERM GETS TRUE
%token <node> OP_POWER OP_EQUAL OP_LESS_EQUAL OP_GREAT_EQUAL OP_CMP
%token <node> OP_DISTINCT OP_IDENTITY OP_ASSIGNPLUS OP_ASSIGNMINS OP_ASSIGNMULT
%token <node> OP_ASSIGNDIV
%type <node> program stmts stmt dot expr lhs variable primary when_body fargs operation expr_list method_call body_class rdef
%nonassoc IF WHILE
%left B_OR B_AND
%right B_NOT
%right '='
%right '?' ':'
%nonassoc OP_CMP OP_EQUAL OP_IDENTITY OP_DISTINCT
%left '>' OP_GREAT_EQUAL '<' OP_LESS_EQUAL
%left '|' '^'
%left '&'
/* %left tLSHFT tRSHFT */
%left '+' '-'
%left '*' '/' '%'
/* %right tUMINUS_NUM tUMINUS */
%right OP_POWER
%right '!' '~' /* tUPLUS */
%left '.'
%%
/* Grammar and rules */
program : stmts {
nodeprogram = $1;
}
;
stmts : stmt stmts {
$$ = ast_make_stmts($1, $2);
}
|
/* empty */ {
$$ = NULL;
}
;
stmt : expr STMT_TERM {
$$ = ast_make_expr($1);
}
;
expr : lhs '=' expr { $$ = ast_make_binop('=', $1, $3); }
| lhs OP_ASSIGNPLUS expr { $$ = ast_make_binop('=', $1, ast_make_binop('+', $1, $3)); }
| lhs OP_ASSIGNMINS expr { $$ = ast_make_binop('=', $1, ast_make_binop('-', $1, $3)); }
| lhs OP_ASSIGNMULT expr { $$ = ast_make_binop('=', $1, ast_make_binop('*', $1, $3)); }
| lhs OP_ASSIGNDIV expr { $$ = ast_make_binop('=', $1, ast_make_binop('/', $1, $3)); }
| NINTEGER { $$ = ast_make_numeric(ruby_integer($1)); }
| NFLOAT { $$ = ast_make_numeric(ruby_float($1)); }
| TSTRING { $$ = ast_make_string(ruby_string($1)); }
| NIL { $$ = ast_make_internal(Qnil); }
| TRUE { $$ = ast_make_internal(Qtrue); }
| FALSE { $$ = ast_make_internal(Qfalse); }
| PUTS expr { $$ = ast_make_puts($2); }
| '-' expr { $$ = ast_make_uniop('-', $2); }
| '+' expr { $$ = ast_make_uniop('+', $2); }
| '!' expr { $$ = ast_make_uniop('!', $2); }
| '~' expr { $$ = ast_make_uniop('~', $2); }
| B_NOT expr { $$ = ast_make_uniop(AST_OP_NOT, $2); }
| expr '+' expr { $$ = ast_make_binop('+', $1, $3); }
| expr '-' expr { $$ = ast_make_binop('-', $1, $3); }
| expr '*' expr { $$ = ast_make_binop('*', $1, $3); }
| expr '/' expr { $$ = ast_make_binop('/', $1, $3); }
| expr '%' expr { $$ = ast_make_binop('%', $1, $3); }
| expr OP_POWER expr { $$ = ast_make_binop(AST_OP_POW, $1, $3); }
| expr B_AND expr { $$ = ast_make_binop(AST_OP_AND, $1, $3); }
| expr B_OR expr { $$ = ast_make_binop(AST_OP_OR, $1, $3); }
| expr '>' expr { $$ = ast_make_binop('>', $1, $3); }
| expr '<' expr { $$ = ast_make_binop('<', $1, $3); }
| expr OP_LESS_EQUAL expr { $$ = ast_make_binop(AST_OP_LE_EQUAL, $1, $3); }
| expr OP_GREAT_EQUAL expr { $$ = ast_make_binop(AST_OP_GR_EQUAL, $1, $3); }
| expr OP_EQUAL expr { $$ = ast_make_binop(AST_OP_EQUALS, $1, $3); }
| expr OP_CMP expr { $$ = ast_make_binop(AST_OP_CMP, $1, $3); }
| expr OP_DISTINCT expr { $$ = ast_make_binop(AST_OP_DISTINCT, $1, $3); }
| expr OP_IDENTITY expr { $$ = ast_make_binop(AST_OP_IDENTITY, $1, $3); }
| expr '?' expr ':' expr { $$ = ast_make_cond($1, $3, $5); }
| '(' expr ')' { $$ = $2; }
| GETS { $$ = ast_make_gets(); }
| dot {$$=$1;}
| primary {$$=$1;}
| RETURN expr { $$ = ast_make_return($2);}
;
dot : expr '.' expr { $$ = ast_make_dot($1,$3);}
;
primary : IF expr STMT_TERM stmts END { $$ = ast_make_cond($2, $4, NULL); }
| IF expr STMT_TERM stmts ELSE STMT_TERM stmts END { $$ = ast_make_cond($2, $4, $7); }
| WHILE expr STMT_TERM stmts END { $$ = ast_make_while($2, $4); }
| CASE STMT_TERM when_body END { $$ = ast_make_case($3); }
| CLASS ID STMT_TERM body_class END {$$ = ast_make_clase($2,$4); }
| rdef { $$=$1; }
| method_call
| variable
;
rdef : DEF ID '(' fargs ')' STMT_TERM stmts END { $$ = ast_make_def($2, $4, $7); }
;
body_class : /**/ { $$=NULL;}
| rdef STMT_TERM body_class {$$= ast_make_body_clase($1,$3);}
;
fargs : /*vacio*/ { $$ = NULL; }
| ID { $$ = ast_make_fargs($1, NULL); }
| ID ',' fargs { $$ = ast_make_fargs($1, $3); }
;
expr_list : /* empty */ { $$ = NULL; }
| expr { $$ = ast_make_exprlist($1, NULL); }
| expr ',' expr_list { $$ = ast_make_exprlist($1, $3); }
;
variable : ID { $$ = ast_make_id($1, AST_ID_C); }
| '@' ID {$$ = ast_make_id($2, AST_ID_A);}
| '$' ID {$$= ast_make_id($2, AST_ID_G);}
;
operation : ID { $$ = ast_make_operation($1, 0); }
| ID '!' { $$ = ast_make_operation($1, '!'); }
| ID '?' { $$ = ast_make_operation($1, '?'); }
;
method_call : operation '(' expr_list ')' { $$ = ast_make_methodcall($1, $3, NULL); }
;
when_body : WHEN expr THEN stmts { $$ = ast_make_when($2, $4, NULL); }
| WHEN expr THEN stmts when_body { $$ = ast_make_when($2, $4, $5); }
;
lhs : variable { $$ = $1; }
;
%%
/* C subroutines */
int main(int argc, char *argv[]) {
if (argc > 1)
yyin = fopen(argv[1], "r");
yyparse();
if (DEBUG) {
printf(" Abstract syntax tree : \n");
printf(" ======================================== \n");
ast_node_print(nodeprogram, 0, "ROOT");
printf(" ======================================== \n");
}
VALUE init;
int status;
scope_init();
scope_begin();
E_STATUS_INFO eval_info;
eval_info.o = 0;
eval(nodeprogram, init, &eval_info, RB_MAIN_CLASS);
scope_end();
if(DEBUG) {
printf(" MORE DEBUG INFO\n");
printf(" ======================================== \n");
scope_print();
heap_print();
printf(" ======================================== \n");
}
switch (eval_info.code) {
case INTERNAL_ERROR:
yyerror("internal error");
break;
case UNRECOGNIZED_NODE_TYPE:
yyerror("unrecognized node type");
break;
case TYPE_MISMATCH:
yyerror("type mismatch");
break;
case UNDEFINED_VARIABLE:
yyerror("undefined variable '%s'", eval_info.node->id);
break;
case ARGUMENT_ERROR:
yyerror("argument error");
break;
case NO_METHOD_ERROR:
yyerror("no method error");
break;
default:
return 0;
break;
}
return 1;
}
/* in code section at the end of the parser */
void
yyerror(char *s, ...)
{
va_list ap;
va_start(ap, s);
if(yylloc.first_line)
fprintf(stderr, "%d.%d-%d.%d: error: ", yylloc.first_line, yylloc.first_column,
yylloc.last_line, yylloc.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
void
lyyerror(YYLTYPE t, char *s, ...)
{
va_list ap;
va_start(ap, s);
if(t.first_line)
fprintf(stderr, "%d.%d-%d.%d: error: ", t.first_line, t.first_column,
t.last_line, t.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}