3 Imports System
.Collections
.Generic
5 Imports MalVal
= Mal
.types
.MalVal
6 Imports MalInt
= Mal
.types
.MalInt
7 Imports MalString
= Mal
.types
.MalString
8 Imports MalSymbol
= Mal
.types
.MalSymbol
9 Imports MalList
= Mal
.types
.MalList
10 Imports MalVector
= Mal
.types
.MalVector
11 Imports MalHashMap
= Mal
.types
.MalHashMap
12 Imports MalFunc
= Mal
.types
.MalFunc
13 Imports MalEnv
= Mal
.env
.Env
18 Shared
Function READ(str
As String) As MalVal
19 Return reader
.read_str(str
)
23 Shared
Function is_pair(x
As MalVal
) As Boolean
24 return TypeOf x Is MalList AndAlso _
25 DirectCast(x
,MalList
).size() > 0
28 Shared
Function quasiquote(ast
As MalVal
) As MalVal
29 If not is_pair(ast
) Then
30 return New MalList(New MalSymbol("quote"), ast
)
32 Dim a0
As MalVal
= DirectCast(ast
,MalList
)(0)
33 If TypeOf a0 Is MalSymbol AndAlso _
34 DirectCast(a0
,MalSymbol
).getName() = "unquote" Then
35 return DirectCast(ast
,MalList
)(1)
36 Else If is_pair(a0
) Then
37 Dim a00
As MalVal
= DirectCast(a0
,MalList
)(0)
38 If TypeOf a00 is MalSymbol AndAlso _
39 DirectCast(a00
,MalSymbol
).getName() = "splice-unquote" Then
40 return New MalList(New MalSymbol("concat"),
41 DirectCast(a0
,MalList
)(1),
42 quasiquote(DirectCast(ast
,MalList
).rest()))
45 return New MalList(New MalSymbol("cons"),
47 quasiquote(DirectCast(ast
,MalList
).rest()))
51 Shared
Function is_macro_call(ast
As MalVal
, env
As MalEnv
) As Boolean
52 If TypeOf ast Is MalList
Then
53 Dim a0
As MalVal
= DirectCast(ast
,MalList
)(0)
54 If TypeOf a0 Is MalSymbol AndAlso _
55 env
.find(DirectCast(a0
,MalSymbol
)) IsNot
Nothing Then
56 Dim mac
As MalVal
= env
.do_get(DirectCast(a0
,MalSymbol
))
57 If TypeOf mac Is MalFunc AndAlso _
58 DirectCast(mac
,MalFunc
).isMacro() Then
66 Shared
Function macroexpand(ast
As MalVal
, env
As MalEnv
) As MalVal
67 While is_macro_call(ast
, env
)
68 Dim a0
As MalSymbol
= DirectCast(DirectCast(ast
,MalList
)(0),MalSymbol
)
69 Dim mac
As MalFunc
= DirectCast(env
.do_get(a0
),MalFunc
)
70 ast
= mac
.apply(DirectCast(ast
,MalList
).rest())
75 Shared
Function eval_ast(ast
As MalVal
, env
As MalEnv
) As MalVal
76 If TypeOf ast Is MalSymbol
Then
77 return env
.do_get(DirectCast(ast
, MalSymbol
))
78 Else If TypeOf ast Is MalList
Then
79 Dim old_lst
As MalList
= DirectCast(ast
, MalList
)
80 Dim new_lst
As MalList
84 new_lst
= DirectCast(New MalVector
, MalList
)
87 For Each mv
in old_lst
.getValue()
88 new_lst
.conj_BANG(EVAL(mv
, env
))
91 Else If TypeOf ast Is MalHashMap
Then
92 Dim new_dict
As New Dictionary(Of
String, MalVal
)
93 Dim entry
As KeyValuePair(Of
String, MalVal
)
94 For Each entry
in DirectCast(ast
,MalHashMap
).getValue()
95 new_dict
.Add(entry
.Key
, EVAL(DirectCast(entry
.Value
,MalVal
), env
))
97 return New MalHashMap(new_dict
)
104 ' TODO: move to types.vb when it is ported
107 Public params
As MalList
109 Function fn(args
as MalList
) As MalVal
110 return EVAL(ast
, new MalEnv(env
, params
, args
))
114 Shared
Function EVAL(orig_ast
As MalVal
, env
As MalEnv
) As MalVal
117 'Console.WriteLine("EVAL: {0}", printer._pr_str(orig_ast, true))
118 If not orig_ast
.list_Q() Then
119 return eval_ast(orig_ast
, env
)
123 Dim expanded
As MalVal
= macroexpand(orig_ast
, env
)
124 if not expanded
.list_Q() Then
125 return eval_ast(expanded
, env
)
127 Dim ast
As MalList
= DirectCast(expanded
, MalList
)
129 If ast
.size() = 0 Then
132 Dim a0
As MalVal
= ast(0)
134 If TypeOf a0 is MalSymbol
Then
135 a0sym
= DirectCast(a0
,MalSymbol
).getName()
142 Dim a1
As MalVal
= ast(1)
143 Dim a2
As MalVal
= ast(2)
144 Dim res
As MalVal
= EVAL(a2
, env
)
145 env
.do_set(DirectCast(a1
,MalSymbol
), res
)
148 Dim a1
As MalVal
= ast(1)
149 Dim a2
As MalVal
= ast(2)
152 Dim let_env
As new MalEnv(env
)
153 For i
As Integer = 0 To (DirectCast(a1
,MalList
)).size()-1 Step
2
154 key
= DirectCast(DirectCast(a1
,MalList
)(i
),MalSymbol
)
155 val
= DirectCast(a1
,MalList
)(i
+1)
156 let_env
.do_set(key
, EVAL(val
, let_env
))
163 orig_ast
= quasiquote(ast(1))
165 Dim a1
As MalVal
= ast(1)
166 Dim a2
As MalVal
= ast(2)
167 Dim res
As MalVal
= EVAL(a2
, env
)
168 DirectCast(res
,MalFunc
).setMacro()
169 env
.do_set(DirectCast(a1
,MalSymbol
), res
)
172 Dim a1
As MalVal
= ast(1)
173 return macroexpand(a1
, env
)
175 eval_ast(ast
.slice(1, ast
.size()-1), env
)
176 orig_ast
= ast(ast
.size()-1)
178 Dim a1
As MalVal
= ast(1)
179 Dim cond
As MalVal
= EVAL(a1
, env
)
180 If cond Is Mal
.types
.Nil
or cond Is Mal
.types
.MalFalse
Then
181 ' eval false slot form
182 If ast
.size() > 3 Then
188 ' eval true slot form
193 Dim fc
As New FClosure()
195 fc
.params
= DirectCast(ast(1),MalLIst
)
197 Dim f
As Func(Of MalList
, MalVal
) = AddressOf fc
.fn
198 Dim mf
As new MalFunc(ast(2), env
,
199 DirectCast(ast(1),MalList
), f
)
200 return DirectCast(mf
,MalVal
)
202 Dim el
As MalList
= DirectCast(eval_ast(ast
, env
), MalList
)
203 Dim f
As MalFunc
= DirectCast(el(0), MalFunc
)
204 Dim fnast
As MalVal
= f
.getAst()
205 If not fnast Is
Nothing
207 env
= f
.genEnv(el
.rest())
209 Return f
.apply(el
.rest())
217 Shared
Function PRINT(exp
As MalVal
) As String
218 return printer
._pr_str(exp
, TRUE)
222 Shared repl_env
As MalEnv
224 Shared
Function REP(str
As String) As String
225 Return PRINT(EVAL(READ(str
), repl_env
))
228 Shared
Function do_eval(args
As MalList
) As MalVal
229 Return EVAL(args(0), repl_env
)
232 Shared
Function Main
As Integer
233 Dim args
As String() = Environment
.GetCommandLineArgs()
235 repl_env
= New MalEnv(Nothing)
237 ' core.vb: defined using VB.NET
238 For Each entry
As KeyValuePair(Of
String,MalVal
) In core
.ns()
239 repl_env
.do_set(new MalSymbol(entry
.Key
), entry
.Value
)
241 repl_env
.do_set(new MalSymbol("eval"), new MalFunc(AddressOf do_eval
))
242 Dim fileIdx
As Integer = 1
243 If args
.Length
> 1 AndAlso
args(1) = "--raw" Then
244 Mal
.readline
.SetMode(Mal
.readline
.Modes
.Raw
)
247 Dim argv
As New MalList()
248 For i
As Integer = fileIdx
+1 To args
.Length
-1
249 argv
.conj_BANG(new MalString(args(i
)))
251 repl_env
.do_set(new MalSymbol("*ARGV*"), argv
)
253 ' core.mal: defined using the language itself
254 REP("(def! not (fn* (a) (if a false true)))")
255 REP("(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) ""\nnil)"")))))")
256 REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))")
258 If args
.Length
> fileIdx
Then
259 REP("(load-file """ & args(fileIdx
) & """)")
267 line
= Mal
.readline
.Readline("user> ")
268 If line is
Nothing Then
274 Catch e
As IOException
275 Console
.WriteLine("IOException: " & e
.Message
)
278 Console
.WriteLine(REP(line
))
280 Console
.WriteLine("Error: " & e
.Message
)
281 Console
.WriteLine(e
.StackTrace
)