Browse Source

naming things

master
Martin Dørum 7 months ago
parent
commit
1c3cb1d6fb
4 changed files with 38 additions and 38 deletions
  1. 1
    1
      include/lang2/vm/vm.h
  2. 15
    15
      lib/vm/builtins.c
  3. 1
    1
      lib/vm/print.c
  4. 21
    21
      lib/vm/vm.c

+ 1
- 1
include/lang2/vm/vm.h View File

@@ -73,7 +73,7 @@ struct l2_vm_value {
};
};

#define l2_vm_value_type(val) ((enum l2_value_type)((val)->flags & 0x0f))
#define l2_value_get_type(val) ((enum l2_value_type)((val)->flags & 0x0f))

struct l2_vm_array {
size_t size;

+ 15
- 15
lib/vm/builtins.c View File

@@ -3,7 +3,7 @@
#include <stdio.h>

static void print_val(struct l2_vm *vm, struct l2_io_writer *out, struct l2_vm_value *val) {
switch (l2_vm_value_type(val)) {
switch (l2_value_get_type(val)) {
case L2_VAL_TYPE_NONE:
l2_io_printf(out, "(none)");
break;
@@ -67,14 +67,14 @@ l2_word l2_builtin_add(struct l2_vm *vm, l2_word argc, l2_word *argv) {
}

struct l2_vm_value *val = &vm->values[argv[0]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

double sum = val->real;
for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[i]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

@@ -94,14 +94,14 @@ l2_word l2_builtin_sub(struct l2_vm *vm, l2_word argc, l2_word *argv) {
}

struct l2_vm_value *val = &vm->values[argv[0]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

double sum = val->real;
for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[i]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

@@ -121,14 +121,14 @@ l2_word l2_builtin_mul(struct l2_vm *vm, l2_word argc, l2_word *argv) {
}

struct l2_vm_value *val = &vm->values[argv[0]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

double sum = val->real;
for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[i]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

@@ -148,14 +148,14 @@ l2_word l2_builtin_div(struct l2_vm *vm, l2_word argc, l2_word *argv) {
}

struct l2_vm_value *val = &vm->values[argv[0]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

double sum = val->real;
for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[i]];
if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
return l2_vm_type_error(vm, val);
}

@@ -180,7 +180,7 @@ l2_word l2_builtin_eq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
return vm->kfalse;
}

enum l2_value_type typ = l2_vm_value_type(a);
enum l2_value_type typ = l2_value_get_type(a);
if (typ == L2_VAL_TYPE_ATOM) {
if (a->atom != b->atom) {
return vm->kfalse;
@@ -227,12 +227,12 @@ l2_word name(struct l2_vm *vm, l2_word argc, l2_word *argv) { \
return vm->ktrue; \
} \
struct l2_vm_value *lhs = &vm->values[argv[0]]; \
if (l2_vm_value_type(lhs) != L2_VAL_TYPE_REAL) { \
if (l2_value_get_type(lhs) != L2_VAL_TYPE_REAL) { \
return l2_vm_type_error(vm, lhs); \
} \
for (l2_word i = 1; i < argc; ++i) { \
struct l2_vm_value *rhs = &vm->values[argv[i]]; \
if (l2_vm_value_type(rhs) != L2_VAL_TYPE_REAL) { \
if (l2_value_get_type(rhs) != L2_VAL_TYPE_REAL) { \
return l2_vm_type_error(vm, rhs); \
} \
if (!(lhs->real op rhs->real)) { \
@@ -272,7 +272,7 @@ l2_word l2_builtin_len(struct l2_vm *vm, l2_word argc, l2_word *argv) {
ret->real = 0;

struct l2_vm_value *val = &vm->values[argv[0]];
switch (l2_vm_value_type(val)) {
switch (l2_value_get_type(val)) {
case L2_VAL_TYPE_NONE:
case L2_VAL_TYPE_ATOM:
case L2_VAL_TYPE_REAL:
@@ -307,7 +307,7 @@ l2_word l2_builtin_if(struct l2_vm *vm, l2_word argc, l2_word *argv) {
struct l2_vm_value *cond = &vm->values[argv[0]];

if (
l2_vm_value_type(cond) == L2_VAL_TYPE_ATOM &&
l2_value_get_type(cond) == L2_VAL_TYPE_ATOM &&
cond->atom == vm->values[vm->ktrue].atom) {
l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
struct l2_vm_value *ret = &vm->values[ret_id];
@@ -331,7 +331,7 @@ struct loop_context {
static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) {
struct l2_vm_value *ret = &vm->values[retval];
if (
l2_vm_value_type(ret) == L2_VAL_TYPE_ATOM &&
l2_value_get_type(ret) == L2_VAL_TYPE_ATOM &&
ret->atom == vm->values[vm->ktrue].atom) {
return cont;
}

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

@@ -38,7 +38,7 @@ static double read_d8le(unsigned char *ops, size_t *ptr) {
}

void l2_vm_print_val(struct l2_vm_value *val) {
switch (l2_vm_value_type(val)) {
switch (l2_value_get_type(val)) {
case L2_VAL_TYPE_NONE:
printf("NONE\n");
break;

+ 21
- 21
lib/vm/vm.c View File

@@ -42,7 +42,7 @@ static void gc_mark(struct l2_vm *vm, l2_word id) {

val->flags |= L2_VAL_MARKED;

int typ = l2_vm_value_type(val);
int typ = l2_value_get_type(val);
if (typ == L2_VAL_TYPE_ARRAY) {
gc_mark_array(vm, val);
} else if (typ == L2_VAL_TYPE_NAMESPACE) {
@@ -91,7 +91,7 @@ static void gc_free(struct l2_vm *vm, l2_word id) {

// Don't need to do anything more; the next round of GC will free
// whichever values were only referenced by the array
int typ = l2_vm_value_type(val);
int typ = l2_value_get_type(val);
if (typ == L2_VAL_TYPE_ARRAY) {
free(val->array);
} else if (typ == L2_VAL_TYPE_BUFFER) {
@@ -261,7 +261,7 @@ l2_word l2_vm_error(struct l2_vm *vm, const char *fmt, ...) {
}

l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val) {
return l2_vm_error(vm, "Unexpected type %s", l2_value_type_name(l2_vm_value_type(val)));
return l2_vm_error(vm, "Unexpected type %s", l2_value_type_name(l2_value_get_type(val)));
}

void l2_vm_free(struct l2_vm *vm) {
@@ -307,7 +307,7 @@ static void call_func(
l2_word stack_base = vm->sptr;

struct l2_vm_value *func = &vm->values[func_id];
enum l2_value_type typ = l2_vm_value_type(func);
enum l2_value_type typ = l2_value_get_type(func);

// C functions are called differently from language functions
if (typ == L2_VAL_TYPE_CFUNCTION) {
@@ -317,7 +317,7 @@ static void call_func(
while (1) {
l2_word cont_id = vm->stack[vm->sptr - 1];
struct l2_vm_value *cont = &vm->values[cont_id];
if (l2_vm_value_type(cont) != L2_VAL_TYPE_CONTINUATION) {
if (l2_value_get_type(cont) != L2_VAL_TYPE_CONTINUATION) {
break;
}

@@ -332,10 +332,10 @@ static void call_func(

struct l2_vm_value *call = &vm->values[call_id];

if (l2_vm_value_type(call) == L2_VAL_TYPE_CFUNCTION) {
if (l2_value_get_type(call) == L2_VAL_TYPE_CFUNCTION) {
l2_word retval = call->cfunc(vm, 0, NULL);
vm->stack[vm->sptr - 1] = cont->cont->callback(vm, retval, cont_id);
} else if (l2_vm_value_type(call) == L2_VAL_TYPE_FUNCTION) {
} else if (l2_value_get_type(call) == L2_VAL_TYPE_FUNCTION) {
// Leave the continuation on the stack,
// let the L2_OP_RET code deal with it
cont->flags |= L2_VAL_CONT_CALLBACK;
@@ -426,7 +426,7 @@ void l2_vm_step(struct l2_vm *vm) {

case L2_OP_DISCARD:
vm->sptr -= 1;
if (l2_vm_value_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
if (l2_value_get_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
vm->halted = 1;
}
@@ -435,7 +435,7 @@ void l2_vm_step(struct l2_vm *vm) {
case L2_OP_SWAP_DISCARD:
vm->stack[vm->sptr - 2] = vm->stack[vm->sptr - 1];
vm->sptr -= 1;
if (l2_vm_value_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
if (l2_value_get_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
vm->halted = 1;
}
@@ -511,7 +511,7 @@ void l2_vm_step(struct l2_vm *vm) {
}

int iscont =
cont != NULL && l2_vm_value_type(cont) == L2_VAL_TYPE_CONTINUATION;
cont != NULL && l2_value_get_type(cont) == L2_VAL_TYPE_CONTINUATION;
int nocallback =
!iscont || (cont->flags & L2_VAL_CONT_CALLBACK && cont->cont == NULL);
if (nocallback) {
@@ -528,7 +528,7 @@ void l2_vm_step(struct l2_vm *vm) {
cont_id = retval;
cont = &vm->values[cont_id];

if (l2_vm_value_type(cont) != L2_VAL_TYPE_CONTINUATION) {
if (l2_value_get_type(cont) != L2_VAL_TYPE_CONTINUATION) {
vm->stack[vm->sptr - 1] = retval;
break;
}
@@ -637,7 +637,7 @@ void l2_vm_step(struct l2_vm *vm) {
l2_word key = read(vm); \
l2_word arr_id = vm->stack[--vm->sptr]; \
struct l2_vm_value *arr = &vm->values[arr_id]; \
if (l2_vm_value_type(arr) != L2_VAL_TYPE_ARRAY) { \
if (l2_value_get_type(arr) != L2_VAL_TYPE_ARRAY) { \
vm->stack[vm->sptr++] = l2_vm_type_error(vm, arr); \
} else if (key >= arr->extra.arr_length) { \
vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range"); \
@@ -653,7 +653,7 @@ void l2_vm_step(struct l2_vm *vm) {
l2_word val = vm->stack[vm->sptr - 1]; \
l2_word arr_id = vm->stack[vm->sptr - 2]; \
struct l2_vm_value *arr = &vm->values[arr_id]; \
if (l2_vm_value_type(arr) != L2_VAL_TYPE_ARRAY) { \
if (l2_value_get_type(arr) != L2_VAL_TYPE_ARRAY) { \
vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, arr); \
} else if (key >= arr->extra.arr_length) { \
vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range"); \
@@ -670,16 +670,16 @@ void l2_vm_step(struct l2_vm *vm) {

struct l2_vm_value *key = &vm->values[key_id];
struct l2_vm_value *container = &vm->values[container_id];
if (l2_vm_value_type(container) == L2_VAL_TYPE_ARRAY) {
if (l2_vm_value_type(key) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(container) == L2_VAL_TYPE_ARRAY) {
if (l2_value_get_type(key) != L2_VAL_TYPE_REAL) {
vm->stack[vm->sptr++] = l2_vm_type_error(vm, key);
} else if (key->real >= container->extra.arr_length) {
vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range");
} else {
vm->stack[vm->sptr++] = container->array->data[(l2_word)key->real];
}
} else if (l2_vm_value_type(container) == L2_VAL_TYPE_NAMESPACE) {
if (l2_vm_value_type(key) != L2_VAL_TYPE_ATOM) {
} else if (l2_value_get_type(container) == L2_VAL_TYPE_NAMESPACE) {
if (l2_value_get_type(key) != L2_VAL_TYPE_ATOM) {
vm->stack[vm->sptr++] = l2_vm_type_error(vm, key);
} else {
vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, container, key->atom);
@@ -700,16 +700,16 @@ void l2_vm_step(struct l2_vm *vm) {
struct l2_vm_value *key = &vm->values[key_id];
struct l2_vm_value *container = &vm->values[container_id];

if (l2_vm_value_type(container) == L2_VAL_TYPE_ARRAY) {
if (l2_vm_value_type(key) != L2_VAL_TYPE_REAL) {
if (l2_value_get_type(container) == L2_VAL_TYPE_ARRAY) {
if (l2_value_get_type(key) != L2_VAL_TYPE_REAL) {
vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, key);
} else if (key->real >= container->extra.arr_length) {
vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range");
} else {
container->array->data[(size_t)key->real] = val;
}
} else if (l2_vm_value_type(container) == L2_VAL_TYPE_NAMESPACE) {
if (l2_vm_value_type(key) != L2_VAL_TYPE_ATOM) {
} else if (l2_value_get_type(container) == L2_VAL_TYPE_NAMESPACE) {
if (l2_value_get_type(key) != L2_VAL_TYPE_ATOM) {
vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, key);
} else {
l2_vm_namespace_set(container, key->atom, val);

Loading…
Cancel
Save