aboutsummaryrefslogtreecommitdiff
path: root/source/lux.lux
blob: 8f02c681dca8fda607f7c988afc06b950e86a64b (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
## Base interfaces & classes
(jvm/definterface Function
  (: apply (-> [java.lang.Object] java.lang.Object)))

(jvm/defclass Tuple0 java.lang.Object
  [])
(jvm/defclass Tuple1 java.lang.Object
  [[java.lang.Object _1]])
(jvm/defclass Tuple2 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]])
(jvm/defclass Tuple3 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3]])
(jvm/defclass Tuple4 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]])
(jvm/defclass Tuple5 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5]])
(jvm/defclass Tuple6 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5] [java.lang.Object _6]])
(jvm/defclass Tuple7 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5] [java.lang.Object _6]
   [java.lang.Object _7]])
(jvm/defclass Tuple8 java.lang.Object
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5] [java.lang.Object _6]
   [java.lang.Object _7] [java.lang.Object _8]])

(jvm/defclass Variant java.lang.Object
  [[java.lang.String tag]])
(jvm/defclass Variant0 lux.Variant
  [])
(jvm/defclass Variant1 lux.Variant
  [[java.lang.Object _1]])
(jvm/defclass Variant2 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]])
(jvm/defclass Variant3 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3]])
(jvm/defclass Variant4 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]])
(jvm/defclass Variant5 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5]])
(jvm/defclass Variant6 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5] [java.lang.Object _6]])
(jvm/defclass Variant7 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5] [java.lang.Object _6]
   [java.lang.Object _7]])
(jvm/defclass Variant8 lux.Variant
  [[java.lang.Object _1] [java.lang.Object _2]
   [java.lang.Object _3] [java.lang.Object _4]
   [java.lang.Object _5] [java.lang.Object _6]
   [java.lang.Object _7] [java.lang.Object _8]])

## Base functions & macros
(defmacro (list xs)
  (case xs
    #Nil
    (#Tag "Nil")

    (#Cons x xs*)
    (#Form (#Cons (#Tag "Cons") (#Cons x (#Cons (list xs*) #Nil))))))

(def (++ xs ys)
  (case xs
    #Nil
    ys

    (#Cons x xs*)
    (#Cons x (++ xs* ys))))

(def (map f xs)
  (case xs
    #Nil
    #Nil

    (#Cons x xs*)
    (#Cons (f x) (map f xs*))))

(def (untemplate-list untemplate tokens)
  (case tokens
    #Nil
    (#Tag "Nil")

    (#Cons token tokens')
    (#Form (list (#Tag "Cons") (untemplate token) (untemplate-list untemplate tokens')))))

(def (untemplate token)
  (case token
    (#Bool elem)
    (#Form (list (#Tag "Bool") (#Bool elem)))

    (#Int elem)
    (#Form (list (#Tag "Int") (#Int elem)))

    (#Real elem)
    (#Form (list (#Tag "Real") (#Real elem)))

    (#Char elem)
    (#Form (list (#Tag "Char") (#Char elem)))

    (#Text elem)
    (#Form (list (#Tag "Text") (#Text elem)))

    (#Tag elem)
    (#Form (list (#Tag "Tag") (#Text elem)))

    (#Ident elem)
    (#Form (list (#Tag "Ident") (#Text elem)))

    (#Form (#Cons (#Ident "~") (#Cons unquoted #Nil)))
    unquoted

    (#Tuple elems)
    (#Form (list (#Tag "Tuple") (untemplate-list untemplate elems)))

    (#Form elems)
    (#Form (list (#Tag "Form") (untemplate-list untemplate elems)))
    ))


## I/O
(def (print x)
  (jvm/invokevirtual java.io.PrintStream "print" [Object]
                     (jvm/getstatic System out) [x]))

(def (println x)
  (jvm/invokevirtual java.io.PrintStream "println" [Object]
                     (jvm/getstatic System out) [x]))

(defmacro (' form)
  (case form
    (#Cons token #Nil)
    (untemplate token)))

(def (+ x y)
  (jvm/i+ x y))

(def inc (+ 1))

(def (id x)
  x)

(def (fold f init values)
  (case values
    #Nil
    init
    (#Cons x xs)
    (fold f (f init x) xs)))

(def length (fold (lambda [l x] (inc l)) 0))

(def (rem dividend divisor)
  (jvm/irem dividend divisor))

(def (= x y)
  (jvm/invokevirtual Object "equals" [Object]
                     x [y]))

(def (pairs list)
  (case list
    (#Cons x (#Cons y list*))
    (#Cons [x y] (pairs list*))

    _
    #Nil))

(def (show x)
  (jvm/invokevirtual Object "toString" []
                     x []))

(def (concat t1 t2)
  (jvm/invokevirtual String "concat" [String]
                     t1 [t2]))

(def (range from to)
  (if (= from to)
    #Nil
    (#Cons from (range (inc from) to))))

(def (text->list text)
  (let length (jvm/invokevirtual String "length" []
                                 text [])
       (map (lambda [idx]
               (jvm/invokevirtual String "charAt" [int]
                                  text [idx]))
            (range 0 length))))

(def (cons tail head)
  (#Cons head tail))

(def (reverse list)
  (fold cons #Nil list))

(def (enumerate list)
  (case (fold (lambda [state x]
                 (case state
                   [idx list']
                   [(inc idx) (#Cons [idx x] list')]))
              [0 #Nil]
              list)
    [_ list']
    (reverse list')))

(def list-map #Nil)

(def (put key val map)
  (case map
    #Nil
    (#Cons [key val] map)

    (#Cons [?key ?val] map')
    (if (= key ?key)
      (#Cons [?key val] map')
      (#Cons [?key ?val] (put key val map')))))

(def (get key map)
  (case map
    #Nil
    #None
    
    (#Cons [?key ?val] map')
    (if (= key ?key)
      (#Some ?val)
      (get key map'))))

(def (show-kv kv)
  (case kv
    [?key ?val]
    (fold concat "" (list "#" ?key " " (show ?val)))))

(def (interpose elem list)
  (case list
    (#Cons x (#Cons y list'))
    (#Cons x (#Cons elem (#Cons y list')))

    _
    list))

(def (show-list xs)
  (case xs
    #Nil
    "#Nil"
    (#Cons x xs')
    (fold concat "" (list "(#Cons " (show x) " " (show-list xs') ")"))))