*
This commit is contained in:
parent
ce345f2440
commit
1185690ce6
24 changed files with 597 additions and 86 deletions
59
src/main.c
59
src/main.c
|
|
@ -11,26 +11,50 @@
|
|||
|
||||
#include "vendor/mpc.h"
|
||||
|
||||
#define REPL_BUFFER_SIZE 4096
|
||||
|
||||
I repl(void) {
|
||||
Vm vm = {0};
|
||||
vm_init(&vm);
|
||||
|
||||
Bc *chunk = chunk_new();
|
||||
char input[REPL_BUFFER_SIZE];
|
||||
|
||||
I idx = chunk_add_constant(chunk, NUM(10));
|
||||
chunk_emit_byte(chunk, OP_CONST);
|
||||
chunk_emit_sleb128(chunk, idx);
|
||||
chunk_emit_byte(chunk, OP_CONST);
|
||||
chunk_emit_sleb128(chunk, idx);
|
||||
chunk_emit_byte(chunk, OP_ADD);
|
||||
chunk_emit_byte(chunk, OP_RETURN);
|
||||
|
||||
disassemble(chunk, "test chunk");
|
||||
I res = vm_run(&vm, chunk, 0);
|
||||
|
||||
chunk_release(chunk);
|
||||
for (;;) {
|
||||
printf("> ");
|
||||
fflush(stdout);
|
||||
if (fgets(input, REPL_BUFFER_SIZE, stdin) == NULL) {
|
||||
printf("\n");
|
||||
break;
|
||||
}
|
||||
I is_empty = 1;
|
||||
for (char *p = input; *p; p++) {
|
||||
if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') {
|
||||
is_empty = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_empty)
|
||||
continue;
|
||||
if (strncmp(input, "bye", 3) == 0 || strncmp(input, "quit", 4) == 0)
|
||||
break;
|
||||
mpc_result_t res;
|
||||
if (!mpc_parse("<repl>", input, Program, &res)) {
|
||||
mpc_err_print(res.error);
|
||||
mpc_err_delete(res.error);
|
||||
continue;
|
||||
}
|
||||
Cm cm = {0};
|
||||
compiler_init(&cm, &vm, "<repl>");
|
||||
Bc *chunk = compile_program(&cm, res.output);
|
||||
mpc_ast_delete(res.output);
|
||||
if (chunk != NULL) {
|
||||
vm_run(&vm, chunk, 0);
|
||||
chunk_release(chunk);
|
||||
}
|
||||
compiler_deinit(&cm);
|
||||
}
|
||||
vm_deinit(&vm);
|
||||
return !res;
|
||||
return 0;
|
||||
}
|
||||
|
||||
I loadfile(const char *fname) {
|
||||
|
|
@ -44,11 +68,14 @@ I loadfile(const char *fname) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
Bc *chunk = compile_program(&vm.gc, res.output);
|
||||
Cm cm = {0};
|
||||
compiler_init(&cm, &vm, fname);
|
||||
|
||||
Bc *chunk = compile_program(&cm, res.output);
|
||||
mpc_ast_delete(res.output);
|
||||
|
||||
if (chunk != NULL) {
|
||||
disassemble(chunk, fname);
|
||||
// disassemble(chunk, fname, &vm.dictionary);
|
||||
I res = vm_run(&vm, chunk, 0);
|
||||
chunk_release(chunk);
|
||||
vm_deinit(&vm);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue