-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c.backup
85 lines (71 loc) · 1.91 KB
/
main.c.backup
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
#include <stdio.h>
#include <sys/types.h>
#include "eval/hashmap.h"
#include "eval/object.h"
#include "lexer/lexer.h"
#include "parser/ast_parser/ast_parser.h"
#include "eval/eval.h"
#include "eval/record.h"
#define SHELL_BUF_INCR 80
#define SHELL_PROMPT ">>> "
hashmap *heap;
char*
shell_readline()
{
printf(SHELL_PROMPT);
char* buf = (char*) malloc(SHELL_BUF_INCR * sizeof(char));
int buf_size = SHELL_BUF_INCR;
int i = 0;
char c;
while ((c = getchar()) != '\n' && c != '\0' && c != EOF) {
buf[i++] = c;
if (i == buf_size) {
buf_size = buf_size + SHELL_BUF_INCR;
buf = (char*) realloc(buf, buf_size + SHELL_BUF_INCR);
}
}
buf[i] = '\0';
return buf;
}
int
main (int argc, char** argv)
{
printf("Experimental Interpreter: A toy interpreter for learning\n");
if (argc < 1) {
fprintf(stderr, "Error: Not implemented yet.\n");
return -1;
}
printf("Type 'exit' to exit the interactive shell.\n");
heap = hm_new(HM_INITIAL_SIZE);
char* input;
u_int8_t shell_continue = 1;
int indent_lvl = 0;
while(shell_continue) {
/* TODO this is ugly, don't do this, add returns lol */
input = shell_readline();
if (!strcmp(input, "exit")) {
shell_continue = 0;
free(input);
break;
}
printf("==> Lex output: ----------------------------------\n");
token *list = lex(input);
free(input);
tk_print_ll(list);
printf("==> Parse output: --------------------------------\n");
/* NOTE: NO NEED TO FREE LIST: PARSER FREES AUTOMATICALLY */
ast_node *test = parse_root(list, &indent_lvl);
ast_print_preorder(test, 0);
printf("Indent level: %d\n", indent_lvl);
printf("==> Evaluation: ----------------------------------\n");
obj *res = eval_ast(heap, test);
printf("Evaluated result: ");
obj_print(res);
res = obj_delete(res);
printf("Heap dump:\n");
hm_print(heap);
test = ast_free(test);
printf("==> End. -----------------------------------------\n");
}
heap = hm_clear(heap);
}