Browse Source

naming things

master
Martin Dørum 3 years 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

}; };
}; };


#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 { struct l2_vm_array {
size_t size; size_t size;

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

#include <stdio.h> #include <stdio.h>


static void print_val(struct l2_vm *vm, struct l2_io_writer *out, struct l2_vm_value *val) { 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: case L2_VAL_TYPE_NONE:
l2_io_printf(out, "(none)"); l2_io_printf(out, "(none)");
break; break;
} }


struct l2_vm_value *val = &vm->values[argv[0]]; 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); return l2_vm_type_error(vm, val);
} }


double sum = val->real; double sum = val->real;
for (l2_word i = 1; i < argc; ++i) { for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[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); return l2_vm_type_error(vm, val);
} }


} }


struct l2_vm_value *val = &vm->values[argv[0]]; 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); return l2_vm_type_error(vm, val);
} }


double sum = val->real; double sum = val->real;
for (l2_word i = 1; i < argc; ++i) { for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[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); return l2_vm_type_error(vm, val);
} }


} }


struct l2_vm_value *val = &vm->values[argv[0]]; 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); return l2_vm_type_error(vm, val);
} }


double sum = val->real; double sum = val->real;
for (l2_word i = 1; i < argc; ++i) { for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[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); return l2_vm_type_error(vm, val);
} }


} }


struct l2_vm_value *val = &vm->values[argv[0]]; 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); return l2_vm_type_error(vm, val);
} }


double sum = val->real; double sum = val->real;
for (l2_word i = 1; i < argc; ++i) { for (l2_word i = 1; i < argc; ++i) {
val = &vm->values[argv[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); return l2_vm_type_error(vm, val);
} }


return vm->kfalse; 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 (typ == L2_VAL_TYPE_ATOM) {
if (a->atom != b->atom) { if (a->atom != b->atom) {
return vm->kfalse; return vm->kfalse;
return vm->ktrue; \ return vm->ktrue; \
} \ } \
struct l2_vm_value *lhs = &vm->values[argv[0]]; \ 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); \ return l2_vm_type_error(vm, lhs); \
} \ } \
for (l2_word i = 1; i < argc; ++i) { \ for (l2_word i = 1; i < argc; ++i) { \
struct l2_vm_value *rhs = &vm->values[argv[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); \ return l2_vm_type_error(vm, rhs); \
} \ } \
if (!(lhs->real op rhs->real)) { \ if (!(lhs->real op rhs->real)) { \
ret->real = 0; ret->real = 0;


struct l2_vm_value *val = &vm->values[argv[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_NONE:
case L2_VAL_TYPE_ATOM: case L2_VAL_TYPE_ATOM:
case L2_VAL_TYPE_REAL: case L2_VAL_TYPE_REAL:
struct l2_vm_value *cond = &vm->values[argv[0]]; struct l2_vm_value *cond = &vm->values[argv[0]];


if ( 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) { cond->atom == vm->values[vm->ktrue].atom) {
l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0); l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
struct l2_vm_value *ret = &vm->values[ret_id]; struct l2_vm_value *ret = &vm->values[ret_id];
static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) { static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) {
struct l2_vm_value *ret = &vm->values[retval]; struct l2_vm_value *ret = &vm->values[retval];
if ( 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) { ret->atom == vm->values[vm->ktrue].atom) {
return cont; return cont;
} }

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

} }


void l2_vm_print_val(struct l2_vm_value *val) { 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: case L2_VAL_TYPE_NONE:
printf("NONE\n"); printf("NONE\n");
break; break;

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



val->flags |= L2_VAL_MARKED; 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) { if (typ == L2_VAL_TYPE_ARRAY) {
gc_mark_array(vm, val); gc_mark_array(vm, val);
} else if (typ == L2_VAL_TYPE_NAMESPACE) { } else if (typ == L2_VAL_TYPE_NAMESPACE) {


// Don't need to do anything more; the next round of GC will free // Don't need to do anything more; the next round of GC will free
// whichever values were only referenced by the array // 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) { if (typ == L2_VAL_TYPE_ARRAY) {
free(val->array); free(val->array);
} else if (typ == L2_VAL_TYPE_BUFFER) { } else if (typ == L2_VAL_TYPE_BUFFER) {
} }


l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val) { 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) { void l2_vm_free(struct l2_vm *vm) {
l2_word stack_base = vm->sptr; l2_word stack_base = vm->sptr;


struct l2_vm_value *func = &vm->values[func_id]; 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 // C functions are called differently from language functions
if (typ == L2_VAL_TYPE_CFUNCTION) { if (typ == L2_VAL_TYPE_CFUNCTION) {
while (1) { while (1) {
l2_word cont_id = vm->stack[vm->sptr - 1]; l2_word cont_id = vm->stack[vm->sptr - 1];
struct l2_vm_value *cont = &vm->values[cont_id]; 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; break;
} }




struct l2_vm_value *call = &vm->values[call_id]; 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); l2_word retval = call->cfunc(vm, 0, NULL);
vm->stack[vm->sptr - 1] = cont->cont->callback(vm, retval, cont_id); 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, // Leave the continuation on the stack,
// let the L2_OP_RET code deal with it // let the L2_OP_RET code deal with it
cont->flags |= L2_VAL_CONT_CALLBACK; cont->flags |= L2_VAL_CONT_CALLBACK;


case L2_OP_DISCARD: case L2_OP_DISCARD:
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); l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
vm->halted = 1; vm->halted = 1;
} }
case L2_OP_SWAP_DISCARD: case L2_OP_SWAP_DISCARD:
vm->stack[vm->sptr - 2] = vm->stack[vm->sptr - 1]; vm->stack[vm->sptr - 2] = vm->stack[vm->sptr - 1];
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); l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
vm->halted = 1; vm->halted = 1;
} }
} }


