aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/common_lisp/common.lux
blob: 51ed09e663cd833e1661660af2e1d437efcd37fd (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
(.using
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" function]
    ["[0]" try]]
   [data
    ["[0]" product]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" dictionary]
     ["[0]" set]
     ["[0]" list (.open: "[1]#[0]" functor mix)]]]
   [math
    [number
     ["f" frac]]]
   ["@" target (.only)
    ["_" common_lisp (.only Expression)]]]]
 ["[0]" ////
  ["/" bundle]
  ["/[1]" //
   ["[0]" extension]
   [generation
    [extension (.only Nullary Unary Binary Trinary
                      nullary unary binary trinary)]
    ["[0]" reference]
    ["//" common_lisp
     ["[1][0]" runtime (.only Operation Phase Handler Bundle Generator)]
     ["[1][0]" case]]]
   [//
    ["[0]" generation]
    ["[0]" synthesis (.only %synthesis)
     ["<s>" \\parser (.only Parser)]]
    [///
     ["[1]" phase]]]]])

(def .public (custom [parser handler])
  (All (_ s)
    (-> [(Parser s)
         (-> Text (Generator s))]
        Handler))
  (function (_ extension_name phase archive input)
    (case (<s>.result parser input)
      {try.#Success input'}
      (handler extension_name phase archive input')

      {try.#Failure error}
      (/////.except extension.invalid_syntax [extension_name %synthesis input]))))

(def !unary
  (template (_ function)
    (|>> list _.apply (|> (_.constant function)))))

... ... TODO: Get rid of this ASAP
... (def lux::syntax_char_case!
...   (..custom [(all <>.and
...                  <s>.any
...                  <s>.any
...                  (<>.some (<s>.tuple (all <>.and
...                                          (<s>.tuple (<>.many <s>.i64))
...                                          <s>.any))))
...              (function (_ extension_name phase archive [input else conditionals])
...                (do [! /////.monad]
...                  [@input (at ! each _.var (generation.symbol "input"))
...                   inputG (phase archive input)
...                   elseG (phase archive else)
...                   conditionalsG (is (Operation (List [Expression Expression]))
...                                    (monad.each ! (function (_ [chars branch])
...                                                   (do !
...                                                     [branchG (phase archive branch)]
...                                                     (in [(|> chars (list#each (|>> .int _.int (_.=/2 @input))) _.or)
...                                                            branchG])))
...                                               conditionals))]
...                  (in (_.let (list [@input inputG])
...                          (list (list#mix (function (_ [test then] else)
...                                             (_.if test then else))
...                                           elseG
...                                           conditionalsG))))))]))

(def lux_procs
  Bundle
  (|> /.empty
      ... (/.install "syntax char case!" lux::syntax_char_case!)
      (/.install "is" (binary _.eq/2))
      ... (/.install "try" (unary //runtime.lux//try))
      ))

... (def (capped operation parameter subject)
...   (-> (-> Expression Expression Expression)
...       (-> Expression Expression Expression))
...   (//runtime.i64//64 (operation parameter subject)))

(def i64_procs
  Bundle
  (<| (/.prefix "i64")
      (|> /.empty
          (/.install "and" (binary _.logand/2))
          (/.install "or" (binary _.logior/2))
          (/.install "xor" (binary _.logxor/2))
          (/.install "left-shift" (binary _.ash/2))
          (/.install "right-shift" (binary (product.uncurried //runtime.i64//right_shifted)))
          (/.install "=" (binary _.=/2))
          (/.install "<" (binary _.</2))
          (/.install "+" (binary _.+/2))
          (/.install "-" (binary _.-/2))
          (/.install "*" (binary _.*/2))
          (/.install "/" (binary _.floor/2))
          (/.install "%" (binary _.rem/2))
          ... (/.install "f64" (unary (_.//2 (_.float +1.0))))
          (/.install "char" (unary (|>> _.code_char/1 _.string/1)))
          )))

(def f64_procs
  Bundle
  (<| (/.prefix "f64")
      (|> /.empty
          ... (/.install "=" (binary (product.uncurried _.=/2)))
          ... (/.install "<" (binary (product.uncurried _.</2)))
          ... (/.install "+" (binary (product.uncurried _.+/2)))
          ... (/.install "-" (binary (product.uncurried _.-/2)))
          ... (/.install "*" (binary (product.uncurried _.*/2)))
          ... (/.install "/" (binary (product.uncurried _.//2)))
          ... (/.install "%" (binary (product.uncurried _.rem/2)))
          ... (/.install "i64" (unary _.truncate/1))
          (/.install "encode" (unary _.write_to_string/1))
          ... (/.install "decode" (unary //runtime.f64//decode))
          )))

(def (text//index [offset sub text])
  (Trinary (Expression Any))
  (//runtime.text//index offset sub text))

(def (text//clip [offset length text])
  (Trinary (Expression Any))
  (//runtime.text//clip offset length text))

(def (text//char [index text])
  (Binary (Expression Any))
  (_.char_code/1 (_.char/2 [text index])))

(def text_procs
  Bundle
  (<| (/.prefix "text")
      (|> /.empty
          (/.install "=" (binary _.string=/2))
          ... (/.install "<" (binary (product.uncurried _.string<?/2)))
          (/.install "concat" (binary (function (_ [left right])
                                        (_.concatenate/3 [(_.symbol "string") left right]))))
          (/.install "index" (trinary ..text//index))
          (/.install "size" (unary _.length/1))
          (/.install "char" (binary ..text//char))
          (/.install "clip" (trinary ..text//clip))
          )))

(def (io//log! message)
  (Unary (Expression Any))
  (_.progn (list (_.write_line/1 message)
                 //runtime.unit)))

(def io_procs
  Bundle
  (<| (/.prefix "io")
      (|> /.empty
          (/.install "log" (unary ..io//log!))
          (/.install "error" (unary _.error/1))
          )))

(def .public bundle
  Bundle
  (<| (/.prefix "lux")
      (|> /.empty
          (dictionary.composite lux_procs)
          (dictionary.composite i64_procs)
          (dictionary.composite f64_procs)
          (dictionary.composite text_procs)
          (dictionary.composite io_procs)
          )))