aboutsummaryrefslogtreecommitdiff
path: root/examples/interpreter.csx
blob: 2d87e8132efd17492075a153bf8957707fa30bf4 (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
{ Base Utilities }

[set no [fn [x] [same x []]]]
[set id [fn [arg] arg]]
[set list [fn args args]]

[set catrev [fn [a b] [if a [catrev [tail a] [pair [head a] b]] b]]]
[set rev [fn [l] [catrev l []]]]
[set cat [fn [a b] [catrev [rev a] b]]]

[set map [fn [f l] [if l [pair [f [head l]] [map f [tail l]]]]]]
[set reduce [fn [f l] [if [no l] [] [if [no [tail l]] [head l]
  [f [head l] [reduce f [tail l]]]
]]]]


{ Input-Output }

[set newline [str [list 10]]]

[set outint [fn [n]
  [set zero 48]
  [set minus 45]
  [if [< n 0]
    [do [out minus] [outint [neg n]]]
    [if [< n 10]
      [out [+ zero n]]
      [do
        [outint [div n 10]]
        [out [+ zero [mod n 10]]]
      ]
    ]
  ]
]]

[set outstr [fn [str]
  [set outstrat [fn [str i len] [if [no [same i len]] [do
    [out [str i]]
    [outstrat str [+ i 1] len]
  ]]]]
  [outstrat str 0 [len str]]
]]

[set output [fn objs [map [fn [obj] [if
  [same [type obj] 'int] [outint obj]
  [same [type obj] 'str] [outstr obj]
]] objs]]]

[set instrlist [fn []
  [set c [in]]
  [if [no [same c 10]] [pair c [instrlist]]]
]]
[set instr [fn [] [str [instrlist]]]]


{ The Program }

[set screenstr [fn [str]
  [set outstrat [fn [str i len] [if [no [same i len]] [do
    [if [same [str i] ["\\" 0]] [output "\\"]]
    [if [same [str i] ["\"" 0]] [output "\\"]]
    [out [str i]]
    [outstrat str [+ i 1] len]
  ]]]]
  [outstrat str 0 [len str]]
]]

[set screenname [fn [str]
  [set outstrat [fn [str i len] [if [no [same i len]] [do
    [if [same [str i] ["\\" 0]] [output "\\"]]
    [if [same [str i] [" " 0]] [output "\\"]]
    [if [same [str i] ["[" 0]] [output "\\"]]
    [if [same [str i] ["]" 0]] [output "\\"]]
    [out [str i]]
    [outstrat str [+ i 1] len]
  ]]]]
  [outstrat str 0 [len str]]
]]

[set writeeach [fn [l] [if l [do
  [write [head l]]
  [if [tail l] [do
    [output " "]
    [writeeach [tail l]]
  ]]
]]]]

[set write [fn [obj] [if
  [no obj] [output "[]"]
  [same [type obj] 'int] [outint obj]
  [same [type obj] 'str] [do [output "\""] [screenstr obj] [output "\""]]
  [same [type obj] 'pair] [do [output "["] [writeeach obj] [output "]"]]
  [same [type obj] 'name] [screenname [str obj]]
  [same [type obj] 'base] [output "<base>"]
  [same [type obj] 'int] [output obj]
  [same [type obj] 'fn] [output "<fn>"]
  [same [type obj] 'sx] [output "<sx>"]
]]]


[set linep [pair [] []]]

[set readcomment [fn [] [if
  [same [head [head linep]] ["}" 0]] [sethead linep [tail [head linep]]]
  [same [head [head linep]] ["{" 0]] [do
    [sethead linep [tail [head linep]]]
    [readcomment]
    [readcomment]
  ]
  [do
    [sethead linep [tail [head linep]]]
    [readcomment]
  ]
]]]

[set pairlist [fn [l] [if l [if [tail l]
  [pair [head l] [pairlist [tail l]]]
  [head l]
]]]]

[set readlist [fn [] [if
  [same [head [head linep]] ["]" 0]] [sethead linep [tail [head linep]]]
  [same [head [head linep]] [" " 0]] [do
    [sethead linep [tail [head linep]]]
    [readlist]
  ]
  [do
    [set res [readobj]]
    [pair res [readlist]]
  ]
]]]

[set readint [fn [num]
  [if [head linep] [if [< 47 [head [head linep]] 58] [do
    [set newnum [+ [* 10 num] [head [head linep]] -48]]
    [sethead linep [tail [head linep]]]
    [readint newnum]
  ] num] num]
]]

[set readname [fn []
  [if [head linep] [if [no [same [head [head linep]] [" " 0]]]
    [if [no [same [head [head linep]] ["[" 0]]] [if [no [same [head [head linep]] ["]" 0]]] [do
      [if [same [head [head linep]] ["\\" 0]] [sethead linep [tail [head linep]]]]
      [set res [head [head linep]]]
      [sethead linep [tail [head linep]]]
      [pair res [readname]]
    ]]]
  ]]
]]

[set readstr [fn []
  [if [head linep] [if [no [same [head [head linep]] ["\"" 0]]][do
    [if [same [head [head linep]] ["\\" 0]] [sethead linep [tail [head linep]]]]
    [set res [head [head linep]]]
    [sethead linep [tail [head linep]]]
    [pair res [readstr]]
  ] [sethead linep [tail [head linep]]]]]
]]

[set readobj [fn [] [if
  [same [head [head linep]] ["{" 0]] [do
    [sethead linep [tail [head linep]]]
    [readcomment]
    [readobj]
  ]
  [same [head [head linep]] [" " 0]] [do
    [sethead linep [tail [head linep]]]
    [readobj]
  ]
  [same [head [head linep]] ["[" 0]] [do
    [sethead linep [tail [head linep]]]
    [readlist]
  ]
  [same [head [head linep]] ["=" 0]] [if
    [same [head [tail [head linep]]] ["[" 0]] [do
      [sethead linep [tail [tail [head linep]]]]
      [pairlist [readlist]]
    ]
    [name [str [readname]]]
  ]
  [same [head [head linep]] ["-" 0]] [if
    [< 47 [head [tail [head linep]]] 58] [do
      [sethead linep [tail [head linep]]]
      [neg [readint 0]]
    ]
    [name [str [readname]]]
  ]
  [same [head [head linep]] ["'" 0]] [do
    [sethead linep [tail [head linep]]]
    [list quote [readobj]]
  ]
  [same [head [head linep]] ["\"" 0]] [do
    [sethead linep [tail [head linep]]]
    [str [readstr]]
  ]
  [< 47 [head [head linep]] 58] [readint 0]
  [name [str [readname]]]
]]]

[set read [fn [usercontext]
  [output "> "]
  [sethead linep [instrlist]]
  [set res [run [readobj] usercontext]]
  [write [head res]]
  [output newline]
  [read [tail res]]
]]

[read [newcontext]]