1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Properly handle recursion with labels.

Use an empty buffer, which has pointer equality semantics, for
tag from a label.
This commit is contained in:
Calvin Rose 2020-02-23 17:35:01 -06:00
parent 05bd5767de
commit 734c85d7ef
2 changed files with 11 additions and 2 deletions

View File

@ -317,7 +317,7 @@
that will be bound to the label." that will be bound to the label."
[name & body] [name & body]
~(do ~(do
(def ,name ',(gensym)) (def ,name @"")
,(apply prompt name body))) ,(apply prompt name body)))
(defn return (defn return

View File

@ -70,7 +70,16 @@
(test-bf "+[+[<<<+>>>>]+<-<-<<<+<++]<<.<++.<++..+++.<<++.<---.>>.>.+++.------.>-.>>--." (test-bf "+[+[<<<+>>>>]+<-<-<<<+<++]<<.<++.<++..+++.<<++.<---.>>.>.+++.------.>-.>>--."
"Hello, World!") "Hello, World!")
# Prompts # Prompts and Labels
(assert (= 10 (label a (for i 0 10 (if (= i 5) (return a 10))))) "label 1")
(defn recur
[lab x y]
(when (= x y) (return lab :done))
(def res (label newlab (recur (or lab newlab) (+ x 1) y)))
(if lab :oops res))
(assert (= :done (recur nil 0 10)) "label 2")
(assert (= 10 (prompt :a (for i 0 10 (if (= i 5) (return :a 10))))) "prompt 1") (assert (= 10 (prompt :a (for i 0 10 (if (= i 5) (return :a 10))))) "prompt 1")