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
|
#include <parser.h>
#include <stdbool.h>
#include <stdlib.h>
static const char *interpreter_operators = "=(";
bool strcmp(const char *a, const char *b)
{
int i = 0;
for (; a[i] == b[i] && a[i] != '\0'; i++);
return a[i] == b[i];
}
bool strncmp(const char *a, const char *b, int count)
{
int i = 0;
for (; a[i] == b[i] && i < count; i++);
return i == count;
}
uint8_t isalpha(char c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
uint8_t isnum(char c)
{
return (c >= '0' && c <= '9');
}
uint8_t isname(char c)
{
return isalpha(c) || isnum(c);
}
uint8_t isspace(char c)
{
return (c == ' ' || c == '\t' || c == '\n');
}
uint8_t isoper(char c)
{
for (uint8_t i = 0; i < sizeof(interpreter_operators); i++) {
if (c == interpreter_operators[i])
return 1;
}
return 0;
}
void interpreter_init(interpreter *interp)
{
interp->status = READY;
interp->vcount = 0;
interp->vars = (variable *)calloc(32, sizeof(variable));
interp->names = (char **)calloc(32, sizeof(char *));
interp->stack = (stack_t *)calloc(64, sizeof(stack_t));
}
void interpreter_define_value(interpreter *interp, const char *name, int32_t value)
{
interp->names[interp->vcount] = (char *)name;
interp->vars[interp->vcount].nameidx = interp->vcount;
interp->vars[interp->vcount].type = VALUE;
interp->vars[interp->vcount].value = (uint32_t)value;
interp->vcount++;
}
void interpreter_define_cfunc(interpreter *interp, const char *name, func_t addr)
{
interp->names[interp->vcount] = (char *)name;
interp->vars[interp->vcount].nameidx = interp->vcount;
interp->vars[interp->vcount].type = CFUNCTION;
interp->vars[interp->vcount].value = (uint32_t)addr;
interp->vcount++;
}
int32_t interpreter_get_value(interpreter *interp, const char *name)
{
for (uint16_t i = 0; i < interp->vcount; i++) {
if (!strcmp(interp->names[i], name))
return (int32_t)interp->vars[i].value;
}
return 0;
}
/**
* doline section
*/
bool namencmp(const char *name, const char *s)
{
uint16_t i;
for (i = 0; name[i] == s[i] && s[i] != '\0'; i++);
return (name[i] == '\0' && !isname(s[i]));
}
uint16_t spacecount(const char *s)
{
uint16_t i;
for (i = 0; isspace(s[i]); i++);
return i;
}
char *copystr(const char *s, char end)
{
uint16_t len = 0;
while (s[len++] != end);
char *buf = (char *)malloc(len);
for (uint16_t i = 0; i < len; i++)
buf[i] = s[i];
return buf;
}
char *copysubstr(const char *s, int end)
{
char *buf = (char *)malloc(end);
for (uint16_t i = 0; i < end; i++)
buf[i] = s[i];
return buf;
}
variable *interpreter_getvar(interpreter *interp, const char *line)
{
for (uint16_t i = 0; i < interp->vcount; i++) {
if (namencmp(interp->names[i], line))
return &interp->vars[i];
}
return 0;
}
int interpreter_doline(interpreter *interp, const char *line)
{
variable *bits[16];
uint16_t offset = 0, boffset = 0;
// check for var/func set or usage
int end;
getvar:
for (end = 0; isname(line[end]); end++);
variable *var = interpreter_getvar(interp, line);
if (var != 0) {
bits[boffset++] = var;
} else {
// defining new variable
interpreter_define_value(interp, copysubstr(line, end), 0);
goto getvar; // try again
}
// skip whitespace/name
offset += end;
offset += spacecount(line + offset);
if (boffset == 0 && line[offset] != '=')
return -1; // variable not found
// find operator
if (line[offset] == '\0') {
// print value
return -99;
} else if (line[offset] == '=') {
// assignment/expression
offset++;
offset += spacecount(line + offset);
bits[0]->value = (uint32_t)copystr(line + offset, '\0');
} else if (line[offset] == '(') {
// function call
offset++;
if (bits[0]->type != FUNCTION && bits[0]->type != CFUNCTION)
return -2;
offset += spacecount(line + offset);
// collect arg offsets
uint16_t offsets[8];
uint8_t ooffset = 0;
while (line[offset] != ')' && line[offset] != '\0') {
offsets[ooffset] = offset;
offset += spacecount(line + offset);
uint8_t isvn = 1;
do {
if (line[offset] == ' ' || line[offset] == '\t') {
offset += spacecount(line + offset);
isvn = 0;
}
if (line[offset] == ',') {
offset++;
ooffset++;
break;
} else if (line[offset] == ')') {
ooffset++;
break;
} else if (isvn == 0) {
return -3;
}
} while (++offset);
}
// populate stack
for (uint8_t i = 0; i < ooffset; i++) {
uint16_t j;
for (j = offsets[i]; line[j] != ' ' && line[j] != '\t' &&
line[j] != ',' && line[j] != ')'; j++);
j -= offsets[i];
variable *var = interpreter_getvar(interp, line + offsets[i]);
if (var != 0)
interp->stack[i] = copystr((char *)var->value, '\0');
else
interp->stack[i] = copysubstr(line + offsets[i], j);
}
((func_t)bits[0]->value)(interp->stack);
} else {
return -2; // invalid operation
}
return 0;
}
|