-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredef_functions.c
68 lines (61 loc) · 1.95 KB
/
predef_functions.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
#include "predef_functions.h"
/*
This are the predefined functions of BuenardoScript.
All of them receive a vector of arguments (ast_nodes).
*/
ast buenardo_sum(vector args) {
int res = 0;
for(int i = 0; i < size(args); i++) {
ast pos_i = at(args, i)->value.a;
typeT pos_i_tT = pos_i->value;
res += pos_i_tT->value.number;
}
printf("Sum -> %d\n", res);
return ast_node_init(Number, T_init(Number, &res));
}
ast buenardo_sub(vector args) {
int res = at(args, 0)->value.a->value->value.number;
for(int i = 1; i < size(args); i++) {
ast pos_i = at(args, i)->value.a;
typeT pos_i_tT = pos_i->value;
res -= pos_i_tT->value.number;
}
printf("Sub -> %d\n", res);
return ast_node_init(Number, T_init(Number, &res));
}
ast buenardo_mul(vector args) {
int res = 1;
for(int i = 0; i < size(args); i++) {
ast pos_i = at(args, i)->value.a;
typeT pos_i_tT = pos_i->value;
res *= pos_i_tT->value.number;
}
printf("Mul -> %d\n", res);
return ast_node_init(Number, T_init(Number, &res));
}
ast buenardo_div(vector args) {
int res = at(args, 0)->value.a->value->value.number;
for(int i = 1; i < size(args); i++) {
res /= at(args, i)->value.a->value->value.number;
}
printf("Div -> %d\n", res);
return ast_node_init(Number, T_init(Number, &res));
}
ast buenardo_pow(vector args) {
int res = at(args, 0)->value.a->value->value.number;
for(int i = 1; i < size(args); i++) {
int pow = at(args, i)->value.a->value->value.number;
while(--pow && pow > 0) {
res *= res;
}
}
printf("Pow -> %d\n", res);
return ast_node_init(Number, T_init(Number, &res));
}
ast execute_predef_function(char* name, vector args) {
if(strcmp(name, "sum") == 0) return buenardo_sum(args);
if(strcmp(name, "sub") == 0) return buenardo_sub(args);
if(strcmp(name, "mul") == 0) return buenardo_mul(args);
if(strcmp(name, "div") == 0) return buenardo_div(args);
if(strcmp(name, "pow") == 0) return buenardo_pow(args);
}