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-02-06 06:25:48 +00:00
|
|
|
static void destructure(DstCompiler *c, Dst left, DstSlot right,
|
2018-03-23 13:18:04 +00:00
|
|
|
void (*leaf)(DstCompiler *c,
|
2018-02-06 06:25:48 +00:00
|
|
|
DstAst *ast,
|
|
|
|
const uint8_t *sym,
|
|
|
|
DstSlot s,
|
2018-03-12 06:06:51 +00:00
|
|
|
DstTable *attr),
|
|
|
|
DstTable *attr) {
|
2018-02-06 06:25:48 +00:00
|
|
|
DstAst *ast = dst_ast_node(left);
|
|
|
|
left = dst_ast_unwrap1(left);
|
|
|
|
switch (dst_type(left)) {
|
|
|
|
default:
|
|
|
|
dstc_cerror(c, ast, "unexpected type in destructuring");
|
|
|
|
break;
|
|
|
|
case DST_SYMBOL:
|
|
|
|
/* Leaf, assign right to left */
|
2018-03-12 06:06:51 +00:00
|
|
|
leaf(c, ast, dst_unwrap_symbol(left), right, attr);
|
2018-02-06 06:25:48 +00:00
|
|
|
break;
|
|
|
|
case DST_TUPLE:
|
|
|
|
case DST_ARRAY:
|
|
|
|
{
|
|
|
|
int32_t i, len, localright, localsub;
|
|
|
|
len = dst_length(left);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
DstSlot newright;
|
|
|
|
Dst subval = dst_getindex(left, i);
|
|
|
|
localright = dstc_preread(c, ast, 0xFF, 1, right);
|
2018-02-06 07:02:28 +00:00
|
|
|
localsub = dstc_lslotn(c, 0xFF, 3);
|
2018-02-06 06:25:48 +00:00
|
|
|
if (i < 0x100) {
|
2018-03-23 13:18:04 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-02-06 06:25:48 +00:00
|
|
|
(i << 24) |
|
|
|
|
(localright << 16) |
|
|
|
|
(localsub << 8) |
|
|
|
|
DOP_GET_INDEX);
|
|
|
|
} else {
|
|
|
|
DstSlot islot = dstc_cslot(dst_wrap_integer(i));
|
|
|
|
int32_t locali = dstc_preread(c, ast, 0xFF, 2, islot);
|
2018-03-23 13:18:04 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-02-06 06:25:48 +00:00
|
|
|
(locali << 24) |
|
|
|
|
(localright << 16) |
|
|
|
|
(localsub << 8) |
|
|
|
|
DOP_GET);
|
|
|
|
dstc_postread(c, islot, locali);
|
|
|
|
}
|
|
|
|
newright.index = localsub;
|
2018-02-12 21:43:59 +00:00
|
|
|
newright.envindex = -1;
|
2018-02-06 06:25:48 +00:00
|
|
|
newright.constant = dst_wrap_nil();
|
|
|
|
newright.flags = DST_SLOTTYPE_ANY;
|
|
|
|
/* Traverse into the structure */
|
2018-03-12 06:06:51 +00:00
|
|
|
destructure(c, subval, newright, leaf, attr);
|
2018-02-06 06:25:48 +00:00
|
|
|
dstc_postread(c, right, localright);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Free right */
|
|
|
|
dstc_freeslot(c, right);
|
|
|
|
break;
|
2018-02-06 07:02:28 +00:00
|
|
|
case DST_TABLE:
|
|
|
|
case DST_STRUCT:
|
|
|
|
{
|
|
|
|
int32_t localright, localsub;
|
|
|
|
const DstKV *kv = NULL;
|
|
|
|
while ((kv = dst_next(left, kv))) {
|
|
|
|
DstSlot newright;
|
|
|
|
DstSlot kslot = dstc_cslot(dst_ast_unwrap(kv->key));
|
|
|
|
Dst subval = kv->value;
|
|
|
|
localright = dstc_preread(c, ast, 0xFF, 1, right);
|
|
|
|
localsub = dstc_lslotn(c, 0xFF, 3);
|
|
|
|
int32_t localk = dstc_preread(c, ast, 0xFF, 2, kslot);
|
2018-03-23 13:18:04 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-02-06 07:02:28 +00:00
|
|
|
(localk << 24) |
|
|
|
|
(localright << 16) |
|
|
|
|
(localsub << 8) |
|
|
|
|
DOP_GET);
|
|
|
|
dstc_postread(c, kslot, localk);
|
|
|
|
newright.index = localsub;
|
2018-02-12 21:43:59 +00:00
|
|
|
newright.envindex = -1;
|
2018-02-06 07:02:28 +00:00
|
|
|
newright.constant = dst_wrap_nil();
|
|
|
|
newright.flags = DST_SLOTTYPE_ANY;
|
|
|
|
/* Traverse into the structure */
|
2018-03-12 06:06:51 +00:00
|
|
|
destructure(c, subval, newright, leaf, attr);
|
2018-02-06 07:02:28 +00:00
|
|
|
dstc_postread(c, right, localright);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Free right */
|
|
|
|
dstc_freeslot(c, right);
|
|
|
|
break;
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-03-12 06:06:51 +00:00
|
|
|
static DstTable *handleattr(DstCompiler *c, int32_t argn, const Dst *argv) {
|
2018-01-13 21:14:40 +00:00
|
|
|
int32_t i;
|
2018-03-12 06:06:51 +00:00
|
|
|
DstTable *tab = dst_table(2);
|
2018-01-13 21:14:40 +00:00
|
|
|
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-03-12 06:06:51 +00:00
|
|
|
break;
|
2018-01-13 21:14:40 +00:00
|
|
|
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-03-12 06:06:51 +00:00
|
|
|
return tab;
|
2018-01-13 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
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 ||
|
2018-02-12 21:43:59 +00:00
|
|
|
ret.envindex >= 0 ||
|
2018-01-21 19:39:32 +00:00
|
|
|
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;
|
2018-02-12 21:43:59 +00:00
|
|
|
localslot.envindex = -1;
|
2018-01-21 19:39:32 +00:00
|
|
|
localslot.constant = dst_wrap_nil();
|
|
|
|
dstc_copy(c, ast, localslot, ret);
|
|
|
|
ret = localslot;
|
|
|
|
}
|
2018-02-03 22:22:04 +00:00
|
|
|
ret.flags |= flags;
|
2018-03-23 13:18:04 +00:00
|
|
|
dstc_nameslot(c, dst_unwrap_symbol(head), ret);
|
2018-01-21 19:39:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-06 06:25:48 +00:00
|
|
|
static void varleaf(
|
|
|
|
DstCompiler *c,
|
|
|
|
DstAst *ast,
|
|
|
|
const uint8_t *sym,
|
|
|
|
DstSlot s,
|
2018-03-12 06:06:51 +00:00
|
|
|
DstTable *attr) {
|
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);
|
2018-03-12 06:06:51 +00:00
|
|
|
reftab->proto = attr;
|
2018-01-12 15:41:27 +00:00
|
|
|
DstArray *ref = dst_array(1);
|
|
|
|
dst_array_push(ref, dst_wrap_nil());
|
2018-03-18 18:01:58 +00:00
|
|
|
dst_table_put(reftab, dst_csymbolv(":ref"), dst_wrap_array(ref));
|
2018-02-06 06:25:48 +00:00
|
|
|
dst_table_put(c->env, dst_wrap_symbol(sym), 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);
|
2018-02-06 06:25:48 +00:00
|
|
|
int32_t retindex = dstc_preread(c, ast, 0xFF, 2, s);
|
2018-01-17 04:18:45 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-01-12 15:41:27 +00:00
|
|
|
(retindex << 16) |
|
|
|
|
(refarrayindex << 8) |
|
|
|
|
DOP_PUT_INDEX);
|
|
|
|
dstc_postread(c, refarrayslot, refarrayindex);
|
2018-02-06 06:25:48 +00:00
|
|
|
dstc_postread(c, s, retindex);
|
2018-01-12 15:41:27 +00:00
|
|
|
} else {
|
2018-02-06 06:25:48 +00:00
|
|
|
namelocal(c, ast, dst_wrap_symbol(sym), DST_SLOT_NAMED | DST_SLOT_MUTABLE, s) ;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 06:25:48 +00:00
|
|
|
DstSlot dstc_var(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
|
|
|
DstSlot ret = dohead(c, opts, ast, &head, argn, argv);
|
|
|
|
if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil());
|
2018-03-12 06:06:51 +00:00
|
|
|
destructure(c, argv[0], ret, varleaf, handleattr(c, argn, argv));
|
2018-02-06 06:25:48 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void defleaf(
|
|
|
|
DstCompiler *c,
|
|
|
|
DstAst *ast,
|
|
|
|
const uint8_t *sym,
|
|
|
|
DstSlot s,
|
2018-03-12 06:06:51 +00:00
|
|
|
DstTable *attr) {
|
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);
|
2018-03-12 06:06:51 +00:00
|
|
|
tab->proto = attr;
|
2018-01-13 21:14:40 +00:00
|
|
|
int32_t tableindex, valsymindex, valueindex;
|
2018-03-18 18:01:58 +00:00
|
|
|
DstSlot valsym = dstc_cslot(dst_csymbolv(":value"));
|
2018-01-13 21:14:40 +00:00
|
|
|
DstSlot tabslot = dstc_cslot(dst_wrap_table(tab));
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Add env entry to env */
|
2018-02-06 06:25:48 +00:00
|
|
|
dst_table_put(c->env, dst_wrap_symbol(sym), 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);
|
2018-02-06 06:25:48 +00:00
|
|
|
valueindex = dstc_preread(c, ast, 0xFF, 3, s);
|
2018-03-23 13:18:04 +00:00
|
|
|
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);
|
2018-02-06 06:25:48 +00:00
|
|
|
dstc_postread(c, s, valueindex);
|
2018-01-12 15:41:27 +00:00
|
|
|
} else {
|
2018-02-06 06:25:48 +00:00
|
|
|
namelocal(c, ast, dst_wrap_symbol(sym), DST_SLOT_NAMED, s);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DstSlot dstc_def(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
|
|
|
|
DstCompiler *c = opts.compiler;
|
|
|
|
Dst head;
|
|
|
|
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-03-12 06:06:51 +00:00
|
|
|
destructure(c, argv[0], ret, defleaf, handleattr(c, argn, argv));
|
2018-02-06 06:25:48 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :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 */
|
2018-03-23 13:18:04 +00:00
|
|
|
target = (drop || tail)
|
2018-03-19 18:51:18 +00:00
|
|
|
? dstc_cslot(dst_wrap_nil())
|
|
|
|
: dstc_gettarget(opts);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* 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);
|
2018-03-23 13:18:04 +00:00
|
|
|
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);
|
2018-03-23 13:18:04 +00:00
|
|
|
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;
|
2018-03-19 18:51:18 +00:00
|
|
|
DstSlot ret = dstc_cslot(dst_wrap_nil());
|
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;
|
2018-03-19 18:51:18 +00:00
|
|
|
} else {
|
|
|
|
subopts = opts;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
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);
|
2018-02-02 22:14:27 +00:00
|
|
|
} else {
|
|
|
|
labelc = 0;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2018-02-12 21:43:59 +00:00
|
|
|
slot.envindex = -1;
|
2018-01-12 15:41:27 +00:00
|
|
|
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
|
|
|
} else {
|
2018-03-12 06:06:51 +00:00
|
|
|
DstSlot s;
|
|
|
|
s.envindex = -1;
|
|
|
|
s.flags = DST_SLOTTYPE_ANY;
|
|
|
|
s.constant = dst_wrap_nil();
|
|
|
|
s.index = dstc_lsloti(c);
|
|
|
|
destructure(c, param, s, defleaf, NULL);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-03-12 06:06:51 +00:00
|
|
|
arity++;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
} 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;
|
2018-02-12 21:43:59 +00:00
|
|
|
slot.envindex = -1;
|
2018-01-19 17:37:37 +00:00
|
|
|
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 */
|
2018-03-12 06:06:51 +00:00
|
|
|
if (parami + 1 == argn) {
|
|
|
|
dstc_emit(c, ast, DOP_RETURN_NIL);
|
|
|
|
} else for (argi = parami + 1; argi < argn; argi++) {
|
2018-01-12 15:41:27 +00:00
|
|
|
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
|
|
|
}
|
2018-03-23 13:18:04 +00:00
|
|
|
|
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;
|
2018-03-22 00:53:39 +00:00
|
|
|
if (selfref) def->name = dst_unwrap_symbol(head);
|
2018-01-12 15:41:27 +00:00
|
|
|
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);
|
2018-03-23 13:18:04 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
if (ret.index != localslot) {
|
2018-03-23 13:18:04 +00:00
|
|
|
dstc_emit(c, ast,
|
2018-01-12 15:41:27 +00:00
|
|
|
(ret.index << 16) |
|
|
|
|
(localslot << 8) |
|
|
|
|
DOP_MOVE_FAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:18:04 +00:00
|
|
|
/* Keep in lexicographic order */
|
2018-01-12 15:41:27 +00:00
|
|
|
static const DstSpecial dstc_specials[] = {
|
2018-03-16 22:15:34 +00:00
|
|
|
{":=", dstc_varset},
|
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},
|
|
|
|
{"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);
|
|
|
|
}
|
|
|
|
|