generated from 8dcc/c-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
126 lines (96 loc) · 3.43 KB
/
main.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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* isatty() */
#include "include/util.h"
#include "include/lexer.h"
#include "include/expr.h"
#include "include/parser.h"
#include "include/eval.h"
#include "include/env.h"
#define INPUT_BUFSZ 100
/* Allocate buffer and store a LISP expression in string format. Must be freed
* by the caller. Returns true if it got EOF. */
static bool input_read(char** input) {
size_t input_sz = INPUT_BUFSZ;
*input = sl_safe_malloc(input_sz);
/* Will increase when we encounter '(' and decrease with ')' */
int nesting_level = 0;
/* If true, we found a symbol/constant with nesting_level at 0 */
bool isolated_symbol = false;
char c;
size_t i = 0;
while ((c = getchar()) != EOF) {
/* If we run out of space, allocate more */
if (i >= input_sz) {
input_sz += INPUT_BUFSZ;
sl_safe_realloc(*input, input_sz);
}
/* Store character in string */
(*input)[i++] = c;
if (c == '(') {
nesting_level++;
} else if (c == ')') {
/* We are still in level 0, we should have opened an expression.
* NOTE: This doesn't check if ')' is inside a string, comment,
* etc. */
if (nesting_level <= 0) {
ERR("Encountered ')' before starting an expression.");
break;
}
nesting_level--;
/* We closed all the expressions we opened, we are done */
if (nesting_level <= 0)
break;
} else if (nesting_level == 0) {
/* We are reading outside of an expression */
if (!isolated_symbol && !is_token_separator(c))
/* We found a token outside of a list, it evaluates to itself */
isolated_symbol = true;
else if (isolated_symbol && is_token_separator(c))
/* We just read an isolated symbol, and we found a separator */
break;
}
}
(*input)[i] = '\0';
/* Return true if the last char was EOF, so the caller knows to not call us
* again after it's done processing this expression. */
return c == EOF;
}
int main(void) {
const bool print_prompt = isatty(0);
/* Initialize global environment with symbols like "nil" */
Env* global_env = env_new();
env_init_defaults(global_env);
bool got_eof = false;
while (!got_eof) {
if (print_prompt)
printf("sl> ");
/* Allocate buffer and read an expression */
char* input = NULL;
got_eof = input_read(&input);
/* Get token array from input */
Token* tokens = tokens_scan(input);
/* We are done with the raw input, free it */
free(input);
/* Get root expression from token array */
Expr* expr = parse(tokens);
/* We are done with the token array, free it */
tokens_free(tokens);
if (expr == NULL)
continue;
/* Evaluate expression recursivelly */
Expr* evaluated = eval(global_env, expr);
/* We are done with the original expression */
expr_free(expr);
if (evaluated == NULL)
continue;
expr_println(evaluated);
/* Free the evaluated expression */
expr_free(evaluated);
if (print_prompt)
putchar('\n');
}
env_free(global_env);
return 0;
}