-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinguo.y
More file actions
208 lines (179 loc) · 6.18 KB
/
linguo.y
File metadata and controls
208 lines (179 loc) · 6.18 KB
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
%{
#include "ast.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
extern int yylex();
extern int yylineno;
extern char* yytext;
void yyerror(const char* s) {
std::cerr << "Error at line " << yylineno << ": " << s << std::endl;
std::cerr << "Near text: " << yytext << std::endl;
}
Program* program;
%}
%union {
Program* program;
std::vector<Stmt*>* stmts;
std::vector<Expr*>* exprs;
std::vector<std::string>* identifiers;
Stmt* stmt;
Expr* expr;
std::string* string;
int token;
int integer;
bool boolean;
}
%token <string> TIDENTIFIER TSTRING
%token <integer> TINTEGER
%token <boolean> TBOOL
%token <token> TREMEMBER TAS TSAY TWHEN TOR TDONE TEND TDO TTHIS TTIMES TCREATE TCALL
%token <token> TASSIGN TINCREASE TDECREASE TMULTIPLY TDIVIDE TBY
%token <token> TIS TEQUAL TTO TNOT TMORE TTHAN TLESS TAND TOR_OP
%token <token> TPLUS TMINUS TMUL TDIV
%token <token> TLPAREN TRPAREN
%token <token> TGIVE TBACK
%token <token> TWITH TCOMMA // New tokens for parameter handling
%type <program> program
%type <stmts> stmts
%type <stmts> block
%type <stmt> stmt
%type <stmt> decl_stmt
%type <stmt> assign_stmt
%type <stmt> if_stmt
%type <stmt> loop_stmt
%type <stmt> print_stmt
%type <stmt> func_def
%type <stmt> func_call
%type <stmt> return_stmt
%type <expr> expr
%type <expr> primary
%type <expr> binary_expr
%type <expr> comparison
%type <expr> logical_expr
%type <exprs> call_args
%type <identifiers> param_list
%type <identifiers> param_list_opt
%left TOR_OP
%left TAND
%left TPLUS TMINUS
%left TMUL TDIV
%left TIS
%nonassoc TEQUAL TNOT TMORE TLESS
%start program
%%
program : stmts { program = new Program($1); }
;
stmts : stmt { $$ = new std::vector<Stmt*>(); $$->push_back($1); }
| stmts stmt { $1->push_back($2); $$ = $1; }
;
block : stmts { $$ = $1; }
| { $$ = new std::vector<Stmt*>(); }
;
stmt : func_def { $$ = $1; }
| decl_stmt { $$ = $1; }
| assign_stmt { $$ = $1; }
| if_stmt { $$ = $1; }
| loop_stmt { $$ = $1; }
| print_stmt { $$ = $1; }
| func_call { $$ = $1; }
| return_stmt { $$ = $1; }
;
decl_stmt : TREMEMBER TIDENTIFIER TAS expr { $$ = new DeclStmt(*$2, $4); delete $2; }
;
assign_stmt : TINCREASE TIDENTIFIER TBY expr {
Expr* var = new VarExpr(*$2);
Expr* binExpr = new BinaryExpr(var, BinaryOp::ADD, $4);
$$ = new AssignStmt(*$2, binExpr);
delete $2;
}
| TDECREASE TIDENTIFIER TBY expr {
Expr* var = new VarExpr(*$2);
Expr* binExpr = new BinaryExpr(var, BinaryOp::SUB, $4);
$$ = new AssignStmt(*$2, binExpr);
delete $2;
}
| TMULTIPLY TIDENTIFIER TBY expr {
Expr* var = new VarExpr(*$2);
Expr* binExpr = new BinaryExpr(var, BinaryOp::MUL, $4);
$$ = new AssignStmt(*$2, binExpr);
delete $2;
}
| TDIVIDE TIDENTIFIER TBY expr {
Expr* var = new VarExpr(*$2);
Expr* binExpr = new BinaryExpr(var, BinaryOp::DIV, $4);
$$ = new AssignStmt(*$2, binExpr);
delete $2;
}
| TIDENTIFIER TASSIGN expr {
$$ = new AssignStmt(*$1,$3);
delete $1;
}
;
if_stmt : TWHEN expr block TDONE { $$ = new IfStmt($2, $3, nullptr); }
| TWHEN expr block TOR block TDONE { $$ = new IfStmt($2, $3, $5); }
;
loop_stmt : TDO TTHIS expr TTIMES block TDONE { $$ = new LoopStmt($3, $5); }
;
print_stmt : TSAY expr { $$ = new PrintStmt($2); }
;
// Updated function definition to support parameters
func_def : TCREATE TIDENTIFIER param_list_opt block TEND {
$$ = new FunctionDef(*$2, *$3, $4);
delete $2;
delete $3;
}
;
// Optional parameter list
param_list_opt : /* empty */ { $$ = new std::vector<std::string>(); }
| param_list { $$ = $1; }
;
// Parameter list
param_list : TIDENTIFIER {
$$ = new std::vector<std::string>();
$$->push_back(*$1);
delete $1;
}
| param_list TIDENTIFIER {
$1->push_back(*$2);
$$ = $1;
delete $2;
}
;
// Updated function call to support arguments
func_call : TCALL TIDENTIFIER { $$ = new FunctionCall(*$2, new std::vector<Expr*>()); delete $2; }
| TCALL TIDENTIFIER TWITH call_args { $$ = new FunctionCall(*$2, $4); delete $2; }
;
return_stmt : TGIVE TBACK expr { $$ = new ReturnStmt($3); }
;
call_args : expr { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| call_args TCOMMA expr { $1->push_back($3); $$ = $1; }
;
expr : primary { $$ = $1; }
| binary_expr { $$ = $1; }
| comparison { $$ = $1; }
| logical_expr { $$ = $1; }
| TLPAREN expr TRPAREN { $$ = $2; } // Support for parentheses
| func_call { $$ = (Expr*)$1; } // Allow function calls in expressions
;
primary : TINTEGER { $$ = new IntLiteral($1); }
| TBOOL { $$ = new BoolLiteral($1); }
| TSTRING { $$ = new StringLiteral(*$1); delete $1; }
| TIDENTIFIER { $$ = new VarExpr(*$1); delete $1; }
;
binary_expr : expr TPLUS expr { $$ = new BinaryExpr($1, BinaryOp::ADD, $3); }
| expr TMINUS expr { $$ = new BinaryExpr($1, BinaryOp::SUB, $3); }
| expr TMUL expr { $$ = new BinaryExpr($1, BinaryOp::MUL, $3); }
| expr TDIV expr { $$ = new BinaryExpr($1, BinaryOp::DIV, $3); }
;
comparison : expr TIS TEQUAL TTO expr { $$ = new BinaryExpr($1, BinaryOp::EQ, $5); }
| expr TIS TNOT TEQUAL TTO expr { $$ = new BinaryExpr($1, BinaryOp::NEQ, $6); }
| expr TIS TMORE TTHAN expr { $$ = new BinaryExpr($1, BinaryOp::GT, $5); }
| expr TIS TLESS TTHAN expr { $$ = new BinaryExpr($1, BinaryOp::LT, $5); }
;
logical_expr : expr TAND expr { $$ = new BinaryExpr($1, BinaryOp::AND, $3); }
| expr TOR_OP expr { $$ = new BinaryExpr($1, BinaryOp::OR, $3); }
;
%%