Various small changes.

This commit is contained in:
Calvin Rose 2017-02-19 11:19:39 -05:00
parent 69260fa3bd
commit 6521ee69bd
4 changed files with 41 additions and 30 deletions

View File

@ -140,6 +140,8 @@ struct GstStackFrame {
uint16_t size;
uint16_t prevSize;
uint16_t ret;
uint16_t errorSlot;
uint16_t *errorJump;
GstFuncEnv *env;
uint16_t *pc;
};

30
value.c
View File

@ -152,12 +152,7 @@ int gst_equals(GstValue x, GstValue y) {
}
result = 0;
break;
case GST_ARRAY:
case GST_BYTEBUFFER:
case GST_CFUNCTION:
case GST_OBJECT:
case GST_FUNCTION:
case GST_THREAD:
default:
/* compare pointers */
result = (x.data.array == y.data.array);
break;
@ -194,12 +189,7 @@ uint32_t gst_hash(GstValue x) {
else
hash = gst_string_hash(x.data.string) = djb2(x.data.string);
break;
case GST_ARRAY:
case GST_BYTEBUFFER:
case GST_CFUNCTION:
case GST_OBJECT:
case GST_FUNCTION:
case GST_THREAD:
default:
/* Cast the pointer */
{
union {
@ -258,12 +248,7 @@ int gst_compare(GstValue x, GstValue y) {
return xlen < ylen ? -1 : 1;
}
}
case GST_ARRAY:
case GST_BYTEBUFFER:
case GST_CFUNCTION:
case GST_FUNCTION:
case GST_OBJECT:
case GST_THREAD:
default:
if (x.data.string == y.data.string) {
return 0;
} else {
@ -360,3 +345,12 @@ void gst_set(Gst *vm, GstValue ds, GstValue key, GstValue value) {
gst_error(vm, "Cannot set.");
}
}
/* Get the meta value associated with a value */
GstValue gst_meta(Gst *vm, GstValue x) {
switch (x.type) {
default: return vm->metas[x.type];
case GST_OBJECT:
return x.data.object->meta;
}
}

View File

@ -26,4 +26,7 @@ uint8_t *gst_to_string(Gst *vm, GstValue x);
/* Generate a hash value for a gst object */
uint32_t gst_hash(GstValue x);
/* Get the meta value for a given value */
GstValue gst_meta(Gst *vm, GstValue x);
#endif /* end of include guard: VALUE_H_1RJPQKFM */

36
vm.c
View File

@ -5,11 +5,11 @@
#include "value.h"
#include "ds.h"
static const char OOM[] = "Out of memory";
static const char NO_UPVALUE[] = "No upvalue";
static const char EXPECTED_FUNCTION[] = "Expected function";
static const char VMS_EXPECTED_NUMBER_ROP[] = "Expected right operand to be number";
static const char VMS_EXPECTED_NUMBER_LOP[] = "Expected left operand to be number";
static const char OOM[] = "out of memory";
static const char NO_UPVALUE[] = "no upvalue";
static const char EXPECTED_FUNCTION[] = "expected function";
static const char VMS_EXPECTED_NUMBER_ROP[] = "expected right operand to be number";
static const char VMS_EXPECTED_NUMBER_LOP[] = "expected left operand to be number";
/* The size of a StackFrame in units of Values. */
#define FRAME_SIZE ((sizeof(GstStackFrame) + sizeof(GstValue) - 1) / sizeof(GstValue))
@ -55,6 +55,7 @@ static void thread_push(Gst *vm, GstThread *thread, GstValue callee, uint32_t si
frame->size = size;
frame->env = NULL;
frame->callee = callee;
frame->errorJump = NULL;
}
/* Copy the current function stack to the current closure
@ -81,7 +82,7 @@ static void thread_pop(Gst *vm) {
if (thread->count) {
thread_split_env(vm);
} else {
gst_error(vm, "Nothing to pop from stack.");
gst_crash(vm, "stack underflow");
}
thread->count -= delta;
vm->base -= delta;
@ -445,7 +446,7 @@ static GstValue gst_vm_closure(Gst *vm, uint16_t literal) {
current = vm->frame->callee.data.function;
constant = gst_vm_literal(vm, current, literal);
if (constant.type != GST_NIL) {
gst_error(vm, "Error trying to create closure");
gst_error(vm, "cannot create closure");
}
fn = gst_alloc(vm, sizeof(GstFunction));
fn->def = (GstFuncDef *) constant.data.pointer;
@ -464,12 +465,23 @@ int gst_start(Gst *vm) {
{
int n;
if ((n = setjmp(vm->jump))) {
vm->lock = 0;
/* Good return */
if (n == 1) {
vm->lock = 0;
return 0;
} else if (n == 2) {
/* Error. Handling TODO. */
do {
if (vm->thread->count == 0)
return n;
thread_pop(vm);
} while (!vm->frame->errorJump);
/* Jump to the error location */
vm->pc = vm->frame->errorJump;
vm->lock = 0;
} else {
/* Error or crash. Handling TODO. */
/* Crash. just return */
vm->lock = 0;
return n;
}
}
@ -726,7 +738,7 @@ int gst_start(Gst *vm) {
break;
default:
gst_error(vm, "Unknown opcode");
gst_error(vm, "unknown opcode");
break;
}
@ -739,14 +751,14 @@ int gst_start(Gst *vm) {
/* Get an argument from the stack */
GstValue gst_arg(Gst *vm, uint16_t index) {
uint16_t frameSize = vm->frame->size;
gst_assert(vm, frameSize > index, "Cannot get arg out of stack bounds");
gst_assert(vm, frameSize > index, "cannot get arg out of stack bounds");
return vm->base[index];
}
/* Put a value on the stack */
void gst_set_arg(Gst* vm, uint16_t index, GstValue x) {
uint16_t frameSize = vm->frame->size;
gst_assert(vm, frameSize > index, "Cannot set arg out of stack bounds");
gst_assert(vm, frameSize > index, "cannot set arg out of stack bounds");
vm->base[index] = x;
}