This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smallc.y
183 lines (146 loc) · 5.44 KB
/
smallc.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
%{
#include <cstdio>
#include <string>
#include <iostream>
#include "ast.h"
AST::Program *program;
const char* fname;
extern int linenum;
void yyerror(const char *s);
extern "C" int yyparse();
extern "C" int yylex();
extern "C" FILE *yyin;
using namespace std;
%}
%union {
int i;
std::string *s;
AST::Namespace *ns;
AST::NamespaceList *nsl;
AST::Class *cs;
AST::ClassList *csl;
AST::Program *p;
AST::Feature *fe;
AST::FeatureList *fl;
AST::Expr *expr;
AST::Integer *inte;
AST::Block *block;
AST::Arg *arg;
AST::ArgList *args;
}
%token ERROR NS CLASS PUBLIC PRIVATE PROTECTED STATIC PLUS MINUS SUB DIV
%token INTTYPE UINTTYPE BYTETYPE UBYTETYPE RETURN
%token <i> INT
%token <s> ID
%type <p> program
%type <nsl> nsl
%type <ns> ns
%type <csl> csl
%type <cs> cs
%type <fe> field feature method
%type <fl> features
%type <expr> expr binop term stmt call
%type <inte> int
%type <block> block stmts
%type <arg> arg
%type <args> args
%start program
%left PLUS MINUS
%left MULT DIV
%%
program: nsl { program = new AST::Program(linenum, $1); }
| { program = new AST::Program(linenum); }
;
nsl: ns { $$ = new AST::NamespaceList(); $$->push_back($<ns>1); }
| nsl ns { $1->push_back($<ns>2); }
;
ns: NS ID '{' csl '}' { $$ = new AST::Namespace(linenum, *$2, $4); delete $2; }
| NS ID '{' '}' { $$ = new AST::Namespace(linenum, *$2); delete $2; }
;
csl: cs { $$ = new AST::ClassList(); $$->push_back($<cs>1); }
| csl cs { $1->push_back($<cs>2); }
;
cs: CLASS ID '{' '}' { $$ = new AST::Class(linenum, *$2, false); delete $2; }
| STATIC CLASS ID '{' '}' { $$ = new AST::Class(linenum, *$3, true); delete $3; }
| CLASS ID '{' features '}' { $$ = new AST::Class(linenum, *$2, $4, false); delete $2; }
| STATIC CLASS ID '{' features '}' { $$ = new AST::Class(linenum, *$3, $5, true); delete $3; }
;
features: feature { $$ = new AST::FeatureList(); $$->push_back($1); }
| features feature { $1->push_back($2); }
;
feature: field { $$ = $1; }
| method { $$ = $1; }
;
field: ID ID ';' { $$ = new AST::FieldFeature(linenum, *$2, *$1, NULL, false); delete $2; delete $1; }
| STATIC ID ID ';' { $$ = new AST::FieldFeature(linenum, *$3, *$2, NULL, true); delete $3; delete $2; }
| ID ID '=' expr ';' { $$ = new AST::FieldFeature(linenum, *$2, *$1, $4, false); delete $2; delete $1; }
| STATIC ID ID '=' expr ';' { $$ = new AST::FieldFeature(linenum, *$3, *$2, $5, true); delete $3; delete $2; }
;
method: ID ID '(' args ')' block { $$ = new AST::MethodFeature(linenum, *$2, *$1, $4, $6, false); delete $2; delete $1; }
| STATIC ID ID '(' args ')' block { $$ = new AST::MethodFeature(linenum, *$3, *$2, $5, $7, true); delete $3; delete $2; }
| ID ID '(' ')' block { $$ = new AST::MethodFeature(linenum, *$2, *$1, new AST::ArgList(), $5, false); delete $2; delete $1; }
| STATIC ID ID '(' ')' block { $$ = new AST::MethodFeature(linenum, *$3, *$2, new AST::ArgList(), $6, true); delete $3; delete $2; }
;
args: arg { $$ = new AST::ArgList(); $$->push_back($1); }
| args ',' arg { $1->push_back($3); }
;
arg: ID ID { $$ = new AST::Arg(*$2, *$1); delete $2; delete $1; }
;
block: '{' stmts '}' { $$ = $2; }
| '{' '}' { $$ = new AST::Block(linenum); }
;
stmts: stmt { $$ = new AST::Block(linenum); $$->sl->push_back($1); }
| stmts stmt { $1->sl->push_back($2); }
;
stmt: ID '=' expr ';' { $$ = new AST::Assign(linenum, *$1, $3); delete $1; }
| ID ID '=' expr ';' { $$ = new AST::Decl(linenum, *$2, *$1, $4); delete $2; delete $1; }
| call ';' { $$ = $1; }
| RETURN ';' { $$ = new AST::Return(linenum); }
| RETURN expr ';' { $$ = new AST::Return(linenum, $2); }
;
binop: expr PLUS expr { $$ = new AST::Plus(linenum, $1, $3); }
| expr MINUS expr { $$ = new AST::Sub(linenum, $1, $3); }
| expr MULT expr { $$ = new AST::Mult(linenum, $1, $3); }
| expr DIV expr { $$ = new AST::Div(linenum, $1, $3); }
;
term: int { $$ = $1; }
| '(' expr ')' { $$ = $2; }
| ID { $$ = new AST::Object(linenum, *$1); delete $1; }
;
expr: binop
| term
| call
;
call: ID '(' ')' { $$ = new AST::Call(linenum, *$1); delete $1; }
;
int: INT INTTYPE { $$ = new AST::Integer(linenum, $1, "int"); }
| INT UINTTYPE { $$ = new AST::Integer(linenum, $1, "uint"); }
| INT BYTETYPE { $$ = new AST::Integer(linenum, $1, "byte"); }
| INT UBYTETYPE { $$ = new AST::Integer(linenum, $1, "ubyte"); }
| INT { $$ = new AST::Integer(linenum, $1, "int"); }
;
%%
int errors;
AST::Program *parse(const char *filename) {
errors = 0;
fname = filename;
yyin = fopen(fname, "r");
if(!yyin) {
cerr << "Error opening file `" << fname << "'." << endl;
return NULL;
}
linenum = 1;
do {
yyparse();
} while(!feof(yyin));
fclose(yyin);
if(errors) {
cout << errors << " errors found" << endl;
exit(2);
}
return program;
}
void yyerror(const char *s) {
cerr << fname << ":" << linenum << ": " << s << endl;
errors++;
}