1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-21 07:54:12 +00:00

Fix sourcemapping bug with closures, add some library functions

to boot.dst
This commit is contained in:
Calvin Rose 2018-06-29 19:44:33 -04:00
parent 11ced5b582
commit 1ea9ebf04f
3 changed files with 37 additions and 28 deletions

View File

@ -1,13 +1,14 @@
# An example file that errors out. Run with ./dst examples/error.dst # An example file that errors out. Run with ./dst examples/error.dst
# to see stack trace for runtime errors. # to see stack trace for runtime errors.
(defn bark [x]
(print "Woof!")
(print x)
(error x)
(print "Woof!"))
(defn bork [x] (defn bork [x]
(defn bark [x]
(print "Woof!")
(print x)
(error x)
(print "Woof!"))
(bark (* 2 x)) (bark (* 2 x))
(bark (* 3 x))) (bark (* 3 x)))

View File

@ -248,10 +248,10 @@ value."
(switch (switch
verb verb
:in-while (do :in-while (do
(def preds @['and (tuple := bindings object)]) (def preds @['and (tuple ':= bindings object)])
(def subloop (doone (+ i 3) preds)) (def subloop (doone (+ i 3) preds))
(tuple 'do (tuple 'do
(tuple 'var bindings) (tuple 'var bindings nil)
(tuple 'while (apply1 tuple preds) (tuple 'while (apply1 tuple preds)
subloop))) subloop)))
:range (do :range (do
@ -895,40 +895,50 @@ to call on any table. Does not print table prototype information."
m? (apply1 m (tuple.slice t 1)) m? (apply1 m (tuple.slice t 1))
(apply1 tuple (map macroexpand-1 t)))) (apply1 tuple (map macroexpand-1 t))))
(switch (type x) (def ret (switch (type x)
:tuple (dotup x) :tuple (dotup x)
:array (map macroexpand-1 x) :array (map macroexpand-1 x)
:struct (table.to-struct (dotable x macroexpand-1)) :struct (table.to-struct (dotable x macroexpand-1))
:table (dotable x macroexpand-1) :table (dotable x macroexpand-1)
x)) x))
ret)
(defn all? [& xs] (defn all? [xs]
(var good true) (var good true)
(loop [x :in xs :while good] (if x nil (:= good false))) (loop [x :in xs :while good] (if x nil (:= good false)))
good) good)
(defn some? [& xs] (defn some? [xs]
(var bad true) (var bad true)
(loop [x :in xs :while bad] (if x (:= bad false))) (loop [x :in xs :while bad] (if x (:= bad false)))
(not bad)) (not bad))
(defn deep-not= [x y]
"Like not=, but mutable types (arrays, tables, buffers) are considered
equal if they have identical structure. Much slower than not=."
(def tx (type x))
(or
(not= tx (type y))
(switch tx
:tuple (or (not= (length x) (length y)) (some? (map deep-not= x y)))
:array (or (not= (length x) (length y)) (some? (map deep-not= x y)))
:struct (deep-not= (pairs x) (pairs y))
:table (deep-not= (table.to-struct x) (table.to-struct y))
:buffer (not= (string x) (string y))
(not= x y))))
(defn deep= [x y]
"Like =, but mutable types (arrays, tables, buffers) are considered
equal if they have identical structure. Much slower than =."
(not (deep-not= x y)))
(defn macroexpand (defn macroexpand
"Expand macros completely." "Expand macros completely."
[x] [x]
(defn deep= [x y]
(def tx (type x))
(and
(= tx (type y))
(switch tx
:tuple (all? (map deep= x y))
:array (all? (map deep= x y))
:struct (deep= (pairs x) (pairs y))
:table (deep= (table.to-struct x) (table.to-struct y))
(= x y))))
(var previous x) (var previous x)
(var current (macroexpand-1 x)) (var current (macroexpand-1 x))
(var counter 0) (var counter 0)
(while (not (deep= current previous)) (while (deep-not= current previous)
(if (> (++ counter) 200) (if (> (++ counter) 200)
(error "macro expansion too nested")) (error "macro expansion too nested"))
(:= previous current) (:= previous current)

View File

@ -40,9 +40,10 @@ static void dstc_ast_push(DstCompiler *c, const Dst *tup) {
if (!mapping.line) { if (!mapping.line) {
/* Reuse previous mapping */ /* Reuse previous mapping */
mapping = c->current_mapping; mapping = c->current_mapping;
} else {
c->current_mapping = mapping;
} }
dst_v_push(c->ast_stack, mapping); dst_v_push(c->ast_stack, mapping);
c->current_mapping = mapping;
} }
static void dstc_ast_pop(DstCompiler *c) { static void dstc_ast_pop(DstCompiler *c) {
@ -927,15 +928,12 @@ DstFuncDef *dstc_pop_funcdef(DstCompiler *c) {
memcpy(def->bytecode, c->buffer + scope.bytecode_start, s); memcpy(def->bytecode, c->buffer + scope.bytecode_start, s);
dst_v__cnt(c->buffer) = scope.bytecode_start; dst_v__cnt(c->buffer) = scope.bytecode_start;
if (NULL != c->mapbuffer) { if (NULL != c->mapbuffer) {
int32_t i; size_t s = sizeof(DstSourceMapping) * def->bytecode_length;
size_t s = sizeof(DstSourceMapping) * dst_v_count(c->mapbuffer);
def->sourcemap = malloc(s); def->sourcemap = malloc(s);
if (NULL == def->sourcemap) { if (NULL == def->sourcemap) {
DST_OUT_OF_MEMORY; DST_OUT_OF_MEMORY;
} }
for (i = 0; i < dst_v_count(c->mapbuffer); i++) { memcpy(def->sourcemap, c->mapbuffer + scope.bytecode_start, s);
def->sourcemap[i] = c->mapbuffer[i];
}
dst_v__cnt(c->mapbuffer) = scope.bytecode_start; dst_v__cnt(c->mapbuffer) = scope.bytecode_start;
} }
} }