summaryrefslogtreecommitdiffstats
path: root/stdlib/infix.tl
blob: 30f95081b670cd29e5b3513783482b4046568eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
;; Copyright 2025
;; Kaz Kylheku <kaz@kylheku.com>
;; Vancouver, Canada
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; 1. Redistributions of source code must retain the above copyright notice,
;;    this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright notice,
;;    this list of conditions and the following disclaimer in the documentation
;;    and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.

(defvarl ifx-ops (hash))
(defvarl ifx-uops (hash))

(defvar *ifx-env*)

(defstruct (ifx-oper prec sym) nil
  sym
  lispsym
  (assoc :left)
  (arity :infix)
  prec
  funp
  power

  (:postinit (self)
    (set [(if (meql self.arity :infix :postfix) ifx-ops ifx-uops) self.sym] self
         self.lispsym (or self.lispsym self.sym))))

;; j0 j1 y0 y1 omitted: they are common variable names.
(each ((fun '(abs signum isqrt square zerop plusp minusp evenp oddp sin cos tan
              asin acos atan log log2 log10 exp sqrt width logcount cbrt erf
              erfc exp10 exp2 expm1 gamma lgamma log1p logb nearbyint
              rint significand tgamma tofloat toint
              trunc floor ceil round lognot)))
  (new (ifx-oper 0 fun) arity :prefix assoc :right funp t))

