-
Notifications
You must be signed in to change notification settings - Fork 6
/
funccall.c
333 lines (268 loc) · 7.17 KB
/
funccall.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
323
324
325
326
327
328
329
330
331
332
/*
funccall.c
SuperCalc
Created by C0deH4cker on 11/7/13.
Copyright (c) 2013 C0deH4cker. All rights reserved.
*/
#include "funccall.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "support.h"
#include "error.h"
#include "generic.h"
#include "value.h"
#include "context.h"
#include "variable.h"
#include "arglist.h"
#include "function.h"
#include "builtin.h"
#include "binop.h"
static Value* callVar(const Context* ctx, const char* name, const ArgList* args);
static char* reprFunc(VALTYPE valtype, const char* name, const ArgList* arglist, bool pretty);
static char* specialRepr(const char* name, const ArgList* arglist, bool pretty);
static char* verboseFunc(const char* name, const ArgList* arglist, unsigned indent);
static char* specialVerbose(const char* name, const ArgList* arglist, unsigned indent);
FuncCall* FuncCall_new(Value* func, ArgList* arglist) {
FuncCall* ret = fmalloc(sizeof(*ret));
ret->func = func;
ret->arglist = arglist;
return ret;
}
FuncCall* FuncCall_create(char* name, ArgList* arglist) {
Value* func = ValVar(name);
return FuncCall_new(func, arglist);
}
void FuncCall_free(FuncCall* call) {
if(!call) {
return;
}
Value_free(call->func);
ArgList_free(call->arglist);
destroy(call);
}
FuncCall* FuncCall_copy(const FuncCall* call) {
return FuncCall_new(Value_copy(call->func), ArgList_copy(call->arglist));
}
static Value* callVar(const Context* ctx, const char* name, const ArgList* args) {
Value* ret;
bool internal = false;
if(*name == '@') {
internal = true;
name++;
}
Variable* var = Variable_get(ctx, name);
if(var == NULL) {
return ValErr(varNotFound(name));
}
switch(var->val->type) {
case VAL_BUILTIN:
ret = Builtin_eval(var->val->blt, ctx, args, internal);
if(!var->val->blt->isFunction) {
if(args->count > 1) {
Value_free(ret);
ret = ValErr(builtinNotFunc(name));
}
else if(args->count == 1) {
/* i.e. pi(2) -> pi * 2 */
ret = ValExpr(BinOp_new(BIN_MUL, ret, Value_copy(args->args[0])));
}
}
break;
case VAL_FUNC:
ret = Function_eval(var->val->func, ctx, args);
break;
case VAL_ERR:
/* Shouldn't be reached... */
ret = ValErr(Error_copy(var->val->err));
break;
default:
ret = ValErr(typeError("Variable %s is not callable", name));
break;
}
return ret;
}
Value* FuncCall_eval(const FuncCall* call, const Context* ctx) {
Value* func;
if(call->func->type == VAL_VAR) {
func = Value_copy(call->func);
}
else {
func = Value_eval(call->func, ctx);
}
if(func->type == VAL_ERR) {
return func;
}
Value* ret;
switch(func->type) {
case VAL_VAR:
ret = callVar(ctx, func->name, call->arglist);
break;
case VAL_FUNC:
ret = Function_eval(func->func, ctx, call->arglist);
break;
case VAL_BUILTIN:
ret = Builtin_eval(func->blt, ctx, call->arglist, false);
break;
case VAL_INT:
case VAL_REAL:
case VAL_FRAC:
case VAL_VEC: {
char* repr = Value_repr(call->func, false, false);
ret = ValErr(typeError("Value %s is not a callable.", repr));
destroy(repr);
break;
}
default:
badValType(func->type);
}
Value_free(func);
return ret;
}
static char* reprFunc(VALTYPE valtype, const char* callable, const ArgList* arglist, bool pretty) {
char* ret;
char* argstr = ArgList_repr(arglist, pretty);
const char* disp = pretty ? getPretty(callable) : callable;
const char* format = "%s(%s)";
if(valtype == VAL_FUNC) {
format = "(%s)(%s)";
}
asprintf(&ret, format, disp, argstr);
destroy(argstr);
return ret;
}
static char* specialRepr(const char* name, const ArgList* arglist, bool pretty) {
char* ret;
if(strcmp(name, "elem") == 0) {
if(arglist->count != 2) {
/* Freak out */
RAISE(internalError("Invalid argument count passed to internal call of elem"), true);
}
char* vec = Value_repr(arglist->args[0], pretty, false);
char* index = Value_repr(arglist->args[1], pretty, false);
asprintf(&ret, "%s[%s]", vec, index);
destroy(index);
destroy(vec);
}
else {
/* Just default to printing the function */
ret = reprFunc(VAL_VAR, name, arglist, pretty);
}
return ret;
}
char* FuncCall_repr(const FuncCall* call, bool pretty) {
if(call->func->type == VAL_VAR && call->func->name[0] == '@') {
/* Internal call */
return specialRepr(call->func->name + 1, call->arglist, pretty);
}
char* callable = Value_repr(call->func, pretty, false);
char* ret = reprFunc(call->func->type, callable, call->arglist, pretty);
destroy(callable);
return ret;
}
char* FuncCall_wrap(const FuncCall* call) {
char* ret;
char* callable = Value_wrap(call->func, false);
char* argstr = ArgList_wrap(call->arglist);
asprintf(&ret, "%s(%s)", callable, argstr);
destroy(argstr);
destroy(callable);
return ret;
}
static char* verboseFunc(const char* name, const ArgList* arglist, unsigned indent) {
char* ret;
char* args = ArgList_verbose(arglist, indent + 1);
asprintf(&ret,
"%3$s(\n" /* name */
"%2$s%4$s\n" /* args */
"%1$s)",
indentation(indent), indentation(indent + 1),
name,
args);
destroy(args);
return ret;
}
static char* specialVerbose(const char* name, const ArgList* arglist, unsigned indent) {
char* ret;
if(strcmp(name, "elem") == 0) {
if(arglist->count != 2) {
/* Freak out */
RAISE(internalError("Invalid argument count passed to internal call of elem"), true);
}
char* vec = Value_verbose(arglist->args[0], indent);
char* index = Value_verbose(arglist->args[1], indent + 1);
asprintf(&ret,
"%3$s[\n"
"%2$s%4$s\n"
"%1$s]",
indentation(indent), indentation(indent + 1),
vec, index);
destroy(index);
destroy(vec);
}
else {
/* Just default to printing the function */
ret = verboseFunc(name, arglist, indent);
}
return ret;
}
char* FuncCall_verbose(const FuncCall* call, unsigned indent) {
if(call->func->type == VAL_VAR && call->func->name[0] == '@') {
/* Internal call */
return specialVerbose(&call->func->name[1], call->arglist, indent);
}
char* callable = Value_verbose(call->func, indent);
char* ret = verboseFunc(callable, call->arglist, indent);
destroy(callable);
return ret;
}
char* FuncCall_xml(const FuncCall* call, unsigned indent) {
/*
sc> ?x atan2(4, 1 + 2)
<call>
<callee>
<var name="atan2"/>
</callee>
<args>
<int>4</int>
<add>
<int>1</int>
<int>2</int>
</add>
</args>
</call>
0.927295218001612
*/
char* ret;
char* callee = Value_xml(call->func, indent + 2);
if(call->arglist->count > 0) {
char* args = ArgList_xml(call->arglist, indent + 2);
asprintf(&ret,
"<call>\n"
"%2$s<callee>\n"
"%3$s%4$s\n" /* func */
"%2$s</callee>\n"
"%2$s<args>\n"
"%5$s\n" /* args */
"%2$s</args>\n"
"%1$s</call>",
indentation(indent), indentation(indent + 1), indentation(indent + 2),
callee,
args);
destroy(args);
}
else {
asprintf(&ret,
"<call>\n"
"%2$s<callee>\n"
"%3$s%4$s\n" /* func */
"%2$s</callee>\n"
"%2$s<args/>\n"
"%1$s</call>",
indentation(indent), indentation(indent + 1), indentation(indent + 2),
callee);
}
destroy(callee);
return ret;
}