diff --git a/src/core/asm.c b/src/core/asm.c index da206d0d..4ec800a3 100644 --- a/src/core/asm.c +++ b/src/core/asm.c @@ -529,6 +529,9 @@ static DstAssembleResult dst_asm1(DstAssembler *parent, Dst source, int flags) { /* Check for function 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 */ x = dst_get(s, dst_csymbolv("arity")); @@ -832,6 +835,9 @@ Dst dst_disasm(DstFuncDef *def) { if (def->flags & DST_FUNCDEF_FLAG_VARARG) { 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 */ if (def->constants_length > 0) { diff --git a/src/core/cfuns.c b/src/core/cfuns.c index ae1ba1f5..a22160bd 100644 --- a/src/core/cfuns.c +++ b/src/core/cfuns.c @@ -36,6 +36,9 @@ static int fixarity1(DstFopts opts, DstSlot *args) { static int fixarity2(DstFopts opts, DstSlot *args) { (void) opts; 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 */ @@ -91,7 +94,8 @@ static DstSlot do_get(DstFopts opts, DstSlot *args) { return opreduce(opts, args, DOP_GET, dst_wrap_nil()); } 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) { return genericSS(opts, DOP_LENGTH, args[0]); @@ -238,7 +242,7 @@ static const DstFunOptimizer optimizers[] = { {fixarity1, do_yield}, {fixarity2, do_resume}, {fixarity2, do_get}, - {fixarity2, do_put}, + {fixarity3, do_put}, {fixarity1, do_length}, {NULL, do_add}, {NULL, do_sub}, diff --git a/src/core/core.dst b/src/core/core.dst index dbd81e56..183aae37 100644 --- a/src/core/core.dst +++ b/src/core/core.dst @@ -490,14 +490,14 @@ (def [i1 i2 i3 i4] inds) (def res (array.new limit)) (case ninds - 1 (loop [i :range [0 limit]] (array.push res (f (get i1 i)))) - 2 (loop [i :range [0 limit]] (array.push res (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)))) - 4 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i) (get i3 i) (get i4 i)))) + 1 (loop [i :range [0 limit]] (put res i (f (get i1 i)))) + 2 (loop [i :range [0 limit]] (put res i (f (get i1 i) (get i2 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]] (put res i (f (get i1 i) (get i2 i) (get i3 i) (get i4 i)))) (loop [i :range [0 limit]] (def args (array.new ninds)) - (loop [j :range [0 ninds]] (array.push args (get (get inds j) i))) - (array.push res (apply1 f args)))) + (loop [j :range [0 ninds]] (put args j (get (get inds j) i))) + (put res i (apply1 f args)))) res) (defn each diff --git a/src/core/io.c b/src/core/io.c index d2e7a671..3e44deec 100644 --- a/src/core/io.c +++ b/src/core/io.c @@ -154,10 +154,9 @@ static int dst_io_popen(DstArgs args) { } flags = (fmode[0] == 'r') ? IO_PIPED | IO_READ : IO_PIPED | IO_WRITE; #ifdef DST_WINDOWS - f = _popen((const char *)fname, (const char *)fmode); -#else - f = popen((const char *)fname, (const char *)fmode); +#define popen _popen #endif + f = popen((const char *)fname, (const char *)fmode); if (!f) { if (errno == EMFILE) { 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"); if (iof->flags & IO_PIPED) { #ifdef DST_WINDOWS - if (_pclose(iof->file)) DST_THROW(args, "could not close file"); -#else - if (pclose(iof->file)) DST_THROW(args, "could not close file"); +#define pclose _pclose #endif + if (pclose(iof->file)) DST_THROW(args, "could not close file"); } else { if (fclose(iof->file)) DST_THROW(args, "could not close file"); } diff --git a/src/core/string.c b/src/core/string.c index 0d272644..882267de 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -355,6 +355,19 @@ void dst_description_b(DstBuffer *buffer, Dst x) { } 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, "'); + break; + } + goto fallthrough; + } fallthrough: default: 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; } + case DST_FUNCTION: + { + DstFunction *fun = dst_unwrap_function(x); + DstFuncDef *def = fun->def; + if (def->name) { + return dst_formatc("", def->name); + } + goto fallthrough; + } fallthrough: default: return string_description(dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x));