/
bird.c
233 lines (191 loc) · 6.08 KB
/
bird.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "util.h"
#include "vm.h"
#include "b_unistd.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if !defined _WIN32 && !defined __CYGWIN__
#include <readline/history.h>
#include <readline/readline.h>
#endif
#include <setjmp.h>
#include <signal.h>
#ifdef _WIN32
#include "win32.h"
#endif
static bool continue_repl = true;
sigjmp_buf ctrlc_buf;
void handle_signals(int sig_no) {
if (sig_no == SIGINT) {
printf("\n<KeyboardInterrupt>\n");
continue_repl = false;
siglongjmp(ctrlc_buf, 1);
}
}
static void repl(b_vm *vm) {
if (signal(SIGINT, handle_signals) == SIG_ERR) {
printf("failed to register interrupts with kernel\n");
}
vm->is_repl = true;
printf("Bird %s (running on BVM %s), REPL/Interactive mode = ON\n",
BIRD_VERSION_STRING, BVM_VERSION);
printf("%s, (Build time = %s, %s)\n", COMPILER, __DATE__, __TIME__);
printf("Type \"exit()\" to quit, \"help()\" or \"credits()\" for more information\n");
char *source = (char *) calloc(1, sizeof(char));
int current_line = 0;
int brace_count = 0, paren_count = 0, bracket_count = 0, single_quote_count = 0, double_quote_count = 0;
for (;;) {
while (sigsetjmp(ctrlc_buf, 1) != 0);
if (!continue_repl) {
current_line = 0;
brace_count = 0;
paren_count = 0;
bracket_count = 0;
single_quote_count = 0;
double_quote_count = 0;
// reset source...
memset(source, 0, strlen(source));
continue_repl = true;
}
current_line++;
const char *cursor = "> ";
if (brace_count > 0 || bracket_count > 0 || paren_count > 0) {
cursor = "| ";
} else if (single_quote_count == 1 || double_quote_count == 1) {
cursor = " ";
}
#if defined _WIN32 || defined __CYGWIN__
char buffer[1024];
printf(cursor);
char *line = fgets(buffer, 1024, stdin);
int line_length = strcspn(line, "\r\n");
line[line_length] = 0;
#else
char *line = readline(cursor);
int line_length = (int) strlen(line);
#endif // _WIN32
// terminate early if we receive a terminating command such as exit()
#if defined _MSC_VER && defined _DEBUG
if (strcmp(line, "exit()", line_length) == 0) {
#else
if (strcmp(line, "exit()") == 0) {
#endif
exit(EXIT_SUCCESS);
}
#if !defined _WIN32 && !defined __CYGWIN__
// allow user to navigate through past input in terminal...
add_history(line);
#endif // !_WIN32
// find count of { and }, ( and ), [ and ]
for (int i = 0; i < line_length; i++) {
// scope openers...
if (line[i] == '{')
brace_count++;
else if (line[i] == '(')
paren_count++;
else if (line[i] == '[')
bracket_count++;
// quotes
else if(((i == 0 && line[i] == '\'') || (line[i] == '\'' && line[i - 1] != '\\')) && double_quote_count == 0) {
if(single_quote_count == 0)
single_quote_count = 1;
else
single_quote_count = 0;
}
else if(((i == 0 && line[i] == '"') || (line[i] == '"' && line[i - 1] != '\\')) && single_quote_count == 0){
if(double_quote_count == 0)
double_quote_count = 1;
else
double_quote_count = 0;
}
// scope closers...
else if (line[i] == '}' && brace_count > 0)
brace_count--;
else if (line[i] == ')' && paren_count > 0)
paren_count--;
else if (line[i] == ']' && bracket_count > 0)
bracket_count--;
}
source = append_strings(source, line);
free(line);
if (line_length > 0) {
source = append_strings(source, "\n");
}
if (bracket_count == 0 && paren_count == 0 && brace_count == 0 && single_quote_count == 0 && double_quote_count == 0) {
interpret(vm, source, "<repl>");
fflush(stdout); // flush all outputs
// reset source...
memset(source, 0, strlen(source));
}
}
}
static void run_file(b_vm *vm, const char *file) {
char *source = read_file(file);
if (source == NULL) {
fprintf(stderr, "(Bird):\n Launch aborted for %s\n Reason: %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
b_ptr_result result = interpret(vm, source, file);
free(source);
fflush(stdout);
if (result == PTR_COMPILE_ERR)
exit(EXIT_COMPILE);
if (result == PTR_RUNTIME_ERR)
exit(EXIT_RUNTIME);
}
void show_usage(char *argv[]) {
fprintf(stderr, "Usage: %s [-[d | j | v | g]] [filename]\n", argv[0]);
fprintf(stderr, " -v Show version string.\n");
fprintf(stderr, " -d Show generated bytecode.\n");
fprintf(stderr, " -j Show stack objects during execution.\n");
fprintf(stderr, " -g Sets the minimum heap size in kilobytes before the GC\n"
" can start. [Default = %d (%dmb)]\n", DEFAULT_GC_START / 1024,
DEFAULT_GC_START / (1024 * 1024));
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
bool should_debug_stack = false;
bool should_print_bytecode = false;
int next_gc_start = DEFAULT_GC_START;
if(argc > 1) {
int opt;
while ((opt = getopt(argc, argv, "djvg:")) != -1) {
switch (opt) {
case 'd': should_print_bytecode = true; break;
case 'j': should_debug_stack = true; break;
case 'v': {
printf("Bird " BIRD_VERSION_STRING " (running on BVM " BVM_VERSION ")\n");
return 0;
}
case 'g': {
int next = (int)strtol(optarg, NULL, 10);
if(next > 0) {
next_gc_start = next * 1024; // expected value is in kilobytes
}
break;
}
default: show_usage(argv); break;
}
}
}
b_vm *vm = (b_vm *) malloc(sizeof(b_vm));
memset(vm, 0, sizeof(b_vm));
init_vm(vm);
// set vm options...
vm->should_debug_stack = should_debug_stack;
vm->should_print_bytecode = should_print_bytecode;
vm->next_gc = next_gc_start;
/*// forcing printf buffering for TTYs and terminals
if (isatty(fileno(stdout))) {
char buffer[8192];
setvbuf(stdout, buffer, _IOFBF, sizeof(buffer));
}*/
if (argc == 1 || argc <= optind) {
repl(vm);
} else {
run_file(vm, argv[optind]);
}
free_vm(vm);
return EXIT_SUCCESS;
}