-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler-parser.y
206 lines (162 loc) · 5.6 KB
/
compiler-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
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
%error-verbose
%locations
%{
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "botscript.h"
using namespace std;
int yylex();
extern int yyparse();
extern void yyerror( const char* s );
extern FILE* yyin;
extern int yylineno;
BSBlock* script;
%}
%union {
int ival;
double dval;
std::string* sval;
BSBlock* block;
BSStatement* stmt;
BSExpression* expr;
BSIdentifier* ident;
BSDictionary* dict;
BSDictionaryItem* dictitem;
BSFunctionCall* func;
BSFunctionDefinition* funcdef;
BSConditionalBlock* cond;
BSAssign* assign;
BSIfBlock* ifstmt;
BSBinaryOperation* bop;
vector<BSDictionaryItem*>* dictitemlist;
ExpressionList* exprlist;
IdentifierList* identlist;
int token;
}
%token <ival> TINTEGER
%token <dval> TDOUBLE
%token <sval> TSTRING TIDENTIFIER
%token TEQUAL TNEQUAL TASSIGN TLBRACE TRBRACE TLBRACKET TRBRACKET TSUBSCRIPT
%token TCOLON TCOMMA TNIL TENDL TIF TELSE TRETURN TFOR TIN TOR TAND TLT TLTE TGT
%token TGTE TADD TADD_ASSIGN
%token TEOF 0
%type <block> botscript statements statement_block
%type <stmt> statement add_assign assignment branch
%type <ident> identifier
%type <dict> dictionary dictionary_items
%type <dictitem> dictionary_item
%type <expr> expression constant_expression numeric
%type <exprlist> expression_list
%type <identlist> identifier_list
%type <func> function_call
%type <funcdef> function_definition
%type <cond> for_loop
%type <ifstmt> if_stmt
%type <bop> binary_operation
%type <token> comparison
%left TEQUAL TNEQUAL
%start botscript
%%
botscript : statements TEOF { script = $1; }
;
statements : statement { $$ = new BSBlock(); if( $1 ) $$->statements.push_back( $1 ); }
| statements statement { if( $2 ) $1->statements.push_back( $2 ); }
;
statement : assignment TENDL
| add_assign TENDL
| expression TENDL { $$ = new BSExpressionStatement( $1 ); }
| branch TENDL
| TRETURN TENDL { $$ = new BSReturn(); }
| TRETURN expression TENDL { $$ = new BSReturn( $2 ); }
| TENDL { $$ = 0; }
;
add_assign : identifier TADD_ASSIGN expression { $$ = new BSAddAssign( $<ident>1, $<expr>2 ); }
;
assignment : identifier TASSIGN expression { $$ = new BSAssign( $1, $3 ); }
| identifier TASSIGN function_definition { $$ = new BSAssign( $1, $3 ); }
;
function_definition : TLBRACKET identifier_list TRBRACKET TCOLON statement_block { $$ = new BSFunctionDefinition( $2, $5 ); }
;
identifier : TIDENTIFIER { $$ = new BSIdentifier( *$1 ); }
| TIDENTIFIER TSUBSCRIPT TIDENTIFIER { $$ = new BSIdentifier( *$1, *$3 ); }
;
identifier_list : empty { $$ = new IdentifierList(); }
| identifier { $$ = new IdentifierList(); $$->push_back( $1 ); }
| identifier_list TCOMMA identifier { $1->push_back( $3 ); }
;
function_call : identifier TLBRACKET TRBRACKET { $$ = new BSFunctionCall( $1, new ExpressionList() ); }
| identifier TLBRACKET expression_list TRBRACKET { $$ = new BSFunctionCall( $1, $3 ); }
;
expression_list : expression { $$ = new ExpressionList(); $$->push_back( $1 ); }
| expression_list TCOMMA expression { $1->push_back( $3 ); }
;
expression : function_call { $$ = $1; }
| constant_expression { $$ = $1; }
| binary_operation { $$ = $1; }
| dictionary { $$ = $1; }
| identifier { $$ = $1; }
| expression TADD expression { $$ = new BSAdd( $1, $3 ); }
;
dictionary : TLBRACE TRBRACE { $$ = new BSDictionary(); }
| TLBRACE dictionary_items TRBRACE { $$ = $2; }
;
empty :
| TENDL
;
dictionary_items : empty dictionary_item empty { $$ = new BSDictionary(); $$->addItem( $2 ); }
| empty dictionary_item empty TCOMMA empty dictionary_items { $$ = new BSDictionary(); $$->addItem( $2 ); }
;
dictionary_item : identifier TCOLON expression { $$ = new BSDictionaryItem( $1, $3 ); }
;
constant_expression : TSTRING { $$ = new BSString( *$1 ); }
| numeric { $$ = $1; }
;
comparison : TEQUAL | TNEQUAL | TAND | TOR | TGT | TGTE | TLT | TLTE ;
binary_operation : expression comparison expression { $$ = new BSBinaryOperation( $1, $3, $2 ); }
;
numeric : TDOUBLE { $$ = new BSDouble( $1 ); }
| TINTEGER { $$ = new BSInteger( $1 ); }
| TNIL { $$ = new BSNil(); }
;
branch : if_stmt { $$ = $1; }
| for_loop { $$ = $1; }
;
for_loop : TFOR identifier_list TIN expression statement_block { $$ = new BSForEachLoop( $2, $4, $5 ); }
if_stmt : TIF expression statement_block { $$ = new BSIfBlock( $2, $3 ); }
| if_stmt TELSE statement_block { $1->else_statements = $3; }
| if_stmt TELSE if_stmt { $1->else_statements->statements.push_back( $3 ); }
;
statement_block : TLBRACE statements TRBRACE { $$ = $2; }
%%
void
usage( const char* cmd )
{
printf( "Usage: %s <filename>\n", cmd );
exit( -1 );
}
main( int argc, char* argv[] )
{
if( argc != 2 ) {
usage( argv[ 0 ] );
}
FILE* f = fopen( argv[ 1 ], "r" );
if( !f ) {
perror( "fopen" );
exit( -1 );
}
yyin = f;
do {
yyparse();
} while( !feof( yyin ) );
script->ensure_return();
ostringstream oss;
oss << "#main FUNC 0" << endl;
script->codegen( oss );
SymbolRegistrar::codegen( oss );
cout << oss.str();
}
void yyerror( const char* s ) {
printf( "autobot.bs:%d: %s\n", yylineno, s );
exit( -1 );
}