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

@@ -176,6 +176,7 @@ static void read_ident(struct l2_lexer *lexer, struct l2_token *tok) {
int ch = peek_ch(lexer);

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

@@ -190,11 +191,12 @@ static void read_ident(struct l2_lexer *lexer, struct l2_token *tok) {
case '.':
case ':':
case EOF:
tok->v.str[idx] = '\0';
return;
}

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

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

@@ -78,7 +78,6 @@ static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val) {
}

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];
l2_bitset_unset(&vm->valueset, id);


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

@@ -18,7 +18,7 @@ static struct l2_vm_value *var_lookup(const char *name) {
return &vm.values[id];
}

static void init(const char *str) {
static int exec(const char *str) {
r.r.read = l2_io_mem_read;
r.idx = 0;
r.len = strlen(str);
@@ -29,21 +29,24 @@ static void init(const char *str) {
w.len = 0;
w.mem = NULL;
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);
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(var_lookup("foo")->real == 10);
}

Loading…
Cancel
Save