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

(template: (!unary function)
  (|>> list _.apply/* (|> (_.constant function))))

... TODO: Get rid of this ASAP
(def: lux::syntax_char_case!
  (..custom [($_ <>.and
                 <s>.any
                 <s>.any
                 (<>.some (<s>.tuple ($_ <>.and
                                         (<s>.tuple (<>.many <s>.i64))
                                         <s>.any))))
             (function (_ extension_name phase archive [input else conditionals])
               (do {! /////.monad}
                 [@input (\ ! map _.var (generation.identifier "input"))
                  inputG (phase archive input)
                  elseG (phase archive else)
                  conditionalsG (: (Operation (List [Expression Expression]))
                                   (monad.map ! (function (_ [chars branch])
                                                  (do !
                                                    [branchG (phase archive branch)]
                                                    (in [(|> chars (list\map (|>> .int _.int (_.=/2 @input))) _.or)
                                                         branchG])))
                                              conditionals))]
                 (in (_.let (list [@input inputG])
                       (list\fold (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 (product.uncurried _.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 (product.uncurried //runtime.i64//and)))
          (/.install "or" (binary (product.uncurried //runtime.i64//or)))
          (/.install "xor" (binary (product.uncurried //runtime.i64//xor)))
          (/.install "left-shift" (binary (product.uncurried //runtime.i64//left_shift)))
          (/.install "right-shift" (binary (product.uncurried //runtime.i64//right_shift)))
          (/.install "=" (binary (product.uncurried _.=/2)))
          (/.install "<" (binary (product.uncurried _.</2)))
          (/.install "+" (binary (product.uncurried (..capped _.+/2))))
          (/.install "-" (binary (product.uncurried (..capped _.-/2))))
          (/.install "*" (binary (product.uncurried (..capped _.*/2))))
          (/.install "/" (binary (product.uncurried //runtime.i64//division)))
          (/.install "%" (binary (product.uncurried _.remainder/2)))
          (/.install "f64" (unary (_.//2 (_.float +1.0))))
          (/.install "char" (unary (|>> _.integer->char/1 (_.make_string/2 (_.int +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 _.remainder/2)))
          (/.install "i64" (unary _.truncate/1))
          (/.install "encode" (unary _.number->string/1))
          (/.install "decode" (unary //runtime.f64//decode)))))

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

(def: (text//clip [paramO extraO subjectO])
  (Trinary Expression)
  (//runtime.text//clip paramO extraO subjectO))

(def: text_procs
  Bundle
  (<| (/.prefix "text")
      (|> /.empty
          (/.install "=" (binary (product.uncurried _.string=?/2)))
          (/.install "<" (binary (product.uncurried _.string<?/2)))
          (/.install "concat" (binary (product.uncurried _.string_append/2)))
          (/.install "index" (trinary ..text//index))
          (/.install "size" (unary _.string_length/1))
          (/.install "char" (binary (product.uncurried //runtime.text//char)))
          (/.install "clip" (trinary ..text//clip))
          )))

(def: (io//log! message)
  (Unary Expression)
  (_.begin (list (_.display/1 message)
                 (_.display/1 (_.string text.new_line))
                 //runtime.unit)))

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

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