aboutsummaryrefslogtreecommitdiff
path: root/vb/types.vb
blob: 4da75bdca42fe96155564ed33b58ae30f2b3ad72 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports Mal

namespace Mal
    Public Class types
        '
        ' Exceptiosn/Errors
        '
        Public Class MalThrowable
            Inherits Exception
            Public Sub New()
                MyBase.New()
            End Sub
            Public Sub New(msg As String)
                MyBase.New(msg)
            End Sub
        End Class
        Public Class MalError
            Inherits MalThrowable
            Public Sub New(msg As String)
                MyBase.New(msg)
            End Sub
        End Class
        Public Class MalContinue
            Inherits MalThrowable
        End Class

        ' Thrown by throw function
        Public Class MalException
            Inherits MalThrowable
            Private value As MalVal

            'string Message
            Public Sub New(new_value As MalVal)
                value = new_value
            End Sub
            Public Sub New(new_value As String)
                MyBase.New(new_value)
                value = New MalString(new_value)
            End Sub
            Public Function getValue() As MalVal
                return value
            End Function
        End Class

        '
        ' General functions
        '
        Public Shared Function _equal_Q(a As MalVal, b As MalVal) As Boolean
            Dim ota As Type = a.GetType()
            Dim otb As Type = b.GetType()
            If not (ota = otb Or
                (TypeOf a Is MalList and TypeOf b Is MalList)) Then
                return False
            Else
                If TypeOf a Is MalInt Then
                    return DirectCast(a,MalInt).getValue() =
                           DirectCast(b,MalInt).getValue()
                Else If TypeOf a Is MalSymbol Then
                    return DirectCast(a,MalSymbol).getName() =
                           DirectCast(b,MalSymbol).getName()
                Else If TypeOf a Is MalString Then
                    return DirectCast(a,MalString).getValue() =
                           DirectCast(b,MalString).getValue()
                Else If TypeOf a Is MalList Then
                    If DirectCast(a,MalList).size() <>
                       DirectCast(b,MalList).size()
                        return False
                    End If
                    for i As Integer = 0 To DirectCast(a,MalList).size()-1
                        If not _equal_Q(DirectCast(a,MalList)(i),
                                        DirectCast(b,MalList)(i))
                            return False
                        End If
                    Next
                    return True
                Else
                    return a Is b
                End If
            End If
        End Function


        Public MustInherit Class MalVal
            Private meta As MalVal = Nil
            Public Overridable Function copy() As MalVal
                return DirectCast(Me.MemberwiseClone(),MalVal)
            End Function

            ' Default is just to call regular toString()
            Public Overridable Function ToString() As String
                throw New MalException("ToString called on abstract MalVal")
            End Function
            Public Overridable Function ToString(print_readably As Boolean) As String
                return Me.ToString()
            End Function
            Public Function getMeta() As MalVal
                return meta
            End Function
            Public Function setMeta(m As MalVal) As MalVal
                meta = m
                return Me
            End Function
            Public Overridable Function list_Q() As Boolean
                return False
            End Function
        End Class

        Public Class MalConstant
            Inherits MalVal
            Private value As String
            Public Sub New(name As String)
                value = name
            End Sub
            Public Shadows Function copy() As MalConstant
                return Me
            End Function

            Public Overrides Function ToString() As String
                return value
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                return value
            End Function
        End Class

        Public Shared Nil As MalConstant = New MalConstant("nil")
        Public Shared MalTrue As MalConstant = New MalConstant("true")
        Public Shared MalFalse As MalConstant = New MalConstant("false")

        Public Class MalInt
            Inherits MalVal
            Private value As Int64
            Public Sub New(v As Int64)
                value = v
            End Sub
            Public Shadows Function copy() As MalInt 
                return Me
            End Function

            Public Function getValue() As Int64
                return value
            End Function
            Public Overrides Function ToString() As String
                return value.ToString()
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                return value.ToString()
            End Function
            Public Shared Operator <(a As MalInt, b As Malint) As MalConstant
                If a.getValue() < b.getValue() Then
                    return MalTrue
                Else
                    return MalFalse
                End If
            End Operator
            Public Shared Operator <=(a As MalInt, b As Malint) As MalConstant
                If a.getValue() <= b.getValue() Then
                    return MalTrue
                Else
                    return MalFalse
                End If
            End Operator
            Public Shared Operator >(a As MalInt, b As Malint) As MalConstant
                If a.getValue() > b.getValue() Then
                    return MalTrue
                Else
                    return MalFalse
                End If
            End Operator
            Public Shared Operator >=(a As MalInt, b As Malint) As MalConstant
                If a.getValue() >= b.getValue() Then
                    return MalTrue
                Else
                    return MalFalse
                End If
            End Operator
            Public Shared Operator +(a As MalInt, b As Malint) As MalInt
                return new MalInt(a.getValue() + b.getValue())
            End Operator
            Public Shared Operator -(a As MalInt, b As Malint) As MalInt
                return new MalInt(a.getValue() - b.getValue())
            End Operator
            Public Shared Operator *(a As MalInt, b As Malint) As MalInt
                return new MalInt(a.getValue() * b.getValue())
            End Operator
            Public Shared Operator /(a As MalInt, b As Malint) As MalInt
                return new MalInt(a.getValue() / b.getValue())
            End Operator
        End Class

        Public Class MalSymbol
            Inherits MalVal
            Private value As String
            Public Sub New(v As String)
                value = v
            End Sub
            Public Sub New(v As MalString)
                value = v.getValue()
            End Sub
            Public Shadows Function copy() As MalSymbol
                return Me
            End Function

            Public Function getName() As String
                return value
            End Function
            Public Overrides Function ToString() As String
                return value
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                return value
            End Function
        End Class

        Public Class MalString
            Inherits MalVal
            Private value As String
            Public Sub New(v As String)
                value = v
            End Sub
            Public Shadows Function copy() As MalString
                return Me
            End Function

            Public Function getValue() As String
                return value
            End Function
            Public Overrides Function ToString() As String
                return """" & value & """"
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                If value.Length > 0 AndAlso value(0) = ChrW(&H029e) Then
                    return ":" & value.Substring(1)
                Else If print_readably Then
                    return """" & _
                           value.Replace("\", "\\") _
                                .Replace("""", "\""") _
                                .Replace(Environment.NewLine, "\n") & _
                                """"
                Else
                    return value
                End If
            End Function
        End Class


        Public Class MalList
            Inherits MalVal
            Public start As String = "("
            Public last As String = ")"
            Private value As List(Of MalVal)
            Public Sub New()
                value = New List(Of MalVal)
            End Sub
            Public Sub New(val As List(Of MalVal))
                value = val
            End Sub
            Public Sub New(ParamArray mvs() As MalVal)
                value = New List(Of MalVal)
                conj_BANG(mvs)
            End Sub

            Public Function getValue() As List(Of MalVal)
                return value
            End Function
            Public Overrides Function list_Q() As Boolean
                return True
            End Function

            Public Overrides Function ToString() As String
                return start & printer.join(value, " ", true) & last
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                return start & printer.join(value, " ", print_readably) & last
            End Function

            Public Function conj_BANG(ParamArray mvs() As MalVal) As MalList 
                For i As Integer = 0 To mvs.Length-1
                    value.Add(mvs(i))
                Next
                return Me
            End Function

            Public Function size() As Int64
                return value.Count
            End Function
            Public Function nth(ByVal idx As Integer) As MalVal 
                If value.Count > idx Then
                    return value(idx)
                Else
                    return Nil
                End If
            End Function
            Default Public ReadOnly Property Item(idx As Integer) As MalVal
                Get
                    If value.Count > idx then
                        return value(idx)
                    Else
                        return Nil
                    End If
                End Get
            End Property
            Public Function rest() As MalList
                If size() > 0 Then
                    return New MalList(value.GetRange(1, value.Count-1))
                Else
                    return New MalList()
                End If
            End Function
            Public Overridable Function slice(start As Int64) As MalList 
                return New MalList(value.GetRange(start, value.Count-start))
            End Function
            Public Overridable Function slice(start As Int64, last As Int64) As MalList
                return New MalList(value.GetRange(start, last-start))
            End Function
        End Class

        Public Class MalVector
            Inherits MalList