int iscont = 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 = int nocallback =
!iscont || (cont->flags & L2_VAL_CONT_CALLBACK && cont->cont == NULL); !iscont || (cont->flags & L2_VAL_CONT_CALLBACK && cont->cont == NULL);
if (nocallback) { if (nocallback) {
cont_id = retval; cont_id = retval;
cont = &vm->values[cont_id]; 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; vm->stack[vm->sptr - 1] = retval;
break; break;
} }
l2_word key = read(vm); \ l2_word key = read(vm); \
l2_word arr_id = vm->stack[--vm->sptr]; \ l2_word arr_id = vm->stack[--vm->sptr]; \
struct l2_vm_value *arr = &vm->values[arr_id]; \ 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); \ vm->stack[vm->sptr++] = l2_vm_type_error(vm, arr); \
} else if (key >= arr->extra.arr_length) { \ } else if (key >= arr->extra.arr_length) { \
vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range"); \ vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range"); \
l2_word val = vm->stack[vm->sptr - 1]; \ l2_word val = vm->stack[vm->sptr - 1]; \
l2_word arr_id = vm->stack[vm->sptr - 2]; \ l2_word arr_id = vm->stack[vm->sptr - 2]; \
struct l2_vm_value *arr = &vm->values[arr_id]; \ 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); \ vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, arr); \
} else if (key >= arr->extra.arr_length) { \ } else if (key >= arr->extra.arr_length) { \
vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range"); \ vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range"); \


struct l2_vm_value *key = &vm->values[key_id]; struct l2_vm_value *key = &vm->values[key_id];
struct l2_vm_value *container = &vm->values[container_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); vm->stack[vm->sptr++] = l2_vm_type_error(vm, key);
} else if (key->real >= container->extra.arr_length) { } else if (key->real >= container->extra.arr_length) {
vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range"); vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range");
} else { } else {
vm->stack[vm->sptr++] = container->array->data[(l2_word)key->real]; 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); vm->stack[vm->sptr++] = l2_vm_type_error(vm, key);
} else { } else {
vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, container, key->atom); vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, container, key->atom);
struct l2_vm_value *key = &vm->values[key_id]; struct l2_vm_value *key = &vm->values[key_id];
struct l2_vm_value *container = &vm->values[container_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); vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, key);
} else if (key->real >= container->extra.arr_length) { } else if (key->real >= container->extra.arr_length) {
vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range"); vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range");
} else { } else {
container->array->data[(size_t)key->real] = val; 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); vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, key);
} else { } else {
l2_vm_namespace_set(container, key->atom, val); l2_vm_namespace_set(container, key->atom, val);

Loading…
Cancel
Save