2017-02-09 20:02:59 +00:00
|
|
|
#include "compile.h"
|
2017-02-09 23:50:47 +00:00
|
|
|
#include "ds.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
#include "value.h"
|
|
|
|
#include "vm.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* During compilation, FormOptions are passed to ASTs
|
|
|
|
* as configuration options to allow for some optimizations. */
|
|
|
|
typedef struct FormOptions FormOptions;
|
|
|
|
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
|
|
|
|
* copmutation */
|
|
|
|
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
|
|
|
|
* can be allocated with CompilerGetLocal. */
|
|
|
|
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-02-12 15:27:18 +00:00
|
|
|
* anything will try to return bil slots. */
|
|
|
|
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. */
|
|
|
|
typedef struct SlotTracker SlotTracker;
|
|
|
|
struct SlotTracker {
|
|
|
|
Slot * slots;
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A Scope is a lexical scope in the program. It is
|
|
|
|
* responsible for aliasing programmer facing names to
|
|
|
|
* Slots and for keeping track of literals. It also
|
|
|
|
* points to the parent Scope, and its current child
|
|
|
|
* Scope. */
|
|
|
|
struct Scope {
|
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;
|
|
|
|
uint16_t * freeHeap;
|
|
|
|
Dictionary * literals;
|
|
|
|
Array * literalsArray;
|
|
|
|
Dictionary * locals;
|
|
|
|
Scope * nextScope;
|
|
|
|
Scope * previousScope;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Provides default FormOptions */
|
|
|
|
static FormOptions FormOptionsDefault() {
|
|
|
|
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-13 02:54:18 +00:00
|
|
|
BufferDefine(UInt32, uint32_t)
|
|
|
|
BufferDefine(Int32, int32_t)
|
|
|
|
BufferDefine(Number, Number)
|
|
|
|
BufferDefine(UInt16, uint16_t)
|
|
|
|
BufferDefine(Int16, int16_t)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* If there is an error during compilation,
|
|
|
|
* jump back to start */
|
2017-02-12 15:27:18 +00:00
|
|
|
static void CError(Compiler * c, const char * 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. */
|
|
|
|
static Scope * CompilerPushScope(Compiler * c, int sameFunction) {
|
2017-02-09 23:50:47 +00:00
|
|
|
Scope * scope = VMAlloc(c->vm, sizeof(Scope));
|
|
|
|
scope->locals = DictNew(c->vm, 10);
|
|
|
|
scope->freeHeap = VMAlloc(c->vm, 10 * sizeof(uint16_t));
|
2017-02-09 20:02:59 +00:00
|
|
|
scope->heapSize = 0;
|
|
|
|
scope->heapCapacity = 10;
|
|
|
|
scope->nextScope = NULL;
|
|
|
|
scope->previousScope = c->tail;
|
2017-02-12 15:27:18 +00:00
|
|
|
scope->frameSize = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
if (c->tail) {
|
2017-02-09 20:02:59 +00:00
|
|
|
c->tail->nextScope = scope;
|
2017-02-10 04:28:11 +00:00
|
|
|
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-12 20:16:55 +00:00
|
|
|
CError(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-02-09 23:50:47 +00:00
|
|
|
scope->literals = DictNew(c->vm, 10);
|
|
|
|
scope->literalsArray = ArrayNew(c->vm, 10);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
c->tail = scope;
|
|
|
|
if (!c->root)
|
|
|
|
c->root = scope;
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the inner most scope from the compiler stack */
|
|
|
|
static void CompilerPopScope(Compiler * c) {
|
2017-02-12 15:27:18 +00:00
|
|
|
Scope * last = c->tail;
|
|
|
|
if (last == NULL) {
|
2017-02-09 20:02:59 +00:00
|
|
|
CError(c, "No scope to pop.");
|
|
|
|
} 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
|
|
|
}
|
|
|
|
c->tail = last->previousScope;
|
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
|
|
|
c->tail->nextScope = NULL;
|
|
|
|
} else {
|
|
|
|
/* We deleted the last scope */
|
2017-02-12 20:16:55 +00:00
|
|
|
c->root = NULL;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next stack position that is open for
|
|
|
|
* a variable */
|
|
|
|
static uint16_t CompilerGetLocal(Compiler * c, Scope * scope) {
|
|
|
|
if (scope->heapSize == 0) {
|
|
|
|
if (scope->nextLocal + 1 == 0) {
|
|
|
|
CError(c, "Too many local variables. Try splitting up your functions :)");
|
|
|
|
}
|
|
|
|
return scope->nextLocal++;
|
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
return scope->freeHeap[--scope->heapSize];
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a slot on the stack for other locals and/or
|
|
|
|
* intermediate values */
|
|
|
|
static void CompilerFreeLocal(Compiler * c, Scope * 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;
|
|
|
|
uint16_t * newData = VMAlloc(c->vm, newCap * sizeof(uint16_t));
|
|
|
|
memcpy(newData, scope->freeHeap, scope->heapSize * sizeof(uint16_t));
|
|
|
|
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. */
|
|
|
|
static void CompilerTrackerInit(Compiler * c, SlotTracker * tracker) {
|
2017-02-09 23:50:47 +00:00
|
|
|
tracker->slots = VMAlloc(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. */
|
|
|
|
static void CompilerDropSlot(Compiler * c, Scope * scope, Slot slot) {
|
2017-02-12 15:27:18 +00:00
|
|
|
if (!slot.isNil && slot.isTemp) {
|
2017-02-09 20:02:59 +00:00
|
|
|
CompilerFreeLocal(c, scope, slot.index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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. */
|
|
|
|
static Slot CompilerReturn(Compiler * c, Slot slot) {
|
|
|
|
Slot ret;
|
|
|
|
ret.hasReturned = 1;
|
|
|
|
ret.isNil = 1;
|
|
|
|
if (slot.hasReturned) {
|
|
|
|
/* Do nothing */
|
|
|
|
} else if (slot.isNil) {
|
|
|
|
/* Return nil */
|
|
|
|
BufferPushUInt16(c->vm, c->buffer, VM_OP_RTN);
|
|
|
|
} else {
|
|
|
|
/* Return normal value */
|
|
|
|
BufferPushUInt16(c->vm, c->buffer, VM_OP_RET);
|
|
|
|
BufferPushUInt16(c->vm, c->buffer, slot.index);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets a temporary slot for the bottom-most scope. */
|
|
|
|
static Slot CompilerGetTemp(Compiler * c) {
|
2017-02-12 20:16:55 +00:00
|
|
|
Scope * scope = c->tail;
|
|
|
|
Slot ret;
|
|
|
|
ret.isTemp = 1;
|
|
|
|
ret.isNil = 0;
|
|
|
|
ret.hasReturned = 0;
|
|
|
|
ret.index = CompilerGetLocal(c, scope);
|
|
|
|
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. */
|
|
|
|
static Slot CompilerGetTarget(Compiler * c, FormOptions opts) {
|
2017-02-12 20:16:55 +00:00
|
|
|
if (opts.canChoose) {
|
|
|
|
return CompilerGetTemp(c);
|
|
|
|
} 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. */
|
|
|
|
static Slot CompilerRealizeSlot(Compiler * c, Slot slot) {
|
2017-02-12 20:16:55 +00:00
|
|
|
if (slot.isNil) {
|
|
|
|
slot = CompilerGetTemp(c);
|
2017-02-12 15:27:18 +00:00
|
|
|
BufferPushUInt16(c->vm, c->buffer, VM_OP_NIL);
|
|
|
|
BufferPushUInt16(c->vm, c->buffer, slot.index);
|
2017-02-12 20:16:55 +00:00
|
|
|
}
|
|
|
|
return slot;
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to get a nil slot */
|
|
|
|
static Slot NilSlot() { Slot ret; ret.isNil = 1; return ret; }
|
|
|
|
|
|
|
|
/* Writes all of the slots in the tracker to the compiler */
|
|
|
|
static void CompilerTrackerWrite(Compiler * c, SlotTracker * tracker, int reverse) {
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t i;
|
2017-02-10 04:28:11 +00:00
|
|
|
Buffer * 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)
|
|
|
|
CError(c, "Trying to write nil slot.");
|
|
|
|
BufferPushUInt16(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. */
|
|
|
|
static void CompilerTrackerFree(Compiler * c, Scope * scope, SlotTracker * tracker) {
|
|
|
|
uint32_t i;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Free in reverse order */
|
|
|
|
for (i = tracker->count - 1; i < tracker->count; --i) {
|
|
|
|
CompilerDropSlot(c, scope, tracker->slots[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new Slot to a slot tracker. */
|
|
|
|
static void CompilerTrackerPush(Compiler * c, SlotTracker * tracker, Slot slot) {
|
|
|
|
if (tracker->count >= tracker->capacity) {
|
|
|
|
uint32_t newCap = 2 * tracker->count;
|
2017-02-09 23:50:47 +00:00
|
|
|
Slot * newData = VMAlloc(c->vm, newCap * sizeof(Slot));
|
2017-02-09 20:02:59 +00:00
|
|
|
memcpy(newData, tracker->slots, tracker->count * sizeof(Slot));
|
|
|
|
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.*/
|
|
|
|
static uint16_t CompilerAddLiteral(Compiler * c, Scope * scope, Value x) {
|
2017-02-10 04:28:11 +00:00
|
|
|
Value checkDup = DictGet(scope->literals, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t literalIndex = 0;
|
|
|
|
if (checkDup.type != TYPE_NIL) {
|
|
|
|
/* An equal literal is already registered in the current scope */
|
|
|
|
return (uint16_t) checkDup.data.number;
|
|
|
|
} else {
|
|
|
|
/* Add our literal for tracking */
|
|
|
|
Value valIndex;
|
|
|
|
valIndex.type = TYPE_NUMBER;
|
|
|
|
literalIndex = scope->literalsArray->count;
|
2017-02-09 20:56:45 +00:00
|
|
|
valIndex.data.number = literalIndex;
|
2017-02-10 04:28:11 +00:00
|
|
|
DictPut(c->vm, scope->literals, x, valIndex);
|
2017-02-09 23:50:47 +00:00
|
|
|
ArrayPush(c->vm, scope->literalsArray, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return literalIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Declare a symbol in a given scope. */
|
|
|
|
static uint16_t CompilerDeclareSymbol(Compiler * c, Scope * scope, Value sym) {
|
|
|
|
if (sym.type != TYPE_SYMBOL) {
|
|
|
|
CError(c, "Expected symbol");
|
|
|
|
}
|
|
|
|
Value x;
|
|
|
|
uint16_t target = CompilerGetLocal(c, scope);
|
|
|
|
x.type = TYPE_NUMBER;
|
|
|
|
x.data.number = target;
|
2017-02-10 04:28:11 +00:00
|
|
|
DictPut(c->vm, scope->locals, sym, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to resolve a symbol. If the symbol can be resovled, return true and
|
|
|
|
* pass back the level and index by reference. */
|
|
|
|
static int ScopeSymbolResolve(Scope * scope, Value x,
|
|
|
|
uint16_t * level, uint16_t * index) {
|
2017-02-10 04:28:11 +00:00
|
|
|
uint32_t currentLevel = scope->level;
|
2017-02-09 20:02:59 +00:00
|
|
|
while (scope) {
|
2017-02-10 04:28:11 +00:00
|
|
|
Value check = DictGet(scope->locals, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
if (check.type != TYPE_NIL) {
|
2017-02-10 04:28:11 +00:00
|
|
|
*level = currentLevel - scope->level;
|
2017-02-09 20:02:59 +00:00
|
|
|
*index = (uint16_t) check.data.number;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
scope = scope->previousScope;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* to the targtet. If target < 0, the Compiler can choose whatever
|
|
|
|
* 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. */
|
|
|
|
static Slot CompileValue(Compiler * c, FormOptions opts, Value x);
|
|
|
|
|
|
|
|
/* Compile a structure that evaluates to a literal value. Useful
|
2017-02-12 15:27:18 +00:00
|
|
|
* for objects like strings, or anything else that cannot be instatiated else {
|
2017-02-12 20:16:55 +00:00
|
|
|
break;
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
2017-02-12 20:16:55 +00:00
|
|
|
* from bytecode and doesn't do anything in the AST. */
|
2017-02-09 20:02:59 +00:00
|
|
|
static Slot CompileLiteral(Compiler * c, FormOptions opts, Value x) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * 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-02-12 15:27:18 +00:00
|
|
|
if (opts.resultUnused) return NilSlot();
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 20:02:59 +00:00
|
|
|
literalIndex = CompilerAddLiteral(c, scope, x);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_CST);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, literalIndex);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile boolean, nil, and number values. */
|
|
|
|
static Slot CompileNonReferenceType(Compiler * c, FormOptions opts, Value x) {
|
|
|
|
Buffer * buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
|
|
|
if (opts.resultUnused) return NilSlot();
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 20:02:59 +00:00
|
|
|
if (x.type == TYPE_NIL) {
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_NIL);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
2017-02-09 20:02:59 +00:00
|
|
|
} else if (x.type == TYPE_BOOLEAN) {
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, x.data.boolean ? VM_OP_TRU : VM_OP_FLS);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
2017-02-09 20:02:59 +00:00
|
|
|
} else if (x.type == TYPE_NUMBER) {
|
|
|
|
Number number = x.data.number;
|
|
|
|
int32_t int32Num = (int32_t) number;
|
|
|
|
if (number == (Number) int32Num) {
|
|
|
|
if (int32Num <= 32767 && int32Num >= -32768) {
|
|
|
|
int16_t int16Num = (int16_t) number;
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_I16);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushInt16(c->vm, buffer, int16Num);
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_I32);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushInt32(c->vm, buffer, int32Num);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_F64);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushNumber(c->vm, buffer, number);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CError(c, "Expected boolean, nil, or number type.");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a symbol. Resolves any kind of symbol. */
|
|
|
|
static Slot CompileSymbol(Compiler * c, FormOptions opts, Value sym) {
|
|
|
|
Buffer * buffer = c->buffer;
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
uint16_t index = 0;
|
|
|
|
uint16_t level = 0;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
|
|
|
if (opts.resultUnused) return NilSlot();
|
|
|
|
if (!ScopeSymbolResolve(scope, sym, &level, &index))
|
|
|
|
CError(c, "Undefined symbol");
|
|
|
|
if (level > 0) {
|
|
|
|
/* We have an upvalue */
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_UPV);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, level);
|
|
|
|
BufferPushUInt16(c->vm, buffer, index);
|
|
|
|
} 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;
|
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_MOV);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, index);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a dictionary literal. The order of compilation
|
|
|
|
* is undefined, although a key is evalated before its value,
|
|
|
|
* assuming the dictionary is unchanged by macros. */
|
|
|
|
static Slot CompileDict(Compiler * c, FormOptions opts, Dictionary * dict) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
FormOptions subOpts = FormOptionsDefault();
|
|
|
|
DictionaryIterator iter;
|
|
|
|
DictBucket * bucket;
|
|
|
|
SlotTracker tracker;
|
|
|
|
/* Calculate sub flags */
|
2017-02-12 15:27:18 +00:00
|
|
|
subOpts.resultUnused = opts.resultUnused;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile all of the arguments */
|
|
|
|
CompilerTrackerInit(c, &tracker);
|
|
|
|
DictIterate(dict, &iter);
|
|
|
|
while (DictIterateNext(&iter, &bucket)) {
|
|
|
|
Slot keySlot = CompileValue(c, subOpts, bucket->key);
|
2017-02-12 15:27:18 +00:00
|
|
|
if (subOpts.resultUnused) CompilerDropSlot(c, scope, keySlot);
|
2017-02-09 20:02:59 +00:00
|
|
|
Slot valueSlot = CompileValue(c, subOpts, bucket->value);
|
2017-02-12 15:27:18 +00:00
|
|
|
if (subOpts.resultUnused) CompilerDropSlot(c, scope, valueSlot);
|
|
|
|
if (!subOpts.resultUnused) {
|
2017-02-09 20:02:59 +00:00
|
|
|
CompilerTrackerPush(c, &tracker, keySlot);
|
|
|
|
CompilerTrackerPush(c, &tracker, valueSlot);
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Free up slots */
|
|
|
|
CompilerTrackerFree(c, scope, &tracker);
|
|
|
|
if (opts.resultUnused) {
|
|
|
|
ret = NilSlot();
|
|
|
|
} else {
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_DIC);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
2017-02-11 19:01:06 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, dict->count * 2);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
/* Write the location of all of the arguments */
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerTrackerWrite(c, &tracker, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Compile values in an array sequentail and track the returned slots.
|
|
|
|
* If the result is unused, immediately drop slots we don't need. Can
|
|
|
|
* also ignore the end of an array. */
|
|
|
|
static void CompilerTrackerInitArray(Compiler * c, FormOptions opts,
|
|
|
|
SlotTracker * tracker, Array * array, uint32_t start, uint32_t fromEnd) {
|
2017-02-09 20:02:59 +00:00
|
|
|
Scope * scope = c->tail;
|
|
|
|
FormOptions subOpts = FormOptionsDefault();
|
|
|
|
uint32_t i;
|
|
|
|
/* Calculate sub flags */
|
2017-02-12 15:27:18 +00:00
|
|
|
subOpts.resultUnused = opts.resultUnused;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile all of the arguments */
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerTrackerInit(c, tracker);
|
|
|
|
/* Nothing to compile */
|
|
|
|
if (array->count <= fromEnd) return;
|
|
|
|
/* Compile body of array */
|
|
|
|
for (i = start; i < (array->count - fromEnd); ++i) {
|
2017-02-09 20:02:59 +00:00
|
|
|
Slot slot = CompileValue(c, subOpts, array->data[i]);
|
2017-02-12 15:27:18 +00:00
|
|
|
if (subOpts.resultUnused)
|
2017-02-09 20:02:59 +00:00
|
|
|
CompilerDropSlot(c, scope, slot);
|
|
|
|
else
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerTrackerPush(c, tracker, CompilerRealizeSlot(c, slot));
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile an array literal. The array is evaluated left
|
|
|
|
* to right. Arrays are normally compiled as forms to be evaluated, however. */
|
|
|
|
static Slot CompileArray(Compiler * c, FormOptions opts, Array * array) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
|
|
|
Slot ret;
|
|
|
|
SlotTracker tracker;
|
|
|
|
/* Compile contents of array */
|
|
|
|
CompilerTrackerInitArray(c, opts, &tracker, array, 0, 0);
|
|
|
|
/* Free up slots in tracker */
|
|
|
|
CompilerTrackerFree(c, scope, &tracker);
|
|
|
|
if (opts.resultUnused) {
|
|
|
|
ret = NilSlot();
|
|
|
|
} else {
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_ARR);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, array->count);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
/* Write the location of all of the arguments */
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerTrackerWrite(c, &tracker, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a special form in the form of an operator. There
|
|
|
|
* are four choices for opcodes - when the operator is called
|
|
|
|
* with 0, 1, 2, or n arguments. When the operator form is
|
|
|
|
* called with n arguments, the number of arguments is written
|
|
|
|
* after the op code, followed by those arguments.
|
|
|
|
*
|
2017-02-13 02:54:18 +00:00
|
|
|
* This makes a few assumptions about the operators. One, no side
|
2017-02-09 20:02:59 +00:00
|
|
|
* effects. With this assumptions, if the result of the operator
|
|
|
|
* is unused, it's calculation can be ignored (the evaluation of
|
|
|
|
* its argument is still carried out, but their results can
|
|
|
|
* also be ignored). */
|
|
|
|
static Slot CompileOperator(Compiler * c, FormOptions opts, Array * form,
|
2017-02-10 04:28:11 +00:00
|
|
|
int16_t op0, int16_t op1, int16_t op2, int16_t opn, int reverseOperands) {
|
2017-02-09 20:02:59 +00:00
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
SlotTracker tracker;
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Compile operands */
|
|
|
|
CompilerTrackerInitArray(c, opts, &tracker, form, 1, 0);
|
|
|
|
/* Free up space */
|
|
|
|
CompilerTrackerFree(c, scope, &tracker);
|
|
|
|
if (opts.resultUnused) {
|
2017-02-12 20:16:55 +00:00
|
|
|
ret = NilSlot();
|
2017-02-11 19:01:06 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
ret = CompilerGetTarget(c, opts);
|
|
|
|
/* Write the correct opcode */
|
|
|
|
if (form->count < 2) {
|
|
|
|
if (op0 < 0) {
|
|
|
|
if (opn < 0) CError(c, "This operator does not take 0 arguments.");
|
2017-02-13 02:54:18 +00:00
|
|
|
/* Use multiple form of op */
|
2017-02-12 15:27:18 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, opn);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, 0);
|
|
|
|
} else {
|
|
|
|
BufferPushUInt16(c->vm, buffer, op0);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
}
|
|
|
|
} else if (form->count == 2) {
|
|
|
|
if (op1 < 0) {
|
|
|
|
if (opn < 0) CError(c, "This operator does not take 1 argument.");
|
2017-02-13 02:54:18 +00:00
|
|
|
/* Use multiple form of op */
|
2017-02-12 15:27:18 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, opn);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, 1);
|
|
|
|
} else {
|
|
|
|
BufferPushUInt16(c->vm, buffer, op1);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
}
|
|
|
|
} else if (form->count == 3) {
|
|
|
|
if (op2 < 0) CError(c, "This operator does not take 2 arguments.");
|
|
|
|
BufferPushUInt16(c->vm, buffer, op2);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
} else {
|
|
|
|
if (opn < 0) CError(c, "This operator does not take n arguments.");
|
|
|
|
BufferPushUInt16(c->vm, buffer, opn);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, form->count - 1);
|
|
|
|
}
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Write the location of all of the arguments */
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerTrackerWrite(c, &tracker, reverseOperands);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Math specials */
|
|
|
|
static Slot CompileAddition(Compiler * c, FormOptions opts, Array * form) {
|
2017-02-12 15:27:18 +00:00
|
|
|
return CompileOperator(c, opts, form, VM_OP_LD0, -1, VM_OP_ADD, VM_OP_ADM, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
static Slot CompileSubtraction(Compiler * c, FormOptions opts, Array * form) {
|
2017-02-12 15:27:18 +00:00
|
|
|
return CompileOperator(c, opts, form, VM_OP_LD0, -1, VM_OP_SUB, VM_OP_SBM, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
static Slot CompileMultiplication(Compiler * c, FormOptions opts, Array * form) {
|
2017-02-12 15:27:18 +00:00
|
|
|
return CompileOperator(c, opts, form, VM_OP_LD1, -1, VM_OP_MUL, VM_OP_MUM, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
static Slot CompileDivision(Compiler * c, FormOptions opts, Array * form) {
|
2017-02-12 15:27:18 +00:00
|
|
|
return CompileOperator(c, opts, form, VM_OP_LD1, -1, VM_OP_DIV, VM_OP_DVM, 0);
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
|
|
|
static Slot CompileEquals(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, VM_OP_TRU, VM_OP_TRU, VM_OP_EQL, -1, 0);
|
|
|
|
}
|
|
|
|
static Slot CompileLessThan(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, VM_OP_TRU, VM_OP_TRU, VM_OP_LTN, -1, 0);
|
|
|
|
}
|
|
|
|
static Slot CompileLessThanOrEqual(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, VM_OP_TRU, VM_OP_TRU, VM_OP_LTE, -1, 0);
|
|
|
|
}
|
|
|
|
static Slot CompileGreaterThan(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, VM_OP_TRU, VM_OP_TRU, VM_OP_LTN, -1, 1);
|
|
|
|
}
|
|
|
|
static Slot CompileGreaterThanOrEqual(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, VM_OP_TRU, VM_OP_TRU, VM_OP_LTE, -1, 1);
|
|
|
|
}
|
|
|
|
static Slot CompileNot(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, VM_OP_FLS, VM_OP_NOT, -1, -1, 0);
|
|
|
|
}
|
2017-02-13 02:54:18 +00:00
|
|
|
static Slot CompileGet(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
return CompileOperator(c, opts, form, -1, -1, VM_OP_GET, -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Associative set */
|
|
|
|
static Slot CompileSet(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Buffer * buffer = c->buffer;
|
|
|
|
FormOptions subOpts = FormOptionsDefault();
|
|
|
|
Slot ds, key, val;
|
|
|
|
if (form->count != 4) CError(c, "Set expects 4 arguments");
|
|
|
|
if (opts.resultUnused) {
|
|
|
|
ds = CompilerRealizeSlot(c, CompileValue(c, subOpts, form->data[1]));
|
|
|
|
} else {
|
|
|
|
subOpts = opts;
|
|
|
|
subOpts.isTail = 0;
|
|
|
|
ds = CompilerRealizeSlot(c, CompileValue(c, subOpts, form->data[1]));
|
|
|
|
subOpts = FormOptionsDefault();
|
|
|
|
}
|
|
|
|
key = CompilerRealizeSlot(c, CompileValue(c, subOpts, form->data[2]));
|
|
|
|
val = CompilerRealizeSlot(c, CompileValue(c, subOpts, form->data[3]));
|
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_SET);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ds.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, key.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, val.index);
|
|
|
|
CompilerDropSlot(c, c->tail, key);
|
|
|
|
CompilerDropSlot(c, c->tail, val);
|
|
|
|
if (opts.resultUnused) {
|
|
|
|
CompilerDropSlot(c, c->tail, ds);
|
|
|
|
return NilSlot();
|
|
|
|
} else {
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 04:28:11 +00:00
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Compile an assignment operation */
|
|
|
|
static Slot CompileAssign(Compiler * c, FormOptions opts, Value left, Value right) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
2017-02-12 15:27:18 +00:00
|
|
|
FormOptions subOpts;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t target = 0;
|
|
|
|
uint16_t level = 0;
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot slot;
|
|
|
|
subOpts.isTail = 0;
|
|
|
|
subOpts.resultUnused = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (ScopeSymbolResolve(scope, left, &level, &target)) {
|
|
|
|
/* 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-12 15:27:18 +00:00
|
|
|
slot = CompilerRealizeSlot(c, CompileValue(c, subOpts, right));
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Set the up value */
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_SUV);
|
|
|
|
BufferPushUInt16(c->vm, buffer, slot.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, level);
|
|
|
|
BufferPushUInt16(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-12 20:16:55 +00:00
|
|
|
slot = CompileValue(c, subOpts, right);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We need to declare a new symbol */
|
|
|
|
subOpts.target = CompilerDeclareSymbol(c, scope, left);
|
|
|
|
subOpts.canChoose = 0;
|
2017-02-12 15:27:18 +00:00
|
|
|
slot = CompileValue(c, subOpts, right);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
if (opts.resultUnused) {
|
|
|
|
CompilerDropSlot(c, scope, slot);
|
|
|
|
return NilSlot();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile series of expressions. This compiles the meat of
|
|
|
|
* function definitions and the inside of do forms. */
|
|
|
|
static Slot CompileBlock(Compiler * c, FormOptions opts, Array * form, uint32_t startIndex) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
FormOptions subOpts = FormOptionsDefault();
|
|
|
|
uint32_t current = startIndex;
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Check for empty body */
|
|
|
|
if (form->count <= startIndex) return NilSlot();
|
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;
|
|
|
|
while (current < form->count - 1) {
|
|
|
|
CompilerDropSlot(c, scope, CompileValue(c, subOpts, form->data[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 */
|
|
|
|
return CompileValue(c, opts, form->data[form->count - 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. */
|
|
|
|
static FuncDef * CompilerGenFuncDef(Compiler * c, uint32_t lastNBytes, uint32_t arity) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
2017-02-09 23:50:47 +00:00
|
|
|
FuncDef * def = VMAlloc(c->vm, sizeof(FuncDef));
|
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-12 20:16:55 +00:00
|
|
|
CError(c, "Trying to extract more bytes from buffer than in buffer.");
|
2017-02-09 23:50:47 +00:00
|
|
|
uint8_t * byteCode = VMAlloc(c->vm, lastNBytes);
|
2017-02-09 20:02:59 +00:00
|
|
|
def->byteCode = (uint16_t *) byteCode;
|
|
|
|
def->byteCodeLen = lastNBytes / 2;
|
|
|
|
/* Copy the last chunk of bytes in the buffer into the new
|
|
|
|
* memory for the function's byteCOde */
|
|
|
|
memcpy(byteCode, buffer->data + buffer->count - lastNBytes, lastNBytes);
|
|
|
|
/* Remove the byteCode from the end of the buffer */
|
|
|
|
buffer->count -= lastNBytes;
|
|
|
|
/* Create the literals used by this function */
|
|
|
|
if (scope->literalsArray->count) {
|
2017-02-09 23:50:47 +00:00
|
|
|
def->literals = VMAlloc(c->vm, scope->literalsArray->count * sizeof(Value));
|
2017-02-09 20:02:59 +00:00
|
|
|
memcpy(def->literals, scope->literalsArray->data,
|
2017-02-12 20:16:55 +00:00
|
|
|
scope->literalsArray->count * sizeof(Value));
|
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 */
|
|
|
|
CompilerPopScope(c);
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Initialize the new FuncDef */
|
|
|
|
def->locals = scope->frameSize;
|
|
|
|
def->arity = arity;
|
2017-02-12 20:16:55 +00:00
|
|
|
return def;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a function from a function literal */
|
|
|
|
static Slot CompileFunction(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
|
|
|
uint32_t current = 1;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t sizeBefore; /* Size of buffer before compiling function */
|
|
|
|
Scope * subScope;
|
|
|
|
Array * params;
|
|
|
|
FormOptions subOpts = FormOptionsDefault();
|
2017-02-12 15:27:18 +00:00
|
|
|
Slot ret;
|
|
|
|
if (opts.resultUnused) return NilSlot();
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 20:02:59 +00:00
|
|
|
subScope = CompilerPushScope(c, 0);
|
|
|
|
/* Check for function documentation - for now just ignore. */
|
2017-02-12 15:27:18 +00:00
|
|
|
if (form->data[current].type == TYPE_STRING)
|
2017-02-09 20:02:59 +00:00
|
|
|
++current;
|
|
|
|
/* Define the function parameters */
|
2017-02-12 15:27:18 +00:00
|
|
|
if (form->data[current].type != TYPE_ARRAY)
|
2017-02-09 20:02:59 +00:00
|
|
|
CError(c, "Expected function arguments");
|
|
|
|
params = form->data[current++].data.array;
|
|
|
|
for (i = 0; i < params->count; ++i) {
|
|
|
|
Value param = params->data[i];
|
2017-02-12 15:27:18 +00:00
|
|
|
if (param.type != TYPE_SYMBOL)
|
2017-02-09 20:02:59 +00:00
|
|
|
CError(c, "Function parameters should be symbols");
|
|
|
|
/* The compiler puts the parameter locals
|
|
|
|
* in the right place by default - at the beginning
|
|
|
|
* of the stack frame. */
|
|
|
|
CompilerDeclareSymbol(c, subScope, param);
|
|
|
|
}
|
|
|
|
/* 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-12 15:27:18 +00:00
|
|
|
CompilerReturn(c, CompileBlock(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. */
|
|
|
|
{
|
|
|
|
Value newVal;
|
|
|
|
uint16_t literalIndex;
|
|
|
|
FuncDef * def = CompilerGenFuncDef(c, buffer->count - sizeBefore, params->count);
|
|
|
|
/* Add this FuncDef as a literal in the outer scope */
|
|
|
|
newVal.type = TYPE_FUNCDEF;
|
|
|
|
newVal.data.funcdef = def;
|
|
|
|
literalIndex = CompilerAddLiteral(c, scope, newVal);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_CLN);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, literalIndex);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Branching special */
|
|
|
|
static Slot CompileIf(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * 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-02-12 15:27:18 +00:00
|
|
|
uint32_t countAtJumpIf;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t countAtJump;
|
|
|
|
uint32_t countAfterFirstBranch;
|
|
|
|
/* Check argument count */
|
2017-02-12 15:27:18 +00:00
|
|
|
if (form->count < 3 || form->count > 4)
|
2017-02-09 20:02:59 +00:00
|
|
|
CError(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-02-09 20:02:59 +00:00
|
|
|
condition = CompileValue(c, condOpts, form->data[1]);
|
2017-02-12 15:27:18 +00:00
|
|
|
/* If the condition is nil, just compile false path */
|
|
|
|
if (condition.isNil) {
|
|
|
|
if (form->count == 4) {
|
2017-02-12 20:16:55 +00:00
|
|
|
return CompileValue(c, opts, form->data[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;
|
|
|
|
/* Write jump instruction. Will later be replaced with correct index. */
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_JIF);
|
|
|
|
BufferPushUInt16(c->vm, buffer, condition.index);
|
|
|
|
BufferPushUInt32(c->vm, buffer, 0);
|
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-02-12 15:27:18 +00:00
|
|
|
left = CompileValue(c, branchOpts, form->data[2]);
|
|
|
|
if (opts.isTail) {
|
|
|
|
CompilerReturn(c, left);
|
|
|
|
} else {
|
|
|
|
/* If we need to jump again, do so */
|
|
|
|
if (form->count == 4) {
|
|
|
|
countAtJump = buffer->count;
|
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_JMP);
|
|
|
|
BufferPushUInt32(c->vm, buffer, 0);
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerDropSlot(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-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_JIF);
|
|
|
|
BufferPushUInt16(c->vm, buffer, condition.index);
|
2017-02-12 15:27:18 +00:00
|
|
|
BufferPushUInt32(c->vm, buffer, (countAfterFirstBranch - countAtJumpIf) / 2);
|
2017-02-09 20:02:59 +00:00
|
|
|
buffer->count = countAfterFirstBranch;
|
|
|
|
/* Compile false path */
|
|
|
|
if (form->count == 4) {
|
2017-02-12 15:27:18 +00:00
|
|
|
right = CompileValue(c, branchOpts, form->data[3]);
|
|
|
|
if (opts.isTail) CompilerReturn(c, right);
|
2017-02-09 20:02:59 +00:00
|
|
|
CompilerDropSlot(c, scope, right);
|
2017-02-12 15:27:18 +00:00
|
|
|
} else if (opts.isTail) {
|
|
|
|
CompilerReturn(c, condition);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Reset the second jump length */
|
2017-02-09 20:02:59 +00:00
|
|
|
if (!opts.isTail && form->count == 4) {
|
|
|
|
countAfterFirstBranch = buffer->count;
|
|
|
|
buffer->count = countAtJump;
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_JMP);
|
|
|
|
BufferPushUInt32(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 */
|
|
|
|
static Slot CompileWhile(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Slot cond;
|
2017-02-12 20:16:55 +00:00
|
|
|
uint32_t countAtStart = c->buffer->count;
|
|
|
|
uint32_t countAtJumpDelta;
|
|
|
|
uint32_t countAtFinish;
|
|
|
|
FormOptions defaultOpts = FormOptionsDefault();
|
2017-02-10 04:28:11 +00:00
|
|
|
CompilerPushScope(c, 1);
|
|
|
|
/* Compile condition */
|
2017-02-12 20:16:55 +00:00
|
|
|
cond = CompileValue(c, defaultOpts, form->data[1]);
|
|
|
|
/* 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-12 15:27:18 +00:00
|
|
|
CompilerDropSlot(c, c->tail, CompileBlock(c, defaultOpts, form, 2));
|
2017-02-10 04:28:11 +00:00
|
|
|
/* Jump back to the loop start */
|
|
|
|
countAtFinish = c->buffer->count;
|
|
|
|
BufferPushUInt16(c->vm, c->buffer, VM_OP_JMP);
|
|
|
|
BufferPushInt32(c->vm, c->buffer, (int32_t)(countAtFinish - countAtStart) / -2);
|
|
|
|
countAtFinish = c->buffer->count;
|
|
|
|
/* Set the jump to the correct length */
|
|
|
|
c->buffer->count = countAtJumpDelta;
|
2017-02-12 20:16:55 +00:00
|
|
|
BufferPushUInt16(c->vm, c->buffer, VM_OP_JIF);
|
|
|
|
BufferPushUInt16(c->vm, c->buffer, cond.index);
|
2017-02-10 04:28:11 +00:00
|
|
|
BufferPushInt32(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-10 04:28:11 +00:00
|
|
|
CompilerPopScope(c);
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Return nil */
|
|
|
|
if (opts.resultUnused)
|
|
|
|
return NilSlot();
|
|
|
|
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 */
|
|
|
|
static Slot CompileDo(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Slot ret;
|
|
|
|
CompilerPushScope(c, 1);
|
|
|
|
ret = CompileBlock(c, opts, form, 1);
|
|
|
|
CompilerPopScope(c);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Quote special - returns its argument as is. */
|
2017-02-09 20:02:59 +00:00
|
|
|
static Slot CompileQuote(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * 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-02-12 15:27:18 +00:00
|
|
|
if (form->count != 2)
|
2017-02-09 20:02:59 +00:00
|
|
|
CError(c, "Quote takes exactly 1 argument.");
|
|
|
|
Value x = form->data[1];
|
|
|
|
if (x.type == TYPE_NIL ||
|
|
|
|
x.type == TYPE_BOOLEAN ||
|
|
|
|
x.type == TYPE_NUMBER) {
|
|
|
|
return CompileNonReferenceType(c, opts, x);
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
if (opts.resultUnused) return NilSlot();
|
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 20:02:59 +00:00
|
|
|
literalIndex = CompilerAddLiteral(c, scope, x);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_CST);
|
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
|
|
|
BufferPushUInt16(c->vm, buffer, literalIndex);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assignment special */
|
2017-02-13 02:54:18 +00:00
|
|
|
static Slot CompileVar(Compiler * c, FormOptions opts, Array * form) {
|
2017-02-12 15:27:18 +00:00
|
|
|
if (form->count != 3)
|
2017-02-09 20:02:59 +00:00
|
|
|
CError(c, "Assignment expects 2 arguments");
|
|
|
|
return CompileAssign(c, opts, form->data[1], form->data[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Define a function type for Special Form helpers */
|
|
|
|
typedef Slot (*SpecialFormHelper) (Compiler * c, FormOptions opts, Array * form);
|
|
|
|
|
|
|
|
/* Dispatch to a special form */
|
|
|
|
static SpecialFormHelper GetSpecial(Array * form) {
|
|
|
|
uint8_t * name;
|
|
|
|
if (form->count < 1 || form->data[0].type != TYPE_SYMBOL)
|
|
|
|
return NULL;
|
|
|
|
name = form->data[0].data.string;
|
|
|
|
/* If we have a symbol with a zero length name, we have other
|
|
|
|
* problems. */
|
|
|
|
if (VStringSize(name) == 0)
|
|
|
|
return NULL;
|
|
|
|
/* One character specials. Mostly math. */
|
|
|
|
if (VStringSize(name) == 1) {
|
|
|
|
switch(name[0]) {
|
|
|
|
case '+': return CompileAddition;
|
|
|
|
case '-': return CompileSubtraction;
|
|
|
|
case '*': return CompileMultiplication;
|
|
|
|
case '/': return CompileDivision;
|
2017-02-10 04:28:11 +00:00
|
|
|
case '>': return CompileGreaterThan;
|
|
|
|
case '<': return CompileLessThan;
|
2017-02-12 20:16:55 +00:00
|
|
|
case '=': return CompileEquals;
|
2017-02-09 20:02:59 +00:00
|
|
|
default:
|
2017-02-12 20:16:55 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Multi character specials. Mostly control flow. */
|
|
|
|
switch (name[0]) {
|
2017-02-10 04:28:11 +00:00
|
|
|
case '>':
|
2017-02-12 20:16:55 +00:00
|
|
|
{
|
|
|
|
if (VStringSize(name) == 2 &&
|
|
|
|
name[1] == '=') {
|
|
|
|
return CompileGreaterThanOrEqual;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-02-10 04:28:11 +00:00
|
|
|
case '<':
|
2017-02-12 20:16:55 +00:00
|
|
|
{
|
|
|
|
if (VStringSize(name) == 2 &&
|
|
|
|
name[1] == '=') {
|
|
|
|
return CompileLessThanOrEqual;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-02-13 02:54:18 +00:00
|
|
|
case 'g':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 3 &&
|
|
|
|
name[1] == 'e' &&
|
|
|
|
name[2] == 't') {
|
|
|
|
return CompileGet;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
case 'd':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 2 &&
|
|
|
|
name[1] == 'o') {
|
|
|
|
return CompileDo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 2 &&
|
|
|
|
name[1] == 'f') {
|
|
|
|
return CompileIf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 2 &&
|
|
|
|
name[1] == 'n') {
|
|
|
|
return CompileFunction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-02-12 20:16:55 +00:00
|
|
|
case 'n':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 3 &&
|
|
|
|
name[1] == 'o' &&
|
|
|
|
name[2] == 't') {
|
|
|
|
return CompileNot;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
case 'q':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 5 &&
|
|
|
|
name[1] == 'u' &&
|
|
|
|
name[2] == 'o' &&
|
|
|
|
name[3] == 't' &&
|
|
|
|
name[4] == 'e') {
|
2017-02-12 20:16:55 +00:00
|
|
|
return CompileQuote;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 3 &&
|
|
|
|
name[1] == 'e' &&
|
|
|
|
name[2] == 't') {
|
|
|
|
return CompileSet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-02-10 04:28:11 +00:00
|
|
|
case 'w':
|
2017-02-12 20:16:55 +00:00
|
|
|
{
|
|
|
|
if (VStringSize(name) == 5 &&
|
|
|
|
name[1] == 'h' &&
|
|
|
|
name[2] == 'i' &&
|
|
|
|
name[3] == 'l' &&
|
|
|
|
name[4] == 'e') {
|
|
|
|
return CompileWhile;
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 02:54:18 +00:00
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
{
|
|
|
|
if (VStringSize(name) == 2 &&
|
|
|
|
name[1] == '=') {
|
|
|
|
return CompileVar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a form. Checks for special forms and macros. */
|
|
|
|
static Slot CompileForm(Compiler * c, FormOptions opts, Array * form) {
|
|
|
|
Scope * scope = c->tail;
|
|
|
|
Buffer * buffer = c->buffer;
|
|
|
|
SpecialFormHelper helper;
|
|
|
|
/* Empty forms evaluate to nil. */
|
|
|
|
if (form->count == 0) {
|
|
|
|
Value temp;
|
|
|
|
temp.type = TYPE_NIL;
|
|
|
|
return CompileNonReferenceType(c, opts, temp);
|
|
|
|
}
|
|
|
|
/* Check and handle special forms */
|
|
|
|
helper = GetSpecial(form);
|
|
|
|
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;
|
|
|
|
FormOptions subOpts = FormOptionsDefault();
|
|
|
|
uint32_t i;
|
|
|
|
CompilerTrackerInit(c, &tracker);
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Compile function to be called */
|
|
|
|
callee = CompilerRealizeSlot(c, CompileValue(c, subOpts, form->data[0]));
|
|
|
|
/* Compile all of the arguments */
|
|
|
|
for (i = 1; i < form->count; ++i) {
|
2017-02-09 20:02:59 +00:00
|
|
|
Slot slot = CompileValue(c, subOpts, form->data[i]);
|
|
|
|
CompilerTrackerPush(c, &tracker, slot);
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Free up some slots */
|
|
|
|
CompilerDropSlot(c, scope, callee);
|
|
|
|
CompilerTrackerFree(c, scope, &tracker);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* If this is in tail position do a tail call. */
|
|
|
|
if (opts.isTail) {
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_TCL);
|
2017-02-12 15:27:18 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, callee.index);
|
|
|
|
ret.hasReturned = 1;
|
|
|
|
ret.isNil = 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
ret = CompilerGetTarget(c, opts);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, VM_OP_CAL);
|
2017-02-12 15:27:18 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, callee.index);
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, ret.index);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-09 23:50:47 +00:00
|
|
|
BufferPushUInt16(c->vm, buffer, form->count - 1);
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Write the location of all of the arguments */
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerTrackerWrite(c, &tracker, 0);
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recursively compile any value or form */
|
|
|
|
static Slot CompileValue(Compiler * c, FormOptions opts, Value x) {
|
|
|
|
switch (x.type) {
|
|
|
|
case TYPE_NIL:
|
|
|
|
case TYPE_BOOLEAN:
|
|
|
|
case TYPE_NUMBER:
|
|
|
|
return CompileNonReferenceType(c, opts, x);
|
|
|
|
case TYPE_SYMBOL:
|
|
|
|
return CompileSymbol(c, opts, x);
|
|
|
|
case TYPE_FORM:
|
|
|
|
return CompileForm(c, opts, x.data.array);
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
return CompileArray(c, opts, x.data.array);
|
|
|
|
case TYPE_DICTIONARY:
|
|
|
|
return CompileDict(c, opts, x.data.dict);
|
|
|
|
default:
|
|
|
|
return CompileLiteral(c, opts, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a Compiler struct */
|
|
|
|
void CompilerInit(Compiler * c, VM * vm) {
|
|
|
|
c->vm = vm;
|
2017-02-09 23:50:47 +00:00
|
|
|
c->buffer = BufferNew(vm, 128);
|
|
|
|
c->env = ArrayNew(vm, 10);
|
2017-02-09 20:02:59 +00:00
|
|
|
c->tail = c->root = NULL;
|
|
|
|
c->error = NULL;
|
|
|
|
CompilerPushScope(c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a global for the compilation environment. */
|
|
|
|
void CompilerAddGlobal(Compiler * c, const char * name, Value x) {
|
2017-02-09 23:50:47 +00:00
|
|
|
Value sym = ValueLoadCString(c->vm, name);
|
2017-02-09 20:02:59 +00:00
|
|
|
sym.type = TYPE_SYMBOL;
|
|
|
|
CompilerDeclareSymbol(c, c->root, sym);
|
2017-02-09 23:50:47 +00:00
|
|
|
ArrayPush(c->vm, c->env, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:01:06 +00:00
|
|
|
/* Register a global c function for the compilation environment. */
|
|
|
|
void CompilerAddGlobalCFunc(Compiler * c, const char * name, CFunction f) {
|
2017-02-12 20:16:55 +00:00
|
|
|
Value func;
|
|
|
|
func.type = TYPE_CFUNCTION;
|
|
|
|
func.data.cfunction = f;
|
2017-02-13 02:54:18 +00:00
|
|
|
CompilerAddGlobal(c, name, func);
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
|
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. */
|
|
|
|
Func * CompilerCompile(Compiler * c, Value form) {
|
|
|
|
FormOptions opts = FormOptionsDefault();
|
|
|
|
FuncDef * def;
|
|
|
|
if (setjmp(c->onError)) {
|
|
|
|
/* Clear all but root scope */
|
|
|
|
c->tail = c->root;
|
|
|
|
c->root->nextScope = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Create a scope */
|
|
|
|
opts.isTail = 1;
|
|
|
|
CompilerPushScope(c, 0);
|
2017-02-12 15:27:18 +00:00
|
|
|
CompilerReturn(c, CompileValue(c, opts, form));
|
2017-02-09 20:02:59 +00:00
|
|
|
def = CompilerGenFuncDef(c, c->buffer->count, 0);
|
|
|
|
{
|
|
|
|
uint32_t envSize = c->env->count;
|
2017-02-09 23:50:47 +00:00
|
|
|
FuncEnv * env = VMAlloc(c->vm, sizeof(FuncEnv));
|
|
|
|
Func * func = VMAlloc(c->vm, sizeof(Func));
|
2017-02-13 02:54:18 +00:00
|
|
|
if (envSize) {
|
|
|
|
env->values = VMAlloc(c->vm, sizeof(Value) * envSize);
|
|
|
|
memcpy(env->values, c->env->data, envSize * sizeof(Value));
|
|
|
|
} else {
|
|
|
|
env->values = NULL;
|
|
|
|
}
|
2017-02-09 20:56:45 +00:00
|
|
|
env->stackOffset = envSize;
|
2017-02-09 20:02:59 +00:00
|
|
|
env->thread = NULL;
|
|
|
|
func->parent = NULL;
|
|
|
|
func->def = def;
|
|
|
|
func->env = env;
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 20:16:55 +00:00
|
|
|
|
|
|
|
/* Macro expansion. Macro expansion happens prior to the compilation process
|
|
|
|
* and is completely separate. This allows the compilation to not have to worry
|
|
|
|
* about garbage collection and other issues that would complicate both the
|
|
|
|
* runtime and the compilation. */
|
|
|
|
int CompileMacroExpand(VM * vm, Value x, Dictionary * macros, Value * out) {
|
|
|
|
while (x.type == TYPE_FORM) {
|
|
|
|
Array * form = x.data.array;
|
|
|
|
Value sym, macroFn;
|
|
|
|
if (form->count == 0) break;
|
|
|
|
sym = form->data[0];
|
|
|
|
macroFn = DictGet(macros, sym);
|
|
|
|
if (macroFn.type != TYPE_FUNCTION && macroFn.type != TYPE_CFUNCTION) break;
|
|
|
|
VMLoad(vm, macroFn);
|
|
|
|
if (VMStart(vm)) {
|
|
|
|
/* We encountered an error during parsing */
|
2017-02-13 02:54:18 +00:00
|
|
|
return 1;
|
2017-02-12 20:16:55 +00:00
|
|
|
} else {
|
|
|
|
x = vm->ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out = x;
|
|
|
|
return 0;
|
|
|
|
}
|