2018-01-12 15:41:27 +00:00
|
|
|
/*
|
2020-01-12 16:50:37 +00:00
|
|
|
* Copyright (c) 2020 Calvin Rose
|
2018-01-12 15:41:27 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-01-12 15:41:27 +00:00
|
|
|
#include "compile.h"
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "vector.h"
|
2018-07-01 15:52:15 +00:00
|
|
|
#include "emit.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_quote(JanetFopts opts, int32_t argn, const Janet *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
if (argn != 1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(opts.compiler, "expected 1 argument");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
return janetc_cslot(argv[0]);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 20:10:04 +00:00
|
|
|
static JanetSlot janetc_splice(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetSlot ret;
|
|
|
|
if (argn != 1) {
|
|
|
|
janetc_cerror(opts.compiler, "expected 1 argument");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
|
|
|
ret = janetc_value(opts, argv[0]);
|
|
|
|
ret.flags |= JANET_SLOT_SPLICED;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-01 03:49:21 +00:00
|
|
|
static JanetSlot qq_slots(JanetFopts opts, JanetSlot *slots, int makeop) {
|
|
|
|
JanetSlot target = janetc_gettarget(opts);
|
2018-12-05 20:10:04 +00:00
|
|
|
janetc_pushslots(opts.compiler, slots);
|
2018-12-01 03:49:21 +00:00
|
|
|
janetc_freeslots(opts.compiler, slots);
|
|
|
|
janetc_emit_s(opts.compiler, makeop, target, 1);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2019-12-04 22:40:53 +00:00
|
|
|
static JanetSlot quasiquote(JanetFopts opts, Janet x, int depth, int level) {
|
|
|
|
if (depth == 0) {
|
|
|
|
janetc_cerror(opts.compiler, "quasiquote too deeply nested");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
2018-12-01 03:49:21 +00:00
|
|
|
JanetSlot *slots = NULL;
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
default:
|
|
|
|
return janetc_cslot(x);
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_TUPLE: {
|
|
|
|
int32_t i, len;
|
|
|
|
const Janet *tup = janet_unwrap_tuple(x);
|
|
|
|
len = janet_tuple_length(tup);
|
|
|
|
if (len > 1 && janet_checktype(tup[0], JANET_SYMBOL)) {
|
|
|
|
const uint8_t *head = janet_unwrap_symbol(tup[0]);
|
2019-12-04 22:40:53 +00:00
|
|
|
if (!janet_cstrcmp(head, "unquote")) {
|
|
|
|
if (level == 0) {
|
|
|
|
return janetc_value(janetc_fopts_default(opts.compiler), tup[1]);
|
|
|
|
} else {
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
} else if (!janet_cstrcmp(head, "quasiquote")) {
|
|
|
|
level++;
|
|
|
|
}
|
2018-12-01 03:49:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
for (i = 0; i < len; i++)
|
2019-12-04 22:40:53 +00:00
|
|
|
janet_v_push(slots, quasiquote(opts, tup[i], depth - 1, level));
|
2019-03-31 18:14:25 +00:00
|
|
|
return qq_slots(opts, slots, (janet_tuple_flag(tup) & JANET_TUPLE_FLAG_BRACKETCTOR)
|
|
|
|
? JOP_MAKE_BRACKET_TUPLE
|
|
|
|
: JOP_MAKE_TUPLE);
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_ARRAY: {
|
|
|
|
int32_t i;
|
|
|
|
JanetArray *array = janet_unwrap_array(x);
|
|
|
|
for (i = 0; i < array->count; i++)
|
2019-12-04 22:40:53 +00:00
|
|
|
janet_v_push(slots, quasiquote(opts, array->data[i], depth - 1, level));
|
2019-02-20 01:51:34 +00:00
|
|
|
return qq_slots(opts, slots, JOP_MAKE_ARRAY);
|
|
|
|
}
|
2018-12-01 03:49:21 +00:00
|
|
|
case JANET_TABLE:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_STRUCT: {
|
|
|
|
const JanetKV *kv = NULL, *kvs = NULL;
|
2019-03-23 17:50:50 +00:00
|
|
|
int32_t len, cap = 0;
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_dictionary_view(x, &kvs, &len, &cap);
|
|
|
|
while ((kv = janet_dictionary_next(kvs, cap, kv))) {
|
2019-12-04 22:40:53 +00:00
|
|
|
JanetSlot key = quasiquote(opts, kv->key, depth - 1, level);
|
|
|
|
JanetSlot value = quasiquote(opts, kv->value, depth - 1, level);
|
2019-02-20 01:51:34 +00:00
|
|
|
key.flags &= ~JANET_SLOT_SPLICED;
|
|
|
|
value.flags &= ~JANET_SLOT_SPLICED;
|
|
|
|
janet_v_push(slots, key);
|
|
|
|
janet_v_push(slots, value);
|
2018-12-01 03:49:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
return qq_slots(opts, slots,
|
|
|
|
janet_checktype(x, JANET_TABLE) ? JOP_MAKE_TABLE : JOP_MAKE_STRUCT);
|
|
|
|
}
|
2018-12-01 03:49:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JanetSlot janetc_quasiquote(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
if (argn != 1) {
|
|
|
|
janetc_cerror(opts.compiler, "expected 1 argument");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
2019-12-04 22:40:53 +00:00
|
|
|
return quasiquote(opts, argv[0], JANET_RECURSION_GUARD, 0);
|
2018-12-01 03:49:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 01:53:13 +00:00
|
|
|
static JanetSlot janetc_unquote(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
(void) argn;
|
|
|
|
(void) argv;
|
|
|
|
janetc_cerror(opts.compiler, "cannot use unquote here");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
|
|
|
|
2019-07-08 04:18:39 +00:00
|
|
|
/* Perform destructuring. Be careful to
|
2019-01-06 08:23:03 +00:00
|
|
|
* keep the order registers are freed.
|
2018-07-04 03:07:35 +00:00
|
|
|
* Returns if the slot 'right' can be freed. */
|
2018-09-06 02:18:42 +00:00
|
|
|
static int destructure(JanetCompiler *c,
|
2019-02-20 01:51:34 +00:00
|
|
|
Janet left,
|
|
|
|
JanetSlot right,
|
|
|
|
int (*leaf)(JanetCompiler *c,
|
|
|
|
const uint8_t *sym,
|
|
|
|
JanetSlot s,
|
|
|
|
JanetTable *attr),
|
|
|
|
JanetTable *attr) {
|
2018-09-06 02:18:42 +00:00
|
|
|
switch (janet_type(left)) {
|
2018-02-06 06:25:48 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(c, "unexpected type in destructuring");
|
2018-07-04 03:07:35 +00:00
|
|
|
return 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_SYMBOL:
|
2018-02-06 06:25:48 +00:00
|
|
|
/* Leaf, assign right to left */
|
2018-09-06 02:18:42 +00:00
|
|
|
return leaf(c, janet_unwrap_symbol(left), right, attr);
|
|
|
|
case JANET_TUPLE:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ARRAY: {
|
2019-03-23 17:50:50 +00:00
|
|
|
int32_t len = 0;
|
|
|
|
const Janet *values = NULL;
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_indexed_view(left, &values, &len);
|
2019-03-23 17:50:50 +00:00
|
|
|
for (int32_t i = 0; i < len; i++) {
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetSlot nextright = janetc_farslot(c);
|
|
|
|
Janet subval = values[i];
|
|
|
|
if (i < 0x100) {
|
|
|
|
janetc_emit_ssu(c, JOP_GET_INDEX, nextright, right, (uint8_t) i, 1);
|
|
|
|
} else {
|
|
|
|
JanetSlot k = janetc_cslot(janet_wrap_integer(i));
|
2019-12-03 03:15:08 +00:00
|
|
|
janetc_emit_sss(c, JOP_IN, nextright, right, k, 1);
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
if (destructure(c, subval, nextright, leaf, attr))
|
|
|
|
janetc_freeslot(c, nextright);
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TABLE:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_STRUCT: {
|
|
|
|
const JanetKV *kvs = NULL;
|
2019-03-23 17:50:50 +00:00
|
|
|
int32_t cap = 0, len = 0;
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_dictionary_view(left, &kvs, &len, &cap);
|
2019-03-23 17:50:50 +00:00
|
|
|
for (int32_t i = 0; i < cap; i++) {
|
2019-02-20 01:51:34 +00:00
|
|
|
if (janet_checktype(kvs[i].key, JANET_NIL)) continue;
|
|
|
|
JanetSlot nextright = janetc_farslot(c);
|
|
|
|
JanetSlot k = janetc_value(janetc_fopts_default(c), kvs[i].key);
|
2019-12-03 03:15:08 +00:00
|
|
|
janetc_emit_sss(c, JOP_IN, nextright, right, k, 1);
|
2019-02-20 01:51:34 +00:00
|
|
|
if (destructure(c, kvs[i].value, nextright, leaf, attr))
|
|
|
|
janetc_freeslot(c, nextright);
|
2018-02-06 07:02:28 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 17:06:50 +00:00
|
|
|
/* Create a source map for definitions. */
|
|
|
|
static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
|
|
|
|
Janet *tup = janet_tuple_begin(3);
|
2019-05-20 08:02:38 +00:00
|
|
|
tup[0] = c->source ? janet_wrap_string(c->source) : janet_wrap_nil();
|
2019-09-22 22:18:28 +00:00
|
|
|
tup[1] = janet_wrap_integer(c->current_mapping.line);
|
|
|
|
tup[2] = janet_wrap_integer(c->current_mapping.column);
|
2018-12-17 17:06:50 +00:00
|
|
|
return janet_tuple_end(tup);
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
if (argn != 2) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(opts.compiler, "expected 2 arguments");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2019-01-07 00:33:27 +00:00
|
|
|
JanetFopts subopts = janetc_fopts_default(opts.compiler);
|
|
|
|
if (janet_checktype(argv[0], JANET_SYMBOL)) {
|
|
|
|
/* Normal var - (set a 1) */
|
|
|
|
const uint8_t *sym = janet_unwrap_symbol(argv[0]);
|
|
|
|
JanetSlot dest = janetc_resolve(opts.compiler, sym);
|
|
|
|
if (!(dest.flags & JANET_SLOT_MUTABLE)) {
|
|
|
|
janetc_cerror(opts.compiler, "cannot set constant");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
|
|
|
subopts.flags = JANET_FOPTS_HINT;
|
|
|
|
subopts.hint = dest;
|
|
|
|
JanetSlot ret = janetc_value(subopts, argv[1]);
|
|
|
|
janetc_copy(opts.compiler, dest, ret);
|
|
|
|
return ret;
|
|
|
|
} else if (janet_checktype(argv[0], JANET_TUPLE)) {
|
|
|
|
/* Set a field (setf behavior) - (set (tab :key) 2) */
|
|
|
|
const Janet *tup = janet_unwrap_tuple(argv[0]);
|
|
|
|
/* Tuple must have 2 elements */
|
|
|
|
if (janet_tuple_length(tup) != 2) {
|
|
|
|
janetc_cerror(opts.compiler, "expected 2 element tuple for l-value to set");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
|
|
|
JanetSlot ds = janetc_value(subopts, tup[0]);
|
|
|
|
JanetSlot key = janetc_value(subopts, tup[1]);
|
|
|
|
/* Can't be tail position because we will emit a PUT instruction afterwards */
|
|
|
|
/* Also can't drop either */
|
|
|
|
opts.flags &= ~(JANET_FOPTS_TAIL | JANET_FOPTS_DROP);
|
|
|
|
JanetSlot rvalue = janetc_value(opts, argv[1]);
|
|
|
|
/* Emit the PUT instruction */
|
|
|
|
janetc_emit_sss(opts.compiler, JOP_PUT, ds, key, rvalue, 0);
|
|
|
|
return rvalue;
|
|
|
|
} else {
|
|
|
|
/* Error */
|
|
|
|
janetc_cerror(opts.compiler, "expected symbol or tuple for l-value to set");
|
2018-09-06 02:18:42 +00:00
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-01-13 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add attributes to a global def or var table */
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv) {
|
2018-01-13 21:14:40 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *tab = janet_table(2);
|
2018-01-13 21:14:40 +00:00
|
|
|
for (i = 1; i < argn - 1; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet attr = argv[i];
|
|
|
|
switch (janet_type(attr)) {
|
2018-01-13 21:14:40 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(c, "could not add metadata to binding");
|
2018-03-12 06:06:51 +00:00
|
|
|
break;
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_put(tab, attr, janet_wrap_true());
|
2018-01-13 21:14:40 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(tab, janet_ckeywordv("doc"), attr);
|
2018-01-13 21:14:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 06:06:51 +00:00
|
|
|
return tab;
|
2018-01-13 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot dohead(JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) {
|
|
|
|
JanetFopts subopts = janetc_fopts_default(c);
|
|
|
|
JanetSlot ret;
|
2018-01-13 21:14:40 +00:00
|
|
|
if (argn < 2) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(c, "expected at least 2 arguments");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-13 21:14:40 +00:00
|
|
|
}
|
2018-06-29 03:36:31 +00:00
|
|
|
*head = argv[0];
|
2018-09-06 02:18:42 +00:00
|
|
|
subopts.flags = opts.flags & ~(JANET_FOPTS_TAIL | JANET_FOPTS_DROP);
|
2018-01-17 04:18:45 +00:00
|
|
|
subopts.hint = opts.hint;
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janetc_value(subopts, argv[argn - 1]);
|
2018-01-21 19:39:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Def or var a symbol in a local scope */
|
2018-09-06 02:18:42 +00:00
|
|
|
static int namelocal(JanetCompiler *c, const uint8_t *head, int32_t flags, JanetSlot ret) {
|
|
|
|
int isUnnamedRegister = !(ret.flags & JANET_SLOT_NAMED) &&
|
2019-02-20 01:51:34 +00:00
|
|
|
ret.index > 0 &&
|
|
|
|
ret.envindex >= 0;
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!isUnnamedRegister) {
|
2018-01-21 19:39:32 +00:00
|
|
|
/* Slot is not able to be named */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot localslot = janetc_farslot(c);
|
|
|
|
janetc_copy(c, localslot, ret);
|
2018-01-21 19:39:32 +00:00
|
|
|
ret = localslot;
|
|
|
|
}
|
2018-02-03 22:22:04 +00:00
|
|
|
ret.flags |= flags;
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_nameslot(c, head, ret);
|
2018-07-04 03:07:35 +00:00
|
|
|
return !isUnnamedRegister;
|
2018-01-21 19:39:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
static int varleaf(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetCompiler *c,
|
|
|
|
const uint8_t *sym,
|
|
|
|
JanetSlot s,
|
2019-06-20 00:07:40 +00:00
|
|
|
JanetTable *reftab) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (c->scope->flags & JANET_SCOPE_TOP) {
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Global var, generate var */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot refslot;
|
2019-07-08 04:18:39 +00:00
|
|
|
JanetTable *entry = janet_table_clone(reftab);
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *ref = janet_array(1);
|
|
|
|
janet_array_push(ref, janet_wrap_nil());
|
2019-07-08 04:18:39 +00:00
|
|
|
janet_table_put(entry, janet_ckeywordv("ref"), janet_wrap_array(ref));
|
|
|
|
janet_table_put(entry, janet_ckeywordv("source-map"),
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_wrap_tuple(janetc_make_sourcemap(c)));
|
2019-07-08 04:18:39 +00:00
|
|
|
janet_table_put(c->env, janet_wrap_symbol(sym), janet_wrap_table(entry));
|
2018-09-06 02:18:42 +00:00
|
|
|
refslot = janetc_cslot(janet_wrap_array(ref));
|
|
|
|
janetc_emit_ssu(c, JOP_PUT_INDEX, refslot, s, 0, 0);
|
2018-07-04 03:07:35 +00:00
|
|
|
return 1;
|
2018-01-12 15:41:27 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return namelocal(c, sym, JANET_SLOT_MUTABLE, s);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_var(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
Janet head;
|
|
|
|
JanetSlot ret = dohead(c, opts, &head, argn, argv);
|
|
|
|
if (c->result.status == JANET_COMPILE_ERROR)
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-09-21 18:09:38 +00:00
|
|
|
destructure(c, argv[0], ret, varleaf, handleattr(c, argn, argv));
|
|
|
|
return ret;
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
static int defleaf(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetCompiler *c,
|
|
|
|
const uint8_t *sym,
|
|
|
|
JanetSlot s,
|
2019-06-20 00:07:40 +00:00
|
|
|
JanetTable *tab) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (c->scope->flags & JANET_SCOPE_TOP) {
|
2019-07-08 04:18:39 +00:00
|
|
|
JanetTable *entry = janet_table_clone(tab);
|
|
|
|
janet_table_put(entry, janet_ckeywordv("source-map"),
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_wrap_tuple(janetc_make_sourcemap(c)));
|
2019-01-03 00:41:07 +00:00
|
|
|
JanetSlot valsym = janetc_cslot(janet_ckeywordv("value"));
|
2019-07-08 04:18:39 +00:00
|
|
|
JanetSlot tabslot = janetc_cslot(janet_wrap_table(entry));
|
2018-01-13 21:14:40 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Add env entry to env */
|
2019-07-08 04:18:39 +00:00
|
|
|
janet_table_put(c->env, janet_wrap_symbol(sym), janet_wrap_table(entry));
|
2018-01-13 21:14:40 +00:00
|
|
|
|
|
|
|
/* Put value in table when evaulated */
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_emit_sss(c, JOP_PUT, tabslot, valsym, s, 0);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2020-11-16 22:41:27 +00:00
|
|
|
return namelocal(c, sym, 0, s);
|
2018-02-06 06:25:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_def(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
Janet head;
|
|
|
|
opts.flags &= ~JANET_FOPTS_HINT;
|
|
|
|
JanetSlot ret = dohead(c, opts, &head, argn, argv);
|
|
|
|
if (c->result.status == JANET_COMPILE_ERROR)
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-09-21 18:09:38 +00:00
|
|
|
destructure(c, argv[0], ret, defleaf, handleattr(c, argn, argv));
|
|
|
|
return ret;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :condition
|
|
|
|
* ...
|
|
|
|
* jump-if-not condition :right
|
|
|
|
* :left
|
|
|
|
* ...
|
|
|
|
* jump done (only if not tail)
|
|
|
|
* :right
|
|
|
|
* ...
|
|
|
|
* :done
|
|
|
|
*/
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_if(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
2018-07-01 15:52:15 +00:00
|
|
|
int32_t labelr, labeljr, labeld, labeljd;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFopts condopts, bodyopts;
|
|
|
|
JanetSlot cond, left, right, target;
|
|
|
|
Janet truebody, falsebody;
|
|
|
|
JanetScope condscope, tempscope;
|
|
|
|
const int tail = opts.flags & JANET_FOPTS_TAIL;
|
|
|
|
const int drop = opts.flags & JANET_FOPTS_DROP;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
if (argn < 2 || argn > 3) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(c, "expected 2 or 3 arguments to if");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 04:18:45 +00:00
|
|
|
/* Get the bodies of the if expression */
|
|
|
|
truebody = argv[1];
|
2018-09-06 02:18:42 +00:00
|
|
|
falsebody = argn > 2 ? argv[2] : janet_wrap_nil();
|
2018-01-17 04:18:45 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Get options */
|
2018-09-06 02:18:42 +00:00
|
|
|
condopts = janetc_fopts_default(c);
|
2018-01-17 04:18:45 +00:00
|
|
|
bodyopts = opts;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-07-12 01:29:39 +00:00
|
|
|
/* Set target for compilation */
|
|
|
|
target = (drop || tail)
|
2019-02-20 01:51:34 +00:00
|
|
|
? janetc_cslot(janet_wrap_nil())
|
|
|
|
: janetc_gettarget(opts);
|
2018-07-12 01:29:39 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile condition */
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_scope(&condscope, c, 0, "if");
|
|
|
|
cond = janetc_value(condopts, argv[0]);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Check constant condition. */
|
|
|
|
/* TODO: Use type info for more short circuits */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (cond.flags & JANET_SLOT_CONSTANT) {
|
|
|
|
if (!janet_truthy(cond.constant)) {
|
2018-01-17 04:18:45 +00:00
|
|
|
/* Swap the true and false bodies */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet temp = falsebody;
|
2018-01-17 04:18:45 +00:00
|
|
|
falsebody = truebody;
|
|
|
|
truebody = temp;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-12-25 20:32:42 +00:00
|
|
|
janetc_scope(&tempscope, c, 0, "if-true");
|
|
|
|
right = janetc_value(bodyopts, truebody);
|
|
|
|
if (!drop && !tail) janetc_copy(c, target, right);
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_popscope(c);
|
|
|
|
janetc_throwaway(bodyopts, falsebody);
|
2018-12-08 04:57:19 +00:00
|
|
|
janetc_popscope(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile jump to right */
|
2018-09-06 02:18:42 +00:00
|
|
|
labeljr = janetc_emit_si(c, JOP_JUMP_IF_NOT, cond, 0, 0);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Condition left body */
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_scope(&tempscope, c, 0, "if-true");
|
|
|
|
left = janetc_value(bodyopts, truebody);
|
|
|
|
if (!drop && !tail) janetc_copy(c, target, left);
|
|
|
|
janetc_popscope(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Compile jump to done */
|
2018-09-06 02:18:42 +00:00
|
|
|
labeljd = janet_v_count(c->buffer);
|
|
|
|
if (!tail) janetc_emit(c, JOP_JUMP);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Compile right body */
|
2018-09-06 02:18:42 +00:00
|
|
|
labelr = janet_v_count(c->buffer);
|
|
|
|
janetc_scope(&tempscope, c, 0, "if-false");
|
|
|
|
right = janetc_value(bodyopts, falsebody);
|
|
|
|
if (!drop && !tail) janetc_copy(c, target, right);
|
|
|
|
janetc_popscope(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-07-12 01:29:39 +00:00
|
|
|
/* Pop main scope */
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_popscope(c);
|
2018-07-12 01:29:39 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Write jumps - only add jump lengths if jump actually emitted */
|
2018-09-06 02:18:42 +00:00
|
|
|
labeld = janet_v_count(c->buffer);
|
2018-01-12 15:41:27 +00:00
|
|
|
c->buffer[labeljr] |= (labelr - labeljr) << 16;
|
|
|
|
if (!tail) c->buffer[labeljd] |= (labeld - labeljd) << 8;
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
if (tail) target.flags |= JANET_SLOT_RETURNED;
|
2018-01-12 15:41:27 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a do form. Do forms execute their body sequentially and
|
|
|
|
* evaluate to the last expression in the body. */
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_do(JanetFopts opts, int32_t argn, const Janet *argv) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot ret = janetc_cslot(janet_wrap_nil());
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
JanetFopts subopts = janetc_fopts_default(c);
|
|
|
|
JanetScope tempscope;
|
|
|
|
janetc_scope(&tempscope, c, 0, "do");
|
2018-01-12 15:41:27 +00:00
|
|
|
for (i = 0; i < argn; i++) {
|
|
|
|
if (i != argn - 1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
subopts.flags = JANET_FOPTS_DROP;
|
2018-03-19 18:51:18 +00:00
|
|
|
} else {
|
|
|
|
subopts = opts;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janetc_value(subopts, argv[i]);
|
2018-01-12 15:41:27 +00:00
|
|
|
if (i != argn - 1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_freeslot(c, ret);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_popscope_keepslot(c, ret);
|
2018-01-12 15:41:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-16 22:41:27 +00:00
|
|
|
|
|
|
|
/* Compile an upscope form. Upscope forms execute their body sequentially and
|
|
|
|
* evaluate to the last expression in the body, but without lexical scope. */
|
|
|
|
static JanetSlot janetc_upscope(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
int32_t i;
|
|
|
|
JanetSlot ret = janetc_cslot(janet_wrap_nil());
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
JanetFopts subopts = janetc_fopts_default(c);
|
|
|
|
for (i = 0; i < argn; i++) {
|
|
|
|
if (i != argn - 1) {
|
|
|
|
subopts.flags = JANET_FOPTS_DROP;
|
|
|
|
} else {
|
|
|
|
subopts = opts;
|
|
|
|
}
|
|
|
|
ret = janetc_value(subopts, argv[i]);
|
|
|
|
if (i != argn - 1) {
|
|
|
|
janetc_freeslot(c, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-12 01:29:39 +00:00
|
|
|
/* Add a funcdef to the top most function scope */
|
2018-09-06 02:18:42 +00:00
|
|
|
static int32_t janetc_addfuncdef(JanetCompiler *c, JanetFuncDef *def) {
|
|
|
|
JanetScope *scope = c->scope;
|
2018-07-12 01:29:39 +00:00
|
|
|
while (scope) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (scope->flags & JANET_SCOPE_FUNCTION)
|
2018-07-12 01:29:39 +00:00
|
|
|
break;
|
|
|
|
scope = scope->parent;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_assert(scope, "could not add funcdef");
|
|
|
|
janet_v_push(scope->defs, def);
|
|
|
|
return janet_v_count(scope->defs) - 1;
|
2018-07-12 01:29:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 21:47:05 +00:00
|
|
|
/*
|
|
|
|
* break
|
|
|
|
*
|
|
|
|
* jump :end or retn if in function
|
|
|
|
*/
|
|
|
|
static JanetSlot janetc_break(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
JanetScope *scope = c->scope;
|
2019-03-10 03:01:10 +00:00
|
|
|
if (argn > 1) {
|
|
|
|
janetc_cerror(c, "expected at most 1 argument");
|
2019-03-09 21:47:05 +00:00
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
2019-03-10 03:01:10 +00:00
|
|
|
|
|
|
|
/* Find scope to break from */
|
2019-03-09 21:47:05 +00:00
|
|
|
while (scope) {
|
|
|
|
if (scope->flags & (JANET_SCOPE_FUNCTION | JANET_SCOPE_WHILE))
|
|
|
|
break;
|
|
|
|
scope = scope->parent;
|
|
|
|
}
|
|
|
|
if (NULL == scope) {
|
|
|
|
janetc_cerror(c, "break must occur in while loop or closure");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
2019-03-10 03:01:10 +00:00
|
|
|
|
|
|
|
/* Emit code to break from that scope */
|
|
|
|
JanetFopts subopts = janetc_fopts_default(c);
|
|
|
|
if (scope->flags & JANET_SCOPE_FUNCTION) {
|
|
|
|
if (!(scope->flags & JANET_SCOPE_WHILE) && argn) {
|
|
|
|
/* Closure body with return argument */
|
|
|
|
subopts.flags |= JANET_FOPTS_TAIL;
|
|
|
|
JanetSlot ret = janetc_value(subopts, argv[0]);
|
|
|
|
ret.flags |= JANET_SLOT_RETURNED;
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
/* while loop IIFE or no argument */
|
|
|
|
if (argn) {
|
|
|
|
subopts.flags |= JANET_FOPTS_DROP;
|
|
|
|
janetc_value(subopts, argv[0]);
|
|
|
|
}
|
|
|
|
janetc_emit(c, JOP_RETURN_NIL);
|
|
|
|
JanetSlot s = janetc_cslot(janet_wrap_nil());
|
|
|
|
s.flags |= JANET_SLOT_RETURNED;
|
|
|
|
return s;
|
|
|
|
}
|
2019-03-09 21:47:05 +00:00
|
|
|
} else {
|
2019-03-10 03:01:10 +00:00
|
|
|
if (argn) {
|
|
|
|
subopts.flags |= JANET_FOPTS_DROP;
|
|
|
|
janetc_value(subopts, argv[0]);
|
|
|
|
}
|
2019-03-09 21:47:05 +00:00
|
|
|
/* Tag the instruction so the while special can turn it into a proper jump */
|
|
|
|
janetc_emit(c, 0x80 | JOP_JUMP);
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:55:07 +00:00
|
|
|
/* Check if a form matches the pattern (not= nil _) */
|
|
|
|
static int janetc_check_notnil_form(Janet x, Janet *capture) {
|
|
|
|
if (!janet_checktype(x, JANET_TUPLE)) return 0;
|
|
|
|
JanetTuple tup = janet_unwrap_tuple(x);
|
|
|
|
if (!janet_checktype(tup[0], JANET_FUNCTION)) return 0;
|
|
|
|
if (3 != janet_tuple_length(tup)) return 0;
|
|
|
|
JanetFunction *fun = janet_unwrap_function(tup[0]);
|
|
|
|
uint32_t tag = fun->def->flags & JANET_FUNCDEF_FLAG_TAG;
|
|
|
|
if (tag != JANET_FUN_NEQ) return 0;
|
|
|
|
if (!janet_checktype(tup[1], JANET_NIL)) return 0;
|
|
|
|
*capture = tup[2];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/*
|
|
|
|
* :whiletop
|
|
|
|
* ...
|
|
|
|
* :condition
|
|
|
|
* jump-if-not cond :done
|
|
|
|
* ...
|
|
|
|
* jump :whiletop
|
|
|
|
* :done
|
|
|
|
*/
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
JanetSlot cond;
|
|
|
|
JanetFopts subopts = janetc_fopts_default(c);
|
|
|
|
JanetScope tempscope;
|
2018-07-01 15:52:15 +00:00
|
|
|
int32_t labelwt, labeld, labeljt, labelc, i;
|
2018-01-12 15:41:27 +00:00
|
|
|
int infinite = 0;
|
2020-01-18 23:55:07 +00:00
|
|
|
int is_notnil_form = 0;
|
|
|
|
uint8_t ifjmp = JOP_JUMP_IF;
|
|
|
|
uint8_t ifnjmp = JOP_JUMP_IF_NOT;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
if (argn < 2) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(c, "expected at least 2 arguments");
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
labelwt = janet_v_count(c->buffer);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2019-03-09 21:47:05 +00:00
|
|
|
janetc_scope(&tempscope, c, JANET_SCOPE_WHILE, "while");
|
2018-07-12 01:29:39 +00:00
|
|
|
|
2020-01-18 23:55:07 +00:00
|
|
|
/* Check for `(not= nil _)` in condition, and if so, use the
|
|
|
|
* jmpnl or jmpnn instructions. This let's us implement `(each ...)`
|
|
|
|
* more efficiently. */
|
|
|
|
Janet condform = argv[0];
|
|
|
|
if (janetc_check_notnil_form(condform, &condform)) {
|
|
|
|
is_notnil_form = 1;
|
|
|
|
ifjmp = JOP_JUMP_IF_NOT_NIL;
|
|
|
|
ifnjmp = JOP_JUMP_IF_NIL;
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile condition */
|
2020-01-18 23:55:07 +00:00
|
|
|
cond = janetc_value(subopts, condform);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Check for constant condition */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (cond.flags & JANET_SLOT_CONSTANT) {
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Loop never executes */
|
2020-01-18 23:55:07 +00:00
|
|
|
int never_executes = is_notnil_form
|
|
|
|
? janet_checktype(cond.constant, JANET_NIL)
|
|
|
|
: !janet_truthy(cond.constant);
|
|
|
|
if (never_executes) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_popscope(c);
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
/* Infinite loop */
|
|
|
|
infinite = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Infinite loop does not need to check condition */
|
2018-07-12 01:29:39 +00:00
|
|
|
labelc = infinite
|
2019-02-20 01:51:34 +00:00
|
|
|
? 0
|
2020-01-18 23:55:07 +00:00
|
|
|
: janetc_emit_si(c, ifnjmp, cond, 0, 0);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Compile body */
|
|
|
|
for (i = 1; i < argn; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
subopts.flags = JANET_FOPTS_DROP;
|
|
|
|
janetc_freeslot(c, janetc_value(subopts, argv[i]));
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 01:29:39 +00:00
|
|
|
/* Check if closure created in while scope. If so,
|
|
|
|
* recompile in a function scope. */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (tempscope.flags & JANET_SCOPE_CLOSURE) {
|
2020-05-16 13:28:50 +00:00
|
|
|
subopts = janetc_fopts_default(c);
|
2018-09-06 02:18:42 +00:00
|
|
|
tempscope.flags |= JANET_SCOPE_UNUSED;
|
|
|
|
janetc_popscope(c);
|
2020-05-19 14:45:45 +00:00
|
|
|
if (c->buffer) janet_v__cnt(c->buffer) = labelwt;
|
|
|
|
if (c->mapbuffer) janet_v__cnt(c->mapbuffer) = labelwt;
|
2018-07-12 01:29:39 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_scope(&tempscope, c, JANET_SCOPE_FUNCTION, "while-iife");
|
2018-07-12 01:29:39 +00:00
|
|
|
|
|
|
|
/* Recompile in the function scope */
|
2020-01-19 22:25:10 +00:00
|
|
|
cond = janetc_value(subopts, condform);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (!(cond.flags & JANET_SLOT_CONSTANT)) {
|
2019-01-06 08:23:03 +00:00
|
|
|
/* If not an infinite loop, return nil when condition false */
|
2020-01-18 23:55:07 +00:00
|
|
|
janetc_emit_si(c, ifjmp, cond, 2, 0);
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_emit(c, JOP_RETURN_NIL);
|
2018-07-12 01:29:39 +00:00
|
|
|
}
|
|
|
|
for (i = 1; i < argn; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
subopts.flags = JANET_FOPTS_DROP;
|
|
|
|
janetc_freeslot(c, janetc_value(subopts, argv[i]));
|
2018-07-12 01:29:39 +00:00
|
|
|
}
|
|
|
|
/* But now add tail recursion */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t tempself = janetc_regalloc_temp(&tempscope.ra, JANETC_REGTEMP_0);
|
|
|
|
janetc_emit(c, JOP_LOAD_SELF | (tempself << 8));
|
|
|
|
janetc_emit(c, JOP_TAILCALL | (tempself << 8));
|
2019-09-15 18:27:49 +00:00
|
|
|
janetc_regalloc_freetemp(&c->scope->ra, tempself, JANETC_REGTEMP_0);
|
2018-07-12 01:29:39 +00:00
|
|
|
/* Compile function */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncDef *def = janetc_pop_funcdef(c);
|
|
|
|
def->name = janet_cstring("_while");
|
2020-07-04 01:41:55 +00:00
|
|
|
janet_def_addflags(def);
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t defindex = janetc_addfuncdef(c, def);
|
2018-07-12 01:29:39 +00:00
|
|
|
/* And then load the closure and call it. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t cloreg = janetc_regalloc_temp(&c->scope->ra, JANETC_REGTEMP_0);
|
|
|
|
janetc_emit(c, JOP_CLOSURE | (cloreg << 8) | (defindex << 16));
|
|
|
|
janetc_emit(c, JOP_CALL | (cloreg << 8) | (cloreg << 16));
|
2019-09-15 18:27:49 +00:00
|
|
|
janetc_regalloc_freetemp(&c->scope->ra, cloreg, JANETC_REGTEMP_0);
|
2018-09-06 02:18:42 +00:00
|
|
|
c->scope->flags |= JANET_SCOPE_CLOSURE;
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-07-12 01:29:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Compile jump to :whiletop */
|
2018-09-06 02:18:42 +00:00
|
|
|
labeljt = janet_v_count(c->buffer);
|
|
|
|
janetc_emit(c, JOP_JUMP);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
|
|
|
/* Calculate jumps */
|
2018-09-06 02:18:42 +00:00
|
|
|
labeld = janet_v_count(c->buffer);
|
2019-03-04 16:17:34 +00:00
|
|
|
if (!infinite) c->buffer[labelc] |= (uint32_t)(labeld - labelc) << 16;
|
|
|
|
c->buffer[labeljt] |= (uint32_t)(labelwt - labeljt) << 8;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2019-03-09 21:47:05 +00:00
|
|
|
/* Calculate breaks */
|
|
|
|
for (int32_t i = labelwt; i < labeld; i++) {
|
|
|
|
if (c->buffer[i] == (0x80 | JOP_JUMP)) {
|
|
|
|
c->buffer[i] = JOP_JUMP | ((labeld - i) << 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Pop scope and return nil slot */
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_popscope(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot janetc_fn(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
JanetFuncDef *def;
|
|
|
|
JanetSlot ret;
|
2018-11-25 19:03:00 +00:00
|
|
|
Janet head;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetScope fnscope;
|
2019-03-12 04:23:14 +00:00
|
|
|
int32_t paramcount, argi, parami, arity, min_arity, max_arity, defindex, i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFopts subopts = janetc_fopts_default(c);
|
|
|
|
const Janet *params;
|
2018-07-01 15:52:15 +00:00
|
|
|
const char *errmsg = NULL;
|
2018-11-25 19:03:00 +00:00
|
|
|
|
|
|
|
/* Function flags */
|
|
|
|
int vararg = 0;
|
2019-05-24 21:03:22 +00:00
|
|
|
int structarg = 0;
|
2019-03-12 04:23:14 +00:00
|
|
|
int allow_extra = 0;
|
2018-01-19 17:37:37 +00:00
|
|
|
int selfref = 0;
|
2018-11-25 19:03:00 +00:00
|
|
|
int seenamp = 0;
|
2019-03-12 04:23:14 +00:00
|
|
|
int seenopt = 0;
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-07-01 15:52:15 +00:00
|
|
|
/* Begin function */
|
2018-09-06 02:18:42 +00:00
|
|
|
c->scope->flags |= JANET_SCOPE_CLOSURE;
|
|
|
|
janetc_scope(&fnscope, c, JANET_SCOPE_FUNCTION, "function");
|
2018-07-01 15:52:15 +00:00
|
|
|
|
2019-10-05 16:47:25 +00:00
|
|
|
if (argn == 0) {
|
|
|
|
errmsg = "expected at least 1 argument to function literal";
|
2018-07-01 15:52:15 +00:00
|
|
|
goto error;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read function parameters */
|
|
|
|
parami = 0;
|
2018-06-29 03:36:31 +00:00
|
|
|
head = argv[0];
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(head, JANET_SYMBOL)) {
|
2018-01-19 17:37:37 +00:00
|
|
|
selfref = 1;
|
|
|
|
parami = 1;
|
|
|
|
}
|
2018-11-25 19:03:00 +00:00
|
|
|
if (parami >= argn || !janet_checktype(argv[parami], JANET_TUPLE)) {
|
2018-07-01 15:52:15 +00:00
|
|
|
errmsg = "expected function parameters";
|
|
|
|
goto error;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-11-25 19:03:00 +00:00
|
|
|
|
2019-06-24 16:44:13 +00:00
|
|
|
/* Keep track of destructured parameters */
|
|
|
|
JanetSlot *destructed_params = NULL;
|
|
|
|
|
2018-11-25 19:03:00 +00:00
|
|
|
/* Compile function parameters */
|
|
|
|
params = janet_unwrap_tuple(argv[parami]);
|
|
|
|
paramcount = janet_tuple_length(params);
|
|
|
|
arity = paramcount;
|
|
|
|
for (i = 0; i < paramcount; i++) {
|
|
|
|
Janet param = params[i];
|
|
|
|
if (janet_checktype(param, JANET_SYMBOL)) {
|
|
|
|
/* Check for varargs and unfixed arity */
|
2019-03-12 04:23:14 +00:00
|
|
|
if (!janet_cstrcmp(janet_unwrap_symbol(param), "&")) {
|
|
|
|
if (seenamp) {
|
|
|
|
errmsg = "& in unexpected location";
|
|
|
|
goto error;
|
|
|
|
} else if (i == paramcount - 1) {
|
|
|
|
allow_extra = 1;
|
2018-01-12 15:41:27 +00:00
|
|
|
arity--;
|
2018-11-25 19:03:00 +00:00
|
|
|
} else if (i == paramcount - 2) {
|
|
|
|
vararg = 1;
|
|
|
|
arity -= 2;
|
|
|
|
} else {
|
2019-03-12 04:23:14 +00:00
|
|
|
errmsg = "& in unexpected location";
|
2018-11-25 19:03:00 +00:00
|
|
|
goto error;
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2019-03-12 04:23:14 +00:00
|
|
|
seenamp = 1;
|
|
|
|
} else if (!janet_cstrcmp(janet_unwrap_symbol(param), "&opt")) {
|
|
|
|
if (seenopt) {
|
|
|
|
errmsg = "only one &opt allowed";
|
|
|
|
goto error;
|
|
|
|
} else if (i == paramcount - 1) {
|
|
|
|
errmsg = "&opt cannot be last item in parameter list";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
min_arity = i;
|
|
|
|
arity--;
|
|
|
|
seenopt = 1;
|
2019-05-24 21:03:22 +00:00
|
|
|
} else if (!janet_cstrcmp(janet_unwrap_symbol(param), "&keys")) {
|
|
|
|
if (seenamp) {
|
|
|
|
errmsg = "&keys in unexpected location";
|
|
|
|
goto error;
|
|
|
|
} else if (i == paramcount - 2) {
|
|
|
|
vararg = 1;
|
|
|
|
structarg = 1;
|
|
|
|
arity -= 2;
|
|
|
|
} else {
|
|
|
|
errmsg = "&keys in unexpected location";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
seenamp = 1;
|
2018-01-12 15:41:27 +00:00
|
|
|
} else {
|
2018-11-25 19:03:00 +00:00
|
|
|
janetc_nameslot(c, janet_unwrap_symbol(param), janetc_farslot(c));
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
2018-11-25 19:03:00 +00:00
|
|
|
} else {
|
2019-06-24 16:44:13 +00:00
|
|
|
janet_v_push(destructed_params, janetc_farslot(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile destructed params */
|
|
|
|
int32_t j = 0;
|
|
|
|
for (i = 0; i < paramcount; i++) {
|
|
|
|
Janet param = params[i];
|
|
|
|
if (!janet_checktype(param, JANET_SYMBOL)) {
|
|
|
|
JanetSlot reg = destructed_params[j++];
|
|
|
|
destructure(c, param, reg, defleaf, NULL);
|
|
|
|
janetc_freeslot(c, reg);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 16:44:13 +00:00
|
|
|
janet_v_free(destructed_params);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2019-03-12 04:23:14 +00:00
|
|
|
max_arity = (vararg || allow_extra) ? INT32_MAX : arity;
|
|
|
|
if (!seenopt) min_arity = arity;
|
|
|
|
|
2018-01-19 17:37:37 +00:00
|
|
|
/* Check for self ref */
|
|
|
|
if (selfref) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot slot = janetc_farslot(c);
|
|
|
|
slot.flags = JANET_SLOT_NAMED | JANET_FUNCTION;
|
|
|
|
janetc_emit_s(c, JOP_LOAD_SELF, slot, 1);
|
|
|
|
janetc_nameslot(c, janet_unwrap_symbol(head), slot);
|
2018-01-19 17:37:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile function body */
|
2018-03-12 06:06:51 +00:00
|
|
|
if (parami + 1 == argn) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_emit(c, JOP_RETURN_NIL);
|
2019-03-12 04:23:14 +00:00
|
|
|
} else {
|
|
|
|
for (argi = parami + 1; argi < argn; argi++) {
|
2019-02-20 01:51:34 +00:00
|
|
|
subopts.flags = (argi == (argn - 1)) ? JANET_FOPTS_TAIL : JANET_FOPTS_DROP;
|
|
|
|
janetc_value(subopts, argv[argi]);
|
|
|
|
if (c->result.status == JANET_COMPILE_ERROR)
|
|
|
|
goto error2;
|
|
|
|
}
|
2019-03-12 04:23:14 +00:00
|
|
|
}
|
2018-03-23 13:18:04 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Build function */
|
2018-09-06 02:18:42 +00:00
|
|
|
def = janetc_pop_funcdef(c);
|
2018-01-12 15:41:27 +00:00
|
|
|
def->arity = arity;
|
2019-03-12 04:23:14 +00:00
|
|
|
def->min_arity = min_arity;
|
|
|
|
def->max_arity = max_arity;
|
2018-11-25 19:03:00 +00:00
|
|
|
if (vararg) def->flags |= JANET_FUNCDEF_FLAG_VARARG;
|
2019-05-24 21:03:22 +00:00
|
|
|
if (structarg) def->flags |= JANET_FUNCDEF_FLAG_STRUCTARG;
|
2018-08-03 17:41:44 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
if (selfref) def->name = janet_unwrap_symbol(head);
|
2020-07-04 01:41:55 +00:00
|
|
|
janet_def_addflags(def);
|
2018-09-06 02:18:42 +00:00
|
|
|
defindex = janetc_addfuncdef(c, def);
|
2018-01-12 15:41:27 +00:00
|
|
|
|
2018-01-12 21:25:24 +00:00
|
|
|
/* Ensure enough slots for vararg function. */
|
2018-11-25 19:03:00 +00:00
|
|
|
if (arity + vararg > def->slotcount) def->slotcount = arity + vararg;
|
2018-01-12 21:25:24 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Instantiate closure */
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janetc_gettarget(opts);
|
|
|
|
janetc_emit_su(c, JOP_CLOSURE, ret, defindex, 1);
|
2018-01-12 15:41:27 +00:00
|
|
|
return ret;
|
2018-07-01 15:52:15 +00:00
|
|
|
|
|
|
|
error:
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_cerror(c, errmsg);
|
2018-07-01 15:52:15 +00:00
|
|
|
error2:
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_popscope(c);
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:18:04 +00:00
|
|
|
/* Keep in lexicographic order */
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetSpecial janetc_specials[] = {
|
2019-03-09 21:47:05 +00:00
|
|
|
{"break", janetc_break},
|
2018-09-06 02:18:42 +00:00
|
|
|
{"def", janetc_def},
|
|
|
|
{"do", janetc_do},
|
|
|
|
{"fn", janetc_fn},
|
|
|
|
{"if", janetc_if},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"quasiquote", janetc_quasiquote},
|
2018-09-06 02:18:42 +00:00
|
|
|
{"quote", janetc_quote},
|
2018-12-17 02:57:32 +00:00
|
|
|
{"set", janetc_varset},
|
2018-12-05 20:10:04 +00:00
|
|
|
{"splice", janetc_splice},
|
2019-12-05 01:53:13 +00:00
|
|
|
{"unquote", janetc_unquote},
|
2020-11-16 22:41:27 +00:00
|
|
|
{"upscope", janetc_upscope},
|
2018-09-06 02:18:42 +00:00
|
|
|
{"var", janetc_var},
|
|
|
|
{"while", janetc_while}
|
2018-01-12 15:41:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Find a special */
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetSpecial *janetc_special(const uint8_t *name) {
|
|
|
|
return janet_strbinsearch(
|
2019-02-20 01:51:34 +00:00
|
|
|
&janetc_specials,
|
|
|
|
sizeof(janetc_specials) / sizeof(JanetSpecial),
|
|
|
|
sizeof(JanetSpecial),
|
|
|
|
name);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|