1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

Better inline for put. Better printing for named functions.

This commit is contained in:
Calvin Rose 2018-07-10 20:01:39 -04:00
parent 5d290a67bf
commit a1bdc3a023
5 changed files with 44 additions and 14 deletions

View File

@ -529,6 +529,9 @@ static DstAssembleResult dst_asm1(DstAssembler *parent, Dst source, int flags) {
/* Check for function name */ /* Check for function name */
a.name = dst_get(s, dst_csymbolv("name")); a.name = dst_get(s, dst_csymbolv("name"));
if (!dst_checktype(a.name, DST_NIL)) {
def->name = dst_to_string(a.name);
}
/* Set function arity */ /* Set function arity */
x = dst_get(s, dst_csymbolv("arity")); x = dst_get(s, dst_csymbolv("arity"));
@ -832,6 +835,9 @@ Dst dst_disasm(DstFuncDef *def) {
if (def->flags & DST_FUNCDEF_FLAG_VARARG) { if (def->flags & DST_FUNCDEF_FLAG_VARARG) {
dst_table_put(ret, dst_csymbolv("vararg"), dst_wrap_true()); dst_table_put(ret, dst_csymbolv("vararg"), dst_wrap_true());
} }
if (NULL != def->name) {
dst_table_put(ret, dst_csymbolv("name"), dst_wrap_string(def->name));
}
/* Add constants */ /* Add constants */
if (def->constants_length > 0) { if (def->constants_length > 0) {

View File

@ -36,6 +36,9 @@ static int fixarity1(DstFopts opts, DstSlot *args) {
static int fixarity2(DstFopts opts, DstSlot *args) { static int fixarity2(DstFopts opts, DstSlot *args) {
(void) opts; (void) opts;
return dst_v_count(args) == 2; return dst_v_count(args) == 2;
}static int fixarity3(DstFopts opts, DstSlot *args) {
(void) opts;
return dst_v_count(args) == 3;
} }
/* Generic hanldling for $A = op $B */ /* Generic hanldling for $A = op $B */
@ -91,7 +94,8 @@ static DstSlot do_get(DstFopts opts, DstSlot *args) {
return opreduce(opts, args, DOP_GET, dst_wrap_nil()); return opreduce(opts, args, DOP_GET, dst_wrap_nil());
} }
static DstSlot do_put(DstFopts opts, DstSlot *args) { static DstSlot do_put(DstFopts opts, DstSlot *args) {
return opreduce(opts, args, DOP_PUT, dst_wrap_nil()); dstc_emit_sss(opts.compiler, DOP_PUT, args[0], args[1], args[2], 0);
return args[0];
} }
static DstSlot do_length(DstFopts opts, DstSlot *args) { static DstSlot do_length(DstFopts opts, DstSlot *args) {
return genericSS(opts, DOP_LENGTH, args[0]); return genericSS(opts, DOP_LENGTH, args[0]);
@ -238,7 +242,7 @@ static const DstFunOptimizer optimizers[] = {
{fixarity1, do_yield}, {fixarity1, do_yield},
{fixarity2, do_resume}, {fixarity2, do_resume},
{fixarity2, do_get}, {fixarity2, do_get},
{fixarity2, do_put}, {fixarity3, do_put},
{fixarity1, do_length}, {fixarity1, do_length},
{NULL, do_add}, {NULL, do_add},
{NULL, do_sub}, {NULL, do_sub},

View File

@ -490,14 +490,14 @@
(def [i1 i2 i3 i4] inds) (def [i1 i2 i3 i4] inds)
(def res (array.new limit)) (def res (array.new limit))
(case ninds (case ninds
1 (loop [i :range [0 limit]] (array.push res (f (get i1 i)))) 1 (loop [i :range [0 limit]] (put res i (f (get i1 i))))
2 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i)))) 2 (loop [i :range [0 limit]] (put res i (f (get i1 i) (get i2 i))))
3 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i) (get i3 i)))) 3 (loop [i :range [0 limit]] (put res i (f (get i1 i) (get i2 i) (get i3 i))))
4 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i) (get i3 i) (get i4 i)))) 4 (loop [i :range [0 limit]] (put res i (f (get i1 i) (get i2 i) (get i3 i) (get i4 i))))
(loop [i :range [0 limit]] (loop [i :range [0 limit]]
(def args (array.new ninds)) (def args (array.new ninds))
(loop [j :range [0 ninds]] (array.push args (get (get inds j) i))) (loop [j :range [0 ninds]] (put args j (get (get inds j) i)))
(array.push res (apply1 f args)))) (put res i (apply1 f args))))
res) res)
(defn each (defn each

View File

@ -154,10 +154,9 @@ static int dst_io_popen(DstArgs args) {
} }
flags = (fmode[0] == 'r') ? IO_PIPED | IO_READ : IO_PIPED | IO_WRITE; flags = (fmode[0] == 'r') ? IO_PIPED | IO_READ : IO_PIPED | IO_WRITE;
#ifdef DST_WINDOWS #ifdef DST_WINDOWS
f = _popen((const char *)fname, (const char *)fmode); #define popen _popen
#else
f = popen((const char *)fname, (const char *)fmode);
#endif #endif
f = popen((const char *)fname, (const char *)fmode);
if (!f) { if (!f) {
if (errno == EMFILE) { if (errno == EMFILE) {
DST_THROW(args, "too many streams are open"); DST_THROW(args, "too many streams are open");
@ -317,10 +316,9 @@ static int dst_io_fclose(DstArgs args) {
DST_THROW(args, "file not closable"); DST_THROW(args, "file not closable");
if (iof->flags & IO_PIPED) { if (iof->flags & IO_PIPED) {
#ifdef DST_WINDOWS #ifdef DST_WINDOWS
if (_pclose(iof->file)) DST_THROW(args, "could not close file"); #define pclose _pclose
#else
if (pclose(iof->file)) DST_THROW(args, "could not close file");
#endif #endif
if (pclose(iof->file)) DST_THROW(args, "could not close file");
} else { } else {
if (fclose(iof->file)) DST_THROW(args, "could not close file"); if (fclose(iof->file)) DST_THROW(args, "could not close file");
} }

View File

@ -355,6 +355,19 @@ void dst_description_b(DstBuffer *buffer, Dst x) {
} }
goto fallthrough; goto fallthrough;
} }
case DST_FUNCTION:
{
DstFunction *fun = dst_unwrap_function(x);
DstFuncDef *def = fun->def;
if (def->name) {
const uint8_t *n = def->name;
dst_buffer_push_cstring(buffer, "<function ");
dst_buffer_push_bytes(buffer, n, dst_string_length(n));
dst_buffer_push_u8(buffer, '>');
break;
}
goto fallthrough;
}
fallthrough: fallthrough:
default: default:
string_description_b(buffer, dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x)); string_description_b(buffer, dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x));
@ -422,6 +435,15 @@ const uint8_t *dst_description(Dst x) {
} }
goto fallthrough; goto fallthrough;
} }
case DST_FUNCTION:
{
DstFunction *fun = dst_unwrap_function(x);
DstFuncDef *def = fun->def;
if (def->name) {
return dst_formatc("<function %S>", def->name);
}
goto fallthrough;
}
fallthrough: fallthrough:
default: default:
return string_description(dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x)); return string_description(dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x));