(new (ifx-oper 0 '=) arity :prefix assoc :right lispsym 'identity)

(new (ifx-oper 10 ':=) assoc :right lispsym 'set)
(new (ifx-oper 10 '+=) lispsym 'inc)
(new (ifx-oper 10 '-=) lispsym 'dec)
(new (ifx-oper 10 '*=) lispsym 'mul)
(new (ifx-oper 10 '/=) lispsym 'div)
(new (ifx-oper 10 '%=) lispsym 'sys:mod-set)
(new (ifx-oper 10 '&&=) lispsym 'sys:and-set)
(new (ifx-oper 10 '||=) lispsym 'sys:or-set)
(new (ifx-oper 10 '|=) lispsym 'sys:logior-set)
(new (ifx-oper 10 '~=) lispsym 'sys:logxor-set)
(new (ifx-oper 10 '&=) lispsym 'sys:logand-set)
(new (ifx-oper 10 '>>=) lispsym 'sys:asr-set)
(new (ifx-oper 10 '<<=) lispsym 'sys:ash-set)

(new (ifx-oper 11 'or))
(new (ifx-oper 11 '||) lispsym 'or)
(new (ifx-oper 12 'and))
(new (ifx-oper 12 '&&) lispsym 'and)
(new (ifx-oper 13 'not) arity :prefix assoc :right)
(new (ifx-oper 13 '!) arity :prefix assoc :right lispsym 'not)

(new (ifx-oper 20 '=) assoc :right)
(new (ifx-oper 20 '!=) assoc :right lispsym '/=)
(new (ifx-oper 20 'eq) assoc :right)
(new (ifx-oper 20 'eql) assoc :right)
(new (ifx-oper 20 'equal) assoc :right)
(new (ifx-oper 20 'neq) assoc :right)
(new (ifx-oper 20 'neql) assoc :right)
(new (ifx-oper 20 'nequal) assoc :right)

(new (ifx-oper 20 '<) assoc :right)
(new (ifx-oper 20 '>) assoc :right)
(new (ifx-oper 20 '<=) assoc :right)
(new (ifx-oper 20 '>=) assoc :right)
(new (ifx-oper 20 'less) assoc :right)
(new (ifx-oper 20 'greater) assoc :right)
(new (ifx-oper 20 'lequal) assoc :right)
(new (ifx-oper 20 'gequal) assoc :right)

(new (ifx-oper 26 '|) lispsym 'logior)
(new (ifx-oper 26 '~) lispsym 'logxor)
(new (ifx-oper 28 '&) lispsym 'logand)

(new (ifx-oper 29 '<<) lispsym 'ash)
(new (ifx-oper 29 '>>) lispsym 'sys:asr)

(new (ifx-oper 30 '-))
(new (ifx-oper 30 '+))

(new (ifx-oper 35 '-) arity :prefix assoc :right)
(new (ifx-oper 35 '+) arity :prefix assoc :right)
(new (ifx-oper 35 '~) arity :prefix assoc :right lispsym 'lognot)

(new (ifx-oper 40 '*))
(new (ifx-oper 40 '/))
(new (ifx-oper 40 '//) lispsym 'floor)
(new (ifx-oper 40 '%) lispsym 'mod)

(new (ifx-oper 50 '**) assoc :right lispsym 'expt)

(new (ifx-oper 55 '++) arity :prefix assoc :right lispsym 'inc)
(new (ifx-oper 55 '--) arity :prefix assoc :right lispsym 'dec)

(new (ifx-oper 60 '++) arity :postfix assoc :left lispsym 'pinc)
(new (ifx-oper 60 '--) arity :postfix assoc :left lispsym 'pdec)

(defmacro sys:mod-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (mod (,getter) ,value))))

(defmacro sys:and-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (and (,getter) ,value))))

(defmacro sys:or-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (or (,getter) ,value))))

(defmacro sys:logand-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (logand (,getter) ,value))))

(defmacro sys:logxor-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (logxor (,getter) ,value))))

(defmacro sys:logior-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (logior (,getter) ,value))))

(defmacro sys:ash-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (ash (,getter) ,value))))

(defmacro sys:asr-set (place value :env env)
  (with-update-expander (getter setter) place env
    ^(,setter (ash (,getter) (- ,value)))))

(defmacro sys:asr (num bits)
  ^(ash ,num (- ,bits)))

(defun infix-error (exp fmt . args)
  (let ((loc (source-loc-str exp)))
    (let ((msg (fmt `@loc: infix: @fmt` . args)))
      (when (and sys:*load-recursive*
                 (null (find-frame 'error 'catch-frame)))
        (dump-deferred-warnings *stderr*)
        (put-line msg *stderr*))
      (throw 'eval-error msg))))

(defun parse-infix (exp)
  (let (nodestack opstack (ucheck t) (oexp exp))
    (flet ((add-node (oper)
              (ecaseql oper.arity
                ((:prefix :postfix)
                 (push (list oper.lispsym (pop nodestack)) nodestack)
                 (if oper.power
                    (push ^(expt ,(pop nodestack) ,oper.power)
                      nodestack)))
                ((:infix)
                 (let ((y (pop nodestack))
                       (x (pop nodestack)))
                   (push (list oper.lispsym x y) nodestack))))))
      (while-true-match-case exp
        ((@[[chain ifx-uops .?funp] @op] (@arg . @args) . @rest)
         (set exp ^((,op (,arg ,*args)) ,*rest)))
        ((@[[chain ifx-uops .?funp] @op] () . @rest)
         (set exp ^((,op) ,*rest)))
        ((@(with @(@o1 [(if ucheck ifx-uops ifx-ops)]) @prec o1.prec) . @rest)
         (unless (or rest (eq o1.arity :postfix))
           (infix-error oexp "operator ~s needs right operand" o1.sym))
         (if (eq o1.arity :infix)
           (iflet ((uo [take-while ifx-uops rest]))
             (set prec (min prec
                            (pred (find-min-key uo :
                                                (opip ifx-uops .prec)))))))
         (if (meq o1.arity :infix :postfix)
           (whilet ((o2 (first opstack))
                    (yes (when o2 (caseq o2.assoc
                                    (:left  (>= o2.prec prec))
                                    (:right (> o2.prec prec))))))
              (pop opstack)
              (add-node o2)))
         (cond
           ((eq o1.arity :postfix)
            (add-node o1)
            (set ucheck nil))
           (t
            (when-match (** @arg . @rrest) rest
              (set o1 (copy o1)
                   o1.power arg
                   rest rrest))
            (push o1 opstack)
            (set ucheck t)))
         (set exp rest))
        ((@(@o [ifx-ops]) . @nil)
         (infix-error oexp "operator ~s needs left operand" o.sym))
        ((@op [. @args] . @rest)
         (set exp ^([,op ,args] ,*rest)))
        ((@op (@arg . @args) . @rest)
         (set exp ^((,op (,arg ,*args)) ,*rest)))
        ((@op () . @rest)
         (set exp ^((,op) ,*rest)))
        ((@tok . @rest)
         (if (or (not ucheck)
                 (eq (first opstack).?arity :postfix))
           (infix-error oexp "operator expected before ~s" tok))
         (if (consp tok)
           (push ^@,tok nodestack)
           (push tok nodestack))
         (set ucheck nil
              exp rest)))
      (whilet ((o (first opstack)))
        (pop opstack)
        (add-node o)))
    (if (rest nodestack)
      (infix-error oexp "nodestack extra entries ~s" nodestack))
    (first nodestack)))

(defun-match finish-infix
  (((@(memq @op '(and or + - * / logand logior logxor))
     (@op @a0 @a1 . @resta) @b . @restb))
   (finish-infix ^(,op ,a0 ,a1 ,*resta ,b ,*restb)))
  (((@(memq @op '(expt and))
     @a (@op @b0 @b1 . @restb)))
   (finish-infix ^(,op ,a ,b0 ,b1 ,*restb)))
  (((@(memq @o0 '#1=(eq eql equal
                     neq neql nequal . #2=(= /= < > <= >=
                                           less greater lequal gequal)))
      @a (@(memq @o1 '#1#) @b . @restb)))
   (cond
     ((or (and (atom b) (not (macro-form-p b *ifx-env*)))
          (if-match (@(meq @nil 'sys:var 'sys:expr) @(atom)) b t))
      (finish-infix ^(and (,o0 ,a ,b) ,(finish-infix ^(,o1 ,b ,*restb)))))
     ((with-gensyms (bg)
        ^(let (,bg)
          ,(finish-infix ^(and (,o0 ,a (set ,bg ,b))
                               ,(finish-infix ^(,o1 ,bg ,*restb)))))))))
  (((and @(sme (@(memq @op '#2#)) nil (@b) a) (@op @b . @restb) . @rest))
   (finish-infix ^(and (,op ,*(butlast a) ,b ,*restb) ,*rest)))
  (((and @expr))
   expr)
  (((@(eq 'sys:expr) @expr))
   expr)
  (((@op . @args))
   (identity (cons op [mapcar finish-infix args])))
  ((@else) else))

(defmacro funp (env sym)
  ^(unless (consp ,sym)
     (or (fboundp ,sym) (lexical-fun-p ,env ,sym))))

(defun-match detect-infix
  (((@x @y . @rest) @env)
   (let* ((xu [ifx-uops x])
          (yu [ifx-uops y])
          (yo [ifx-ops y])
          (yb (if-match [. @toks] y
                (or (and (consp toks)
                         (null (cdr toks)))
                    (detect-infix toks env))))
          (ya (and (not yu) (not yo) (not (funp env y))))
          (xa (and (not xu) (not (funp env x)))))
     (or
       (and xa (or yo yb))
       (and xu ya (neq xu.sym xu.lispsym) (null rest))
       (and xu (eq y '**) (eq xu.sym xu.lispsym))
       (if-match (@z . @nil) rest
         (let* ((zu [ifx-uops z])
                (zo [ifx-ops z])
                (za (and (not zu) (not zo) (not (funp env z)))))
           (or
             (and xu ya (neq xu.sym xu.lispsym) (not za))
             (and xu yu (or zu za (eq z '**)))
             (and xu ya zo))))))))

(defun infix-expand-hook (exp env type-sym)
  (ignore env)
  (cond
    ((eq type-sym :macro) exp)
    ((and (meq type-sym :fun nil) (detect-infix exp env))
     (let ((*ifx-env* env))
       (finish-infix (parse-infix exp))))
    ((match-case exp
       ([] exp)
       ([. @nil] exp)
       (@(require (@tok)
                  (null type-sym)
                  (or (not (bindable tok))
                      (lexical-var-p env tok)
                      (boundp tok)))
         tok)
       (@(require (@x @y . @rest)
                  (consp rest)
                  (and (not (funp env x)) (funp env y)))
        (let ((rexp (infix-expand-hook rest env nil)))
          (if (eq rexp rest)
            ^(,y ,x ,*rest)
            ^(,y ,x ,rexp))))
       (@(require (@x . @rest)
                  (and (consp rest) (cdr rest) (funp env x)))
        (let ((rexp (infix-expand-hook rest env nil)))
          (if (eq rexp rest)
            exp
            ^(,x ,rexp))))))
    (exp)))

(defmacro usr:ifx (. body)
  ^(expander-let ((*expand-hook* [expand-hook-combine infix-expand-hook
                                                      *expand-hook*]))
     ,*body))