2018-01-12 15:41:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
2018-01-30 04:38:49 +00:00
|
|
|
#include <dst/dstcorelib.h>
|
2018-01-19 21:43:19 +00:00
|
|
|
#include <dst/dstcompile.h>
|
|
|
|
#include <dst/dstparse.h>
|
2018-01-12 15:41:27 +00:00
|
|
|
#include "compile.h"
|
2018-01-19 21:43:19 +00:00
|
|
|
#include <headerlibs/strbinsearch.h>
|
|
|
|
#include <headerlibs/vector.h>
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_quote(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
if (argn != 1) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(opts.compiler, ast, "expected 1 argument");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
return dstc_cslot(dst_ast_unwrap(argv[0]));
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 20:15:09 +00:00
|
|
|
DstSlot dstc_astquote(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
|
|
|
if (argn != 1) {
|
|
|
|
dstc_cerror(opts.compiler, ast, "expected 1 argument");
|
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
return dstc_cslot(argv[0]);
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_varset(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
|
|
|
DstFopts subopts = dstc_fopts_default(opts.compiler);
|
2018-01-13 21:14:40 +00:00
|
|
|
DstSlot ret, dest;
|
2018-01-17 04:18:45 +00:00
|
|
|
Dst head;
|
2018-01-12 15:41:27 +00:00
|
|
|
if (argn != 2) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(opts.compiler, ast, "expected 2 arguments");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
head = dst_ast_unwrap(argv[0]);
|
|
|
|
if (!dst_checktype(head, DST_SYMBOL)) {
|
|
|
|
dstc_cerror(opts.compiler, ast, "expected symbol");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
dest = dstc_resolve(opts.compiler, ast, dst_unwrap_symbol(head));
|
2018-01-13 21:14:40 +00:00
|
|
|
if (!(dest.flags & DST_SLOT_MUTABLE)) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(opts.compiler, ast, "cannot set constant");
|
2018-01-13 21:14:40 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
subopts.flags = DST_FOPTS_HINT;
|
|
|
|
subopts.hint = dest;
|
2018-01-17 04:18:45 +00:00
|
|
|
ret = dstc_value(subopts, argv[1]);
|
|
|
|
dstc_copy(opts.compiler, ast, dest, ret);
|
2018-01-13 21:14:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add attributes to a global def or var table */
|
2018-01-17 04:18:45 +00:00
|
|
|
static void handleattr(DstCompiler *c, int32_t argn, const Dst *argv, DstTable *tab) {
|
2018-01-13 21:14:40 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 1; i < argn - 1; i++) {
|
2018-01-17 04:18:45 +00:00
|
|
|
Dst attr = dst_ast_unwrap1(argv[i]);
|
2018-01-13 21:14:40 +00:00
|
|
|
switch (dst_type(attr)) {
|
|
|
|
default:
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, dst_ast_node(argv[i]), "could not add metadata to binding");
|
2018-01-13 21:14:40 +00:00
|
|
|
return;
|
|
|
|
case DST_SYMBOL:
|
|
|
|
dst_table_put(tab, attr, dst_wrap_true());
|
|
|
|
break;
|
|
|
|
case DST_STRING:
|
|
|
|
dst_table_put(tab, dst_csymbolv("doc"), attr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:39:32 +00:00
|
|
|
static DstSlot dohead(DstCompiler *c, DstFopts opts, DstAst *ast, Dst *head, int32_t argn, const Dst *argv) {
|
2018-01-17 04:18:45 +00:00
|
|
|
DstFopts subopts = dstc_fopts_default(c);
|
2018-01-13 21:14:40 +00:00
|
|
|
DstSlot ret;
|
|
|
|
if (argn < 2) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, ast, "expected at least 2 arguments");
|
2018-01-13 21:14:40 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-21 19:39:32 +00:00
|
|
|
*head = dst_ast_unwrap1(argv[0]);
|
|
|
|
if (!dst_checktype(*head, DST_SYMBOL)) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, dst_ast_node(argv[0]), "expected symbol");
|
2018-01-13 21:14:40 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-21 19:39:32 +00:00
|
|
|
subopts.flags = opts.flags & ~(DST_FOPTS_TAIL | DST_FOPTS_DROP);
|
2018-01-17 04:18:45 +00:00
|
|
|
subopts.hint = opts.hint;
|
|
|
|
ret = dstc_value(subopts, argv[argn - 1]);
|
2018-01-21 19:39:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Def or var a symbol in a local scope */
|
|
|
|
static DstSlot namelocal(DstCompiler *c, DstAst *ast, Dst head, int32_t flags, DstSlot ret) {
|
|
|
|
/* Non root scope, bring to local slot */
|
|
|
|
if (ret.flags & DST_SLOT_NAMED ||
|
|
|
|
ret.envindex != 0 ||
|
|
|
|
ret.index < 0 ||
|
|
|
|
ret.index > 0xFF) {
|
|
|
|
/* Slot is not able to be named */
|
|
|
|
DstSlot localslot;
|
|
|
|
localslot.index = dstc_lsloti(c);
|
|
|
|
/* infer type? */
|
|
|
|
localslot.flags = flags;
|
|
|
|
localslot.envindex = 0;
|
|
|
|
localslot.constant = dst_wrap_nil();
|
|
|
|
dstc_copy(c, ast, localslot, ret);
|
|
|
|
ret = localslot;
|
|
|
|
}
|
|
|
|
dstc_nameslot(c, dst_unwrap_symbol(head), ret);
|
|
|
|
ret.flags |= DST_SLOT_NAMED;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DstSlot dstc_var(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
|
|
|
DstCompiler *c = opts.compiler;
|
|
|
|
Dst head;
|
|
|
|
DstSlot ret = dohead(c, opts, ast, &head, argn, argv);
|
|
|
|
if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
if (dst_v_last(c->scopes).flags & DST_SCOPE_TOP) {
|
|
|
|
DstSlot refslot, refarrayslot;
|
|
|
|
/* Global var, generate var */
|
|
|
|
DstTable *reftab = dst_table(1);
|
|
|
|
DstArray *ref = dst_array(1);
|
|
|
|
dst_array_push(ref, dst_wrap_nil());
|
|
|
|
dst_table_put(reftab, dst_csymbolv("ref"), dst_wrap_array(ref));
|
2018-01-17 04:18:45 +00:00
|
|
|
handleattr(c, argn, argv, reftab);
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_table_put(c->env, head, dst_wrap_table(reftab));
|
2018-01-12 15:41:27 +00:00
|
|
|
refslot = dstc_cslot(dst_wrap_array(ref));
|
|
|
|
refarrayslot = refslot;
|
|
|
|
refslot.flags |= DST_SLOT_REF | DST_SLOT_NAMED | DST_SLOT_MUTABLE;
|
|
|
|
/* Generate code to set ref */
|
2018-01-17 04:18:45 +00:00
|
|
|
int32_t refarrayindex = dstc_preread(c, ast, 0xFF, 1, refarrayslot);
|
|
|
|
int32_t retindex = dstc_preread(c, ast, 0xFF, 2, ret);
|
|
|
|
dstc_emit(c, ast,
|
2018-01-12 15:41:27 +00:00
|
|
|
(retindex << 16) |
|
|
|
|
(refarrayindex << 8) |
|
|
|
|
DOP_PUT_INDEX);
|
|
|
|
dstc_postread(c, refarrayslot, refarrayindex);
|
|
|
|
dstc_postread(c, ret, retindex);
|
|
|
|
ret = refslot;
|
|
|
|
} else {
|
2018-01-21 19:39:32 +00:00
|
|
|
ret = namelocal(c, ast, head, DST_SLOT_NAMED | DST_SLOT_MUTABLE, ret);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_def(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
2018-01-17 04:18:45 +00:00
|
|
|
Dst head;
|
2018-01-21 19:39:32 +00:00
|
|
|
opts.flags &= ~DST_FOPTS_HINT;
|
|
|
|
DstSlot ret = dohead(c, opts, ast, &head, argn, argv);
|
|
|
|
if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
if (dst_v_last(c->scopes).flags & DST_SCOPE_TOP) {
|
2018-01-13 21:14:40 +00:00
|
|
|
DstTable *tab = dst_table(2);
|
|
|
|
int32_t tableindex, valsymindex, valueindex;
|
|
|
|
DstSlot valsym = dstc_cslot(dst_csymbolv("value"));
|
|
|
|
DstSlot tabslot = dstc_cslot(dst_wrap_table(tab));
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Add env entry to env */
|
2018-01-17 04:18:45 +00:00
|
|
|
handleattr(c, argn, argv, tab);
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_table_put(c->env, head, dst_wrap_table(tab));
|
2018-01-13 21:14:40 +00:00
|
|
|
|
|
|
|
/* Put value in table when evaulated */
|
2018-01-17 04:18:45 +00:00
|
|
|
tableindex = dstc_preread(c, ast, 0xFF, 1, tabslot);
|
|
|
|
valsymindex = dstc_preread(c, ast, 0xFF, 2, valsym);
|
|
|
|
valueindex = dstc_preread(c, ast, 0xFF, 3, ret);
|
|
|
|
dstc_emit(c, ast,
|
2018-01-13 21:14:40 +00:00
|
|
|
(valueindex << 24) |
|
|
|
|
(valsymindex << 16) |
|
|
|
|
(tableindex << 8) |
|
2018-01-12 15:41:27 +00:00
|
|
|
DOP_PUT);
|
2018-01-13 21:14:40 +00:00
|
|
|
dstc_postread(c, tabslot, tableindex);
|
|
|
|
dstc_postread(c, valsym, valsymindex);
|
|
|
|
dstc_postread(c, ret, valueindex);
|
2018-01-12 15:41:27 +00:00
|
|
|
} else {
|
2018-01-21 19:39:32 +00:00
|
|
|
ret = namelocal(c, ast, head, DST_SLOT_NAMED, ret);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :condition
|
|
|
|
* ...
|
|
|
|
* jump-if-not condition :right
|
|
|
|
* :left
|
|
|
|
* ...
|
|
|
|
* jump done (only if not tail)
|
|
|
|
* :right
|
|
|
|
* ...
|
|
|
|
* :done
|
|
|
|
*/
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_if(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
|
|
|
int32_t labelr, labeljr, labeld, labeljd, condlocal;
|
2018-01-17 04:18:45 +00:00
|
|
|
DstFopts condopts, bodyopts;
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSlot cond, left, right, target;
|
2018-01-17 04:18:45 +00:00
|
|
|
Dst truebody, falsebody;
|
2018-01-12 15:41:27 +00:00
|
|
|
const int tail = opts.flags & DST_FOPTS_TAIL;
|
|
|
|
const int drop = opts.flags & DST_FOPTS_DROP;
|
|
|
|
|
|
|
|
if (argn < 2 || argn > 3) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, ast, "expected 2 or 3 arguments to if");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
/* Get the bodies of the if expression */
|
|
|
|
truebody = argv[1];
|
|
|
|
falsebody = argn > 2 ? argv[2] : dst_wrap_nil();
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Get options */
|
2018-01-17 04:18:45 +00:00
|
|
|
condopts = dstc_fopts_default(c);
|
|
|
|
bodyopts = opts;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Compile condition */
|
2018-01-17 04:18:45 +00:00
|
|
|
cond = dstc_value(condopts, argv[0]);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Check constant condition. */
|
|
|
|
/* TODO: Use type info for more short circuits */
|
2018-01-21 19:39:32 +00:00
|
|
|
if (cond.flags & DST_SLOT_CONSTANT) {
|
2018-01-17 04:18:45 +00:00
|
|
|
if (!dst_truthy(cond.constant)) {
|
|
|
|
/* Swap the true and false bodies */
|
|
|
|
Dst temp = falsebody;
|
|
|
|
falsebody = truebody;
|
|
|
|
truebody = temp;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
dstc_scope(c, 0);
|
2018-01-17 04:18:45 +00:00
|
|
|
target = dstc_value(bodyopts, truebody);
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_popscope(c);
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_throwaway(bodyopts, falsebody);
|
2018-01-12 15:41:27 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set target for compilation */
|
|
|
|
target = (!drop && !tail)
|
|
|
|
? dstc_gettarget(opts)
|
|
|
|
: dstc_cslot(dst_wrap_nil());
|
|
|
|
|
|
|
|
/* Compile jump to right */
|
2018-01-17 04:18:45 +00:00
|
|
|
condlocal = dstc_preread(c, ast, 0xFF, 1, cond);
|
2018-01-12 15:41:27 +00:00
|
|
|
labeljr = dst_v_count(c->buffer);
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_emit(c, ast, DOP_JUMP_IF_NOT | (condlocal << 8));
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_postread(c, cond, condlocal);
|
|
|
|
dstc_freeslot(c, cond);
|
|
|
|
|
|
|
|
/* Condition left body */
|
|
|
|
dstc_scope(c, 0);
|
2018-01-17 04:18:45 +00:00
|
|
|
left = dstc_value(bodyopts, truebody);
|
|
|
|
if (!drop && !tail) dstc_copy(c, ast, target, left);
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_popscope(c);
|
|
|
|
|
|
|
|
/* Compile jump to done */
|
|
|
|
labeljd = dst_v_count(c->buffer);
|
2018-01-17 04:18:45 +00:00
|
|
|
if (!tail) dstc_emit(c, ast, DOP_JUMP);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Compile right body */
|
|
|
|
labelr = dst_v_count(c->buffer);
|
|
|
|
dstc_scope(c, 0);
|
2018-01-17 04:18:45 +00:00
|
|
|
right = dstc_value(bodyopts, falsebody);
|
|
|
|
if (!drop && !tail) dstc_copy(c, ast, target, right);
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_popscope(c);
|
|
|
|
|
|
|
|
/* Write jumps - only add jump lengths if jump actually emitted */
|
|
|
|
labeld = dst_v_count(c->buffer);
|
|
|
|
c->buffer[labeljr] |= (labelr - labeljr) << 16;
|
|
|
|
if (!tail) c->buffer[labeljd] |= (labeld - labeljd) << 8;
|
|
|
|
|
|
|
|
if (tail) target.flags |= DST_SLOT_RETURNED;
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a do form. Do forms execute their body sequentially and
|
|
|
|
* evaluate to the last expression in the body. */
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_do(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t i;
|
|
|
|
DstSlot ret;
|
2018-01-17 04:18:45 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
|
|
|
DstFopts subopts = dstc_fopts_default(c);
|
|
|
|
(void) ast;
|
|
|
|
dstc_scope(c, 0);
|
2018-01-12 15:41:27 +00:00
|
|
|
for (i = 0; i < argn; i++) {
|
|
|
|
if (i != argn - 1) {
|
|
|
|
subopts.flags = DST_FOPTS_DROP;
|
|
|
|
} else if (opts.flags & DST_FOPTS_TAIL) {
|
|
|
|
subopts.flags = DST_FOPTS_TAIL;
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
ret = dstc_value(subopts, argv[i]);
|
2018-01-12 15:41:27 +00:00
|
|
|
if (i != argn - 1) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_freeslot(c, ret);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_popscope_keepslot(c, ret);
|
2018-01-12 15:41:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :whiletop
|
|
|
|
* ...
|
|
|
|
* :condition
|
|
|
|
* jump-if-not cond :done
|
|
|
|
* ...
|
|
|
|
* jump :whiletop
|
|
|
|
* :done
|
|
|
|
*/
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_while(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
|
|
|
DstSlot cond;
|
2018-01-17 04:18:45 +00:00
|
|
|
DstFopts subopts = dstc_fopts_default(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t condlocal, labelwt, labeld, labeljt, labelc, i;
|
|
|
|
int infinite = 0;
|
|
|
|
|
|
|
|
if (argn < 2) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, ast, "expected at least 2 arguments");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
|
|
|
labelwt = dst_v_count(c->buffer);
|
|
|
|
|
|
|
|
/* Compile condition */
|
2018-01-17 04:18:45 +00:00
|
|
|
cond = dstc_value(subopts, argv[0]);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Check for constant condition */
|
|
|
|
if (cond.flags & DST_SLOT_CONSTANT) {
|
|
|
|
/* Loop never executes */
|
|
|
|
if (!dst_truthy(cond.constant)) {
|
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
/* Infinite loop */
|
|
|
|
infinite = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstc_scope(c, 0);
|
|
|
|
|
|
|
|
/* Infinite loop does not need to check condition */
|
|
|
|
if (!infinite) {
|
2018-01-17 04:18:45 +00:00
|
|
|
condlocal = dstc_preread(c, ast, 0xFF, 1, cond);
|
2018-01-12 15:41:27 +00:00
|
|
|
labelc = dst_v_count(c->buffer);
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_emit(c, ast, DOP_JUMP_IF_NOT | (condlocal << 8));
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_postread(c, cond, condlocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile body */
|
|
|
|
for (i = 1; i < argn; i++) {
|
|
|
|
subopts.flags = DST_FOPTS_DROP;
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_freeslot(c, dstc_value(subopts, argv[i]));
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile jump to whiletop */
|
|
|
|
labeljt = dst_v_count(c->buffer);
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_emit(c, ast, DOP_JUMP);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Calculate jumps */
|
|
|
|
labeld = dst_v_count(c->buffer);
|
|
|
|
if (!infinite) c->buffer[labelc] |= (labeld - labelc) << 16;
|
|
|
|
c->buffer[labeljt] |= (labelwt - labeljt) << 8;
|
|
|
|
|
|
|
|
/* Pop scope and return nil slot */
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_popscope(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a funcdef to the top most function scope */
|
|
|
|
static int32_t dstc_addfuncdef(DstCompiler *c, DstFuncDef *def) {
|
|
|
|
DstScope *scope = &dst_v_last(c->scopes);
|
|
|
|
while (scope >= c->scopes) {
|
|
|
|
if (scope->flags & DST_SCOPE_FUNCTION)
|
|
|
|
break;
|
|
|
|
scope--;
|
|
|
|
}
|
|
|
|
dst_assert(scope >= c->scopes, "could not add funcdef");
|
|
|
|
dst_v_push(scope->defs, def);
|
|
|
|
return dst_v_count(scope->defs) - 1;
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
|
|
|
DstFuncDef *def;
|
|
|
|
DstSlot ret;
|
2018-01-17 04:18:45 +00:00
|
|
|
Dst head, paramv;
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t paramcount, argi, parami, arity, localslot, defindex;
|
2018-01-17 04:18:45 +00:00
|
|
|
DstFopts subopts = dstc_fopts_default(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
const Dst *params;
|
|
|
|
int varargs = 0;
|
2018-01-19 17:37:37 +00:00
|
|
|
int selfref = 0;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
if (argn < 2) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, ast, "expected at least 2 arguments to function literal");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Begin function */
|
|
|
|
dstc_scope(c, DST_SCOPE_FUNCTION);
|
|
|
|
|
|
|
|
/* Read function parameters */
|
|
|
|
parami = 0;
|
|
|
|
arity = 0;
|
2018-01-19 17:37:37 +00:00
|
|
|
head = dst_ast_unwrap1(argv[0]);
|
|
|
|
if (dst_checktype(head, DST_SYMBOL)) {
|
|
|
|
selfref = 1;
|
|
|
|
parami = 1;
|
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
if (parami >= argn) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, dst_ast_node(argv[0]), "expected function parameters");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
paramv = dst_ast_unwrap(argv[parami]);
|
|
|
|
if (dst_seq_view(paramv, ¶ms, ¶mcount)) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < paramcount; i++) {
|
2018-01-19 17:37:37 +00:00
|
|
|
Dst param = dst_ast_unwrap1(params[i]);
|
2018-01-17 04:18:45 +00:00
|
|
|
if (dst_checktype(param, DST_SYMBOL)) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSlot slot;
|
|
|
|
/* Check for varargs */
|
2018-01-17 04:18:45 +00:00
|
|
|
if (0 == dst_cstrcmp(dst_unwrap_symbol(param), "&")) {
|
2018-01-12 15:41:27 +00:00
|
|
|
if (i != paramcount - 2) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, dst_ast_node(params[i]), "variable argument symbol in unexpected location");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
varargs = 1;
|
|
|
|
arity--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
slot.flags = DST_SLOT_NAMED;
|
|
|
|
slot.envindex = 0;
|
|
|
|
slot.constant = dst_wrap_nil();
|
|
|
|
slot.index = dstc_lsloti(c);
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_nameslot(c, dst_unwrap_symbol(param), slot);
|
2018-01-12 15:41:27 +00:00
|
|
|
arity++;
|
|
|
|
} else {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, dst_ast_node(params[i]), "expected symbol as function parameter");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_cerror(c, ast, "expected function parameters");
|
2018-01-12 15:41:27 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
2018-01-19 17:37:37 +00:00
|
|
|
/* Check for self ref */
|
|
|
|
if (selfref) {
|
|
|
|
DstSlot slot;
|
|
|
|
slot.envindex = 0;
|
|
|
|
slot.flags = DST_SLOT_NAMED | DST_FUNCTION;
|
|
|
|
slot.constant = dst_wrap_nil();
|
|
|
|
slot.index = dstc_lsloti(c);
|
|
|
|
dstc_emit(c, ast, (slot.index << 8) | DOP_LOAD_SELF);
|
|
|
|
dstc_nameslot(c, dst_unwrap_symbol(head), slot);
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile function body */
|
|
|
|
for (argi = parami + 1; argi < argn; argi++) {
|
|
|
|
DstSlot s;
|
|
|
|
subopts.flags = argi == (argn - 1) ? DST_FOPTS_TAIL : DST_FOPTS_DROP;
|
2018-01-17 04:18:45 +00:00
|
|
|
s = dstc_value(subopts, argv[argi]);
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_freeslot(c, s);
|
2018-01-21 19:39:32 +00:00
|
|
|
if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build function */
|
|
|
|
def = dstc_pop_funcdef(c);
|
|
|
|
def->arity = arity;
|
|
|
|
if (varargs) def->flags |= DST_FUNCDEF_FLAG_VARARG;
|
|
|
|
defindex = dstc_addfuncdef(c, def);
|
|
|
|
|
2018-01-12 21:25:24 +00:00
|
|
|
/* Ensure enough slots for vararg function. */
|
|
|
|
if (arity + varargs > def->slotcount) def->slotcount = arity + varargs;
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Instantiate closure */
|
2018-01-16 04:31:39 +00:00
|
|
|
ret = dstc_gettarget(opts);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
localslot = ret.index > 0xF0 ? 0xF1 : ret.index;
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-01-12 15:41:27 +00:00
|
|
|
(defindex << 16) |
|
|
|
|
(localslot << 8) |
|
|
|
|
DOP_CLOSURE);
|
|
|
|
|
|
|
|
if (ret.index != localslot) {
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-01-12 15:41:27 +00:00
|
|
|
(ret.index << 16) |
|
|
|
|
(localslot << 8) |
|
|
|
|
DOP_MOVE_FAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep in lexographic order */
|
|
|
|
static const DstSpecial dstc_specials[] = {
|
2018-01-27 20:15:09 +00:00
|
|
|
{"ast-quote", dstc_astquote},
|
2018-01-12 15:41:27 +00:00
|
|
|
{"def", dstc_def},
|
|
|
|
{"do", dstc_do},
|
|
|
|
{"fn", dstc_fn},
|
|
|
|
{"if", dstc_if},
|
|
|
|
{"quote", dstc_quote},
|
|
|
|
{"var", dstc_var},
|
|
|
|
{"varset!", dstc_varset},
|
|
|
|
{"while", dstc_while}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Find a special */
|
|
|
|
const DstSpecial *dstc_special(const uint8_t *name) {
|
|
|
|
return dst_strbinsearch(
|
|
|
|
&dstc_specials,
|
|
|
|
sizeof(dstc_specials)/sizeof(DstSpecial),
|
|
|
|
sizeof(DstSpecial),
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
|