-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.c
More file actions
320 lines (306 loc) · 9.8 KB
/
parser.c
File metadata and controls
320 lines (306 loc) · 9.8 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright 2024 TensorArray-Creators
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#include "token.h"
#include "open_file.h"
#include "vm_instruction.h"
#include "sym_map.h"
#include "vm_type.h"
void emit(unsigned size, ...)
{
va_list args;
va_start(args, size);
// Process the variable arguments as needed
for (unsigned i = 0; i < size; ++i) {
++text;
*text = va_arg(args, VM_INSTRUCTION);
}
va_end(args);
}
void match(int tk)
{
if (tkn == tk) {
token_next(); // Move to the next token
} else {
if (tk < 0x80) {
fprintf(stderr, "Error: Expected token %ld but found %ld\n", tk, tkn);
} else {
char* tn = tknname[tk - 0x80];
fprintf(stderr, "Error: Expected token %s but found %s\n", tn, tkn);
}
exit(1);
}
}
void expression(int level)
{
sym_data* temp = NULL; // Temporary variable to hold intermediate values
int isArrRef = 0; // Flag to check if we are dealing with an array reference
// This function would handle parsing and evaluating expressions
// For now, it is a placeholder
// You can implement your logic here
switch (tkn)
{
case TOKEN_NUM:
/* code */
emit(3, IMM, TYPE_INT, tkn_val);
match(TOKEN_NUM);
break;
case TOKEN_ID:
/* code */
temp = sym_cur;
match(TOKEN_ID);
if (temp->type)
{
if (tkn == '(')
{
/* code */
match('(');
match(')');
emit(2, CALL, temp->data);
}
}
else
{
emit(3, IMM, TYPE_PTR, tkn_val);
emit(1, GET);
}
break;
case '"':
{
emit(3, IMM, TYPE_STRING, tkn_val);
match('"'); // Match the opening quote
}
break;
case '[':
if (temp == NULL)
{
*text = PTR_PUSH; // Push the current value onto the stack
match('['); // Match the opening bracket
expression(TOKEN_ASSIGN); // Parse the expression inside the brackets
emit(1, GETELEM); // Emit get element instruction
match(']'); // Match the closing bracket
}
break;
default:
break;
}
while (tkn >= level)
{
switch (tkn)
{
case TOKEN_ASSIGN:
if (*text != GET && *text != GETELEM)
{
fprintf(stderr, "Error: Assignment without a variable\n");
exit(1);
}
*text = PTR_PUSH; // Push the current value onto the stack
match(TOKEN_ASSIGN);
expression(TOKEN_ASSIGN); // Parse the right-hand side expression
if (isArrRef) emit(1, SETELEM); // Emit set element instruction if it's an array reference
else emit(1, SET); // Emit set instruction
break;
case TOKEN_EQ:
emit(1, PUSH);
match(TOKEN_EQ);
expression(TOKEN_SHL); // Parse the right-hand side expression
emit(1, EQ); // Emit equality instruction
break;
case TOKEN_NE:
emit(1, PUSH);
match(TOKEN_NE);
expression(TOKEN_SHL); // Parse the right-hand side expression
emit(1, NE); // Emit not equal instruction
break;
case TOKEN_LT:
emit(1, PUSH);
match(TOKEN_LT);
expression(TOKEN_SHL); // Parse the right-hand side expression
emit(1, LT); // Emit less than instruction
break;
case TOKEN_GT:
emit(1, PUSH);
match(TOKEN_GT);
expression(TOKEN_SHL); // Parse the right-hand side expression
emit(1, GT); // Emit greater than instruction
break;
case TOKEN_LE:
emit(1, PUSH);
match(TOKEN_LE);
expression(TOKEN_SHL); // Parse the right-hand side expression
emit(1, LE); // Emit less than or equal instruction
break;
case TOKEN_GE:
emit(1, PUSH);
match(TOKEN_GE);
expression(TOKEN_SHL); // Parse the right-hand side expression
emit(1, GE); // Emit greater than or equal instruction
break;
case TOKEN_SHL:
emit(1, PUSH);
match(TOKEN_SHL);
expression(TOKEN_ADD); // Parse the right-hand side expression
emit(1, SHL); // Emit shift left instruction
break;
case TOKEN_SHR:
emit(1, PUSH);
match(TOKEN_SHR);
expression(TOKEN_ADD); // Parse the right-hand side expression
emit(1, SHR); // Emit shift right instruction
break;
case TOKEN_ADD:
emit(1, PUSH);
match(TOKEN_ADD);
expression(TOKEN_MUL); // Parse the right-hand side expression
emit(1, ADD); // Emit add instruction
break;
case TOKEN_SUB:
emit(1, PUSH);
match(TOKEN_SUB);
expression(TOKEN_MUL); // Parse the right-hand side expression
emit(1, SUB); // Emit subtract instruction
break;
case TOKEN_MUL:
emit(1, PUSH);
match(TOKEN_MUL);
expression(TOKEN_MATMUL); // Parse the right-hand side expression
emit(1, MUL); // Emit multiply instruction
break;
case TOKEN_DIV:
emit(1, PUSH);
match(TOKEN_DIV);
expression(TOKEN_MATMUL); // Parse the right-hand side expression
emit(1, DIV); // Emit divide instruction
break;
case TOKEN_MATMUL:
emit(1, PUSH);
match(TOKEN_MATMUL);
expression(TOKEN_INC); // Parse the right-hand side expression
emit(1, MATMUL); // Emit matrix multiply instruction
break;
default:
fprintf(stderr, "Error: Unrecognized token in expression\n");
exit(1);
}
}
}
void statement()
{
// This function would handle parsing and executing statements
// For now, it is a placeholder
// You can implement your logic here
switch (tkn)
{
case TOKEN_IF:
{
match(TOKEN_IF);
match('(');
expression(TOKEN_ASSIGN); // Parse the condition expression
match(')');
emit(1, JZ); // Emit jump if zero instruction
VM_INSTRUCTION *b = ++text; // Placeholder for jump address
statement(); // Parse the statement inside the if block
if (tkn == TOKEN_ELSE)
{
match(TOKEN_ELSE);
emit(1, JMP); // Emit jump instruction
*b = text + 2; // Set the jump address to the next instruction
b = ++text;
statement(); // Parse the else block
}
*b = text + 1; // Set the jump address to the next instruction
}
break;
case TOKEN_WHILE:
{
VM_INSTRUCTION *a = NULL; // Placeholder for jump address
VM_INSTRUCTION *b = text+1; // Placeholder for jump address
match(TOKEN_WHILE);
match('(');
expression(TOKEN_ASSIGN); // Parse the condition expression
match(')');
emit(1, JZ); // Emit jump if zero instruction
a=++text; // Set the jump address to the start of the while block
statement(); // Parse the statement inside the while block
emit(1, JMP); // Emit jump instruction to loop back
emit(1, b); // Emit the address to jump back to
*a = text + 1; // Set the jump address to the next instruction
}
break;
case TOKEN_FUNC:
match(TOKEN_FUNC);
if (tkn != TOKEN_ID)
{
fprintf(stderr, "Error: function name\n");
exit(1);
}
sym_cur->type = TYPE_FUNC;
sym_cur->data = malloc(1024*8);
VM_INSTRUCTION *save = text;
text = sym_cur->data;
match(TOKEN_ID);
match('(');
match(')');
statement();
if (*text != RET) emit(1, RET);
text = save;
break;
case TOKEN_RETURN:
match(TOKEN_RETURN);
expression(TOKEN_ASSIGN);
emit(1, RET);
break;
case '{':
match('{');
while (tkn != '}')
statement();
match('}');
break;
case '\0':
return;
default:
expression(TOKEN_ASSIGN);
if (tkn == ';')
match(';');
break;
}
}
void program()
{
while (1)
{
// This is a placeholder for the main program loop
// You would typically call emit or other functions here based on your program logic
// Add your program logic here
interp_malloc();
orig = text+1;
char *isrc = src;
VM_INSTRUCTION *itext = text;
interp_memreset();
printf(">>> ");
fflush(stdout);
fgets(src, poolsize-1, stdin); // Read input from stdin
token_next();
statement();
emit(1, EXIT); // Emit a token with value 0 to indicate end of processing
eval();
printf("eval \n");
puts("");
free(itext);
free(isrc);
}
}