aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/js/extension/common.lux
blob: 7a38dc5cd8a1bc120d96e4b0a9da0698d2f68835 (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
(.module:
  [lux #*
   [host (#+ import:)]
   [abstract
    ["." monad (#+ do)]]
   [data
    ["." product]
    [collection
     ["." dictionary]]]
   [target
    ["_" js (#+ Expression)]]]
  ["." /// #_
   ["#." runtime (#+ Operation Phase Handler Bundle)]
   ["#." primitive]
   [//
    [extension (#+ Nullary Unary Binary Trinary
                   nullary unary binary trinary)]
    [//
     [extension
      ["." bundle]]]]])

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

  [i64//left-shift             ///runtime.i64//left-shift]
  [i64//arithmetic-right-shift ///runtime.i64//arithmetic-right-shift]
  [i64//logical-right-shift    ///runtime.i64//logic-right-shift]
  )

## [[Numbers]]
(import: #long java/lang/Double
  (#static MIN_VALUE Double)
  (#static MAX_VALUE Double))

(template [<name> <const>]
  [(def: (<name> _)
     (Nullary Expression)
     (///primitive.f64 <const>))]

  [frac//smallest (java/lang/Double::MIN_VALUE)]
  [frac//min      (f/* -1.0 (java/lang/Double::MAX_VALUE))]
  [frac//max      (java/lang/Double::MAX_VALUE)]
  )

(def: frac//decode
  (Unary Expression)
  (|>> list
       (_.apply/* (_.var "parseFloat"))
       _.return
       (_.closure (list))
       ///runtime.lux//try))

(def: int//char
  (Unary Expression)
  (|>> ///runtime.i64//to-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))

(def: (io//exit codeG)
  (Unary Expression)
  (let [@@process (_.var "process")
        @@window (_.var "window")
        @@location (_.var "location")]
    ($_ _.or
        ($_ _.and
            (_.not (_.= _.undefined (_.type-of @@process)))
            (_.the "exit" @@process)
            (_.do "exit" (list (///runtime.i64//to-number codeG)) @@process))
        (_.do "close" (list) @@window)
        (_.do "reload" (list) @@location))))

(def: (io//current-time _)
  (Nullary Expression)
  (|> (_.new (_.var "Date") (list))
      (_.do "getTime" (list))
      ///runtime.i64//from-number))

## [Bundles]
(def: lux-procs
  Bundle
  (|> bundle.empty
      (bundle.install "is" (binary (product.uncurry _.=)))
      (bundle.install "try" (unary ///runtime.lux//try))))

(def: i64-procs
  Bundle
  (<| (bundle.prefix "i64")
      (|> bundle.empty
          (bundle.install "and" (binary (product.uncurry ///runtime.i64//and)))
          (bundle.install "or" (binary (product.uncurry ///runtime.i64//or)))
          (bundle.install "xor" (binary (product.uncurry ///runtime.i64//xor)))
          (bundle.install "left-shift" (binary i64//left-shift))
          (bundle.install "logical-right-shift" (binary i64//logical-right-shift))
          (bundle.install "arithmetic-right-shift" (binary i64//arithmetic-right-shift))
          (bundle.install "=" (binary (product.uncurry ///runtime.i64//=)))
          (bundle.install "+" (binary (product.uncurry ///runtime.i64//+)))
          (bundle.install "-" (binary (product.uncurry ///runtime.i64//-)))
          )))

(def: int-procs
  Bundle
  (<| (bundle.prefix "int")
      (|> bundle.empty
          (bundle.install "<" (binary (product.uncurry ///runtime.i64//<)))
          (bundle.install "*" (binary (product.uncurry ///runtime.i64//*)))
          (bundle.install "/" (binary (product.uncurry ///runtime.i64///)))
          (bundle.install "%" (binary (product.uncurry ///runtime.i64//%)))
          (bundle.install "frac" (unary ///runtime.i64//to-number))
          (bundle.install "char" (unary int//char)))))

(def: frac-procs
  Bundle
  (<| (bundle.prefix "frac")
      (|> bundle.empty
          (bundle.install "+" (binary (product.uncurry _.+)))
          (bundle.install "-" (binary (product.uncurry _.-)))
          (bundle.install "*" (binary (product.uncurry _.*)))
          (bundle.install "/" (binary (product.uncurry _./)))
          (bundle.install "%" (binary (product.uncurry _.%)))
          (bundle.install "=" (binary (product.uncurry _.=)))
          (bundle.install "<" (binary (product.uncurry _.<)))
          (bundle.install "smallest" (nullary frac//smallest))
          (bundle.install "min" (nullary frac//min))
          (bundle.install "max" (nullary frac//max))
          (bundle.install "int" (unary ///runtime.i64//from-number))
          (bundle.install "encode" (unary (_.do "toString" (list))))
          (bundle.install "decode" (unary frac//decode)))))

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

(def: io-procs
  Bundle
  (<| (bundle.prefix "io")
      (|> bundle.empty
          (bundle.install "log" (unary io//log))
          (bundle.install "error" (unary ///runtime.io//error))
          (bundle.install "exit" (unary io//exit))
          (bundle.install "current-time" (nullary io//current-time)))))

(def: #export bundle
  Bundle
  (<| (bundle.prefix "lux")
      (|> lux-procs
          (dictionary.merge i64-procs)
          (dictionary.merge int-procs)
          (dictionary.merge frac-procs)
          (dictionary.merge text-procs)
          (dictionary.merge io-procs)
          )))