-
Notifications
You must be signed in to change notification settings - Fork 6
/
statement.c
323 lines (258 loc) · 6.95 KB
/
statement.c
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
321
322
/*
statement.c
SuperCalc
Created by C0deH4cker on 11/11/13.
Copyright (c) 2013 C0deH4cker. All rights reserved.
*/
#include "statement.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "generic.h"
#include "error.h"
#include "value.h"
#include "variable.h"
#include "context.h"
#include "binop.h"
#include "function.h"
#include "binop.h"
Statement* Statement_new(Variable* var) {
Statement* ret = fmalloc(sizeof(*ret));
ret->var = var;
return ret;
}
void Statement_free(Statement* stmt) {
if(!stmt) {
return;
}
Variable_free(stmt->var);
destroy(stmt);
}
Statement* Statement_parse(const char** expr) {
Statement* ret = NULL;
Variable* var;
Value* val;
const char* equals = strchr(*expr, '=');
if(equals == NULL) {
/* No assignment, just a plain expression. */
val = Value_parseTop(expr);
if(val->type == VAL_ERR) {
var = VarErr(val->err);
val->err = CAST_NONNULL(NULL);
Value_free(val);
}
else {
var = Variable_new(NULL, val);
}
return Statement_new(var);
}
/* There is an assignment */
equals++;
/* First, parse the left side */
char* name = nextToken(expr);
if(name == NULL) {
return Statement_new(VarErr(syntaxError(*expr, "No variable to assign to.")));
}
trimSpaces(expr);
if(**expr == '(') {
/* Defining a function */
(*expr)++;
Error* err = NULL;
Function* func = Function_parseArgs(expr, ',', ')', &err);
if(func == NULL) {
destroy(name);
return Statement_new(VarErr(err));
}
trimSpaces(expr);
if(**expr != '=') {
destroy(name);
Function_free(func);
return Statement_new(VarErr(badChar(*expr)));
}
/* Now, parse the right side of the equals sign (function body) */
val = Value_parseTop(&equals);
if(val->type == VAL_ERR) {
/* A parse error occurred */
destroy(name);
Function_free(func);
var = VarErr(Error_copy(val->err));
Value_free(val);
return Statement_new(var);
}
/* Construct function and return it */
func->body = val;
Variable* var = Variable_new(name, ValFunc(func));
name = NULL;
ret = Statement_new(var);
}
else {
/* Defining a variable */
BINTYPE bin = BIN_UNK;
if(**expr != '=') {
/* In-place manipulation */
bin = BinOp_nextType(expr, 0, 0);
/* Still not an equals sign means invalid character */
if(**expr != '=') {
destroy(name);
return Statement_new(VarErr(badChar(*expr)));
}
}
/* Now, parse the right side of the equals sign (variable's expression) */
val = Value_parseTop(&equals);
if(val->type == VAL_ERR) {
/* A parse error occurred */
destroy(name);
var = VarErr(Error_copy(val->err));
Value_free(val);
return Statement_new(var);
}
/* Is this an in-place binop like "+="? */
if(bin != BIN_UNK) {
val = ValExpr(BinOp_new(bin, ValVar(strdup(name)), val));
}
ret = Statement_new(Variable_new(name, val));
name = NULL;
}
return ret;
}
Value* Statement_eval(const Statement* stmt, Context* ctx, VERBOSITY v) {
Value* ret;
Variable* var = stmt->var;
/* Evaluate right side */
ret = Value_eval(var->val, ctx);
/* If an error occurred, bail */
if(ret->type == VAL_ERR) {
return ret;
}
/* Statement result is a variable? */
if(ret->type == VAL_VAR) {
Variable* func = Variable_get(ctx, ret->name);
if(func == NULL) {
Error* err = varNotFound(ret->name);
Value_free(ret);
return ValErr(err);
}
if(var->name != NULL) {
Context_setGlobal(ctx, CAST_NONNULL(var->name), Value_copy(func->val));
}
else if((v & (V_REPR|V_TREE|V_XML)) == 0) {
/* Coerce the variable to a Value */
Value* val = Variable_eval(func, ctx);
Value_free(ret);
ret = val;
}
}
else {
/* To handle statements like "pi" */
if(ret->type == VAL_BUILTIN && !ret->blt->isFunction) {
Value* tmp = Value_coerce(ret, ctx);
Value_free(ret);
ret = tmp;
}
/* Update ans */
Context_setGlobal(ctx, "ans", Value_copy(ret));
/* Save the newly evaluated variable */
if(var->name != NULL) {
Context_setGlobal(ctx, CAST_NONNULL(var->name), Value_copy(ret));
}
}
return ret;
}
bool Statement_didError(const Statement* stmt) {
return (stmt->var->val->type == VAL_ERR);
}
char* Statement_repr(const Statement* stmt, const Context* ctx, bool pretty) {
if(stmt->var->val->type == VAL_VAR) {
/* Return the reprint of the variable in ctx */
Variable* var = Variable_get(ctx, stmt->var->val->name);
if(var == NULL) {
/* If the variable doesn't exist, just return its name */
if(pretty) {
return strdup(getPretty(stmt->var->val->name));
}
return strdup(stmt->var->val->name);
}
return Variable_repr(var, pretty);
}
return Variable_repr(stmt->var, pretty);
}
char* Statement_wrap(const Statement* stmt, const Context* ctx) {
if(stmt->var->val->type == VAL_VAR) {
/* Return the reprint of the variable in ctx */
Variable* var = Variable_get(ctx, stmt->var->val->name);
if(var == NULL) {
/* If the variable doesn't exist, just return its name */
return strdup(stmt->var->val->name);
}
return Variable_wrap(var);
}
return Variable_wrap(stmt->var);
}
char* Statement_verbose(const Statement* stmt, const Context* ctx) {
if(stmt->var->val->type == VAL_VAR) {
/* Return the verbose representation of the variable in ctx */
Variable* var = Variable_get(ctx, stmt->var->val->name);
if(var == NULL) {
/* If the variable doesn't exist, just return its name */
return Value_verbose(stmt->var->val, 0);
}
return Variable_verbose(var);
}
return Variable_verbose(stmt->var);
}
char* Statement_xml(const Statement* stmt, const Context* ctx) {
if(stmt->var->val->type == VAL_VAR) {
/* Return the xml representation of the variable in ctx */
Variable* var = Variable_get(ctx, stmt->var->val->name);
if(var == NULL) {
/* If the variable doesn't exist, just return its name */
return Value_xml(stmt->var->val, 0);
}
return Variable_xml(var);
}
return Variable_xml(stmt->var);
}
void Statement_print(const Statement* stmt, const SuperCalc* sc, VERBOSITY v) {
/* Error parsing? */
if(Statement_didError(stmt)) {
return;
}
int needNewline = 0;
if(v & V_XML) {
needNewline++;
/* Dump XML output because why not? */
char* xml = Statement_xml(stmt, sc->ctx);
printf("%s\n", xml);
destroy(xml);
}
if(v & V_TREE) {
if(needNewline++ && sc->interactive) {
putchar('\n');
}
/* Dump parse tree */
char* tree = Statement_verbose(stmt, sc->ctx);
printf("%s\n", tree);
destroy(tree);
}
if(v & V_WRAP) {
if(needNewline++ && sc->interactive) {
putchar('\n');
}
/* Wrap lots of stuff in parentheses for clarity */
char* wrapped = Statement_wrap(stmt, sc->ctx);
printf("%s\n", wrapped);
destroy(wrapped);
}
if(v & V_REPR) {
if(needNewline++ && sc->interactive) {
putchar('\n');
}
/* Print parenthesized statement */
char* reprinted = Statement_repr(stmt, sc->ctx, !!(v & V_PRETTY));
printf("%s\n", reprinted);
destroy(reprinted);
}
if(needNewline++ && sc->interactive) {
putchar('\n');
}
}