-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.c
565 lines (449 loc) · 13.9 KB
/
interpret.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* ************************************************************************ */
/* */
/* The MIT License (MIT) */
/* Copyright (c) 2016 Jiří Fatka <[email protected]> */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE */
/* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION */
/* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */
/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/* */
/* ************************************************************************ */
/* Declaration */
#include "interpret.h"
/* C library */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
/* LISP */
#include "tokenizer.h"
#include "functions.h"
/* ************************************************************************ */
/**
* @brief Maximum length of variable name.
*/
#ifndef MAX_VARIABLE_NAME_LENGTH
#define MAX_VARIABLE_NAME_LENGTH 9
#endif
/* ************************************************************************ */
/**
* @brief Maximum length of function name.
*/
#ifndef MAX_FUNCTION_NAME_LENGTH
#define MAX_FUNCTION_NAME_LENGTH 10
#endif
/* ************************************************************************ */
/**
* @brief Structure for storing variables.
*/
struct Variable
{
/** Variable name. */
char name[MAX_VARIABLE_NAME_LENGTH];
/** Variable value. */
int value;
};
/* ************************************************************************ */
/**
* @brief Structure for storing functions by name.
*/
struct Function
{
/** Function name. */
char name[MAX_FUNCTION_NAME_LENGTH];
/** Function pointer. */
func_t function;
};
/* ************************************************************************ */
/**
* @brief Current line number.
*/
static unsigned int l_line_no = 0;
/* ************************************************************************ */
/**
* @brief Array for storage of variables.
*/
static struct Variable *l_variables = NULL;
/* ************************************************************************ */
/**
* @brief Number of stored variables.
*/
static unsigned int l_variable_count = 0;
/* ************************************************************************ */
/**
* @brief Array of supported functions.
*/
static const struct Function l_functions[] = {
{"QUIT", func_quit},
{"EXIT", func_quit},
{"SET", func_set},
{"+", func_add},
{"-", func_sub},
{"*", func_mult},
{"/", func_div},
{"=", func_eq},
{"/=", func_neq},
{"QUOTE", func_quote},
{"LIST", func_list},
{"CAR", func_car},
{"CDR", func_cdr},
{">", func_gt},
{">=", func_ge},
{"<", func_lt},
{"<=", func_le}
};
/* ************************************************************************ */
/**
* @brief Number of supported functions.
*/
static const unsigned int l_function_count = sizeof(l_functions) / sizeof(struct Function);
/* ************************************************************************ */
/**
* @brief Current expression for `clean_up()` when error is occured.
*/
static struct SExpression* l_current_expr = NULL;
/* ************************************************************************ */
/**
* @brief Find variable object by name.
*
* @param name Variable name.
*
* @return Found variable object pointer or NULL.
*/
static struct Variable *find_variable(const char *name)
{
unsigned int i;
/* No variables */
if (l_variables == NULL)
return NULL;
/* Foreach whole table */
for (i = 0; i < l_variable_count; ++i)
{
/* Check variable name */
if (!strcmp(l_variables[i].name, name))
return &l_variables[i];
}
/* Not found */
return NULL;
}
/* ************************************************************************ */
/**
* @brief Finds empty place for new variable.
*
* When a variable is unset, only name is removed and array is not reallocated
* to save some time. After that when a new variable is set there is no need
* for array reallocation, we can just use this empty place.
*
* @return A pointer to empty variable.
*/
static struct Variable *find_variable_empty()
{
return find_variable("");
}
/* ************************************************************************ */
/**
* @brief Finds function object by name.
*
* @param name Function name.
*
* @return A pointer to function structure or NULL.
*/
static const struct Function* find_function(const char *name)
{
unsigned int i;
/* Foreach whole table */
for (i = 0; i < l_function_count; ++i)
{
/* Check function name */
if (!strcmp(l_functions[i].name, name))
return &l_functions[i];
}
/* Not found */
return NULL;
}
/* ************************************************************************ */
void syntax_error(const char *err)
{
fprintf(stderr, "Syntax error: %s\n", err);
exit(EXIT_FAILURE);
}
/* ************************************************************************ */
void eval_file(FILE *file)
{
assert(file);
/* Register clean-up function */
atexit(&clean_up);
/* Set source file */
set_source(file);
/* Evaluate separate lines */
while (!eval_line())
continue;
}
/* ************************************************************************ */
int eval_line(void)
{
struct SExpression *expr = NULL;
int quoted = 0;
if (is_source_stdin())
printf("[%d]> ", ++l_line_no);
/* Read symbols */
while (1)
{
/* Get next symbol */
enum Sym sym = get_sym();
/* Nothing to read */
if (sym == SYM_EOF)
return 1;
/* Quote */
if (sym == SYM_QUOTE)
{
quoted = 1;
}
else if (sym == SYM_NAME)
{
expr = alloc_sexpr(TYPE_VALUE);
/* Variable name */
if (isalpha(name[0]))
{
if (has_variable(name))
{
int value = get_variable(name);
sprintf(expr->lvalue, "%d", value);
}
else
{
/* No variable */
expr->type = TYPE_NIL;
}
}
else
{
/* Value */
strncpy(expr->lvalue, name, sizeof(expr->lvalue));
}
break;
}
else if (sym == SYM_LPAREN)
{
/* Evaluate expression */
expr = eval_list(quoted ? TYPE_QUOTED : TYPE_NIL);
break;
}
}
/* Print current command for non-stdin input */
if (!is_source_stdin())
printf("[%d]> %s", ++l_line_no, cur_line());
assert(expr);
/* Print result and free it */
print_sexpr(expr);
free_sexpr(expr);
printf("\n");
return 0;
}
/* ************************************************************************ */
struct SExpression *eval_list(enum Type type)
{
/* Allocate initial expression and store it as current */
struct SExpression *expr = l_current_expr = alloc_sexpr(type);
struct SExpression *rexpr = expr;
/* Must starts as list */
assert(cur_sym() == SYM_LPAREN);
/* Read until right paren is found */
while (get_sym() != SYM_RPAREN)
{
if (cur_sym() == SYM_EOF)
{
/* Free expression */
syntax_error("Missing )");
}
/* Inner list */
if (cur_sym() == SYM_LPAREN)
{
/* Recursive evaluation */
rexpr->type = TYPE_SEXPR;
rexpr = rexpr->right = eval_list(type);
}
else if (cur_sym() == SYM_NAME)
{
if (rexpr->type == TYPE_NIL)
rexpr->type = TYPE_SEXPR;
if (strlen(rexpr->lvalue))
{
rexpr = rexpr->right = alloc_sexpr(isdigit(name[0]) ? TYPE_VALUE : type);
}
strncpy(rexpr->lvalue, name, sizeof(expr->lvalue));
}
}
/* Evaluate parsed expression */
expr = eval_sexpr(expr);
/* After that there is no chance to error */
l_current_expr = NULL;
return expr;
}
/* ************************************************************************ */
struct SExpression *eval_sexpr(struct SExpression *expr)
{
const struct Function *func;
assert(expr);
/* Nothing to evaluate */
if (expr->type == TYPE_NIL || expr->type == TYPE_QUOTED)
return expr;
/* Find function */
func = find_function(expr->lvalue);
/* Function found */
if (func)
{
/* Call found function */
return func->function(expr);
}
/* Unable to find function with given name */
{
char tmp[200];
/* Construct syntax error */
strcpy(tmp, "Undefined function: ");
strcat(tmp, expr->lvalue);
syntax_error(tmp);
}
/* Get rid of no-return warning */
return NULL;
}
/* ************************************************************************ */
void set_variable(const char *name, int value)
{
struct Variable *var;
/* Try to find variable with given name */
var = find_variable(name);
/* Not found */
if (!var)
{
/* Try to find empty place */
var = find_variable_empty();
if (!var)
{
/* Variables needs to be reallocated */
struct Variable* tmp = realloc(l_variables, (++l_variable_count) *
sizeof(struct Variable));
if (tmp == NULL)
{
perror("Unable to allocate memory for variables\n");
exit(EXIT_FAILURE);
}
l_variables = tmp;
/* Pointer to variable structure */
var = &l_variables[l_variable_count - 1];
/* Store name */
strcpy(var->name, name);
}
}
assert(var);
/* Store variable value */
var->value = value;
}
/* ************************************************************************ */
int has_variable(const char *name)
{
/* Try to find variable */
return (find_variable(name) != NULL);
}
/* ************************************************************************ */
int get_variable(const char *name)
{
/* Try to find variable */
struct Variable *var = find_variable(name);
/* Return variable value */
if (var)
return var->value;
/* Not found */
return 0;
}
/* ************************************************************************ */
void unset_variable(const char *name)
{
/* Try to find variable */
struct Variable *var = find_variable(name);
/* Remove variable name */
if (var)
var->name[0] = '\0';
}
/* ************************************************************************ */
void clean_up(void)
{
if (l_current_expr)
free_sexpr(l_current_expr);
if (l_variables)
free(l_variables);
printf("Bye.\n");
}
/* ************************************************************************ */
struct SExpression *func_arithm_base(struct SExpression *expr, arithm_func_t func)
{
int result;
int* argv;
unsigned int argc = 0;
int* args_ptr;
struct SExpression *tmp;
assert(expr);
assert(func);
/* NIL */
if (!expr->right)
return expr;
/* Count number of arguments */
/* The first place is for function name */
for (tmp = expr->right; tmp != NULL; tmp = tmp->right)
{
argc++;
}
/* Allocate memory for arguments */
/* NOTE: C99 - variable-length automatic array */
argv = calloc(argc, sizeof(int));
if (argv == NULL)
{
perror("Cannot allocate memory for function arguments!\n");
exit(EXIT_FAILURE);
}
/* Store arguments */
for (tmp = expr->right, args_ptr = argv; tmp != NULL;
tmp = tmp->right, args_ptr++)
{
/* No empty argument */
assert(strlen(tmp->lvalue) > 0);
/* Check first character for alpha */
if (isalpha(tmp->lvalue[0]))
{
*args_ptr = get_variable(tmp->lvalue);
}
else
{
*args_ptr = atoi(tmp->lvalue);
}
}
/* Call function to evaluate */
result = func(argc, argv);
/* Free arguments */
free(argv);
/* Free argument expressions */
free_sexpr(expr->right);
expr->right = NULL;
/* Store result into expression */
sprintf(expr->lvalue, "%d", result);
/* Result is value */
expr->type = TYPE_VALUE;
return expr;
}
/* ************************************************************************ */