Browse Source

minor stuff

master
Martin Dørum 3 years ago
parent
commit
4af243e30c
3 changed files with 17 additions and 13 deletions
  1. 3
    1
      lib/parse/lex.c
  2. 0
    1
      lib/vm/vm.c
  3. 14
    11
      test/src/eval.t.c

+ 3
- 1
lib/parse/lex.c View File

int ch = peek_ch(lexer); int ch = peek_ch(lexer);


if (is_whitespace(ch)) { if (is_whitespace(ch)) {
tok->v.str[idx] = '\0';
return; return;
} }


case '.': case '.':
case ':': case ':':
case EOF: case EOF:
tok->v.str[idx] = '\0';
return; return;
} }


tok->v.str[idx++] = (char)read_ch(lexer); tok->v.str[idx++] = (char)read_ch(lexer);
if (idx >= size) {
if (idx + 1 >= size) {
size *= 2; size *= 2;
char *newbuf = realloc(tok->v.str, size); char *newbuf = realloc(tok->v.str, size);
if (newbuf == NULL) { if (newbuf == NULL) {

+ 0
- 1
lib/vm/vm.c View File

} }


static void gc_free(struct l2_vm *vm, l2_word id) { static void gc_free(struct l2_vm *vm, l2_word id) {
printf("GC FREE %i\n", id);
struct l2_vm_value *val = &vm->values[id]; struct l2_vm_value *val = &vm->values[id];
l2_bitset_unset(&vm->valueset, id); l2_bitset_unset(&vm->valueset, id);



+ 14
- 11
test/src/eval.t.c View File

return &vm.values[id]; return &vm.values[id];
} }


static void init(const char *str) {
static int exec(const char *str) {
r.r.read = l2_io_mem_read; r.r.read = l2_io_mem_read;
r.idx = 0; r.idx = 0;
r.len = strlen(str); r.len = strlen(str);
w.len = 0; w.len = 0;
w.mem = NULL; w.mem = NULL;
l2_gen_init(&gen, (struct l2_io_writer *)&w); l2_gen_init(&gen, (struct l2_io_writer *)&w);
}


static void done() {
l2_gen_free(&gen);
if (l2_parse_program(&lex, &gen, &err) < 0) {
return -1;
}

l2_vm_init(&vm, (l2_word *)w.mem, w.len / sizeof(l2_word));
l2_vm_run(&vm);

free(w.mem); free(w.mem);
return 0;
} }


describe(parse) {
test("parse program") {
init("foo := 10");
defer(done());
assert(l2_parse_program(&lex, &gen, &err) >= 0);
describe(exec) {
test("exec assignment") {
exec("foo := 10");
defer(l2_vm_free(&vm));
defer(l2_gen_free(&gen));


l2_vm_init(&vm, (l2_word *)w.mem, w.len / sizeof(l2_word));
l2_vm_run(&vm);
assert(l2_vm_value_type(var_lookup("foo")) == L2_VAL_TYPE_REAL); assert(l2_vm_value_type(var_lookup("foo")) == L2_VAL_TYPE_REAL);
assert(var_lookup("foo")->real == 10); assert(var_lookup("foo")->real == 10);
} }

Loading…
Cancel
Save