2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
#include "internal.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
#define DST_LOCAL_FLAG_MUTABLE 1
|
2017-06-25 20:36:20 +00:00
|
|
|
|
2017-07-09 19:35:17 +00:00
|
|
|
/* Compiler typedefs */
|
2017-09-09 18:39:51 +00:00
|
|
|
typedef struct DstCompiler DstCompiler;
|
2017-07-09 19:35:17 +00:00
|
|
|
typedef struct FormOptions FormOptions;
|
|
|
|
typedef struct SlotTracker SlotTracker;
|
2017-09-09 18:39:51 +00:00
|
|
|
typedef struct DstScope DstScope;
|
2017-07-09 19:35:17 +00:00
|
|
|
|
|
|
|
/* Compilation state */
|
2017-09-09 18:39:51 +00:00
|
|
|
struct DstCompiler {
|
|
|
|
Dst *vm;
|
|
|
|
DstValue error;
|
2017-07-09 19:35:17 +00:00
|
|
|
jmp_buf onError;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstScope *tail;
|
|
|
|
DstBuffer *buffer;
|
|
|
|
DstTable *env;
|
2017-07-09 19:35:17 +00:00
|
|
|
int recursionGuard;
|
|
|
|
};
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* During compilation, FormOptions are passed to ASTs
|
|
|
|
* as configuration options to allow for some optimizations. */
|
|
|
|
struct FormOptions {
|
|
|
|
/* The location the returned Slot must be in. Can be ignored
|
|
|
|
* if either canDrop or canChoose is true */
|
|
|
|
uint16_t target;
|
2017-02-12 15:27:18 +00:00
|
|
|
/* If the result of the value being compiled is not going to
|
|
|
|
* be used, some forms can simply return a nil slot and save
|
2017-06-25 20:36:20 +00:00
|
|
|
* co,putation */
|
2017-02-12 15:27:18 +00:00
|
|
|
uint16_t resultUnused : 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Allows the sub expression to evaluate into a
|
|
|
|
* temporary slot of it's choice. A temporary Slot
|
2017-09-09 18:39:51 +00:00
|
|
|
* can be allocated with DstCompilerGetLocal. */
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t canChoose : 1;
|
|
|
|
/* True if the form is in the tail position. This allows
|
|
|
|
* for tail call optimization. If a helper receives this
|
2017-02-12 15:27:18 +00:00
|
|
|
* flag, it is free to return a returned slot and generate bytecode
|
|
|
|
* for a return, including tail calls. */
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t isTail : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A Slot represent a location of a local variable
|
|
|
|
* on the stack. Also contains some meta information. */
|
|
|
|
typedef struct Slot Slot;
|
|
|
|
struct Slot {
|
|
|
|
/* The index of the Slot on the stack. */
|
|
|
|
uint16_t index;
|
2017-02-12 15:27:18 +00:00
|
|
|
/* A nil Slot should not be expected to contain real data. (ignore index).
|
2017-02-09 20:02:59 +00:00
|
|
|
* Forms that have side effects but don't evaulate to
|
2017-03-01 01:20:29 +00:00
|
|
|
* anything will try to return nil slots. */
|
2017-02-12 15:27:18 +00:00
|
|
|
uint16_t isNil : 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* A temp Slot is a Slot on the stack that does not
|
|
|
|
* belong to a named local. They can be freed whenever,
|
|
|
|
* and so are used in intermediate calculations. */
|
|
|
|
uint16_t isTemp : 1;
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Flag indicating if byteCode for returning this slot
|
|
|
|
* has been written to the buffer. Should only ever be true
|
|
|
|
* when the isTail option is passed */
|
|
|
|
uint16_t hasReturned : 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A SlotTracker provides a handy way to keep track of
|
|
|
|
* Slots on the stack and free them in bulk. */
|
|
|
|
struct SlotTracker {
|
2017-02-16 02:02:00 +00:00
|
|
|
Slot *slots;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
|
|
|
};
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* A DstScope is a lexical scope in the program. It is
|
2017-02-09 20:02:59 +00:00
|
|
|
* responsible for aliasing programmer facing names to
|
|
|
|
* Slots and for keeping track of literals. It also
|
2017-09-09 18:39:51 +00:00
|
|
|
* points to the parent DstScope, and its current child
|
|
|
|
* DstScope. */
|
|
|
|
struct DstScope {
|
2017-02-10 04:28:11 +00:00
|
|
|
uint32_t level;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t nextLocal;
|
2017-02-12 15:27:18 +00:00
|
|
|
uint16_t frameSize;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t heapCapacity;
|
|
|
|
uint32_t heapSize;
|
2017-05-11 21:08:29 +00:00
|
|
|
uint16_t touchParent;
|
|
|
|
uint16_t touchEnv;
|
2017-02-16 02:02:00 +00:00
|
|
|
uint16_t *freeHeap;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstTable *literals;
|
|
|
|
DstArray *literalsArray;
|
|
|
|
DstTable *locals;
|
|
|
|
DstScope *parent;
|
2017-02-09 20:02:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Provides default FormOptions */
|
2017-02-16 02:02:00 +00:00
|
|
|
static FormOptions form_options_default() {
|
2017-02-09 20:02:59 +00:00
|
|
|
FormOptions opts;
|
|
|
|
opts.canChoose = 1;
|
|
|
|
opts.isTail = 0;
|
2017-02-12 15:27:18 +00:00
|
|
|
opts.resultUnused = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
opts.target = 0;
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create some helpers that allows us to push more than just raw bytes
|
|
|
|
* to the byte buffer. This helps us create the byte code for the compiled
|
|
|
|
* functions. */
|
2017-02-16 02:02:00 +00:00
|
|
|
BUFFER_DEFINE(i32, int32_t)
|
2017-04-24 20:02:54 +00:00
|
|
|
BUFFER_DEFINE(i64, int64_t)
|
2017-09-09 18:39:51 +00:00
|
|
|
BUFFER_DEFINE(real, DstReal)
|
2017-02-16 02:02:00 +00:00
|
|
|
BUFFER_DEFINE(u16, uint16_t)
|
|
|
|
BUFFER_DEFINE(i16, int16_t)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* If there is an error during compilation,
|
|
|
|
* jump back to start */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void c_error(DstCompiler *c, const char *e) {
|
|
|
|
c->error = dst_string_cv(c->vm, e);
|
2017-05-07 20:48:35 +00:00
|
|
|
longjmp(c->onError, 1);
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
static void c_error1(DstCompiler *c, DstValue e) {
|
2017-02-12 20:16:55 +00:00
|
|
|
c->error = e;
|
|
|
|
longjmp(c->onError, 1);
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* Push a new scope in the compiler and return
|
|
|
|
* a pointer to it for configuration. There is
|
|
|
|
* more configuration that needs to be done if
|
|
|
|
* the new scope is a function declaration. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static DstScope *compiler_push_scope(DstCompiler *c, int sameFunction) {
|
|
|
|
DstScope *scope = dst_alloc(c->vm, sizeof(DstScope));
|
|
|
|
scope->locals = dst_table(c->vm, 4);
|
|
|
|
scope->freeHeap = dst_alloc(c->vm, 4 * sizeof(uint16_t));
|
2017-02-09 20:02:59 +00:00
|
|
|
scope->heapSize = 0;
|
2017-06-25 20:36:20 +00:00
|
|
|
scope->heapCapacity = 4;
|
2017-02-13 04:45:52 +00:00
|
|
|
scope->parent = c->tail;
|
2017-02-12 15:27:18 +00:00
|
|
|
scope->frameSize = 0;
|
2017-05-11 21:08:29 +00:00
|
|
|
scope->touchParent = 0;
|
|
|
|
scope->touchEnv = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
if (c->tail) {
|
|
|
|
scope->level = c->tail->level + (sameFunction ? 0 : 1);
|
|
|
|
} else {
|
2017-02-12 20:16:55 +00:00
|
|
|
scope->level = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
if (sameFunction) {
|
|
|
|
if (!c->tail) {
|
2017-02-26 16:47:50 +00:00
|
|
|
c_error(c, "cannot inherit scope when root scope");
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
scope->nextLocal = c->tail->nextLocal;
|
|
|
|
scope->literals = c->tail->literals;
|
|
|
|
scope->literalsArray = c->tail->literalsArray;
|
|
|
|
} else {
|
|
|
|
scope->nextLocal = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
scope->literals = dst_table(c->vm, 4);
|
|
|
|
scope->literalsArray = dst_array(c->vm, 4);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
c->tail = scope;
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the inner most scope from the compiler stack */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void compiler_pop_scope(DstCompiler *c) {
|
|
|
|
DstScope *last = c->tail;
|
2017-02-12 15:27:18 +00:00
|
|
|
if (last == NULL) {
|
2017-02-26 16:47:50 +00:00
|
|
|
c_error(c, "no scope to pop");
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
if (last->nextLocal > last->frameSize) {
|
2017-02-12 20:16:55 +00:00
|
|
|
last->frameSize = last->nextLocal;
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
2017-02-13 04:45:52 +00:00
|
|
|
c->tail = last->parent;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (c->tail) {
|
2017-02-12 15:27:18 +00:00
|
|
|
if (last->frameSize > c->tail->frameSize) {
|
2017-02-12 20:16:55 +00:00
|
|
|
c->tail->frameSize = last->frameSize;
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next stack position that is open for
|
|
|
|
* a variable */
|
2017-09-09 18:39:51 +00:00
|
|
|
static uint16_t compiler_get_local(DstCompiler *c, DstScope *scope) {
|
2017-02-09 20:02:59 +00:00
|
|
|
if (scope->heapSize == 0) {
|
|
|
|
if (scope->nextLocal + 1 == 0) {
|
2017-02-26 16:47:50 +00:00
|
|
|
c_error(c, "too many local variables");
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-05-07 22:37:19 +00:00
|
|
|
return scope->nextLocal++;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
return scope->freeHeap[--scope->heapSize];
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a slot on the stack for other locals and/or
|
|
|
|
* intermediate values */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void compiler_free_local(DstCompiler *c, DstScope *scope, uint16_t slot) {
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Ensure heap has space */
|
|
|
|
if (scope->heapSize >= scope->heapCapacity) {
|
|
|
|
uint32_t newCap = 2 * scope->heapSize;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint16_t *newData = dst_alloc(c->vm, newCap * sizeof(uint16_t));
|
|
|
|
dst_memcpy(newData, scope->freeHeap, scope->heapSize * sizeof(uint16_t));
|
2017-02-12 15:27:18 +00:00
|
|
|
scope->freeHeap = newData;
|
|
|
|
scope->heapCapacity = newCap;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
scope->freeHeap[scope->heapSize++] = slot;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initializes a SlotTracker. SlotTrackers
|
|
|
|
* are used during compilation to free up slots on the stack
|
|
|
|
* after they are no longer needed. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void tracker_init(DstCompiler *c, SlotTracker *tracker) {
|
|
|
|
tracker->slots = dst_alloc(c->vm, 10 * sizeof(Slot));
|
2017-02-09 20:02:59 +00:00
|
|
|
tracker->count = 0;
|
|
|
|
tracker->capacity = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free up a slot if it is a temporary slot (does not
|
|
|
|
* belong to a named local). If the slot does belong
|
|
|
|
* to a named variable, does nothing. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void compiler_drop_slot(DstCompiler *c, DstScope *scope, Slot slot) {
|
2017-02-12 15:27:18 +00:00
|
|
|
if (!slot.isNil && slot.isTemp) {
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_free_local(c, scope, slot.index);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Helper function to return a slot. Useful for compiling things that return
|
|
|
|
* nil. (set, while, etc.). Use this to wrap compilation calls that need
|
|
|
|
* to return things. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compiler_return(DstCompiler *c, Slot slot) {
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
|
|
|
ret.hasReturned = 1;
|
|
|
|
ret.isNil = 1;
|
|
|
|
if (slot.hasReturned) {
|
|
|
|
/* Do nothing */
|
|
|
|
} else if (slot.isNil) {
|
|
|
|
/* Return nil */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, DST_OP_RTN);
|
2017-02-12 15:27:18 +00:00
|
|
|
} else {
|
|
|
|
/* Return normal value */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, DST_OP_RET);
|
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, slot.index);
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets a temporary slot for the bottom-most scope. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compiler_get_temp(DstCompiler *c) {
|
|
|
|
DstScope *scope = c->tail;
|
2017-02-12 20:16:55 +00:00
|
|
|
Slot ret;
|
|
|
|
ret.isTemp = 1;
|
|
|
|
ret.isNil = 0;
|
|
|
|
ret.hasReturned = 0;
|
2017-02-16 02:02:00 +00:00
|
|
|
ret.index = compiler_get_local(c, scope);
|
2017-02-12 20:16:55 +00:00
|
|
|
return ret;
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a slot that is the target Slot given some FormOptions. Will
|
|
|
|
* Create a temporary slot if needed, so be sure to drop the slot after use. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compiler_get_target(DstCompiler *c, FormOptions opts) {
|
2017-02-12 20:16:55 +00:00
|
|
|
if (opts.canChoose) {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compiler_get_temp(c);
|
2017-02-12 20:16:55 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
|
|
|
ret.isTemp = 0;
|
|
|
|
ret.isNil = 0;
|
|
|
|
ret.hasReturned = 0;
|
|
|
|
ret.index = opts.target;
|
|
|
|
return ret;
|
2017-02-12 20:16:55 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If a slot is a nil slot, create a slot that has
|
|
|
|
* an actual location on the stack. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compiler_realize_slot(DstCompiler *c, Slot slot) {
|
2017-02-12 20:16:55 +00:00
|
|
|
if (slot.isNil) {
|
2017-02-16 02:02:00 +00:00
|
|
|
slot = compiler_get_temp(c);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, DST_OP_NIL);
|
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, slot.index);
|
2017-02-12 20:16:55 +00:00
|
|
|
}
|
|
|
|
return slot;
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:36:20 +00:00
|
|
|
/* Coerce a slot to match form options. Can write to buffer. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compiler_coerce_slot(DstCompiler *c, FormOptions opts, Slot slot) {
|
|
|
|
DstScope *scope = c->tail;
|
2017-06-25 20:36:20 +00:00
|
|
|
if (opts.resultUnused) {
|
|
|
|
compiler_drop_slot(c, scope, slot);
|
|
|
|
slot.isNil = 1;
|
|
|
|
return slot;
|
|
|
|
} else {
|
|
|
|
slot = compiler_realize_slot(c, slot);
|
|
|
|
}
|
|
|
|
if (opts.canChoose) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (slot.index != opts.target) {
|
|
|
|
/* We need to move the variable. This
|
|
|
|
* would occur in a simple assignment like a = b. */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstBuffer *buffer = c->buffer;
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_MOV);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, opts.target);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, slot.index);
|
2017-06-25 20:36:20 +00:00
|
|
|
slot.index = opts.target;
|
|
|
|
slot.isTemp = 0; /* We don't own the slot anymore */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Helper to get a nil slot */
|
2017-02-22 23:19:46 +00:00
|
|
|
static Slot nil_slot() { Slot ret; ret.isNil = 1; ret.hasReturned = 0; return ret; }
|
2017-02-12 15:27:18 +00:00
|
|
|
|
|
|
|
/* Writes all of the slots in the tracker to the compiler */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void compiler_tracker_write(DstCompiler *c, SlotTracker *tracker, int reverse) {
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
for (i = 0; i < tracker->count; ++i) {
|
|
|
|
Slot s;
|
|
|
|
if (reverse)
|
|
|
|
s = tracker->slots[tracker->count - 1 - i];
|
|
|
|
else
|
|
|
|
s = tracker->slots[i];
|
|
|
|
if (s.isNil)
|
2017-02-26 16:47:50 +00:00
|
|
|
c_error(c, "trying to write nil slot");
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, s.index);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the tracker after creation. This unlocks the memory
|
|
|
|
* that was allocated by the GC an allows it to be collected. Also
|
|
|
|
* frees slots that were tracked by this tracker in the given scope. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void compiler_tracker_free(DstCompiler *c, DstScope *scope, SlotTracker *tracker) {
|
2017-02-12 15:27:18 +00:00
|
|
|
uint32_t i;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Free in reverse order */
|
|
|
|
for (i = tracker->count - 1; i < tracker->count; --i) {
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_drop_slot(c, scope, tracker->slots[i]);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new Slot to a slot tracker. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static void compiler_tracker_push(DstCompiler *c, SlotTracker *tracker, Slot slot) {
|
2017-02-09 20:02:59 +00:00
|
|
|
if (tracker->count >= tracker->capacity) {
|
|
|
|
uint32_t newCap = 2 * tracker->count;
|
2017-09-09 18:39:51 +00:00
|
|
|
Slot *newData = dst_alloc(c->vm, newCap * sizeof(Slot));
|
|
|
|
dst_memcpy(newData, tracker->slots, tracker->count * sizeof(Slot));
|
2017-02-09 20:02:59 +00:00
|
|
|
tracker->slots = newData;
|
|
|
|
tracker->capacity = newCap;
|
|
|
|
}
|
|
|
|
tracker->slots[tracker->count++] = slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Registers a literal in the given scope. If an equal literal is found, uses
|
|
|
|
* that one instead of creating a new literal. This allows for some reuse
|
|
|
|
* of things like string constants.*/
|
2017-09-09 18:39:51 +00:00
|
|
|
static uint16_t compiler_add_literal(DstCompiler *c, DstScope *scope, DstValue x) {
|
|
|
|
DstValue checkDup = dst_table_get(scope->literals, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t literalIndex = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (checkDup.type != DST_NIL) {
|
2017-02-09 20:02:59 +00:00
|
|
|
/* An equal literal is already registered in the current scope */
|
2017-04-24 20:02:54 +00:00
|
|
|
return (uint16_t) checkDup.data.integer;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
|
|
|
/* Add our literal for tracking */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue valIndex;
|
|
|
|
valIndex.type = DST_INTEGER;
|
2017-02-09 20:02:59 +00:00
|
|
|
literalIndex = scope->literalsArray->count;
|
2017-04-24 20:02:54 +00:00
|
|
|
valIndex.data.integer = literalIndex;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_table_put(c->vm, scope->literals, x, valIndex);
|
|
|
|
dst_array_push(c->vm, scope->literalsArray, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return literalIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Declare a symbol in a given scope. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static uint16_t compiler_declare_symbol(DstCompiler *c, DstScope *scope, DstValue sym, uint16_t flags) {
|
|
|
|
DstValue x;
|
2017-02-16 02:02:00 +00:00
|
|
|
uint16_t target;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (sym.type != DST_SYMBOL)
|
2017-07-02 21:17:24 +00:00
|
|
|
c_error(c, "expected symbol");
|
2017-02-16 02:02:00 +00:00
|
|
|
target = compiler_get_local(c, scope);
|
2017-09-09 18:39:51 +00:00
|
|
|
x.type = DST_INTEGER;
|
2017-06-25 20:36:20 +00:00
|
|
|
x.data.integer = target + (flags << 16);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_table_put(c->vm, scope->locals, sym, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:36:20 +00:00
|
|
|
/* Try to resolve a symbol. If the symbol can be resolved, return true and
|
2017-02-09 20:02:59 +00:00
|
|
|
* pass back the level and index by reference. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static int symbol_resolve(DstCompiler *c, DstValue x, uint16_t *level, uint16_t *index, uint16_t *flags, DstValue *out) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstValue check;
|
2017-02-10 04:28:11 +00:00
|
|
|
uint32_t currentLevel = scope->level;
|
2017-02-09 20:02:59 +00:00
|
|
|
while (scope) {
|
2017-09-09 18:39:51 +00:00
|
|
|
check = dst_table_get(scope->locals, x);
|
|
|
|
if (check.type != DST_NIL) {
|
2017-02-10 04:28:11 +00:00
|
|
|
*level = currentLevel - scope->level;
|
2017-06-25 20:36:20 +00:00
|
|
|
*index = (uint16_t) (check.data.integer & 0xFFFF);
|
|
|
|
if (flags) *flags = check.data.integer >> 16;
|
2017-02-09 20:02:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-13 04:45:52 +00:00
|
|
|
scope = scope->parent;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-06-25 20:36:20 +00:00
|
|
|
/* Check for named literals */
|
2017-09-09 18:39:51 +00:00
|
|
|
check = dst_table_get(c->env, x);
|
|
|
|
if (check.type != DST_NIL) {
|
2017-06-25 20:36:20 +00:00
|
|
|
/* Check metadata for var (mutable) */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstTable *metas = dst_env_meta(c->vm, c->env);
|
|
|
|
DstValue maybeMeta = dst_table_get(metas, x);
|
|
|
|
if (maybeMeta.type == DST_TABLE) {
|
|
|
|
DstValue isMutable = dst_table_get(maybeMeta.data.table, dst_string_cv(c->vm, "mutable"));
|
|
|
|
if (dst_truthy(isMutable)) {
|
|
|
|
if (flags) *flags = DST_LOCAL_FLAG_MUTABLE;
|
2017-06-25 20:36:20 +00:00
|
|
|
*out = check;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags) *flags = 0;
|
|
|
|
*out = check;
|
2017-07-02 01:51:16 +00:00
|
|
|
return 2;
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
|
|
|
/* Check for nil named literal */
|
2017-09-09 18:39:51 +00:00
|
|
|
check = dst_table_get(dst_env_nils(c->vm, c->env), x);
|
|
|
|
if (check.type != DST_NIL) {
|
2017-06-25 20:36:20 +00:00
|
|
|
if (flags) *flags = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
*out = dst_wrap_nil();
|
2017-06-25 20:36:20 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
/* Compile a value and return it stack location after loading.
|
|
|
|
* If a target > 0 is passed, the returned value must be equal
|
2017-09-09 18:39:51 +00:00
|
|
|
* to the targtet. If target < 0, the DstCompiler can choose whatever
|
2017-02-09 20:02:59 +00:00
|
|
|
* slot location it likes. If, for example, a symbol resolves to
|
|
|
|
* whatever is in a given slot, it makes sense to use that location
|
|
|
|
* to 'return' the value. For other expressions, like function
|
|
|
|
* calls, the compiler will just pick the lowest free slot
|
|
|
|
* as the location on the stack. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_value(DstCompiler *c, FormOptions opts, DstValue x);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* Compile boolean, nil, and number values. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_nonref_type(DstCompiler *c, FormOptions opts, DstValue x) {
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
2017-02-16 02:02:00 +00:00
|
|
|
if (opts.resultUnused) return nil_slot();
|
|
|
|
ret = compiler_get_target(c, opts);
|
2017-09-09 18:39:51 +00:00
|
|
|
if (x.type == DST_NIL) {
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_NIL);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
} else if (x.type == DST_BOOLEAN) {
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, x.data.boolean ? DST_OP_TRU : DST_OP_FLS);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
} else if (x.type == DST_REAL) {
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_F64);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_real(c->vm, buffer, x.data.real);
|
|
|
|
} else if (x.type == DST_INTEGER) {
|
2017-04-24 20:02:54 +00:00
|
|
|
if (x.data.integer <= 32767 && x.data.integer >= -32768) {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_I16);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_i16(c->vm, buffer, x.data.integer);
|
2017-04-24 20:02:54 +00:00
|
|
|
} else if (x.data.integer <= 2147483647 && x.data.integer >= -2147483648) {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_I32);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_i32(c->vm, buffer, x.data.integer);
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_I64);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_i64(c->vm, buffer, x.data.integer);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-26 16:47:50 +00:00
|
|
|
c_error(c, "expected boolean, nil, or number type");
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-05 20:52:05 +00:00
|
|
|
/* Compile a structure that evaluates to a literal value. Useful
|
|
|
|
* for objects like strings, or anything else that cannot be instantiated
|
|
|
|
* from bytecode and doesn't do anything in the AST. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_literal(DstCompiler *c, FormOptions opts, DstValue x) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-05-05 20:52:05 +00:00
|
|
|
Slot ret;
|
|
|
|
uint16_t literalIndex;
|
|
|
|
if (opts.resultUnused) return nil_slot();
|
|
|
|
switch (x.type) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
|
|
|
case DST_REAL:
|
|
|
|
case DST_BOOLEAN:
|
|
|
|
case DST_NIL:
|
2017-05-05 20:52:05 +00:00
|
|
|
return compile_nonref_type(c, opts, x);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = compiler_get_target(c, opts);
|
|
|
|
literalIndex = compiler_add_literal(c, scope, x);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_CST);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, literalIndex);
|
2017-05-05 20:52:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-02 21:17:24 +00:00
|
|
|
/* Quote a value */
|
2017-09-09 18:39:51 +00:00
|
|
|
static DstValue quote(Dst *vm, DstValue x) {
|
|
|
|
DstValue *tuple = dst_tuple_begin(vm, 2);
|
|
|
|
tuple[0] = dst_string_cvs(vm, "quote");
|
2017-07-02 21:17:24 +00:00
|
|
|
tuple[1] = x;
|
2017-09-09 18:39:51 +00:00
|
|
|
return dst_wrap_tuple(dst_tuple_end(vm, tuple));
|
2017-07-02 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile a symbol. Resolves any kind of symbol. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_symbol(DstCompiler *c, FormOptions opts, DstValue sym) {
|
|
|
|
DstValue lit = dst_wrap_nil();
|
|
|
|
DstBuffer * buffer = c->buffer;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t index = 0;
|
|
|
|
uint16_t level = 0;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
2017-06-25 20:36:20 +00:00
|
|
|
int status = symbol_resolve(c, sym, &level, &index, NULL, &lit);
|
2017-05-05 20:52:05 +00:00
|
|
|
if (!status) {
|
2017-05-07 20:48:35 +00:00
|
|
|
c_error1(c, sym);
|
2017-04-13 01:21:46 +00:00
|
|
|
}
|
2017-05-05 20:52:05 +00:00
|
|
|
if (opts.resultUnused) return nil_slot();
|
|
|
|
if (status == 2) {
|
|
|
|
/* We have a named literal */
|
|
|
|
return compile_literal(c, opts, lit);
|
2017-06-25 20:36:20 +00:00
|
|
|
} else if (status == 3) {
|
|
|
|
/* We have a global variable */
|
2017-09-09 18:39:51 +00:00
|
|
|
const DstValue *tup;
|
|
|
|
Dst *vm= c->vm;
|
|
|
|
DstValue *t = dst_tuple_begin(vm, 3);
|
|
|
|
t[0] = dst_string_cvs(vm, "get"); /* Todo - replace with actual cfunc or bytecode */
|
2017-06-25 20:36:20 +00:00
|
|
|
t[1] = quote(vm, lit);
|
2017-09-09 18:39:51 +00:00
|
|
|
t[2] = dst_wrap_integer(0);
|
|
|
|
tup = dst_tuple_end(vm, t);
|
|
|
|
return compile_value(c, opts, dst_wrap_tuple(tup));
|
2017-05-05 20:52:05 +00:00
|
|
|
} else if (level > 0) {
|
2017-05-12 01:30:18 +00:00
|
|
|
/* We have an upvalue from a parent function. Make
|
|
|
|
* sure that the chain of functions up to the upvalue keep
|
|
|
|
* their parent references */
|
|
|
|
uint32_t i = level;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstScope *scope = c->tail;
|
2017-05-12 01:30:18 +00:00
|
|
|
for (i = level; i > 1; --i) {
|
|
|
|
scope->touchParent = 1;
|
|
|
|
scope = scope->parent;
|
2017-05-11 21:08:29 +00:00
|
|
|
}
|
2017-05-12 01:30:18 +00:00
|
|
|
scope->touchEnv = 1;
|
2017-02-16 02:02:00 +00:00
|
|
|
ret = compiler_get_target(c, opts);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_UPV);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, level);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, index);
|
2017-02-12 15:27:18 +00:00
|
|
|
} else {
|
|
|
|
/* Local variable on stack */
|
|
|
|
ret.isTemp = 0;
|
|
|
|
ret.isNil = 0;
|
|
|
|
ret.hasReturned = 0;
|
|
|
|
if (opts.canChoose) {
|
|
|
|
ret.index = index;
|
|
|
|
} else {
|
|
|
|
/* We need to move the variable. This
|
|
|
|
* would occur in a simple assignment like a = b. */
|
|
|
|
ret.index = opts.target;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_MOV);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, index);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile an assignment operation */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_assign(DstCompiler *c, FormOptions opts, DstValue left, DstValue right) {
|
|
|
|
DstValue lit = dst_wrap_nil();
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-06-25 23:17:54 +00:00
|
|
|
FormOptions subOpts = form_options_default();
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t target = 0;
|
|
|
|
uint16_t level = 0;
|
2017-06-25 20:36:20 +00:00
|
|
|
uint16_t flags = 0;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot slot;
|
2017-05-05 20:52:05 +00:00
|
|
|
int status;
|
2017-02-12 15:27:18 +00:00
|
|
|
subOpts.isTail = 0;
|
|
|
|
subOpts.resultUnused = 0;
|
2017-06-25 20:36:20 +00:00
|
|
|
status = symbol_resolve(c, left, &level, &target, &flags, &lit);
|
|
|
|
if (status == 1) {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!(flags & DST_LOCAL_FLAG_MUTABLE))
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "cannot varset immutable binding");
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Check if we have an up value. Otherwise, it's just a normal
|
|
|
|
* local variable */
|
|
|
|
if (level != 0) {
|
2017-02-12 15:27:18 +00:00
|
|
|
subOpts.canChoose = 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Evaluate the right hand side */
|
2017-02-16 02:02:00 +00:00
|
|
|
slot = compiler_realize_slot(c, compile_value(c, subOpts, right));
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Set the up value */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_SUV);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, slot.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, level);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, target);
|
2017-02-10 04:28:11 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Local variable */
|
2017-02-10 04:28:11 +00:00
|
|
|
subOpts.canChoose = 0;
|
|
|
|
subOpts.target = target;
|
2017-02-16 02:02:00 +00:00
|
|
|
slot = compile_value(c, subOpts, right);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-06-25 20:36:20 +00:00
|
|
|
} else if (status == 3) {
|
|
|
|
/* Global var */
|
2017-09-09 18:39:51 +00:00
|
|
|
const DstValue *tup;
|
|
|
|
Dst *vm= c->vm;
|
|
|
|
DstValue *t = dst_tuple_begin(vm, 4);
|
|
|
|
t[0] = dst_string_cvs(vm, "set!"); /* Todo - replace with ref ro actual cfunc */
|
2017-07-02 21:17:24 +00:00
|
|
|
t[1] = quote(c->vm, lit);
|
2017-09-09 18:39:51 +00:00
|
|
|
t[2] = dst_wrap_integer(0);
|
2017-06-25 20:36:20 +00:00
|
|
|
t[3] = right;
|
2017-09-09 18:39:51 +00:00
|
|
|
tup = dst_tuple_end(vm, t);
|
2017-06-25 20:36:20 +00:00
|
|
|
subOpts.resultUnused = 1;
|
2017-09-09 18:39:51 +00:00
|
|
|
compile_value(c, subOpts, dst_wrap_tuple(tup));
|
2017-06-25 20:36:20 +00:00
|
|
|
return compile_value(c, opts, left);
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "cannot varset immutable binding");
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
if (opts.resultUnused) {
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_drop_slot(c, scope, slot);
|
|
|
|
return nil_slot();
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
return slot;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:36:20 +00:00
|
|
|
/* Set a var */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_varset(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
if (dst_tuple_length(form) != 3)
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "expected 2 arguments to varset");
|
2017-09-09 18:39:51 +00:00
|
|
|
if (DST_SYMBOL != form[1].type)
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "expected symbol as first argument");
|
|
|
|
return compile_assign(c, opts, form[1], form[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Global var */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_global_var(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
const DstValue *tup;
|
|
|
|
Dst *vm= c->vm;
|
|
|
|
DstValue *t = dst_tuple_begin(vm, 3);
|
|
|
|
t[0] = dst_string_cvs(vm, "global-var"); /* Todo - replace with ref ro actual cfunc */
|
2017-07-02 21:17:24 +00:00
|
|
|
t[1] = form[1];
|
2017-09-09 18:39:51 +00:00
|
|
|
t[1].type = DST_STRING;
|
2017-06-25 20:36:20 +00:00
|
|
|
t[2] = form[2];
|
2017-09-09 18:39:51 +00:00
|
|
|
tup = dst_tuple_end(vm, t);
|
|
|
|
return compile_value(c, opts, dst_wrap_tuple(tup));
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Global define */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_global_def(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
const DstValue *tup;
|
|
|
|
Dst *vm= c->vm;
|
|
|
|
DstValue *t = dst_tuple_begin(vm, 3);
|
|
|
|
t[0] = dst_string_cvs(vm, "global-def"); /* Todo - replace with ref ro actual cfunc */
|
2017-07-02 21:17:24 +00:00
|
|
|
t[1] = form[1];
|
2017-09-09 18:39:51 +00:00
|
|
|
t[1].type = DST_STRING;
|
2017-06-25 20:36:20 +00:00
|
|
|
t[2] = form[2];
|
2017-09-09 18:39:51 +00:00
|
|
|
tup = dst_tuple_end(vm, t);
|
|
|
|
return compile_value(c, opts, dst_wrap_tuple(tup));
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile def */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_def(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
if (dst_tuple_length(form) != 3)
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "expected 2 arguments to def");
|
2017-09-09 18:39:51 +00:00
|
|
|
if (DST_SYMBOL != form[1].type)
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "expected symbol as first argument");
|
|
|
|
if (scope->parent) {
|
|
|
|
FormOptions subOpts;
|
|
|
|
Slot slot;
|
|
|
|
subOpts.isTail = opts.isTail;
|
|
|
|
subOpts.resultUnused = 0;
|
|
|
|
subOpts.canChoose = 0;
|
|
|
|
subOpts.target = compiler_declare_symbol(c, scope, form[1], 0);
|
|
|
|
slot = compile_value(c, subOpts, form[2]);
|
|
|
|
return compiler_coerce_slot(c, opts, slot);
|
|
|
|
} else {
|
|
|
|
return compile_global_def(c, opts, form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile var */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_var(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
if (dst_tuple_length(form) != 3)
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "expected 2 arguments to var");
|
2017-09-09 18:39:51 +00:00
|
|
|
if (DST_SYMBOL != form[1].type)
|
2017-06-25 20:36:20 +00:00
|
|
|
c_error(c, "expected symbol as first argument");
|
|
|
|
if (scope->parent) {
|
|
|
|
FormOptions subOpts;
|
|
|
|
Slot slot;
|
|
|
|
subOpts.isTail = opts.isTail;
|
|
|
|
subOpts.resultUnused = 0;
|
|
|
|
subOpts.canChoose = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
subOpts.target = compiler_declare_symbol(c, scope, form[1], DST_LOCAL_FLAG_MUTABLE);
|
2017-06-25 20:36:20 +00:00
|
|
|
slot = compile_value(c, subOpts, form[2]);
|
|
|
|
return compiler_coerce_slot(c, opts, slot);
|
|
|
|
} else {
|
|
|
|
return compile_global_var(c, opts, form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile series of expressions. This compiles the meat of
|
|
|
|
* function definitions and the inside of do forms. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_block(DstCompiler *c, FormOptions opts, const DstValue *form, uint32_t startIndex) {
|
|
|
|
DstScope *scope = c->tail;
|
2017-02-16 02:02:00 +00:00
|
|
|
FormOptions subOpts = form_options_default();
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t current = startIndex;
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Check for empty body */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) <= startIndex) return nil_slot();
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile the body */
|
2017-02-12 15:27:18 +00:00
|
|
|
subOpts.resultUnused = 1;
|
|
|
|
subOpts.isTail = 0;
|
|
|
|
subOpts.canChoose = 1;
|
2017-09-09 18:39:51 +00:00
|
|
|
while (current < dst_tuple_length(form) - 1) {
|
2017-03-07 20:29:40 +00:00
|
|
|
compiler_drop_slot(c, scope, compile_value(c, subOpts, form[current]));
|
2017-02-09 20:02:59 +00:00
|
|
|
++current;
|
|
|
|
}
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Compile the last expression in the body */
|
2017-09-09 18:39:51 +00:00
|
|
|
return compile_value(c, opts, form[dst_tuple_length(form) - 1]);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the last n bytes from the buffer and use them to construct
|
|
|
|
* a function definition. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static DstFuncDef *compiler_gen_funcdef(DstCompiler *c, uint32_t lastNBytes, uint32_t arity, int varargs) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
|
|
|
DstFuncDef *def = dst_alloc(c->vm, sizeof(DstFuncDef));
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Create enough space for the new byteCode */
|
2017-02-12 15:27:18 +00:00
|
|
|
if (lastNBytes > buffer->count)
|
2017-02-26 16:47:50 +00:00
|
|
|
c_error(c, "trying to extract more bytes from buffer than in buffer");
|
2017-09-09 18:39:51 +00:00
|
|
|
uint8_t * byteCode = dst_alloc(c->vm, lastNBytes);
|
2017-02-16 02:02:00 +00:00
|
|
|
def->byteCode = (uint16_t *)byteCode;
|
2017-02-09 20:02:59 +00:00
|
|
|
def->byteCodeLen = lastNBytes / 2;
|
|
|
|
/* Copy the last chunk of bytes in the buffer into the new
|
|
|
|
* memory for the function's byteCOde */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_memcpy(byteCode, buffer->data + buffer->count - lastNBytes, lastNBytes);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Remove the byteCode from the end of the buffer */
|
|
|
|
buffer->count -= lastNBytes;
|
|
|
|
/* Create the literals used by this function */
|
|
|
|
if (scope->literalsArray->count) {
|
2017-09-09 18:39:51 +00:00
|
|
|
def->literals = dst_alloc(c->vm, scope->literalsArray->count * sizeof(DstValue));
|
|
|
|
dst_memcpy(def->literals, scope->literalsArray->data,
|
|
|
|
scope->literalsArray->count * sizeof(DstValue));
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-12 20:16:55 +00:00
|
|
|
def->literals = NULL;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
def->literalsLen = scope->literalsArray->count;
|
|
|
|
/* Delete the sub scope */
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_pop_scope(c);
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Initialize the new FuncDef */
|
|
|
|
def->locals = scope->frameSize;
|
|
|
|
def->arity = arity;
|
2017-09-09 18:39:51 +00:00
|
|
|
def->flags = (varargs ? DST_FUNCDEF_FLAG_VARARG : 0) |
|
|
|
|
(scope->touchParent ? DST_FUNCDEF_FLAG_NEEDSPARENT : 0) |
|
|
|
|
(scope->touchEnv ? DST_FUNCDEF_FLAG_NEEDSENV : 0);
|
2017-02-12 20:16:55 +00:00
|
|
|
return def;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 21:46:28 +00:00
|
|
|
/* Check if a string a cstring are equal */
|
|
|
|
static int equal_cstr(const uint8_t *str, const char *cstr) {
|
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
for (i = 0; i < dst_string_length(str); ++i) {
|
2017-05-06 21:46:28 +00:00
|
|
|
if (cstr[i] == 0) return 0;
|
|
|
|
if (str[i] != ((const uint8_t *)cstr)[i]) return 0;
|
|
|
|
}
|
|
|
|
return cstr[i] == 0;
|
|
|
|
}
|
|
|
|
|
2017-03-08 21:03:14 +00:00
|
|
|
/* Compile a function from a function literal source form */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_function(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t current = 1;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t sizeBefore; /* Size of buffer before compiling function */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstScope *subDstScope;
|
|
|
|
DstArray *params;
|
2017-02-16 02:02:00 +00:00
|
|
|
FormOptions subOpts = form_options_default();
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
2017-05-07 20:48:35 +00:00
|
|
|
int varargs = 0;
|
2017-05-06 21:46:28 +00:00
|
|
|
uint32_t arity;
|
2017-02-16 02:02:00 +00:00
|
|
|
if (opts.resultUnused) return nil_slot();
|
|
|
|
ret = compiler_get_target(c, opts);
|
2017-09-09 18:39:51 +00:00
|
|
|
subDstScope = compiler_push_scope(c, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Define the function parameters */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (form[current].type != DST_ARRAY)
|
2017-03-07 20:29:40 +00:00
|
|
|
c_error(c, "expected function arguments array");
|
|
|
|
params = form[current++].data.array;
|
2017-05-06 21:46:28 +00:00
|
|
|
arity = params->count;
|
2017-02-09 20:02:59 +00:00
|
|
|
for (i = 0; i < params->count; ++i) {
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue param = params->data[i];
|
|
|
|
if (param.type != DST_SYMBOL)
|
2017-07-02 21:17:24 +00:00
|
|
|
c_error(c, "function parameters should be symbols");
|
2017-05-06 21:46:28 +00:00
|
|
|
/* Check for varargs */
|
|
|
|
if (equal_cstr(param.data.string, "&")) {
|
|
|
|
if (i != params->count - 1) {
|
|
|
|
c_error(c, "& is reserved for vararg argument in function");
|
|
|
|
}
|
|
|
|
varargs = 1;
|
|
|
|
arity--;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
/* The compiler puts the parameter locals
|
|
|
|
* in the right place by default - at the beginning
|
|
|
|
* of the stack frame. */
|
2017-09-09 18:39:51 +00:00
|
|
|
compiler_declare_symbol(c, subDstScope, param, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
/* Mark where we are on the stack so we can
|
|
|
|
* return to it later. */
|
|
|
|
sizeBefore = buffer->count;
|
|
|
|
/* Compile the body in the subscope */
|
|
|
|
subOpts.isTail = 1;
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_return(c, compile_block(c, subOpts, form, current));
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Create a new FuncDef as a constant in original scope by splicing
|
|
|
|
* out the relevant code from the buffer. */
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue newVal;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t literalIndex;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstFuncDef *def = compiler_gen_funcdef(c, buffer->count - sizeBefore, arity, varargs);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Add this FuncDef as a literal in the outer scope */
|
2017-09-09 18:39:51 +00:00
|
|
|
newVal.type = DST_FUNCDEF;
|
2017-03-19 16:16:40 +00:00
|
|
|
newVal.data.def = def;
|
2017-02-16 02:02:00 +00:00
|
|
|
literalIndex = compiler_add_literal(c, scope, newVal);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_CLN);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, literalIndex);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Branching special */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_if(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
FormOptions condOpts = opts;
|
|
|
|
FormOptions branchOpts = opts;
|
2017-02-09 20:02:59 +00:00
|
|
|
Slot left, right, condition;
|
2017-03-07 20:29:40 +00:00
|
|
|
uint32_t countAtJumpIf = 0;
|
|
|
|
uint32_t countAtJump = 0;
|
|
|
|
uint32_t countAfterFirstBranch = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Check argument count */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) < 3 || dst_tuple_length(form) > 4)
|
2017-02-16 02:02:00 +00:00
|
|
|
c_error(c, "if takes either 2 or 3 arguments");
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Compile the condition */
|
|
|
|
condOpts.isTail = 0;
|
|
|
|
condOpts.resultUnused = 0;
|
2017-03-07 20:29:40 +00:00
|
|
|
condition = compile_value(c, condOpts, form[1]);
|
2017-02-12 15:27:18 +00:00
|
|
|
/* If the condition is nil, just compile false path */
|
|
|
|
if (condition.isNil) {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) == 4) {
|
2017-03-07 20:29:40 +00:00
|
|
|
return compile_value(c, opts, form[3]);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
return condition;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
/* Mark where the buffer is now so we can write the jump
|
|
|
|
* length later */
|
2017-02-12 15:27:18 +00:00
|
|
|
countAtJumpIf = buffer->count;
|
2017-02-22 23:19:46 +00:00
|
|
|
buffer->count += sizeof(int32_t) + 2 * sizeof(uint16_t);
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Configure branch form options */
|
|
|
|
branchOpts.canChoose = 0;
|
|
|
|
branchOpts.target = condition.index;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile true path */
|
2017-03-07 20:29:40 +00:00
|
|
|
left = compile_value(c, branchOpts, form[2]);
|
2017-02-12 15:27:18 +00:00
|
|
|
if (opts.isTail) {
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_return(c, left);
|
2017-02-12 15:27:18 +00:00
|
|
|
} else {
|
|
|
|
/* If we need to jump again, do so */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) == 4) {
|
2017-02-12 15:27:18 +00:00
|
|
|
countAtJump = buffer->count;
|
2017-02-22 23:19:46 +00:00
|
|
|
buffer->count += sizeof(int32_t) + sizeof(uint16_t);
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_drop_slot(c, scope, left);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Reinsert jump with correct index */
|
|
|
|
countAfterFirstBranch = buffer->count;
|
2017-02-12 15:27:18 +00:00
|
|
|
buffer->count = countAtJumpIf;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_JIF);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, condition.index);
|
|
|
|
dst_buffer_push_i32(c->vm, buffer, (countAfterFirstBranch - countAtJumpIf) / 2);
|
2017-02-09 20:02:59 +00:00
|
|
|
buffer->count = countAfterFirstBranch;
|
|
|
|
/* Compile false path */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) == 4) {
|
2017-03-07 20:29:40 +00:00
|
|
|
right = compile_value(c, branchOpts, form[3]);
|
2017-02-16 02:02:00 +00:00
|
|
|
if (opts.isTail) compiler_return(c, right);
|
|
|
|
compiler_drop_slot(c, scope, right);
|
2017-02-12 15:27:18 +00:00
|
|
|
} else if (opts.isTail) {
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_return(c, condition);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Reset the second jump length */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!opts.isTail && dst_tuple_length(form) == 4) {
|
2017-02-09 20:02:59 +00:00
|
|
|
countAfterFirstBranch = buffer->count;
|
|
|
|
buffer->count = countAtJump;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_JMP);
|
|
|
|
dst_buffer_push_i32(c->vm, buffer, (countAfterFirstBranch - countAtJump) / 2);
|
2017-02-09 20:02:59 +00:00
|
|
|
buffer->count = countAfterFirstBranch;
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
if (opts.isTail)
|
|
|
|
condition.hasReturned = 1;
|
|
|
|
return condition;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 04:28:11 +00:00
|
|
|
/* While special */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_while(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
2017-02-10 04:28:11 +00:00
|
|
|
Slot cond;
|
2017-02-12 20:16:55 +00:00
|
|
|
uint32_t countAtStart = c->buffer->count;
|
|
|
|
uint32_t countAtJumpDelta;
|
|
|
|
uint32_t countAtFinish;
|
2017-02-16 02:02:00 +00:00
|
|
|
FormOptions defaultOpts = form_options_default();
|
|
|
|
compiler_push_scope(c, 1);
|
2017-02-10 04:28:11 +00:00
|
|
|
/* Compile condition */
|
2017-03-07 20:29:40 +00:00
|
|
|
cond = compile_value(c, defaultOpts, form[1]);
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Assert that cond is a real value - otherwise do nothing (nil is false,
|
2017-02-12 15:27:18 +00:00
|
|
|
* so loop never runs.) */
|
2017-02-12 20:16:55 +00:00
|
|
|
if (cond.isNil) return cond;
|
|
|
|
/* Leave space for jump later */
|
|
|
|
countAtJumpDelta = c->buffer->count;
|
|
|
|
c->buffer->count += sizeof(uint16_t) * 2 + sizeof(int32_t);
|
|
|
|
/* Compile loop body */
|
|
|
|
defaultOpts.resultUnused = 1;
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_drop_slot(c, c->tail, compile_block(c, defaultOpts, form, 2));
|
2017-02-10 04:28:11 +00:00
|
|
|
/* Jump back to the loop start */
|
|
|
|
countAtFinish = c->buffer->count;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, DST_OP_JMP);
|
|
|
|
dst_buffer_push_i32(c->vm, c->buffer, (int32_t)(countAtFinish - countAtStart) / -2);
|
2017-02-10 04:28:11 +00:00
|
|
|
countAtFinish = c->buffer->count;
|
|
|
|
/* Set the jump to the correct length */
|
|
|
|
c->buffer->count = countAtJumpDelta;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, DST_OP_JIF);
|
|
|
|
dst_buffer_push_u16(c->vm, c->buffer, cond.index);
|
|
|
|
dst_buffer_push_i32(c->vm, c->buffer, (int32_t)(countAtFinish - countAtJumpDelta) / 2);
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Pop scope */
|
|
|
|
c->buffer->count = countAtFinish;
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_pop_scope(c);
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Return nil */
|
|
|
|
if (opts.resultUnused)
|
2017-02-16 02:02:00 +00:00
|
|
|
return nil_slot();
|
2017-02-12 20:16:55 +00:00
|
|
|
else
|
2017-02-12 15:27:18 +00:00
|
|
|
return cond;
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Do special */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_do(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
2017-02-09 20:02:59 +00:00
|
|
|
Slot ret;
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_push_scope(c, 1);
|
|
|
|
ret = compile_block(c, opts, form, 1);
|
|
|
|
compiler_pop_scope(c);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Quote special - returns its argument as is. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_quote(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t literalIndex;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) != 2)
|
2017-02-22 23:19:46 +00:00
|
|
|
c_error(c, "quote takes exactly 1 argument");
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue x = form[1];
|
|
|
|
if (x.type == DST_NIL ||
|
|
|
|
x.type == DST_BOOLEAN ||
|
|
|
|
x.type == DST_REAL ||
|
|
|
|
x.type == DST_INTEGER) {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_nonref_type(c, opts, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
if (opts.resultUnused) return nil_slot();
|
|
|
|
ret = compiler_get_target(c, opts);
|
|
|
|
literalIndex = compiler_add_literal(c, scope, x);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_CST);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, literalIndex);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 13:43:49 +00:00
|
|
|
/* Apply special */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_apply(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-04-19 13:43:49 +00:00
|
|
|
/* Empty forms evaluate to nil. */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) < 3)
|
2017-04-19 13:43:49 +00:00
|
|
|
c_error(c, "apply expects at least 2 arguments");
|
|
|
|
{
|
|
|
|
Slot ret, callee;
|
|
|
|
SlotTracker tracker;
|
|
|
|
FormOptions subOpts = form_options_default();
|
|
|
|
uint32_t i;
|
|
|
|
tracker_init(c, &tracker);
|
|
|
|
/* Compile function to be called */
|
|
|
|
callee = compiler_realize_slot(c, compile_value(c, subOpts, form[1]));
|
|
|
|
/* Compile all of the arguments */
|
2017-09-09 18:39:51 +00:00
|
|
|
for (i = 2; i < dst_tuple_length(form) - 1; ++i) {
|
2017-04-19 13:43:49 +00:00
|
|
|
Slot slot = compile_value(c, subOpts, form[i]);
|
|
|
|
compiler_tracker_push(c, &tracker, slot);
|
|
|
|
}
|
2017-05-05 20:52:05 +00:00
|
|
|
/* Write last item */
|
2017-04-19 13:43:49 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
Slot slot = compile_value(c, subOpts, form[dst_tuple_length(form) - 1]);
|
2017-04-19 13:43:49 +00:00
|
|
|
slot = compiler_realize_slot(c, slot);
|
|
|
|
/* Free up some slots */
|
|
|
|
compiler_drop_slot(c, scope, callee);
|
|
|
|
compiler_drop_slot(c, scope, slot);
|
|
|
|
compiler_tracker_free(c, scope, &tracker);
|
|
|
|
/* Write first arguments */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_PSK);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, tracker.count);
|
2017-04-19 13:43:49 +00:00
|
|
|
/* Write the location of all of the arguments */
|
|
|
|
compiler_tracker_write(c, &tracker, 0);
|
|
|
|
/* Write last arguments */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_PAR);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, slot.index);
|
2017-04-19 13:43:49 +00:00
|
|
|
}
|
|
|
|
/* If this is in tail position do a tail call. */
|
|
|
|
if (opts.isTail) {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_TCL);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, callee.index);
|
2017-04-19 13:43:49 +00:00
|
|
|
ret.hasReturned = 1;
|
|
|
|
ret.isNil = 1;
|
|
|
|
} else {
|
|
|
|
ret = compiler_get_target(c, opts);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_CAL);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, callee.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
2017-04-19 13:43:49 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:21:30 +00:00
|
|
|
/* Transfer special */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_tran(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-05-09 23:21:30 +00:00
|
|
|
Slot t, v, r;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) != 3 && dst_tuple_length(form) != 2)
|
2017-05-09 23:21:30 +00:00
|
|
|
c_error(c, "tran expects 2 or 3 arguments");
|
|
|
|
t = compiler_realize_slot(c, compile_value(c, form_options_default(), form[1]));
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) == 3)
|
2017-05-09 23:21:30 +00:00
|
|
|
v = compiler_realize_slot(c, compile_value(c, form_options_default(), form[2]));
|
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
v = compile_value(c, form_options_default(), dst_wrap_nil());
|
2017-05-09 23:21:30 +00:00
|
|
|
r = compiler_get_target(c, opts);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_TRN);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, r.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, t.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, v.index);
|
2017-05-09 23:21:30 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Define a function type for Special Form helpers */
|
2017-09-09 18:39:51 +00:00
|
|
|
typedef Slot (*SpecialFormHelper) (DstCompiler *c, FormOptions opts, const DstValue *form);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* Dispatch to a special form */
|
2017-09-09 18:39:51 +00:00
|
|
|
static SpecialFormHelper get_special(const DstValue *form) {
|
2017-03-22 22:35:54 +00:00
|
|
|
const uint8_t *name;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) < 1 || form[0].type != DST_SYMBOL)
|
2017-02-09 20:02:59 +00:00
|
|
|
return NULL;
|
2017-03-07 20:29:40 +00:00
|
|
|
name = form[0].data.string;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* If we have a symbol with a zero length name, we have other
|
|
|
|
* problems. */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 0)
|
2017-02-09 20:02:59 +00:00
|
|
|
return NULL;
|
2017-06-25 23:17:54 +00:00
|
|
|
/* Specials */
|
2017-02-09 20:02:59 +00:00
|
|
|
switch (name[0]) {
|
2017-04-19 13:43:49 +00:00
|
|
|
case 'a':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 5 &&
|
2017-04-19 13:43:49 +00:00
|
|
|
name[1] == 'p' &&
|
|
|
|
name[2] == 'p' &&
|
|
|
|
name[3] == 'l' &&
|
|
|
|
name[4] == 'y') {
|
|
|
|
return compile_apply;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 03:01:34 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
case 'd':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 2 &&
|
2017-02-09 20:02:59 +00:00
|
|
|
name[1] == 'o') {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_do;
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (dst_string_length(name) == 3 &&
|
2017-06-25 20:36:20 +00:00
|
|
|
name[1] == 'e' &&
|
|
|
|
name[2] == 'f') {
|
|
|
|
return compile_def;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 2 &&
|
2017-02-09 20:02:59 +00:00
|
|
|
name[1] == 'f') {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_if;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 2 &&
|
2017-02-09 20:02:59 +00:00
|
|
|
name[1] == 'n') {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_function;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 5 &&
|
2017-02-09 20:02:59 +00:00
|
|
|
name[1] == 'u' &&
|
|
|
|
name[2] == 'o' &&
|
|
|
|
name[3] == 't' &&
|
|
|
|
name[4] == 'e') {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_quote;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-05-09 23:21:30 +00:00
|
|
|
case 't':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 4 &&
|
2017-05-09 23:21:30 +00:00
|
|
|
name[1] == 'r' &&
|
|
|
|
name[2] == 'a' &&
|
|
|
|
name[3] == 'n') {
|
|
|
|
return compile_tran;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 03:01:34 +00:00
|
|
|
break;
|
2017-06-25 20:36:20 +00:00
|
|
|
case 'v':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 3 &&
|
2017-06-25 20:36:20 +00:00
|
|
|
name[1] == 'a' &&
|
|
|
|
name[2] == 'r') {
|
|
|
|
return compile_var;
|
2017-07-02 01:51:16 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 7 &&
|
2017-06-25 20:36:20 +00:00
|
|
|
name[1] == 'a' &&
|
|
|
|
name[2] == 'r' &&
|
|
|
|
name[3] == 's' &&
|
|
|
|
name[4] == 'e' &&
|
2017-06-29 02:51:24 +00:00
|
|
|
name[5] == 't' &&
|
|
|
|
name[6] == '!') {
|
2017-06-25 20:36:20 +00:00
|
|
|
return compile_varset;
|
2017-07-02 01:51:16 +00:00
|
|
|
}
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-02-10 04:28:11 +00:00
|
|
|
case 'w':
|
2017-02-12 20:16:55 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_string_length(name) == 5 &&
|
2017-02-12 20:16:55 +00:00
|
|
|
name[1] == 'h' &&
|
|
|
|
name[2] == 'i' &&
|
|
|
|
name[3] == 'l' &&
|
|
|
|
name[4] == 'e') {
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_while;
|
2017-02-12 20:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 02:54:18 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:29:40 +00:00
|
|
|
/* Compile an array */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_array(DstCompiler *c, FormOptions opts, DstArray *array) {
|
|
|
|
DstScope *scope = c->tail;
|
2017-03-07 20:29:40 +00:00
|
|
|
FormOptions subOpts = form_options_default();
|
2017-09-09 18:39:51 +00:00
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-03-07 20:29:40 +00:00
|
|
|
Slot ret;
|
|
|
|
SlotTracker tracker;
|
|
|
|
uint32_t i, count;
|
|
|
|
count = array->count;
|
|
|
|
ret = compiler_get_target(c, opts);
|
|
|
|
tracker_init(c, &tracker);
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
Slot slot = compile_value(c, subOpts, array->data[i]);
|
|
|
|
compiler_tracker_push(c, &tracker, compiler_realize_slot(c, slot));
|
|
|
|
}
|
|
|
|
compiler_tracker_free(c, scope, &tracker);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_ARR);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, count);
|
2017-03-07 20:29:40 +00:00
|
|
|
compiler_tracker_write(c, &tracker, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile an object literal */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_table(DstCompiler *c, FormOptions opts, DstTable *tab) {
|
|
|
|
DstScope *scope = c->tail;
|
2017-03-07 20:29:40 +00:00
|
|
|
FormOptions subOpts = form_options_default();
|
2017-09-09 18:39:51 +00:00
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-03-07 20:29:40 +00:00
|
|
|
Slot ret;
|
|
|
|
SlotTracker tracker;
|
|
|
|
uint32_t i, cap;
|
2017-04-25 01:00:56 +00:00
|
|
|
cap = tab->capacity;
|
2017-03-07 20:29:40 +00:00
|
|
|
ret = compiler_get_target(c, opts);
|
|
|
|
tracker_init(c, &tracker);
|
2017-04-16 13:39:41 +00:00
|
|
|
for (i = 0; i < cap; i += 2) {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (tab->data[i].type != DST_NIL) {
|
2017-04-25 01:00:56 +00:00
|
|
|
Slot slot = compile_value(c, subOpts, tab->data[i]);
|
2017-03-07 20:29:40 +00:00
|
|
|
compiler_tracker_push(c, &tracker, compiler_realize_slot(c, slot));
|
2017-04-25 01:00:56 +00:00
|
|
|
slot = compile_value(c, subOpts, tab->data[i + 1]);
|
2017-03-07 20:29:40 +00:00
|
|
|
compiler_tracker_push(c, &tracker, compiler_realize_slot(c, slot));
|
2017-03-10 05:17:34 +00:00
|
|
|
}
|
2017-03-07 20:29:40 +00:00
|
|
|
}
|
|
|
|
compiler_tracker_free(c, scope, &tracker);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_DIC);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, tab->count * 2);
|
2017-03-07 20:29:40 +00:00
|
|
|
compiler_tracker_write(c, &tracker, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:36:20 +00:00
|
|
|
/* Compile a form. Checks for special forms. */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_form(DstCompiler *c, FormOptions opts, const DstValue *form) {
|
|
|
|
DstScope *scope = c->tail;
|
|
|
|
DstBuffer *buffer = c->buffer;
|
2017-02-09 20:02:59 +00:00
|
|
|
SpecialFormHelper helper;
|
|
|
|
/* Empty forms evaluate to nil. */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_tuple_length(form) == 0) {
|
|
|
|
DstValue temp;
|
|
|
|
temp.type = DST_NIL;
|
2017-02-16 02:02:00 +00:00
|
|
|
return compile_nonref_type(c, opts, temp);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
/* Check and handle special forms */
|
2017-02-16 02:02:00 +00:00
|
|
|
helper = get_special(form);
|
2017-02-09 20:02:59 +00:00
|
|
|
if (helper != NULL) {
|
|
|
|
return helper(c, opts, form);
|
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret, callee;
|
2017-02-09 20:02:59 +00:00
|
|
|
SlotTracker tracker;
|
2017-02-16 02:02:00 +00:00
|
|
|
FormOptions subOpts = form_options_default();
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t i;
|
2017-02-16 02:02:00 +00:00
|
|
|
tracker_init(c, &tracker);
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Compile function to be called */
|
2017-03-07 20:29:40 +00:00
|
|
|
callee = compiler_realize_slot(c, compile_value(c, subOpts, form[0]));
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Compile all of the arguments */
|
2017-09-09 18:39:51 +00:00
|
|
|
for (i = 1; i < dst_tuple_length(form); ++i) {
|
2017-03-07 20:29:40 +00:00
|
|
|
Slot slot = compile_value(c, subOpts, form[i]);
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_tracker_push(c, &tracker, slot);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Free up some slots */
|
2017-02-16 02:02:00 +00:00
|
|
|
compiler_drop_slot(c, scope, callee);
|
|
|
|
compiler_tracker_free(c, scope, &tracker);
|
2017-04-19 13:02:12 +00:00
|
|
|
/* Prepare next stack frame */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_PSK);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, dst_tuple_length(form) - 1);
|
2017-04-19 13:02:12 +00:00
|
|
|
/* Write the location of all of the arguments */
|
|
|
|
compiler_tracker_write(c, &tracker, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* If this is in tail position do a tail call. */
|
|
|
|
if (opts.isTail) {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_TCL);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, callee.index);
|
2017-02-12 15:27:18 +00:00
|
|
|
ret.hasReturned = 1;
|
|
|
|
ret.isNil = 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
ret = compiler_get_target(c, opts);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_push_u16(c->vm, buffer, DST_OP_CAL);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, callee.index);
|
|
|
|
dst_buffer_push_u16(c->vm, buffer, ret.index);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recursively compile any value or form */
|
2017-09-09 18:39:51 +00:00
|
|
|
static Slot compile_value(DstCompiler *c, FormOptions opts, DstValue x) {
|
2017-06-29 02:51:24 +00:00
|
|
|
Slot ret;
|
|
|
|
/* Check if recursion is too deep */
|
2017-07-09 17:31:30 +00:00
|
|
|
if (--c->recursionGuard == 0) {
|
2017-06-29 02:51:24 +00:00
|
|
|
c_error(c, "recursed too deeply");
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
switch (x.type) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_NIL:
|
|
|
|
case DST_BOOLEAN:
|
|
|
|
case DST_REAL:
|
|
|
|
case DST_INTEGER:
|
2017-06-29 02:51:24 +00:00
|
|
|
ret = compile_nonref_type(c, opts, x);
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_SYMBOL:
|
2017-06-29 02:51:24 +00:00
|
|
|
ret = compile_symbol(c, opts, x);
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
2017-06-29 02:51:24 +00:00
|
|
|
ret = compile_form(c, opts, x.data.tuple);
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_ARRAY:
|
2017-06-29 02:51:24 +00:00
|
|
|
ret = compile_array(c, opts, x.data.array);
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TABLE:
|
2017-06-29 02:51:24 +00:00
|
|
|
ret = compile_table(c, opts, x.data.table);
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
default:
|
2017-06-29 02:51:24 +00:00
|
|
|
ret = compile_literal(c, opts, x);
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-07-09 17:31:30 +00:00
|
|
|
c->recursionGuard++;
|
2017-06-29 02:51:24 +00:00
|
|
|
return ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile interface. Returns a function that evaluates the
|
|
|
|
* given AST. Returns NULL if there was an error during compilation. */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue dst_compile(Dst *vm, DstTable *env, DstValue form) {
|
|
|
|
DstCompiler c;
|
2017-02-16 02:02:00 +00:00
|
|
|
FormOptions opts = form_options_default();
|
2017-09-09 18:39:51 +00:00
|
|
|
DstFuncDef *def;
|
2017-07-09 19:35:17 +00:00
|
|
|
if (setjmp(c.onError)) {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (c.error.type == DST_NIL)
|
|
|
|
return dst_string_cv(vm, "unknown error");
|
2017-07-09 19:35:17 +00:00
|
|
|
return c.error;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
c.error.type = DST_NIL;
|
2017-07-09 19:35:17 +00:00
|
|
|
c.env = env;
|
|
|
|
c.vm = vm;
|
|
|
|
c.tail = NULL;
|
2017-09-09 18:39:51 +00:00
|
|
|
c.buffer = dst_buffer(vm, 24);
|
|
|
|
c.recursionGuard = DST_RECURSION_GUARD;
|
2017-07-09 19:35:17 +00:00
|
|
|
compiler_push_scope(&c, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
opts.isTail = 1;
|
2017-07-09 19:35:17 +00:00
|
|
|
compiler_return(&c, compile_value(&c, opts, form));
|
|
|
|
def = compiler_gen_funcdef(&c, c.buffer->count, 0, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
DstFuncEnv *env = dst_alloc(vm, sizeof(DstFuncEnv));
|
|
|
|
DstFunction *func = dst_alloc(vm, sizeof(DstFunction));
|
2017-05-05 20:52:05 +00:00
|
|
|
env->values = NULL;
|
|
|
|
env->stackOffset = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
env->thread = NULL;
|
|
|
|
func->parent = NULL;
|
|
|
|
func->def = def;
|
|
|
|
func->env = env;
|
2017-09-09 18:39:51 +00:00
|
|
|
return dst_wrap_function(func);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-06-24 18:27:29 +00:00
|
|
|
}
|