VB.Net, C#: fix cmd line arg handling with --raw
[jackhill/mal.git] / vb / step3_env.vb
CommitLineData
ee7cd585
JM
1Imports System
2Imports System.IO
3Imports System.Collections.Generic
4Imports Mal
5Imports MalVal = Mal.types.MalVal
6Imports MalInt = Mal.types.MalInt
7Imports MalSymbol = Mal.types.MalSymbol
8Imports MalList = Mal.types.MalList
9Imports MalVector = Mal.types.MalVector
10Imports MalHashMap = Mal.types.MalHashMap
11Imports MalFunc = Mal.types.MalFunc
12Imports MalEnv = Mal.env.Env
13
14Namespace Mal
aaba2493 15 Class step3_env
ee7cd585
JM
16 ' read
17 Shared Function READ(str As String) As MalVal
18 Return reader.read_str(str)
19 End Function
20
21 ' eval
22 Shared Function eval_ast(ast As MalVal, env As MalEnv) As MalVal
23 If TypeOf ast Is MalSymbol Then
24 Dim sym As MalSymbol = DirectCast(ast, MalSymbol)
25 return env.do_get(sym.getName())
26 Else If TypeOf ast Is MalList Then
27 Dim old_lst As MalList = DirectCast(ast, MalList)
28 Dim new_lst As MalList
29 If ast.list_Q() Then
30 new_lst = New MalList
31 Else
32 new_lst = DirectCast(New MalVector, MalList)
33 End If
34 Dim mv As MalVal
35 For Each mv in old_lst.getValue()
36 new_lst.conj_BANG(EVAL(mv, env))
37 Next
38 return new_lst
39 Else If TypeOf ast Is MalHashMap Then
40 Dim new_dict As New Dictionary(Of String, MalVal)
41 Dim entry As KeyValuePair(Of String, MalVal)
42 For Each entry in DirectCast(ast,MalHashMap).getValue()
43 new_dict.Add(entry.Key, EVAL(DirectCast(entry.Value,MalVal), env))
44 Next
45 return New MalHashMap(new_dict)
46 Else
47 return ast
48 End If
49 return ast
50 End Function
51
52 Shared Function EVAL(orig_ast As MalVal, env As MalEnv) As MalVal
53 'Console.WriteLine("EVAL: {0}", printer._pr_str(orig_ast, true))
54 If not orig_ast.list_Q() Then
55 return eval_ast(orig_ast, env)
56 End If
57
58 ' apply list
59 Dim ast As MalList = DirectCast(orig_ast, MalList)
60 If ast.size() = 0 Then
61 return ast
62 End If
63 Dim a0 As MalVal = ast(0)
64 Select DirectCast(a0,MalSymbol).getName()
65 Case "def!"
66 Dim a1 As MalVal = ast(1)
67 Dim a2 As MalVal = ast(2)
68 Dim res As MalVal = EVAL(a2, env)
69 env.do_set(DirectCast(a1,MalSymbol).getName(), res)
70 return res
71 Case "let*"
72 Dim a1 As MalVal = ast(1)
73 Dim a2 As MalVal = ast(2)
74 Dim key As MalSymbol
75 Dim val as MalVal
76 Dim let_env As new MalEnv(env)
77 For i As Integer = 0 To (DirectCast(a1,MalList)).size()-1 Step 2
78 key = DirectCast(DirectCast(a1,MalList)(i),MalSymbol)
79 val = DirectCast(a1,MalList)(i+1)
80 let_env.do_set(key.getName(), EVAL(val, let_env))
81 Next
82 return EVAL(a2, let_env)
83 Case Else
84 Dim el As MalList = DirectCast(eval_ast(ast, env), MalList)
85 Dim f As MalFunc = DirectCast(el(0), MalFunc)
86 Return f.apply(el.rest())
87 End Select
88 End Function
89
90 ' print
91 Shared Function PRINT(exp As MalVal) As String
92 return printer._pr_str(exp, TRUE)
93 End Function
94
95 ' repl
96 Shared repl_env As MalEnv
97
98 Shared Function REP(str As String) As String
99 Return PRINT(EVAL(READ(str), repl_env))
100 End Function
101
102 Shared Function add(a As MalList) As MalVal
103 Return DirectCast(a.Item(0),MalInt) + DirectCast(a.Item(1),MalInt)
104 End Function
105
106 Shared Function minus(a As MalList) As MalVal
107 Return DirectCast(a.Item(0),MalInt) - DirectCast(a.Item(1),MalInt)
108 End Function
109
110 Shared Function mult(a As MalList) As MalVal
111 Return DirectCast(a.Item(0),MalInt) * DirectCast(a.Item(1),MalInt)
112 End Function
113
114 Shared Function div(a As MalList) As MalVal
115 Return DirectCast(a.Item(0),MalInt) / DirectCast(a.Item(1),MalInt)
116 End Function
117
118 Shared Function Main As Integer
119 Dim args As String() = Environment.GetCommandLineArgs()
120
121 repl_env = New MalEnv(Nothing)
122 repl_env.do_set("+", New MalFunc(AddressOf add))
123 repl_env.do_set("-", New MalFunc(AddressOf minus))
124 repl_env.do_set("*", New MalFunc(AddressOf mult))
125 repl_env.do_set("/", New MalFunc(AddressOf div))
126
127
128 If args.Length > 1 AndAlso args(1) = "--raw" Then
129 Mal.readline.SetMode(Mal.readline.Modes.Raw)
130 End If
131
132 ' repl loop
133 Dim line As String
134 Do
135 Try
136 line = Mal.readline.Readline("user> ")
137 If line is Nothing Then
138 Exit Do
139 End If
140 If line = "" Then
141 Continue Do
142 End If
143 Catch e As IOException
144 Console.WriteLine("IOException: " & e.Message)
145 End Try
146 Try
147 Console.WriteLine(REP(line))
148 Catch e as Exception
149 Console.WriteLine("Error: " & e.Message)
150 Console.WriteLine(e.StackTrace)
151 Continue Do
152 End Try
153 Loop While True
154 End function
aaba2493 155 End Class
ee7cd585 156End Namespace