'            ' Same implementation except for instantiation methods
            Public Sub New()
                MyBase.New()
                start = "["
                last = "]"
            End Sub
            Public Sub New(val As List(Of MalVal))
                MyBase.New(val)
                start = "["
                last = "]"
            End Sub

            Public Overrides Function list_Q() As Boolean
                return False
            End Function

            Public Overrides Function slice(start As Int64, last As Int64) As MalList
                Dim val As List(Of MalVal) = Me.getValue()
                return New MalVector(val.GetRange(start, val.Count-start))
            End Function
        End Class

        Public Class MalHashMap
            Inherits MalVal
            Private value As Dictionary(Of string, MalVal)
            Public Sub New(val As Dictionary(Of String, MalVal))
                value = val
            End Sub
            Public Sub New(lst As MalList)
                value = New Dictionary(Of String, MalVal)
                assoc_BANG(lst)
            End Sub
            Public Shadows Function copy() As MalHashMap 
                Dim new_self As MalHashMap = DirectCast(Me.MemberwiseClone(),MalHashMap)
                new_self.value = New Dictionary(Of String, MalVal)(value)
                return new_self
            End Function

            Public Function getValue() As Dictionary(Of String, MalVal)
                return value
            End Function

            Public Overrides Function ToString() As String
                return "{" & printer.join(value, " ", true) & "}"
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                return "{" & printer.join(value, " ", print_readably) & "}"
            End Function

            Public Function assoc_BANG(lst As MalList) As MalHashMap
                For i As Integer = 0 To lst.size()-1 Step 2
                    value(DirectCast(lst(i),MalString).getValue()) = lst(i+1)
                Next
                return Me
            End Function

            Public Function dissoc_BANG(lst As MalList) As MalHashMap
                for i As Integer = 0 To lst.size()-1
                    value.Remove(DirectCast(lst.nth(i),MalString).getValue())
                Next
                return Me
            End Function
        End Class

        Public Class MalAtom
            Inherits MalVal
            Private value As MalVal
            Public Sub New(val As MalVal)
                value = val
            End Sub
            'Public MalAtom copy() { return New MalAtom(value) }
            Public Function getValue() As MalVal
                return value
            End Function
            Public Function setValue(val As MalVal) As MalVal
                value = val
                return value
            End Function
            Public Overrides Function ToString() As String
                return "(atom " & printer._pr_str(value, true) & ")"
            End Function
            Public Overrides Function ToString(print_readably As Boolean) As String
                return "(atom " & printer._pr_str(value, print_readably) & ")"
            End Function
        End Class

        Public Class MalFunc
            Inherits MalVal
            Private fn As Func(Of MalList, MalVal) = Nothing
            Private ast As MalVal = Nothing
            Private env As Mal.env.Env = Nothing
            Private fparams As MalList
            Private macro As Boolean = False
            Public Sub New(new_fn As Func(Of MalList, MalVal))
                fn = new_fn
            End Sub
            Public Sub New(new_ast As MalVal, new_env As Mal.env.Env,
                           new_fparams As MalList, new_fn As Func(Of MalList, MalVal))
                fn = new_fn
                ast = new_ast
                env = new_env
                fparams = new_fparams
            End Sub

            Public Overrides Function ToString() As String
                If Not ast Is Nothing Then
                    return "<fn* " & Mal.printer._pr_str(fparams,true) &
                           " " & Mal.printer._pr_str(ast, true) & ">"
                Else
                    return "<builtin_function " & fn.ToString() & ">"
                End If
            End Function

            Public Function apply(args As MalList) As MalVal
                return fn(args)
            End Function

            Public Function getAst() As MalVal
                return ast
            End Function
            Public Function getEnv() As Mal.env.Env
                return env
            End Function
            Public Function getFParams() As MalList
                return fparams
            End Function
            Public Function genEnv(args As MalList) As Mal.env.Env
                return New Mal.env.Env(env, fparams, args)
            End Function
            Public Function isMacro() As Boolean
                return macro
            End Function
            Public Sub setMacro()
                macro = true
            End Sub
        End Class
    End Class
End Namespace