-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
144 lines (115 loc) · 4.78 KB
/
parser.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
%{
#include "node.h"
#include "parser.h"
NProgram *programBlock; /* the top level root node of our final AST */
extern int yylex();
void yyerror(const char *s) { printf("ERROR: %sn", s); }
%}
/* Represents the many different ways we can access our data */
%union {
Node *node;
NBlock *block;
NExpression *expr;
NStatement *stmt;
NIdentifier *ident;
NVariableDeclaration *var_decl;
NConstDeclaration *const_decl;
NInternalDeclaration *internal_decl;
NSuperDeclaration *super_decl;
std::vector<NVariableDeclaration*> *varvec;
std::vector<NExpression*> *exprvec;
std::string *string;
int token;
}
/* Define our terminal symbols (tokens). This should
match our tokens.l lex file. We also define the node type
they represent.
*/
%token <string> TIDENTIFIER TINTEGER TDOUBLE
%token <token> TCEQ TCNE TCLT TCLE TCGT TCGE TEQUAL
%token <token> TLPAREN TRPAREN TLBRACE TRBRACE TCOMMA TDOT
%token <token> TPLUS TMINUS TMUL TDIV TDQUOTE TSQUOTE
%token <token> TCOMMENT TROCKET TPACKAGE TREQUIRE TVAR TRETURN TCHAR
%token <token> TCONST TINTERNAL TTHING TSUPER TFUNC TRSQUARE TLSQUARE
%token <token> TSTRTYPE TINTTYPE TFLOATTYPE
/* Define the type of node our nonterminal symbols represent.
The types refer to the %union declaration above. Ex: when
we call an ident (defined by union type ident) we are really
calling an (NIdentifier*). It makes the compiler happy.
*/
%type <ident> ident
%type <expr> numeric expr string string_chars
%type <varvec> func_decl_args
%type <exprvec> call_args
%type <block> program stmts block
%type <stmt> stmt var_decl func_decl const_decl internal_decl super_decl thing_decl require_decl ret
%type <token> comparison
/* Operator precedence for mathematical operators */
%left TPLUS TMINUS
%left TMUL TDIV
/*
Track locations of symbols to allow for better error messages
in the compiler
*/
%locations
%start program
%%
program : TPACKAGE ident stmts { programBlock = new NProgram(currentFile, @$.first_line, @$.first_column, *$2, *$3); }
;
stmts : stmt { $$ = new NBlock(); $$->statements.push_back($<stmt>1); }
| stmts stmt { $1->statements.push_back($<stmt>2); }
;
stmt : var_decl | func_decl | const_decl | internal_decl | super_decl | thing_decl
| expr { $$ = new NExpressionStatement(*$1); }
| TRETURN expr { $$ = new NReturn(*$2); }
;
block : TLBRACE stmts TRBRACE { $$ = $2; }
| TLBRACE TRBRACE { $$ = new NBlock(); }
;
require_decl : TREQUIRE TCLT ident TCGT { $$ = new NStdlibRequirementDeclaration(*$1); }
| TREQUIRE TDQUOTE ident TDQUOTE { $$ = new NRelativeRequirementDeclaration(*$1); }
;
var_decl : TVAR ident ident TEQUAL expr { $$ = new NVariableDeclaration(*$2, *$3, $5); }
| TVAR ident ident { $$ = new NVariableDeclaration(*$2, *$3); }
| ident ident { $$ = new NVariableDeclaration(*$1, *$2); }
;
const_decl : TCONST ident ident TEQUAL expr { $$ = new NConstDeclaration(*$2, *$3, $5); }
| TCONST ident ident { $$ = new NConstDeclaration(*$2, *$3); }
;
internal_decl : TINTERNAL ident ident TEQUAL expr { $$ = new NInternalDeclaration(*$2, *$3, $5); }
;
super_decl : TSUPER ident { $$ = new NSuperDeclaration(*$2); }
;
thing_decl : TTHING ident block { $$ = new NThingDeclaration(*$2, *$3); }
;
func_decl : TFUNC ident TLPAREN func_decl_args TRPAREN TROCKET TLPAREN ident TRPAREN block
{ $$ = new NFunctionDeclaration(*$2, *$4, *$8, *$10); delete $4; }
;
func_decl_args : /*blank*/ { $$ = new VariableList(); }
| var_decl { $$ = new VariableList(); $$->push_back($<var_decl>1); }
| func_decl_args TCOMMA var_decl { $1->push_back($<var_decl>3); }
;
ident : TIDENTIFIER { $$ = new NIdentifier(currentFile, @$.first_line, @$.first_column, *$1); delete $1; }
;
numeric : TINTEGER { $$ = new NInteger(atol($1->c_str())); delete $1; }
| TDOUBLE { $$ = new NDouble(atof($1->c_str())); delete $1; }
;
string : TDQUOTE string_chars TDQUOTE { $$ = new NString(*$2); }
;
string_chars : TCHAR | string_chars TCHAR
;
expr : ident TEQUAL expr { $$ = new NAssignment(currentFile, @$.first_line, @$.first_column, *$<ident>1, *$3); }
| ident TLPAREN call_args TRPAREN { $$ = new NMethodCall(*$1, *$3); delete $3; }
| ident { $<ident>$ = $1; }
| numeric
| expr comparison expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| TLPAREN expr TRPAREN { $$ = $2; }
;
call_args : /*blank*/ { $$ = new ExpressionList(); }
| expr { $$ = new ExpressionList(); $$->push_back($1); }
| call_args TCOMMA expr { $1->push_back($3); }
;
comparison : TCEQ | TCNE | TCLT | TCLE | TCGT | TCGE
| TPLUS | TMINUS | TMUL | TDIV
;
%%