1 | ;;;; slib-prec-test.scm -*- scheme -*- |
---|
2 | ;;;; Kon Lovett, Apr '20 |
---|
3 | |
---|
4 | (import test) |
---|
5 | |
---|
6 | (test-begin "SLIB Precedence Parser") |
---|
7 | |
---|
8 | (import slib-prec) |
---|
9 | |
---|
10 | ;; |
---|
11 | |
---|
12 | ;;; |
---|
13 | |
---|
14 | (import (chicken base)) |
---|
15 | (import utf8) |
---|
16 | (import utf8-srfi-13) |
---|
17 | |
---|
18 | ;; Operator Aliaes |
---|
19 | |
---|
20 | (define ^ expt) |
---|
21 | (define mod modulo) |
---|
22 | |
---|
23 | ;; |
---|
24 | |
---|
25 | ;defgrammar doesn't `define' |
---|
26 | (import slib-basic-grammars) |
---|
27 | (import slib-standard-grammar) |
---|
28 | (import slib-tex-grammar) |
---|
29 | |
---|
30 | ;init by id-# grammar lookup |
---|
31 | (define-constant TEST-GRAMMARS '(standard disp2d tex null scheme schemepretty)) |
---|
32 | (clear-grammar-ids) |
---|
33 | (grammar-id-name-setup! TEST-GRAMMARS) |
---|
34 | (active-grammar-set! 'standard #f 'schemepretty) |
---|
35 | |
---|
36 | ;; |
---|
37 | |
---|
38 | (test-group "Grammer IDs" |
---|
39 | (test (length TEST-GRAMMARS) (count-of-grammar-ids)) |
---|
40 | (test 'standard (grammar-id-name 0)) |
---|
41 | (test 'scheme (grammar-id-name 4)) |
---|
42 | ) |
---|
43 | |
---|
44 | ;; |
---|
45 | |
---|
46 | (define-syntax test-parse |
---|
47 | (syntax-rules () |
---|
48 | ((test-parse ?out ?in) |
---|
49 | (test ?in ?out (read-sexp-from-string ?in)) ) |
---|
50 | ((test-parse ?msg ?out ?in) |
---|
51 | (test ?msg ?out (read-sexp-from-string ?in)) ) ) ) |
---|
52 | |
---|
53 | (test-group "Jacal Grammar (English)" |
---|
54 | (test-parse '(+ 1 (* 2 a)) "1 + 2 * a") |
---|
55 | (test-parse '(* (+ 1 2) a) "(1 + 2) * a") |
---|
56 | (test-parse '(* (+ 1 2) (f a b)) "(1 + 2) * f(a, b)") |
---|
57 | (test-parse '(or (/ (* (+ 1 2) (f a b)) 27.9)) "{(1 + 2) * f(a, b) / 27.9}") |
---|
58 | |
---|
59 | ;FIXME handle ratios |
---|
60 | ;(test-parse '(* (+ 1 2) 1/5) "(1 + 2) * 1/5") |
---|
61 | (test-parse "N/D (ratio) => (/ N D)" '(/ (* (+ 1 2) 1) 5) "(1 + 2) * 1/5") |
---|
62 | |
---|
63 | (test 15 (eval (read-sexp-from-string "(1 + 2) * 5;"))) |
---|
64 | (test 15.0 (eval (read-sexp-from-string "(1.0 + 2) * 5"))) |
---|
65 | (test 3/5 (eval (read-sexp-from-string "(1 + 2) * 1/5"))) |
---|
66 | |
---|
67 | (test 8 (eval (read-sexp-from-string "2**3"))) |
---|
68 | ) |
---|
69 | |
---|
70 | ;; |
---|
71 | |
---|
72 | #| ;comment to activate for csi (csc needs extension) |
---|
73 | (read-syntax-setup!) |
---|
74 | |
---|
75 | (test-group "Reader #{...}" |
---|
76 | (test "w/ spaces" 3/5 #{ (1 + 2) / 5 }) ;(* (+ 1 2) 5) |
---|
77 | (test "w/o spaces" 3/5 #{(1+2)/5}) ;(* (+ 1 2) 5) |
---|
78 | (test 1 #{ {1} }) ;(or 1) |
---|
79 | (test #(1) #{ [1] }) ;(vector 1) |
---|
80 | (test 8 #{ expt(2, 3) }) ;(expt 2 3) |
---|
81 | (test 8 #{ 2**3 }) ;(expt 2 3) |
---|
82 | ) |
---|
83 | |
---|
84 | (test-group "Reader #<id>{...}" |
---|
85 | (test 15 #0{(1 + 2) * 5}) ;(* (+ 1 2) 5) |
---|
86 | (test 1 #0{{1}}) ;(or 1) |
---|
87 | (test 8 #0{expt(2,3)}) ;(expt 2 3) |
---|
88 | (test 8 #0{2^3}) ;(expt 2 3) |
---|
89 | (test #(1) #0{[1]}) ;(vector 1) |
---|
90 | (test #(1) #1{[1]}) ;(vector 1) |
---|
91 | ) |
---|
92 | |# |
---|
93 | |
---|
94 | ;;; |
---|
95 | |
---|
96 | (test-end "SLIB Precedence Parser") |
---|
97 | |
---|
98 | :: |
---|
99 | |
---|
100 | (test-exit) |
---|