mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 00:10:27 +00:00
44e752d737
These are similar to the function literals from Clojure (also Fennel), and should make short functions for maps, filters, etc. easier to write.
160 lines
6.0 KiB
Clojure
160 lines
6.0 KiB
Clojure
# Copyright (c) 2019 Calvin Rose & contributors
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to
|
|
# deal in the Software without restriction, including without limitation the
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
# IN THE SOFTWARE.
|
|
|
|
(import ./helper :prefix "" :exit true)
|
|
(start-suite 7)
|
|
|
|
# Using a large test grammar
|
|
|
|
(def- core-env (table/getproto (fiber/getenv (fiber/current))))
|
|
(def- specials {'fn true
|
|
'var true
|
|
'do true
|
|
'while true
|
|
'def true
|
|
'splice true
|
|
'set true
|
|
'unquote true
|
|
'quasiquote true
|
|
'quote true
|
|
'if true})
|
|
|
|
(defn- check-number [text] (and (scan-number text) text))
|
|
|
|
(defn capture-sym
|
|
[text]
|
|
(def sym (symbol text))
|
|
[(if (or (core-env sym) (specials sym)) :coresym :symbol) text])
|
|
|
|
(def grammar
|
|
~{:ws (set " \v\t\r\f\n\0")
|
|
:readermac (set "';~,")
|
|
:symchars (+ (range "09" "AZ" "az" "\x80\xFF") (set "!$%&*+-./:<?=>@^_|"))
|
|
:token (some :symchars)
|
|
:hex (range "09" "af" "AF")
|
|
:escape (* "\\" (+ (set "ntrvzf0e\"\\")
|
|
(* "x" :hex :hex)
|
|
(error (constant "bad hex escape"))))
|
|
:comment (/ '(* "#" (any (if-not (+ "\n" -1) 1))) (constant :comment))
|
|
:symbol (/ ':token ,capture-sym)
|
|
:keyword (/ '(* ":" (any :symchars)) (constant :keyword))
|
|
:constant (/ '(+ "true" "false" "nil") (constant :constant))
|
|
:bytes (* "\"" (any (+ :escape (if-not "\"" 1))) "\"")
|
|
:string (/ ':bytes (constant :string))
|
|
:buffer (/ '(* "@" :bytes) (constant :string))
|
|
:long-bytes {:delim (some "`")
|
|
:open (capture :delim :n)
|
|
:close (cmt (* (not (> -1 "`")) (-> :n) ':delim) ,=)
|
|
:main (drop (* :open (any (if-not :close 1)) :close))}
|
|
:long-string (/ ':long-bytes (constant :string))
|
|
:long-buffer (/ '(* "@" :long-bytes) (constant :string))
|
|
:number (/ (cmt ':token ,check-number) (constant :number))
|
|
:raw-value (+ :comment :constant :number :keyword
|
|
:string :buffer :long-string :long-buffer
|
|
:parray :barray :ptuple :btuple :struct :dict :symbol)
|
|
:value (* (? '(some (+ :ws :readermac))) :raw-value '(any :ws))
|
|
:root (any :value)
|
|
:root2 (any (* :value :value))
|
|
:ptuple (* '"(" :root (+ '")" (error "")))
|
|
:btuple (* '"[" :root (+ '"]" (error "")))
|
|
:struct (* '"{" :root2 (+ '"}" (error "")))
|
|
:parray (* '"@" :ptuple)
|
|
:barray (* '"@" :btuple)
|
|
:dict (* '"@" :struct)
|
|
:main (+ :root (error ""))})
|
|
|
|
(def p (peg/compile grammar))
|
|
|
|
# Just make sure is valgrind clean.
|
|
(def p (-> p make-image load-image))
|
|
|
|
(assert (peg/match p "abc") "complex peg grammar 1")
|
|
(assert (peg/match p "[1 2 3 4]") "complex peg grammar 2")
|
|
|
|
#
|
|
# fn compilation special
|
|
#
|
|
(defn myfn1 [[x y z] & more]
|
|
more)
|
|
(defn myfn2 [head & more]
|
|
more)
|
|
(assert (= (myfn1 [1 2 3] 4 5 6) (myfn2 [:a :b :c] 4 5 6)) "destructuring and varargs")
|
|
|
|
#
|
|
# Test propagation of signals via fibers
|
|
#
|
|
|
|
(def f (fiber/new (fn [] (error :abc) 1) :ei))
|
|
(def res (resume f))
|
|
(assert-error :abc (propagate res f) "propagate 1")
|
|
|
|
# table/clone
|
|
|
|
(defn check-table-clone [x msg]
|
|
(assert (= (table/to-struct x) (table/to-struct (table/clone x))) msg))
|
|
|
|
(check-table-clone @{:a 123 :b 34 :c :hello : 945 0 1 2 3 4 5} "table/clone 1")
|
|
(check-table-clone @{} "table/clone 1")
|
|
|
|
# Issue #142
|
|
|
|
(def buffer (tarray/buffer 8))
|
|
(def buffer-float64-view (tarray/new :float64 1 1 0 buffer))
|
|
(def buffer-uint32-view (tarray/new :uint32 2 1 0 buffer))
|
|
|
|
(set (buffer-uint32-view 1) 0xfffe9234)
|
|
(set (buffer-uint32-view 0) 0x56789abc)
|
|
|
|
(assert (buffer-float64-view 0) "issue #142 nanbox hijack 1")
|
|
(assert (= (type (buffer-float64-view 0)) :number) "issue #142 nanbox hijack 2")
|
|
(assert (= (type (unmarshal @"\xC8\xbc\x9axV4\x92\xfe\xff")) :number) "issue #142 nanbox hijack 3")
|
|
|
|
# Make sure Carriage Returns don't end up in doc strings.
|
|
|
|
(assert (not (string/find "\r" (get ((fiber/getenv (fiber/current)) 'cond) :doc))) "no \\r in doc strings")
|
|
|
|
# module/expand-path regression
|
|
(with-dyns [:syspath ".janet/.janet"]
|
|
(assert (= (string (module/expand-path "hello" ":sys:/:all:.janet"))
|
|
".janet/.janet/hello.janet") "module/expand-path 1"))
|
|
|
|
# comp should be variadic
|
|
(assert (= 10 ((comp +) 1 2 3 4)) "variadic comp 1")
|
|
(assert (= 11 ((comp inc +) 1 2 3 4)) "variadic comp 2")
|
|
(assert (= 12 ((comp inc inc +) 1 2 3 4)) "variadic comp 3")
|
|
(assert (= 13 ((comp inc inc inc +) 1 2 3 4)) "variadic comp 4")
|
|
(assert (= 14 ((comp inc inc inc inc +) 1 2 3 4)) "variadic comp 5")
|
|
(assert (= 15 ((comp inc inc inc inc inc +) 1 2 3 4)) "variadic comp 6")
|
|
(assert (= 16 ((comp inc inc inc inc inc inc +) 1 2 3 4)) "variadic comp 7")
|
|
|
|
# Function shorthand
|
|
(assert (= (|(+ 1 2 3)) 6) "function shorthand 1")
|
|
(assert (= (|(+ 1 2 3 $) 4) 10) "function shorthand 2")
|
|
(assert (= (|(+ 1 2 3 $0) 4) 10) "function shorthand 3")
|
|
(assert (= (|(+ $0 $0 $0 $0) 4) 16) "function shorthand 4")
|
|
(assert (= (|(+ $ $ $ $) 4) 16) "function shorthand 5")
|
|
(assert (= (|4) 4) "function shorthand 6")
|
|
(assert (= (((|||4))) 4) "function shorthand 7")
|
|
(assert (= (|(+ $1 $1 $1 $1) 2 4) 16) "function shorthand 8")
|
|
(assert (= (|(+ $0 $1 $3 $2 $6) 0 1 2 3 4 5 6) 12) "function shorthand 9")
|
|
|
|
|
|
(end-suite)
|