1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-29 14:47:42 +00:00

Fix variadic functions.

This commit is contained in:
Calvin Rose
2017-05-07 16:48:35 -04:00
parent 7b83247c07
commit 96a605fbc0
12 changed files with 83 additions and 104 deletions

View File

@@ -117,6 +117,11 @@ BUFFER_DEFINE(i16, int16_t)
/* If there is an error during compilation,
* jump back to start */
static void c_error(GstCompiler *c, const char *e) {
c->error = gst_string_cv(c->vm, e);
longjmp(c->onError, 1);
}
static void c_error1(GstCompiler *c, GstValue e) {
c->error = e;
longjmp(c->onError, 1);
}
@@ -183,7 +188,11 @@ static uint16_t compiler_get_local(GstCompiler *c, GstScope *scope) {
if (scope->nextLocal + 1 == 0) {
c_error(c, "too many local variables");
}
return scope->nextLocal++;
++scope->nextLocal;
if (scope->nextLocal > scope->frameSize) {
scope->frameSize = scope->nextLocal;
}
return scope->nextLocal - 1;
} else {
return scope->freeHeap[--scope->heapSize];
}
@@ -471,7 +480,7 @@ static Slot compile_symbol(GstCompiler *c, FormOptions opts, GstValue sym) {
Slot ret;
int status = symbol_resolve(c, sym, &level, &index, &lit);
if (!status) {
c_error(c, "undefined symbol");
c_error1(c, sym);
}
if (opts.resultUnused) return nil_slot();
if (status == 2) {
@@ -626,7 +635,7 @@ static Slot compile_function(GstCompiler *c, FormOptions opts, const GstValue *f
GstArray *params;
FormOptions subOpts = form_options_default();
Slot ret;
int varargs;
int varargs = 0;
uint32_t arity;
if (opts.resultUnused) return nil_slot();
ret = compiler_get_target(c, opts);
@@ -1092,7 +1101,7 @@ void gst_compiler(GstCompiler *c, Gst *vm) {
c->vm = vm;
c->buffer = gst_buffer(vm, 128);
c->tail = NULL;
c->error = NULL;
c->error.type = GST_NIL;
c->trackers = NULL;
compiler_push_scope(c, 0);
}
@@ -1135,8 +1144,8 @@ GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form) {
c->trackers = NULL;
if (c->tail)
c->tail->parent = NULL;
if (c->error == NULL)
c->error = "unknown error";
if (c->error.type == GST_NIL)
c->error = gst_string_cv(c->vm, "unknown error");
return NULL;
}
/* Create a scope */
@@ -1169,6 +1178,7 @@ static void gst_compiler_mark(Gst *vm, void *data, uint32_t len) {
return;
/* Mark compiler */
gst_mark_value(vm, gst_wrap_buffer(c->buffer));
gst_mark_value(vm, c->error);
/* Mark trackers - the trackers themselves are all on the stack. */
st = (SlotTracker *) c->trackers;
while (st) {
@@ -1232,7 +1242,7 @@ static int gst_stl_compiler_compile(Gst *vm) {
gst_c_throwc(vm, "expected compiler");
ret = gst_compiler_compile(c, gst_arg(vm, 1));
if (ret == NULL)
gst_c_throwc(vm, c->error);
gst_c_throw(vm, c->error);
gst_c_return(vm, gst_wrap_function(ret));
}

View File

@@ -153,7 +153,7 @@ static int is_symbol_char(uint8_t c) {
if (c >= '0' && c <= ':') return 1;
if (c >= '<' && c <= '@') return 1;
if (c >= '*' && c <= '/') return 1;
if (c >= '$' && c == '&') return 1;
if (c >= '#' && c <= '&') return 1;
if (c == '_') return 1;
if (c == '^') return 1;
if (c == '!') return 1;
@@ -444,22 +444,6 @@ static int form_state(GstParser *p, uint8_t c) {
static void dispatch_char(GstParser *p, uint8_t c) {
int done = 0;
++p->index;
/* Handle comments */
if (p->flags & GST_PARSER_FLAG_INCOMMENT) {
if (c == '\n') {
p->flags = GST_PARSER_FLAG_EXPECTING_COMMENT;
}
return;
} else if (p->flags & GST_PARSER_FLAG_EXPECTING_COMMENT) {
if (c == '#') {
p->flags = GST_PARSER_FLAG_INCOMMENT;
return;
} else if (!is_whitespace(c)) {
p->flags = 0;
} else {
return;
}
}
/* Dispatch character to state */
while (!done) {
GstParseState *top = parser_peek(p);
@@ -504,6 +488,11 @@ int gst_parse_string(GstParser *p, const uint8_t *string) {
return i;
}
/* Parse a single byte */
void gst_parse_byte(GstParser *p, uint8_t byte) {
dispatch_char(p, byte);
}
/* Check if a parser has a value that needs to be handled. If
* so, the parser will not parse any more input until that value
* is consumed. */
@@ -529,7 +518,6 @@ void gst_parser(GstParser *p, Gst *vm) {
p->error = NULL;
p->status = GST_PARSER_ROOT;
p->value.type = GST_NIL;
p->flags = GST_PARSER_FLAG_EXPECTING_COMMENT;
parser_push(p, PTYPE_ROOT, ' ');
}

View File

@@ -85,11 +85,13 @@ void gst_thread_pushnil(Gst *vm, GstThread *thread, uint32_t n) {
void gst_thread_tuplepack(Gst *vm, GstThread *thread, uint32_t n) {
GstValue *stack = thread->data + thread->count;
uint32_t size = gst_frame_size(stack);
if (n >= size) {
if (n > size) {
/* Push one extra nil to ensure space for tuple */
gst_thread_pushnil(vm, thread, n - size + 1);
stack = thread->data + thread->count;
stack[n].type = GST_TUPLE;
stack[n].data.tuple = gst_tuple_end(vm, gst_tuple_begin(vm, 0));
gst_frame_size(stack) = n + 1;
} else {
uint32_t i;
GstValue *tuple = gst_tuple_begin(vm, size - n);
@@ -97,7 +99,6 @@ void gst_thread_tuplepack(Gst *vm, GstThread *thread, uint32_t n) {
tuple[i - n] = stack[i];
stack[n].type = GST_TUPLE;
stack[n].data.tuple = gst_tuple_end(vm, tuple);
gst_frame_size(stack) = n + 1;
}
}

View File

@@ -242,6 +242,7 @@ int gst_continue(Gst *vm) {
stack = gst_thread_stack(vm->thread);
for (i = 0; i < count; ++i)
stack[oldsize + i] = data[i];
/*gst_frame_size(stack) += count;*/
pc += 2;
}
break;