-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.c
85 lines (58 loc) · 2.58 KB
/
interpreter.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "ast.h"
#include "environment.h"
#include "parse_utils.h"
int eval(const node_t* node, environment_t* e) {
switch (node->type) {
case FUNCTION:
return eval(((function_node_t*)node)->body, e);
case ID:
return find(e, ((id_node_t*)node)->value).integer;
case BLOCK: {
environment_t* scope_env = beginScope(e);
block_node_t* bnode = (block_node_t*)node;
declaration_list_t* dae = bnode->declaration_list; // this declaration_list is freed when the whole ast is freed
// NOTE: the ids will be freed with the ast (they were allocated with it, so they should be deallocated with it)
// For each declaration in this scope create an association in this scope's evaluation environment
for (int i = 0; i < dae->size; i++)
// TODO .node could be NULL
assoc(scope_env, dae->declarations[i].id, (union association_v){ .integer = eval(dae->declarations[i].node, scope_env)});
int val = eval(((block_node_t*)node)->body, scope_env);
endScope(scope_env); // free the scope environment and its association array
return val;
}
case NUM:
return ((num_node_t*)node)->value;
case ADD:
return eval(((binary_node_t*)node)->left, e) + eval(((binary_node_t*)node)->right, e);
case SUB:
return eval(((binary_node_t*)node)->left, e) - eval(((binary_node_t*)node)->right, e);
case MUL:
return eval(((binary_node_t*)node)->left, e) * eval(((binary_node_t*)node)->right, e);
case DIV:
return eval(((binary_node_t*)node)->left, e) / eval(((binary_node_t*)node)->right, e);
case UMINUS:
return - eval(((unary_node_t*)node)->child, e);
default:
fprintf(stderr, "ERROR: Undefined eval for operation %d\n!", node->type);
exit(1);
}
}
int main(int argc, char *argv[]) {
fprintf(stderr, "WARNING: The interpreter is lagging behind the compiler and will probably be deprecated soon\n");
printf("Input an expression: ");
printf("Parsing...\n");
node_t* root = parse_root();
environment_t* topenv = newEnvironment();
printf("Evaluating...\n");
int val = eval(root, topenv);
free(topenv);
printf("Freeing AST...\n");
free_ast(root);
printf("Result: %d\n", val);
return 0;
}