-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheme.c
executable file
·162 lines (140 loc) · 3.19 KB
/
scheme.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
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <assert.h>
#include <stdlib.h>
#include <gc.h>
#include "type.h"
#include "common.h"
#include "error.h"
#include "io.h"
#include "eval.h"
#include "env.h"
#include "symbol.h"
#include "str.h"
#include "port.h"
#include "primitive_procedure.h"
#include "stack.h"
#include "read.h"
#include "char.h"
#include "elab.h"
static
void
populate_initial_environment(int argc, char** argv, TYPE* env)
{
TYPE* sexp;
TYPE* port;
const char* prelude = "/home/per/git/scm/prelude.scm";
port = open_input_file(mk_string_with_length(prelude,
strlen(prelude)));
do
{
sexp = read_from_port(port);
if (!is_eof_object(sexp))
{
stack_init();
sexp = xlat(sexp);
eval(sexp, env);
}
}
while (!is_eof_object(sexp));
close_input_port(port);
}
void
interactive(TYPE* env)
{
TYPE* sexp;
while (TRUE)
{
fprintf(stdout, ">");
if (fflush(NULL) == -1)
{
fprintf(stderr, "INTERACTIVE: could not flush buffers.\n");
exit(1);
}
stack_init();
sexp = scm_read();
sexp = xlat(sexp);
display(eval(sexp, env));
}
}
void
script_mode(int argc, char** argv, TYPE* env)
{
TYPE* sexp;
const char* file_name = argv[1];
TYPE* port = open_input_file(mk_string_with_length(file_name,
strlen(file_name)));
TYPE* hash = mk_char('#');
TYPE* newline = mk_char('\n');
TYPE* c = peek_char_from_port(port);
/* if the first line has # we assume it is a "#!/..." line */
if (is_char_equal(c, hash))
{
do
{
c = read_char_from_port(port);
}
while (!is_eof_object(c) && !is_char_equal(c, newline));
}
do
{
stack_init();
sexp = read_from_port(port);
if (is_eof_object(sexp))
{
break;
}
sexp = xlat(sexp);
eval(sexp, env);
}
while(TRUE);
}
int
main(int argc, char** argv)
{
TYPE* env;
TYPE* environment_symbol;
int script_has_run = FALSE;
GC_INIT();
init_symbol_table();
init_primitive_procedures();
env = get_global_env();
environment_symbol = mk_symbol("environment");
define_variable(environment_symbol, env, env);
/* @todo fix reading of prelude in a more robust way */
populate_initial_environment(argc, argv, env);
int status = setjmp(__c_env__);
switch(status)
{
case SETJMP_INIT:
/* first time in setjmp */
break;
case NO_ERROR:
case PARSE_ERROR:
case EVAL_ERROR:
case APPLY_ERROR:
case TYPE_ERROR:
case CONSTRAINT_ERROR:
case OS_ERROR:
printf("%s\n", __error_info__);
break;
default:
assert(FALSE && "CATCH_ERROR: not an implemented error");
}
if (argc < 2)
{
interactive(env);
}
else
{
if (script_has_run)
{
fprintf(stderr, "MAIN: error in script\n");
return 1;
}
script_has_run = TRUE;
script_mode(argc, argv, env);
}
return 0;
}