aboutsummaryrefslogtreecommitdiff
path: root/examples/interpreter.csx
blob: b82caab52886c283971bc006748584e55c25ee8a (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
{ 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 readed [readobj]]
  [write readed] [output newline]
  [set res [run readed usercontext]]
  [write [head res]]
  [output newline]
  [read [tail res]]
]]

[output "-= CSX interpreter loaded =-" newline]
[output "========= Base functions: ========================================" newline]
[output "[set name value] | name will have the value value" newline]
[output "[set? name]      | check if name is set as some value" newline]
[output "[sethead p v]    | head of p will be changed to the v value" newline]
[output "[settail p v]    | head of p will be changed to the v value" newline]
[output "[pair head tail] | creates pair of head and value" newline]
[output "[head pair]      | returns head of pair" newline]
[output "[tail pair]      | returns tail of pair" newline]
[output "[quote x]        | returns unevaluated x" newline]
[output "[same a b]       | checks if a and b are the same thing" newline]
[output "[type x]         | returns type of x" newline]
[output "[do ...]         | runs statements returning value of the last one" newline]
[output "[fn args ...]    | creates function" newline]
[output "                 | you can make vararg function with pairs" newline]
[output "[sx args ...]    | same as fn but for macro" newline]
[output "[if a b ... c]   | executes b if a (for each a), else c" newline]
[output "[+ ...]          | sum of the arguments" newline]
[output "[* ...]          | product of the arguments" newline]
[output "[neg a]          | negate a" newline]
[output "[div a b]        | a / b" newline]
[output "[mod a b]        | a % b" newline]
[output "[< ...]          | checks if arguments are increasing" newline]
[output "[> ...]          | checks if arguments are decreasing" newline]
[output "[out ch]         | outputs character with ch code" newline]
[output "[in]             | null if input ended, else character code" newline]
[output "[name str]       | creates name from str string" newline]
[output "[str obj]        | creates str from name or list of codes" newline]
[output "[len obj]        | length of list or string" newline]
[output "[run x]          | runs the x expression" newline]
[output "[run x context]  | runs the x expression in given context" newline]
[output "                 | returns pair of value and new context" newline]
[output "[context]        | returns current context" newline]
[output "[newcontext]     | returns new base context" newline]
[output "==================================================================" newline]
[output "Have a good time!" newline]
[read [newcontext]]