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()))
52 Shared
Function eval_ast(ast
As MalVal
, env
As MalEnv
) As MalVal
53 If TypeOf ast Is MalSymbol
Then
54 return env
.do_get(DirectCast(ast
, MalSymbol
))
55 Else If TypeOf ast Is MalList
Then
56 Dim old_lst
As MalList
= DirectCast(ast
, MalList
)
57 Dim new_lst
As MalList
61 new_lst
= DirectCast(New MalVector
, MalList
)
64 For Each mv
in old_lst
.getValue()
65 new_lst
.conj_BANG(EVAL(mv
, env
))
68 Else If TypeOf ast Is MalHashMap
Then
69 Dim new_dict
As New Dictionary(Of
String, MalVal
)
70 Dim entry
As KeyValuePair(Of
String, MalVal
)
71 For Each entry
in DirectCast(ast
,MalHashMap
).getValue()
72 new_dict
.Add(entry
.Key
, EVAL(DirectCast(entry
.Value
,MalVal
), env
))
74 return New MalHashMap(new_dict
)
81 ' TODO: move to types.vb when it is ported
84 Public params
As MalList
86 Function fn(args
as MalList
) As MalVal
87 return EVAL(ast
, new MalEnv(env
, params
, args
))
91 Shared
Function EVAL(orig_ast
As MalVal
, env
As MalEnv
) As MalVal
94 'Console.WriteLine("EVAL: {0}", printer._pr_str(orig_ast, true))
95 If not orig_ast
.list_Q() Then
96 return eval_ast(orig_ast
, env
)
100 Dim ast
As MalList
= DirectCast(orig_ast
, MalList
)
101 If ast
.size() = 0 Then
104 Dim a0
As MalVal
= ast(0)
106 If TypeOf a0 is MalSymbol
Then
107 a0sym
= DirectCast(a0
,MalSymbol
).getName()
114 Dim a1
As MalVal
= ast(1)
115 Dim a2
As MalVal
= ast(2)
116 Dim res
As MalVal
= EVAL(a2
, env
)
117 env
.do_set(DirectCast(a1
,MalSymbol
), res
)
120 Dim a1
As MalVal
= ast(1)
121 Dim a2
As MalVal
= ast(2)
124 Dim let_env
As new MalEnv(env
)
125 For i
As Integer = 0 To (DirectCast(a1
,MalList
)).size()-1 Step
2
126 key
= DirectCast(DirectCast(a1
,MalList
)(i
),MalSymbol
)
127 val
= DirectCast(a1
,MalList
)(i
+1)
128 let_env
.do_set(key
, EVAL(val
, let_env
))
135 orig_ast
= quasiquote(ast(1))
137 eval_ast(ast
.slice(1, ast
.size()-1), env
)
138 orig_ast
= ast(ast
.size()-1)
140 Dim a1
As MalVal
= ast(1)
141 Dim cond
As MalVal
= EVAL(a1
, env
)
142 If cond Is Mal
.types
.Nil
or cond Is Mal
.types
.MalFalse
Then
143 ' eval false slot form
144 If ast
.size() > 3 Then
150 ' eval true slot form
155 Dim fc
As New FClosure()
157 fc
.params
= DirectCast(ast(1),MalLIst
)
159 Dim f
As Func(Of MalList
, MalVal
) = AddressOf fc
.fn
160 Dim mf
As new MalFunc(ast(2), env
,
161 DirectCast(ast(1),MalList
), f
)
162 return DirectCast(mf
,MalVal
)
164 Dim el
As MalList
= DirectCast(eval_ast(ast
, env
), MalList
)
165 Dim f
As MalFunc
= DirectCast(el(0), MalFunc
)
166 Dim fnast
As MalVal
= f
.getAst()
167 If not fnast Is
Nothing
169 env
= f
.genEnv(el
.rest())
171 Return f
.apply(el
.rest())
179 Shared
Function PRINT(exp
As MalVal
) As String
180 return printer
._pr_str(exp
, TRUE)
184 Shared repl_env
As MalEnv
186 Shared
Function REP(str
As String) As String
187 Return PRINT(EVAL(READ(str
), repl_env
))
190 Shared
Function do_eval(args
As MalList
) As MalVal
191 Return EVAL(args(0), repl_env
)
194 Shared
Function Main
As Integer
195 Dim args
As String() = Environment
.GetCommandLineArgs()
197 repl_env
= New MalEnv(Nothing)
199 ' core.vb: defined using VB.NET
200 For Each entry
As KeyValuePair(Of
String,MalVal
) In core
.ns()
201 repl_env
.do_set(new MalSymbol(entry
.Key
), entry
.Value
)
203 repl_env
.do_set(new MalSymbol("eval"), new MalFunc(AddressOf do_eval
))
204 Dim fileIdx
As Integer = 1
205 If args
.Length
> 1 AndAlso
args(1) = "--raw" Then
206 Mal
.readline
.SetMode(Mal
.readline
.Modes
.Raw
)
209 Dim argv
As New MalList()
210 For i
As Integer = fileIdx
+1 To args
.Length
-1
211 argv
.conj_BANG(new MalString(args(i
)))
213 repl_env
.do_set(new MalSymbol("*ARGV*"), argv
)
215 ' core.mal: defined using the language itself
216 REP("(def! not (fn* (a) (if a false true)))")
217 REP("(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) ""\nnil)"")))))")
219 If args
.Length
> fileIdx
Then
220 REP("(load-file """ & args(fileIdx
) & """)")
228 line
= Mal
.readline
.Readline("user> ")
229 If line is
Nothing Then
235 Catch e
As IOException
236 Console
.WriteLine("IOException: " & e
.Message
)
239 Console
.WriteLine(REP(line
))
241 Console
.WriteLine("Error: " & e
.Message
)
242 Console
.WriteLine(e
.StackTrace
)