aboutsummaryrefslogtreecommitdiff
path: root/qklib/infix/rule-set.scm
blob: 122aa561fb66e43e1ee790ad234c5985d9b893ea (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
;;; Copyright 2024 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(define-library (qklib infix rule-set)
  (export rule-set
          rule-set?
          rule-set-infix-ref
          rule-set-prefix-ref

          operator
          operator?
          operator-symbol
          operator-prefix-symbol
          operator-precedence
          operator-left?
          operator-right?
          operator-prefix
          operator-unit

          unit
          unit?
          unit-value
          unit-inv?
          unit-unary?

          prefix
          prefix?
          prefix-symbol
          prefix-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 prefix-hashmap)
      rule-set?
      (operator-hashmap rule-set-operator-hashmap)
      (prefix-hashmap rule-set-prefix-hashmap))

    (define (rule-set operator-list)
      (make-rule-set (list->operator-hashmap operator-list)
                     (list->prefix-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->prefix-hashmap ops)
      (hashmap-unfold null?
                      (lambda (ops)
                        (let ((op (car ops)))
                          (values (operator-prefix-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-prefix-ref rule-set key)
      (hashmap-ref/default (rule-set-prefix-hashmap rule-set) key #f))

    (define-record-type <operator>
      (make-operator symbol precedence assoc unit prefix)
      operator?
      (symbol operator-symbol)
      (precedence operator-precedence)
      (assoc operator-assoc)
      (unit operator-unit)
      (prefix operator-prefix))

    (define operator
      (case-lambda
        ((symbol precedence assoc)
         (operator symbol precedence assoc #f))
        ((symbol precedence assoc unit)
         (operator symbol precedence assoc unit #f))
        ((symbol precedence assoc unit prefix)
         (unless (or (eq? 'none assoc)
                     (eq? 'left assoc)
                     (eq? 'right assoc)
                     (eq? 'both assoc))
           (error "operator: The 3rd argument must be 'none, 'left, 'right or 'both" assoc))
         (make-operator symbol precedence assoc unit prefix))))

    (define (operator-left? x)
      (let ((a (operator-assoc x)))
        (or (eq? a 'left)
            (eq? a 'both))))

    (define (operator-right? x)
      (let ((a (operator-assoc x)))
        (or (eq? a 'right)
            (eq? a 'both))))

    (define (operator-prefix-symbol op)
      (cond ((operator-prefix op) => prefix-symbol)
            (else (operator-symbol op))))

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

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

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

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