1
0
mirror of https://github.com/janet-lang/janet synced 2025-11-01 08:03:02 +00:00

Add new tests. Add recursion guard to compile, serialize, and

deserialize.
This commit is contained in:
Calvin Rose
2017-06-28 22:51:24 -04:00
parent 9d66d85778
commit 7e46ead2f4
8 changed files with 138 additions and 151 deletions

View File

@@ -5,9 +5,7 @@
(print _ )
"Comment"
(do
(: i 0)
(while (< i 1000)
(print i)
(: i (+ i 1)))
)
(var i 0)
(while (< i 1000)
(print i)
(varset i (+ i 1)))

View File

@@ -1,65 +0,0 @@
(do
(: pp nil)
"Pretty print an array or tuple"
(: print-seq (fn [start end a seen]
(: seen (if seen seen {}))
(if (get seen a) (get seen a)
(do
(set! seen a "<cycle>")
(: parts [])
(: len (length a))
(: i 0)
(while (< i len)
(push! parts (pp (get a i) seen))
(push! parts " ")
(: i (+ 1 i)))
(if (> len 0) (pop! parts))
(push! parts end)
(: ret (apply string start parts))
(set! seen a ret)
ret))))
"Pretty print an object or struct"
(: print-struct (fn [start end s seen]
(: seen (if seen seen {}))
(if (get seen s) (get seen s)
(do
(set! seen s "<cycle>")
(: parts [])
(: key (next s))
(while (not (= key nil))
(push! parts (pp key seen))
(push! parts " ")
(push! parts (pp (get s key) seen))
(push! parts " ")
(: key (next s key)))
(if (> (length parts) 0) (pop! parts))
(push! parts end)
(: ret (apply string start parts))
(set! seen s ret)
ret))))
"Type handlers"
(: handlers {
"array" (fn [a seen] (print-seq "[" "]" a seen))
"tuple" (fn [a seen] (print-seq "(" ")" a seen))
"table" (fn [s seen] (print-struct "{" "}" s seen))
"struct" (fn [s seen] (print-struct "#{" "}" s seen))
})
"Define pretty print"
(: pp (fn [x seen]
(: handler (get handlers (type x)))
(: handler (if handler handler tostring))
(handler x seen)))
"Export pretty print"
(export! "pp" pp)
(: arr [1 2 3 4])
(push! arr arr)
(print (pp arr))
)

View File

@@ -1,22 +0,0 @@
(export! "scheck" (fn [x]
(: dat (serialize x))
(: deser (deserialize dat))
(print (debugp deser))
deser
))
(scheck 1)
(scheck true)
(scheck nil)
(scheck "asdasdasd")
(scheck (struct 1 2 3 4))
(scheck (tuple 1 2 3))
(scheck 123412.12)
(scheck (funcdef (fn [] 1)))
(scheck (funcenv (fn [] 1)))
(do
(: producer (fn [a] (fn [] a)))
(: f (producer "hello!"))
(scheck (funcenv f))
)
(scheck (fn [] 1))