Changeset 38725 in project


Ignore:
Timestamp:
06/01/20 00:09:30 (6 weeks ago)
Author:
Kon Lovett
Message:

scheme-ify grammar

Location:
release/5/slib-prec/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/5/slib-prec/trunk/slib-standard-grammar.scm

    r38718 r38725  
    4444
    4545;;; Character classes
    46 (prec:define-grammar (tok:char-group 70 #\^ list2string))
    4746(prec:define-grammar (tok:char-group 49 #\* list2string))
    4847(prec:define-grammar (tok:char-group 50 #\/ list2string))
    4948(prec:define-grammar (tok:char-group 51 '(#\+ #\-) list2string))
    5049(prec:define-grammar (tok:char-group 20 '(#\|) list2string))
     50(prec:define-grammar (tok:char-group 22 #\^ list2string))
     51(prec:define-grammar (tok:char-group 21 '(#\&) list2string))
    5152(prec:define-grammar (tok:char-group 30 '(#\< #\> #\= #\: #\~) list2string))
    5253(prec:define-grammar (tok:char-group
     
    7273;;; Delimiters used to be defined here, but now are defined
    7374;;; dynamically by parse functions.
    74 (prec:define-grammar (prec:delim #\])
    75                      ;;(prec:delim #\;)
    76                      ;;(prec:delim #\,)
    77                      ;;(prec:postfix #\$ (lambda (x) (write x)) 0)
    78                      )
     75(prec:define-grammar (prec:delim #\]))
    7976
    8077;;;prefix operators
    8178(prec:define-grammar (prec:prefix '+ #f 150))
    82 (prec:define-grammar (prec:prefix '- 'negate 150))
    83 (prec:define-grammar (prec:prefix "+/-" 'u+/- 150))
    84 (prec:define-grammar (prec:prefix "-/+" 'u-/+ 150))
     79(prec:define-grammar (prec:prefix '- '- 150))
    8580
    86 (prec:define-grammar (prec:prefix '(not ~) 'impl:not 70))
     81(prec:define-grammar (prec:prefix '~ 'bitwise-not 120))
    8782
    88 (prec:define-grammar (prec:prefix ":" 'settemplate! 20))
     83(prec:define-grammar (prec:prefix #\! 'not 70))
     84
     85;(prec:define-grammar (prec:prefix ":" 'settemplate! 20))
    8986
    9087;;;nary operators
     
    9390(prec:define-grammar (prec:nary '+ '+ 100))
    9491(prec:define-grammar (prec:nary '- '- 100))
    95 (prec:define-grammar (prec:nary "+/-" 'b+/- 100))
    96 (prec:define-grammar (prec:nary "-/+" 'b-/+ 100))
    9792
    98 (prec:define-grammar (prec:nary '(and #\&) '& 60))
    99 (prec:define-grammar (prec:nary 'or 'or 50))
    100 (prec:define-grammar (prec:nary "||" 'parallel 110))
     93(prec:define-grammar (prec:nary "&&" 'and 60))
     94(prec:define-grammar (prec:nary "||" 'or 50))
     95
     96(prec:define-grammar (prec:nary "&" 'bitwise-and 110))
     97(prec:define-grammar (prec:nary "|" 'bitwise-ior 100))
     98(prec:define-grammar (prec:nary "^" 'bitwise-xor 100))
    10199
    102100;;;infix operators
    103101;(prec:infix 'x 'crossproduct 111 110)
    104 (prec:define-grammar (prec:infix #\. 'ncmult 110 109))
    105 (prec:define-grammar (prec:infix '(^ **) '^ 140 139))
     102;(prec:define-grammar (prec:infix #\. 'ncmult 110 109))
    106103
    107 (prec:define-grammar (prec:infix '^^ '^^ 210 210))
     104;(prec:define-grammar (prec:infix '(^ **) '^ 140 139))
     105(prec:define-grammar (prec:infix '** 'expt 140 139))
    108106
    109 (prec:define-grammar (prec:infix '(":=" ":") 'define 180 20))
     107;(prec:define-grammar (prec:infix '^^ '^^ 210 210))
     108
     109;(prec:define-grammar (prec:infix '(":=" ":") 'define 180 20))
     110(prec:define-grammar (prec:infix ":" 'define 180 20))
    110111
    111112(prec:define-grammar (prec:infix '= '= 80 80))
    112 ;(prec:define-grammar (prec:infix '(~= <>) 'make-not-equal 80 80))
     113;(prec:define-grammar (prec:infix '(~= <>) '<> 80 80))
     114(prec:define-grammar (prec:infix '<> '<> 80 80))
    113115
    114116(prec:define-grammar (prec:infix 'mod 'mod 70 70))
    115117
    116118(prec:define-grammar (prec:infix ':: 'suchthat 190 40))
    117 (prec:define-grammar (prec:infix "|" 'suchthat 190 40))
    118 
    119 ;;; I don't remember what I had in mind here.
    120 ;(prec:define-grammar (prec:infix "" '* 120 120)) ;null operator
    121119
    122120;;;postfix operators
    123 (prec:define-grammar (prec:postfix #\! 'factorial 160))
    124 (prec:define-grammar (prec:postfix #\' 'differential 170))
     121;(prec:define-grammar (prec:postfix #\! 'factorial 160))
     122;(prec:define-grammar (prec:postfix #\' 'differential 170))
    125123
    126124;;;matchfix operators
    127 (prec:define-grammar (prec:matchfix #\( identity #f #\)))
    128 (prec:define-grammar (prec:matchfix #\[ vector #\, #\]))
    129 (prec:define-grammar (prec:matchfix #\{ 'or #\, #\}
    130                                    (prec:infix "|" 'suchthat 190 40)))
     125(prec:define-grammar (prec:matchfix #\( identity #f #\))) ;MUST be procedure
     126(prec:define-grammar (prec:matchfix #\[ 'vector #\, #\]))
     127
     128;(prec:define-grammar (prec:matchfix #\{ 'or #\, #\} (prec:infix "|" 'suchthat 190 40)))
     129
    131130(prec:define-grammar (prec:matchfix #\\ 'lambda #\, #\;))
    132 (prec:define-grammar (prec:matchfix "|" 'abs #f "|"))
     131
     132;(prec:define-grammar (prec:matchfix "|" 'abs #f "|"))
    133133
    134134;;;special operators
    135 (prec:define-grammar (prec:inmatchfix #\( list #\, #\) 200))
    136 (prec:define-grammar (prec:inmatchfix #\[ 'rapply #\, #\] 200))
     135(prec:define-grammar (prec:inmatchfix #\( list #\, #\) 200)) ;MUST be procedure
     136;(prec:define-grammar (prec:inmatchfix #\[ 'rapply #\, #\] 200))
    137137
    138138;;;rest operator reads expressions up to next delimiter.
    139 (prec:define-grammar (prec:prestfix 'set 'set 10))
    140 (prec:define-grammar (prec:prestfix 'show 'show 10))
     139;(prec:define-grammar (prec:prestfix 'set 'set 10))
     140;(prec:define-grammar (prec:prestfix 'show 'show 10))
    141141
    142142;;;miscellany
    143143;(prec:define-grammar (prec:prefix 'load 'load 50))
    144 (prec:define-grammar (prec:nofix '% '%))
    145 (prec:define-grammar (prec:nofix 'help 'help))
    146 (prec:define-grammar (prec:nofix 'qed 'qed))
     144;(prec:define-grammar (prec:nofix '% '%))
     145;(prec:define-grammar (prec:nofix 'help 'help))
     146;(prec:define-grammar (prec:nofix 'qed 'qed))
    147147
    148148(prec:define-grammar (prec:commentfix
  • release/5/slib-prec/trunk/tests/slib-prec-test.scm

    r38717 r38725  
    5151      (test ?msg ?out (read-sexp-from-string ?in)) ) ) )
    5252
    53 (test-group "Jacal Grammar (English)"
     53(test-group "Arithmetic Grammer (Jacal, English, derived)"
     54
    5455  (test-parse '(+ 1 (* 2 a)) "1 + 2 * a")
    55   (test-parse '(* (+ 1 2) a) "(1 + 2) * a")
     56  (test-parse '(+ 1 2 (* abc a)) "1 + 2 + abc * a")
    5657  (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}")
    5858
    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")
     59  (test-parse
     60    '(or (/ (* (+ 1 2) (f a b)) 27.9) PI)
     61    "(1 + 2) * f(a, b) / 27.9 || PI")
     62  (test-parse
     63    '(and (/ (* (+ 1 2) (f a b)) 27.9) PI)
     64    "(1 + 2) * f(a, b) / 27.9 && PI")
     65  (test-parse
     66    '(or (and BAZ PI) (and (not FOO) BAR))
     67    "BAZ && PI || !FOO && BAR")
     68
     69  (test-parse
     70    '(bitwise-ior (bitwise-xor (bitwise-ior a (bitwise-and b c)) (bitwise-not b)) d)
     71    "a | b & c ^ ~b | d")
     72
     73  ;FIXME handle ratios ? / in a/b vs a / b ?
     74  #; ; "N/D (ratio) => (/ N D)"
     75  (test-parse '(* (+ 1 2) 1/5) "(1 + 2) * 1/5")
     76  ;#; ;
     77  (test-parse '(/ (* (+ 1 2) 1) 5) "(1 + 2) * 1/5")
    6278
    6379  (test 15 (eval (read-sexp-from-string "(1 + 2) * 5;")))
Note: See TracChangeset for help on using the changeset viewer.