1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-19 09:47:40 +00:00

Change syntax for namespaces.

Add quasiquote, unquote, and unquote-splicing
as specials rather than a macro.
This commit is contained in:
Calvin Rose
2018-11-30 22:49:21 -05:00
parent 25c50f5026
commit 4e4dd31164
28 changed files with 678 additions and 609 deletions

View File

@@ -19,4 +19,4 @@
(defn end-suite []
(print "\nTest suite " suite-num " finished.")
(print num-tests-passed " of " num-tests-run " tests passed.\n")
(if (not= num-tests-passed num-tests-run) (os.exit 1)))
(if (not= num-tests-passed num-tests-run) (os/exit 1)))

View File

@@ -18,7 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
(import test.helper :prefix "" :exit true)
(import test/helper :prefix "" :exit true)
(start-suite 0)
(assert (= 10 (+ 1 2 3 4)) "addition")
@@ -38,7 +38,7 @@
(assert (= -7 (% -20 13)) "modulo 2")
(assert (order< nil false true
(fiber.new (fn [] 1))
(fiber/new (fn [] 1))
1 1.0 "hi"
(quote hello)
(array 1 2 3)
@@ -81,15 +81,15 @@
# Mcarthy's 91 function
(var f91 nil)
(:= f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11))))))
(assert (= 91 (f91 10)), "f91(10) = 91")
(assert (= 91 (f91 11)), "f91(11) = 91")
(assert (= 91 (f91 20)), "f91(20) = 91")
(assert (= 91 (f91 31)), "f91(31) = 91")
(assert (= 91 (f91 100)), "f91(100) = 91")
(assert (= 91 (f91 101)), "f91(101) = 91")
(assert (= 92 (f91 102)), "f91(102) = 92")
(assert (= 93 (f91 103)), "f91(103) = 93")
(assert (= 94 (f91 104)), "f91(104) = 94")
(assert (= 91 (f91 10)) "f91(10) = 91")
(assert (= 91 (f91 11)) "f91(11) = 91")
(assert (= 91 (f91 20)) "f91(20) = 91")
(assert (= 91 (f91 31)) "f91(31) = 91")
(assert (= 91 (f91 100)) "f91(100) = 91")
(assert (= 91 (f91 101)) "f91(101) = 91")
(assert (= 92 (f91 102)) "f91(102) = 92")
(assert (= 93 (f91 103)) "f91(103) = 93")
(assert (= 94 (f91 104)) "f91(104) = 94")
# Fibonacci
(def fib (do (var fib nil) (:= fib (fn [n] (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))))
@@ -154,7 +154,7 @@
# Fiber tests
(def afiber (fiber.new (fn []
(def afiber (fiber/new (fn []
(def x (yield))
(error (string "hello, " x))) :ye))
@@ -162,16 +162,16 @@
(def afiber-result (resume afiber "world!"))
(assert (= afiber-result "hello, world!") "fiber error result")
(assert (= (fiber.status afiber) :error) "fiber error status")
(assert (= (fiber/status afiber) :error) "fiber error status")
# yield tests
(def t (fiber.new (fn [&] (yield 1) (yield 2) 3)))
(def t (fiber/new (fn [&] (yield 1) (yield 2) 3)))
(assert (= 1 (resume t)) "initial transfer to new fiber")
(assert (= 2 (resume t)) "second transfer to fiber")
(assert (= 3 (resume t)) "return from fiber")
(assert (= (fiber.status t) :dead) "finished fiber is dead")
(assert (= (fiber/status t) :dead) "finished fiber is dead")
# Var arg tests
@@ -215,7 +215,7 @@
# Merge sort
# Imperative merge sort merge
# Imperative (and verbose) merge sort merge
(defn merge
[xs ys]
(def ret @[])
@@ -228,17 +228,17 @@
(def xi (get xs i))
(def yj (get ys j))
(if (< xi yj)
(do (array.push ret xi) (:= i (+ i 1)))
(do (array.push ret yj) (:= j (+ j 1)))))
(do (array/push ret xi) (:= i (+ i 1)))
(do (array/push ret yj) (:= j (+ j 1)))))
# Push rest of xs
(while (< i xlen)
(def xi (get xs i))
(array.push ret xi)
(array/push ret xi)
(:= i (+ i 1)))
# Push rest of ys
(while (< j ylen)
(def yj (get ys j))
(array.push ret yj)
(array/push ret yj)
(:= j (+ j 1)))
ret)
@@ -260,9 +260,9 @@
# Let
(assert (= (let [a 1 b 2] (+ a b)) 3), "simple let")
(assert (= (let [[a b] @[1 2]] (+ a b)) 3), "destructured let")
(assert (= (let [[a [c d] b] @[1 (tuple 4 3) 2]] (+ a b c d)) 10), "double destructured let")
(assert (= (let [a 1 b 2] (+ a b)) 3) "simple let")
(assert (= (let [[a b] @[1 2]] (+ a b)) 3) "destructured let")
(assert (= (let [[a [c d] b] @[1 (tuple 4 3) 2]] (+ a b c d)) 10) "double destructured let")
# Macros

View File

@@ -18,11 +18,11 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
(import test.helper :prefix "" :exit true)
(import test/helper :prefix "" :exit true)
(start-suite 1)
(assert (= 400.0 (math.sqrt 160000)) "sqrt(160000)=400")
(assert (= (real 400) (math.sqrt 160000)) "sqrt(160000)=400")
(assert (= 400.0 (math/sqrt 160000)) "sqrt(160000)=400")
(assert (= (real 400) (math/sqrt 160000)) "sqrt(160000)=400")
(def test-struct {'def 1 'bork 2 'sam 3 'a 'b 'het @[1 2 3 4 5]})
(assert (= (get test-struct 'def) 1) "struct get")
@@ -47,7 +47,7 @@
(:= good false)))
(assert good e))
(assert-many (fn [] (>= 1 (math.random) 0)) 200 "(random) between 0 and 1")
(assert-many (fn [] (>= 1 (math/random) 0)) 200 "(random) between 0 and 1")
## Table prototypes
@@ -59,7 +59,7 @@
:childprop 456
})
(table.setproto childtab roottab)
(table/setproto childtab roottab)
(assert (= 123 (get roottab :parentprop)) "table get 1")
(assert (= 123 (get childtab :parentprop)) "table get proto")
@@ -70,7 +70,7 @@
(assert (= "hello, world" `hello, world`) "simple long string")
(assert (= "hello, \"world\"" `hello, "world"`) "long string with embedded quotes")
(assert (= "hello, \\\\\\ \"world\"" `hello, \\\ "world"`),
(assert (= "hello, \\\\\\ \"world\"" `hello, \\\ "world"`)
"long string with embedded quotes and backslashes")
# More fiber semantics
@@ -78,19 +78,19 @@
(var myvar 0)
(defn fiberstuff [&]
(++ myvar)
(def f (fiber.new (fn [&] (++ myvar) (debug) (++ myvar))))
(def f (fiber/new (fn [&] (++ myvar) (debug) (++ myvar))))
(resume f)
(++ myvar))
(def myfiber (fiber.new fiberstuff :dey))
(def myfiber (fiber/new fiberstuff :dey))
(assert (= myvar 0) "fiber creation does not call fiber function")
(resume myfiber)
(assert (= myvar 2) "fiber debug statement breaks at proper point")
(assert (= (fiber.status myfiber) :debug) "fiber enters debug state")
(assert (= (fiber/status myfiber) :debug) "fiber enters debug state")
(resume myfiber)
(assert (= myvar 4) "fiber resumes properly from debug state")
(assert (= (fiber.status myfiber) :dead) "fiber properly dies from debug state")
(assert (= (fiber/status myfiber) :dead) "fiber properly dies from debug state")
# Test max triangle program
@@ -98,8 +98,8 @@
# of the triangle to the leaves of the triangle.
(defn myfold [xs ys]
(let [xs1 (tuple.prepend xs 0)
xs2 (tuple.append xs 0)
(let [xs1 (tuple/prepend xs 0)
xs2 (tuple/append xs 0)
m1 (map + xs1 ys)
m2 (map + xs2 ys)]
(map max m1 m2)))
@@ -119,12 +119,12 @@
(assert (= (maxpath triangle) 25) `max triangle`)
(assert (= (string.join @["one" "two" "three"]) "onetwothree") "string.join 1 argument")
(assert (= (string.join @["one" "two" "three"] ", ") "one, two, three") "string.join 2 arguments")
(assert (= (string.join @[] ", ") "") "string.join empty array")
(assert (= (string/join @["one" "two" "three"]) "onetwothree") "string/join 1 argument")
(assert (= (string/join @["one" "two" "three"] ", ") "one, two, three") "string/join 2 arguments")
(assert (= (string/join @[] ", ") "") "string/join empty array")
(assert (= (string.find "123" "abc123def") 3) "string.find positive")
(assert (= (string.find "1234" "abc123def") nil) "string.find negative")
(assert (= (string/find "123" "abc123def") 3) "string/find positive")
(assert (= (string/find "1234" "abc123def") nil) "string/find negative")
# Test destructuring
(do
@@ -169,13 +169,13 @@
(testmarsh (fn thing [x] (+ 11 x x 30)) "marshal function 3")
(testmarsh map "marshal function 4")
(testmarsh reduce "marshal function 5")
(testmarsh (fiber.new (fn [] (yield 1) 2)) "marshal simple fiber 1")
(testmarsh (fiber.new (fn [&] (yield 1) 2)) "marshal simple fiber 2")
(testmarsh (fiber/new (fn [] (yield 1) 2)) "marshal simple fiber 1")
(testmarsh (fiber/new (fn [&] (yield 1) 2)) "marshal simple fiber 2")
# Large functions
(def manydefs (seq [i :range [0 300]] (tuple 'def (gensym) (string "value_" i))))
(array.push manydefs (tuple * 10000 3 5 7 9))
(def f (compile (tuple.prepend manydefs 'do) *env*))
(array/push manydefs (tuple * 10000 3 5 7 9))
(def f (compile (tuple/prepend manydefs 'do) *env*))
(assert (= (f) (* 10000 3 5 7 9)) "long function compilation")
# Some higher order functions and macros
@@ -201,9 +201,9 @@
6 :six
7 :seven
8 :eight
9 :nine)), "case macro")
9 :nine)) "case macro")
(assert (= 7 (case :a :b 5 :c 6 :u 10 7)), "case with default")
(assert (= 7 (case :a :b 5 :c 6 :u 10 7)) "case with default")
# Testing the loop and for macros
(def xs (apply tuple (seq [x :range [0 10] :when (even? x)] (tuple (/ x 2) x))))
@@ -215,11 +215,11 @@
# Closure in while loop
(def closures (seq [i :range [0 5]] (fn [] i)))
(assert (= 0 ((get closures 0))) "closure in loop 0")
(assert (= 1 ((get closures 1))) "closure in loop 1")
(assert (= 2 ((get closures 2))) "closure in loop 2")
(assert (= 3 ((get closures 3))) "closure in loop 3")
(assert (= 4 ((get closures 4))) "closure in loop 4")
(assert (= 0 (closures.0)) "closure in loop 0")
(assert (= 1 (closures.1)) "closure in loop 1")
(assert (= 2 (closures.2)) "closure in loop 2")
(assert (= 3 (closures.3)) "closure in loop 3")
(assert (= 4 (closures.4)) "closure in loop 4")
# More numerical tests
(assert (== 1 1.0) "numerical equal 1")
@@ -237,12 +237,12 @@
(= (apply tuple a) (apply tuple b))))
(assert (= (apply tuple @[1 2 3 4 5]) (tuple 1 2 3 4 5)) "array to tuple")
(def arr (array))
(array.push arr :hello)
(array.push arr :world)
(array/push arr :hello)
(array/push arr :world)
(assert (array= arr @[:hello :world]) "array comparision")
(assert (array= @[1 2 3 4 5] @[1 2 3 4 5]) "array comparison 2")
(assert (array= @[:one :two :three :four :five] @[:one :two :three :four :five]) "array comparison 3")
(assert (array= (array.slice @[1 2 3] 0 2) @[1 2]) "array.slice 1")
(assert (array= (array.slice @[0 7 3 9 1 4] 2 -2) @[3 9 1]) "array.slice 2")
(assert (array= (array/slice @[1 2 3] 0 2) @[1 2]) "array/slice 1")
(assert (array= (array/slice @[0 7 3 9 1 4] 2 -2) @[3 9 1]) "array/slice 2")
(end-suite)

View File

@@ -18,7 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
(import test.helper :prefix "" :exit true)
(import test/helper :prefix "" :exit true)
(start-suite 2)
# Buffer stuff
@@ -41,7 +41,7 @@
# Looping idea
(def xs
(seq [x :in '[-1 0 1], y :in '[-1 0 1] :when (not= x y 0)] (tuple x y)))
(seq [x :in '[-1 0 1] y :in '[-1 0 1] :when (not= x y 0)] (tuple x y)))
(def txs (apply tuple xs))
(assert (= txs '[[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]) "nested seq")
@@ -61,26 +61,26 @@
(assert (= X1 100) "X1 as symbol")
# String functions
(assert (= 3 (string.find "abc" " abcdefghijklmnop")) "string.find 1")
(assert (= nil (string.find "" "")) "string.find 2")
(assert (= 0 (string.find "A" "A")) "string.find 3")
(assert (= (string.replace "X" "." "XXX...XXX...XXX") ".XX...XXX...XXX") "string.replace 1")
(assert (= (string.replace-all "X" "." "XXX...XXX...XXX") "...............") "string.replace-all 1")
(assert (= (string.replace-all "XX" "." "XXX...XXX...XXX") ".X....X....X") "string.replace-all 2")
(assert (= (string.ascii-lower "ABCabc&^%!@:;.") "abcabc&^%!@:;.") "string.ascii-lower")
(assert (= (string.ascii-upper "ABCabc&^%!@:;.") "ABCABC&^%!@:;.") "string.ascii-lower")
(assert (= (string.reverse "") "") "string.reverse 1")
(assert (= (string.reverse "a") "a") "string.reverse 2")
(assert (= (string.reverse "abc") "cba") "string.reverse 3")
(assert (= (string.reverse "abcd") "dcba") "string.reverse 4")
(assert (= (string.join @["one" "two" "three"] ",") "one,two,three") "string.join 1")
(assert (= (string.join @["one" "two" "three"] ", ") "one, two, three") "string.join 2")
(assert (= (string.join @["one" "two" "three"]) "onetwothree") "string.join 3")
(assert (= (string.join @[] "hi") "") "string.join 4")
(assert (deep= (string.split "," "one,two,three") @["one" "two" "three"]) "string.split 1")
(assert (deep= (string.split "," "onetwothree") @["onetwothree"]) "string.split 2")
(assert (deep= (string.find-all "e" "onetwothree") @[2 9 10]) "string.find-all 1")
(assert (deep= (string.find-all "," "onetwothree") @[]) "string.find-all 2")
(assert (= 3 (string/find "abc" " abcdefghijklmnop")) "string/find 1")
(assert (= nil (string/find "" "")) "string/find 2")
(assert (= 0 (string/find "A" "A")) "string/find 3")
(assert (= (string/replace "X" "." "XXX...XXX...XXX") ".XX...XXX...XXX") "string/replace 1")
(assert (= (string/replace-all "X" "." "XXX...XXX...XXX") "...............") "string/replace-all 1")
(assert (= (string/replace-all "XX" "." "XXX...XXX...XXX") ".X....X....X") "string/replace-all 2")
(assert (= (string/ascii-lower "ABCabc&^%!@:;.") "abcabc&^%!@:;.") "string/ascii-lower")
(assert (= (string/ascii-upper "ABCabc&^%!@:;.") "ABCABC&^%!@:;.") "string/ascii-lower")
(assert (= (string/reverse "") "") "string/reverse 1")
(assert (= (string/reverse "a") "a") "string/reverse 2")
(assert (= (string/reverse "abc") "cba") "string/reverse 3")
(assert (= (string/reverse "abcd") "dcba") "string/reverse 4")
(assert (= (string/join @["one" "two" "three"] ",") "one,two,three") "string/join 1")
(assert (= (string/join @["one" "two" "three"] ", ") "one, two, three") "string/join 2")
(assert (= (string/join @["one" "two" "three"]) "onetwothree") "string/join 3")
(assert (= (string/join @[] "hi") "") "string/join 4")
(assert (deep= (string/split "," "one,two,three") @["one" "two" "three"]) "string/split 1")
(assert (deep= (string/split "," "onetwothree") @["onetwothree"]) "string/split 2")
(assert (deep= (string/find-all "e" "onetwothree") @[2 9 10]) "string/find-all 1")
(assert (deep= (string/find-all "," "onetwothree") @[]) "string/find-all 2")
# Check if abstract test works
(assert (abstract? stdout) "abstract? stdout")