-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoe.c
124 lines (102 loc) · 2.32 KB
/
moe.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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cfunc.h"
#include "cons.h"
#include "error.h"
#include "eval.h"
#include "gc.h"
#include "number.h"
#include "object.h"
#include "print.h"
#include "read.h"
#include "symbol.h"
#include "builtin_core.h"
#include "builtin_math.h"
static pobject global_env = NIL;
static char * input()
{
static char buf[1024];
printf("\n> ");
fflush(stdout);
return fgets(buf, 1024, stdin);
}
static pobject eval_file(char *filename)
{
pobject ast, result;
long size;
char *buf;
FILE *fp;
fp = fopen(filename, "r");
if (fp) {
fseek(fp, 0, SEEK_END);
size = ftell(fp);
buf = malloc(size + 9);
memcpy(buf, "(begin ", 7);
rewind(fp);
size = fread(buf + 7, sizeof(char), size, fp);
buf[size+7] = ')';
buf[size+8] = '\0';
ast = moe_read(buf);
result = eval(global_env, ast);
free(buf);
return result;
} else {
fprintf(stderr, "cannot open file: %s\n", filename);
}
return NIL;
}
static void init()
{
object_new_count = 0;
object_free_count = 0;
object_true = symbol_intern("#t");
symbol_parent_env = symbol_intern("__parent_env__");
symbol_quote = symbol_intern("quote");
builtin_core_init(&global_env);
builtin_math_init(&global_env);
}
static void cleanup()
{
gc_collect(NIL);
symbol_cleanup();
if ((object_new_count - object_free_count) > 0) {
moe_warning("%d leaked objects detected after cleanup.",
object_new_count - object_free_count);
}
}
static void cleanup_signal_handler(int sig)
{
cleanup();
signal(sig, SIG_DFL);
kill(getpid(), sig);
}
static void run()
{
char *code;
while ((code = input())) {
pobject ast, result;
ast = moe_read(code);
result = eval(global_env, ast);
printf(" --> ");
println(result);
}
}
int main(int argc, char *argv[])
{
init();
atexit(cleanup);
signal(SIGINT, cleanup_signal_handler);
signal(SIGKILL, cleanup_signal_handler);
eval_file("startup.lisp");
if (argc > 1) {
int i;
for (i = 1; i < argc; ++i)
eval_file(argv[i]);
} else {
run();
}
return 0;
}