aboutsummaryrefslogtreecommitdiff
path: root/infix-to-scheme/rule-set.scm
blob: 22ffcc34f9cd2adc6e7d5e63327fde25f42a1fed (about) (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
;;; Infix-to-Scheme --- Library for converting infix formula to Scheme expression
;;; Copyright © 2024 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; This file is part of Infix-to-Scheme.
;;;
;;; Infix-to-Scheme is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Infix-to-Scheme is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Infix-to-Scheme. If not, see
;;; <https://www.gnu.org/licenses/>.

(define-library (infix-to-scheme rule-set)
  (export rule-set
          rule-set?
          rule-set-infix-ref
          rule-set-scheme-ref

          operator
          operator?
          operator-symbol
          operator-scheme-symbol
          operator-precedence
          operator-left?
          operator-right?
          operator-associative?
          operator-scheme
          operator-identity

          identity
          identity?
          identity-value
          identity-inv?
          identity-unary?
          identity-unary-precedence

          direction
          direction?
          direction-left?
          direction-associative?

          scheme
          scheme?
          scheme-symbol
          scheme-binary-only?)
  (import (scheme base)
          (scheme case-lambda)
          (only (srfi 128) make-eqv-comparator))
  (cond-expand
    ((library (srfi srfi-146 hash)) ;; for guile-srfi-146
     (import (only (srfi srfi-146 hash) hashmap-ref/default hashmap-unfold)))
    ((library (srfi 146 hash))
     (import (only (srfi 146 hash) hashmap-ref/default hashmap-unfold))))
  (begin
    (define-record-type <rule-set>
      (make-rule-set operator-hashmap scheme-hashmap)
      rule-set?
      (operator-hashmap rule-set-operator-hashmap)
      (scheme-hashmap rule-set-scheme-hashmap))

    (define (rule-set operator-list)
      (make-rule-set (list->operator-hashmap operator-list)
                     (list->scheme-hashmap operator-list)))

    (define (list->operator-hashmap ops)
      (hashmap-unfold null?
                      (lambda (ops)
                        (values (operator-symbol (car ops))
                                (car ops)))
                      cdr
                      ops
                      (make-eqv-comparator)))

    (define (list->scheme-hashmap ops)
      (hashmap-unfold null?
                      (lambda (ops)
                        (let ((op (car ops)))
                          (values (operator-scheme-symbol op)
                                  (car ops))))
                      cdr
                      ops
                      (make-eqv-comparator)))

    (define (rule-set-infix-ref rule-set key)
      (hashmap-ref/default (rule-set-operator-hashmap rule-set) key #f))

    (define (rule-set-scheme-ref rule-set key)
      (hashmap-ref/default (rule-set-scheme-hashmap rule-set) key #f))

    (define-record-type <operator>
      (make-operator symbol precedence dir identity scheme)
      operator?
      (symbol operator-symbol)
      (precedence operator-precedence)
      (dir operator-direction)
      (identity operator-identity)
      (scheme operator-scheme))

    (define operator
      (case-lambda
        ((symbol precedence)
         (operator symbol precedence #f))
        ((symbol precedence direction)
         (operator symbol precedence direction #f))
        ((symbol precedence direction identity)
         (operator symbol precedence direction identity #f))
        ((symbol precedence direction identity scheme)
         (when (and identity (identity-unary? identity)
                    (not (and direction (direction-left? direction))))
           (error "operator: unary operator must be left direction" symbol))
         (make-operator symbol precedence direction identity scheme))))

    (define-record-type <direction>
      (make-direction left? associative?)
      direction?
      (left? direction-left?)
      (associative? direction-associative?))

    (define (direction-right? assoc)
      (not (direction-left? assoc)))

    (define direction
      (case-lambda
        ((dir)
         (direction dir #f))
        ((dir associative?)
         (unless (or (eq? 'left dir)
                     (eq? 'right dir))
           (error "direction: The 1st argument must be 'left or 'right" dir))
         (make-direction (eq? 'left dir) associative?))))

    (define (operator-left? x)
      (cond ((operator-direction x) => direction-left?)
            (else #f)))

    (define (operator-right? x)
      (cond ((operator-direction x) => (lambda (a) (not (direction-left? a))))
            (else #f)))

    (define (operator-associative? x)
      (cond ((operator-direction x) => direction-associative?)
            (else #f)))

    (define (operator-scheme-symbol op)
      (cond ((operator-scheme op) => (lambda (p)
                                       (and (scheme-has-symbol? p)
                                            (scheme-symbol p))))
            (else (operator-symbol op))))

    (define-record-type <scheme>
      (make-scheme binary-only? has-symbol? symbol)
      scheme?
      (binary-only? scheme-binary-only?)
      (has-symbol? scheme-has-symbol?)
      (symbol scheme-symbol))

    (define scheme
      (case-lambda
        ((binary-only?)
         (make-scheme binary-only? #f #f))
        ((binary-only? symbol)
         (make-scheme binary-only?  #t symbol))))

    (define-record-type <identity>
      (make-identity value inv? unary? unary-precedence)
      identity?
      (value identity-value)
      (inv? identity-inv?)
      (unary? identity-unary?)
      (unary-precedence identity-unary-precedence))

    (define identity
      (case-lambda
        ((value) (identity value #f))
        ((value inv?) (identity value inv? #f))
        ((value inv? unary?) (identity value inv? unary? #f))
        ((value inv? unary? unary-precedence) (make-identity value inv? unary? unary-precedence))))))