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