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

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

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

## [Procedures]
## [[Bits]]
(template [<name> <op>]
  [(def: (<name> [paramG subjectG])
     (Binary Expression)
     (<op> subjectG (//runtime.i64//number paramG)))]

  [i64//left_shifted  //runtime.i64//left_shifted]
  [i64//right_shifted //runtime.i64//right_shifted]
  )

## [[Numbers]]
(def: f64//decode
  (Unary Expression)
  (|>> list
       (_.apply/* (_.var "parseFloat"))
       _.return
       (_.closure (list))
       //runtime.lux//try))

(def: i64//char
  (Unary Expression)
  (|>> //runtime.i64//number
       (list)
       (_.apply/* (_.var "String.fromCharCode"))))

## [[Text]]
(def: (text//concat [leftG rightG])
  (Binary Expression)
  (|> leftG (_.do "concat" (list rightG))))

(def: (text//clip [startG endG subjectG])
  (Trinary Expression)
  (//runtime.text//clip startG endG subjectG))

(def: (text//index [startG partG subjectG])
  (Trinary Expression)
  (//runtime.text//index startG partG subjectG))

## [[IO]]
(def: (io//log messageG)
  (Unary Expression)
  ($_ _.,
      (//runtime.io//log messageG)
      //runtime.unit))

## 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}
                 [inputG (phase archive input)
                  elseG (phase archive else)
                  conditionalsG (: (Operation (List [(List Literal)
                                                     Statement]))
                                   (monad.map ! (function (_ [chars branch])
                                                  (do !
                                                    [branchG (phase archive branch)]
                                                    (in [(list\map (|>> .int _.int) chars)
                                                         (_.return branchG)])))
                                              conditionals))]
                 (in (_.apply/* (_.closure (list)
                                           (_.switch (_.the //runtime.i64_low_field inputG)
                                                     conditionalsG
                                                     (#.Some (_.return elseG))))
                                (list)))))]))

## [Bundles]
(def: lux_procs
  Bundle
  (|> /.empty
      (/.install "syntax char case!" lux::syntax_char_case!)
      (/.install "is" (binary (product.uncurry _.=)))
      (/.install "try" (unary //runtime.lux//try))))

(def: i64_procs
  Bundle
  (<| (/.prefix "i64")
      (|> /.empty
          (/.install "and" (binary (product.uncurry //runtime.i64//and)))
          (/.install "or" (binary (product.uncurry //runtime.i64//or)))
          (/.install "xor" (binary (product.uncurry //runtime.i64//xor)))
          (/.install "left-shift" (binary i64//left_shifted))
          (/.install "right-shift" (binary i64//right_shifted))
          (/.install "=" (binary (product.uncurry //runtime.i64//=)))
          (/.install "<" (binary (product.uncurry //runtime.i64//<)))
          (/.install "+" (binary (product.uncurry //runtime.i64//+)))
          (/.install "-" (binary (product.uncurry //runtime.i64//-)))
          (/.install "*" (binary (product.uncurry //runtime.i64//*)))
          (/.install "/" (binary (product.uncurry //runtime.i64///)))
          (/.install "%" (binary (product.uncurry //runtime.i64//%)))
          (/.install "f64" (unary //runtime.i64//number))
          (/.install "char" (unary i64//char))
          )))

(def: f64_procs
  Bundle
  (<| (/.prefix "f64")
      (|> /.empty
          (/.install "+" (binary (product.uncurry _.+)))
          (/.install "-" (binary (product.uncurry _.-)))
          (/.install "*" (binary (product.uncurry _.*)))
          (/.install "/" (binary (product.uncurry _./)))
          (/.install "%" (binary (product.uncurry _.%)))
          (/.install "=" (binary (product.uncurry _.=)))
          (/.install "<" (binary (product.uncurry _.<)))
          (/.install "i64" (unary //runtime.i64//of_number))
          (/.install "encode" (unary (_.do "toString" (list))))
          (/.install "decode" (unary f64//decode)))))

(def: text_procs
  Bundle
  (<| (/.prefix "text")
      (|> /.empty
          (/.install "=" (binary (product.uncurry _.=)))
          (/.install "<" (binary (product.uncurry _.<)))
          (/.install "concat" (binary text//concat))
          (/.install "index" (trinary text//index))
          (/.install "size" (unary (|>> (_.the "length") //runtime.i64//of_number)))
          (/.install "char" (binary (product.uncurry //runtime.text//char)))
          (/.install "clip" (trinary text//clip))
          )))

(def: io_procs
  Bundle
  (<| (/.prefix "io")
      (|> /.empty
          (/.install "log" (unary io//log))
          (/.install "error" (unary //runtime.io//error)))))

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