Initial parser implementation that runs a simple hello_world.fun script.
This commit is contained in:
parent
284b4d3fa9
commit
7d587fe11b
5 changed files with 237 additions and 45 deletions
57
src/fun.c
57
src/fun.c
|
|
@ -1,52 +1,31 @@
|
|||
#include "bytecode.h"
|
||||
#include "value.h"
|
||||
#include "vm.h"
|
||||
#include "parser.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char **argv) {
|
||||
VM vm;
|
||||
vm_init(&vm);
|
||||
|
||||
/* Example: 0..4 loop */
|
||||
Bytecode *bc = bytecode_new();
|
||||
int c0 = bytecode_add_constant(bc, make_int(0));
|
||||
int c1 = bytecode_add_constant(bc, make_int(1));
|
||||
int c5 = bytecode_add_constant(bc, make_int(5));
|
||||
// If a script path is provided, parse and run it
|
||||
if (argc > 1) {
|
||||
const char *path = argv[1];
|
||||
Bytecode *bc = parse_file_to_bytecode(path);
|
||||
if (!bc) {
|
||||
fprintf(stderr, "Failed to compile script: %s\n", path);
|
||||
return 1;
|
||||
}
|
||||
vm_run(&vm, bc);
|
||||
|
||||
// initialize i = 0
|
||||
bytecode_add_instruction(bc, OP_LOAD_CONST, c0);
|
||||
bytecode_add_instruction(bc, OP_STORE_LOCAL, 0);
|
||||
// Print captured output for user scripts
|
||||
vm_print_output(&vm);
|
||||
vm_clear_output(&vm);
|
||||
|
||||
int lbl_start = bc->instr_count;
|
||||
|
||||
// load i
|
||||
bytecode_add_instruction(bc, OP_LOAD_LOCAL, 0);
|
||||
bytecode_add_instruction(bc, OP_PRINT, 0);
|
||||
|
||||
// i = i + 1
|
||||
bytecode_add_instruction(bc, OP_LOAD_LOCAL, 0);
|
||||
bytecode_add_instruction(bc, OP_LOAD_CONST, c1);
|
||||
bytecode_add_instruction(bc, OP_ADD, 0);
|
||||
bytecode_add_instruction(bc, OP_STORE_LOCAL, 0);
|
||||
|
||||
// check i < 5
|
||||
bytecode_add_instruction(bc, OP_LOAD_LOCAL, 0);
|
||||
bytecode_add_instruction(bc, OP_LOAD_CONST, c5);
|
||||
bytecode_add_instruction(bc, OP_LT, 0);
|
||||
int jmp_back = bytecode_add_instruction(bc, OP_JUMP_IF_FALSE, 0);
|
||||
bytecode_add_instruction(bc, OP_JUMP, lbl_start);
|
||||
|
||||
int end_lbl = bc->instr_count;
|
||||
bytecode_set_operand(bc, jmp_back, end_lbl);
|
||||
|
||||
bytecode_add_instruction(bc, OP_HALT, 0);
|
||||
|
||||
vm_run(&vm, bc);
|
||||
|
||||
// flush captured outputs
|
||||
vm_clear_output(&vm);
|
||||
|
||||
bytecode_free(bc);
|
||||
bytecode_free(bc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue