Browse Source

Delete monty.sol

Benjamin Lannon 5 years ago
parent
commit
1e785c3f38
1 changed files with 0 additions and 472 deletions
  1. 0
    472
      monty.sol

+ 0
- 472
monty.sol View File

@@ -1,472 +0,0 @@
1
-TOK = {LPAREN = 1, RPAREN = 2, INT = 3, BOOL = 4, NAME = 5, QUOTE = 6, EOF = 7}
2
-keys = []
3
-for k in TOK do keys:insert(#keys, k) end
4
-for k in keys do TOK[TOK[k]]=k end
5
-
6
-token = {
7
-	new = func (type, value)
8
-		return {type = type, value = value, __index = token}
9
-	end,
10
-	pretty = func(self)
11
-		tname = TOK[self.type]
12
-		tval = tostring(self.value)
13
-		return '{'+tname+':'+tval+'}'
14
-	end
15
-}
16
-
17
-tokenizer = {
18
-	WS = " "+chr(8)+chr(9)+chr(10),
19
-	NAMESET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=+-*/.<>?!@$%^~",
20
-	DIGITS = "0123456789",
21
-	EOF = {},
22
-	new = func (str)
23
-		res = {str = str, pushed = None, __index = tokenizer}
24
-		res:init()
25
-		return res
26
-	end,
27
-	init = func(self)
28
-		--print('In init, self is', self)
29
-		res.cur = res:token()
30
-		res.next = res:token()
31
-	end,
32
-	next_char = func(self)
33
-		if self.pushed == None then
34
-			--print('In next_char, self is', self)
35
-			--print('In next_char, self.str is', self.str)
36
-			if self.str:eof() then return self.EOF end
37
-			res = self.str:read(1)
38
-		else
39
-			--print('Retrieving from pushback', self.pushed)
40
-			res = self.pushed[0]
41
-			self.pushed = self.pushed:sub(1)
42
-			if self.pushed == "" then self.pushed = None end
43
-		end
44
-		--print(res)
45
-		return res
46
-	end,
47
-	push_back = func(self, s)
48
-		--print('Pushing back', s)
49
-		if s == self.EOF then print('WARNING: Attempted to push_back EOF'); return end
50
-		if self.pushed == None then
51
-			self.pushed = s
52
-		else
53
-			self.pushed = s + self.pushed
54
-		end
55
-		--print('self.pushed:', self.pushed)
56
-	end,
57
-	token = func (self)
58
-		--print('In token, self is', self)
59
-		--print('In token, self.str is', self.str)
60
-		c = self:next_char()
61
-		while !(c == self.EOF) do
62
-			if c == "" then return token.new(TOK.EOF, None) end
63
-			if c == "(" then return token.new(TOK.LPAREN, c) end
64
-			if c == ")" then return token.new(TOK.RPAREN, c) end
65
-			if self.NAMESET:find(c) >= 0 then
66
-				--print('{NAME}')
67
-				name = c
68
-				c = self:next_char()
69
-				while 1 do
70
-					found = 0
71
-					if self.NAMESET:find(c) >= 0 then found = 1 end
72
-					if self.DIGITS:find(c) >= 0 then found = 1 end
73
-					if !found then break end
74
-					name += c
75
-					c = self:next_char()
76
-					if c == self.EOF then continue end
77
-				end
78
-				self:push_back(c)
79
-				return token.new(TOK.NAME, name)
80
-			end
81
-			if self.DIGITS:find(c) >= 0 then
82
-				val = c
83
-				c = self:next_char()
84
-				while self.DIGITS:find(c) >= 0 do
85
-					val += c
86
-					c = self:next_char()
87
-					if c == self.EOF then continue end
88
-				end
89
-				self:push_back(c)
90
-				return token.new(TOK.INT, toint(val))
91
-			end
92
-			if c == "#" then
93
-				c = self:next_char()
94
-				if c == "t" then return token.new(TOK.BOOL, 1) end
95
-				if c == "f" then return token.new(TOK.BOOL, 0) end
96
-				error("Invalid value for bool literal: "+c)
97
-			end
98
-			if c == "'" then return token.new(TOK.QUOTE, c) end
99
-			if self.WS:find(c) >= 0 then
100
-				c = self:next_char()
101
-				continue
102
-			end
103
-			if c == ";" then
104
-				c = self:next_char()
105
-				while 1 do
106
-					if c == chr(10) then break end
107
-					c = self:next_char()
108
-				end
109
-				c = self:next_char()
110
-				continue
111
-			end
112
-			error("Invalid character in token stream: "+c)
113
-		end
114
-		return token.new(TOK.EOF, None)
115
-	end,
116
-	advance = func(self)
117
-		self.cur = self.next
118
-		self.next = self:token()
119
-	end
120
-}
121
-
122
-
123
-
124
-ttreegen = {
125
-	new = func(tok)
126
-		return {tok = tok, __index = ttreegen}
127
-	end,
128
-	generate = func(self, consume)
129
-		res = self.TT_DISPATCH[self.tok.cur.type](self, self.tok.cur)
130
-		if None == consume then self.tok:advance() end
131
-		return res
132
-	end,
133
-	TT_DISPATCH = {
134
-		[TOK.LPAREN] = func(self, tok)
135
-			toklist = []
136
-			self.tok:advance()
137
-			tok = self.tok.cur
138
-			while 1 do
139
-				if tok.type == TOK.RPAREN then break end
140
-				if tok.type == TOK.EOF then error('Encountered EOF while matching delimiter') end
141
-				toklist:insert(#toklist, self.TT_DISPATCH[tok.type](self, tok))
142
-				self.tok:advance()
143
-				tok = self.tok.cur
144
-			end
145
-			return toklist
146
-		end,
147
-		[TOK.RPAREN] = func(self, tok)
148
-			error("Unexpected right parenthesis")
149
-		end,
150
-		[TOK.INT] = func(self, tok)
151
-			return tok
152
-		end,
153
-		[TOK.BOOL] = func(self, tok)
154
-			return tok
155
-		end,
156
-		[TOK.NAME] = func(self, tok)
157
-			return tok
158
-		end,
159
-		[TOK.QUOTE] = func(self, tok)
160
-			self.tok:advance()
161
-			tok.quoting = self:generate(0)
162
-			return tok
163
-		end,
164
-		[TOK.EOF] = func(self, tok)
165
-			return None
166
-		end
167
-	}
168
-}
169
-
170
-EX = {CALL=1, ASSIGN=2, FUNCDECL=3, SCOPE=4, IFELSE=5, DATUM=6, LIT=7, REF=8, LIST=9}
171
-keys = []
172
-for k in EX do keys:insert(#keys, k) end
173
-for k in keys do EX[EX[k]]=k end
174
-
175
-node = {
176
-	new = func(type, value)
177
-		return {type=type, value=value, __index=node}
178
-	end,
179
-	pretty = func(self) return self.PRETTY_DISPATCH[self.type](self) end
180
-	PRETTY_DISPATCH = {
181
-		[EX.CALL] = func(self)
182
-			return '<CALL NAME='+(self.value.name)+' ARGS='+tostring(self.value.args:copy():map(func(i) if !(None == i) then return i:pretty() else return tostring(i) end end))+'>'
183
-		end,
184
-		[EX.ASSIGN] = func(self)
185
-			return '<ASSIGN NAME='+(self.value.name)+' VALUE='+(self.value.value:pretty())+'>'
186
-		end,
187
-		[EX.FUNCDECL] = func(self)
188
-			return '<FUNCDECL PARAMS='+tostring(self.value.params)+' BODY='+(self.value.body:pretty())+'>'
189
-		end,
190
-		[EX.SCOPE] = func(self)
191
-			return '<SCOPE '+(self.value:copy():map(func(i) return i:pretty() end))+'>'
192
-		end,
193
-		[EX.IFELSE] = func(self)
194
-			return '<IFELSE COND='+(self.value.cond:pretty())+' IFT='+(self.value.ift:pretty())+' IFF='+(self.value.iff:pretty())+'>'
195
-		end,
196
-		[EX.DATUM] = func(self)
197
-			return '#'+tostring(self.value)
198
-		end,
199
-		[EX.LIT] = func(self)
200
-			if type(self.value) == 'list' then
201
-				return '/'+tostring(self.value:copy():map(func(i) return i:pretty() end))
202
-			end
203
-			return '/'+tostring(self.value)
204
-		end,
205
-		[EX.REF] = func(self)
206
-			--print('In EX.REF, self is', self)
207
-			res = '@'+tostring(self.value)
208
-			--print('In EX.REF, returning', res)
209
-			return res
210
-		end,
211
-		[EX.LIST] = func(self)
212
-			--print('In EX.LIST, self is', self)
213
-			return '<LIST '+(self.value:copy():map(func(i) return i:pretty() end))+'>'
214
-		end
215
-	}
216
-}
217
-
218
-parser = {
219
-	new = func(ttgen)
220
-		return {ttgen = ttgen, __index = parser}
221
-	end,
222
-	parse = func(self, tt)
223
-		if type(tt) == 'map' then
224
-			--print('In parse, self is', self)
225
-			--print('In parse, tt is', tt)
226
-			--print('In parse, dispatch to', self.TT_PARSE_DISPATCH[tt.type])
227
-			res = self.TT_PARSE_DISPATCH[tt.type](self, tt)
228
-		else
229
-			name = tt[0]
230
-			if !(name.type == TOK.NAME) then
231
-				error('Expected name as first element of expression-list')
232
-			end
233
-			rest = tt:copy()
234
-			rest:remove(0)
235
-			sc = self.SCALL_DISPATCH[name.value]
236
-			if !(None == sc) then
237
-				sc = None
238
-				res = self.SCALL_DISPATCH[name.value](self, rest)
239
-			else
240
-				res = node.new(EX.CALL, {name=name.value, args=rest:map(func(i) return self:parse(i) end)})
241
-			end
242
-		end
243
-		--print('In parse, returning', res:pretty())
244
-		return res
245
-	end,
246
-	TT_PARSE_DISPATCH = {
247
-		[TOK.INT] = func(self, tok)
248
-			return node.new(EX.LIT, tok.value)
249
-		end,
250
-		[TOK.BOOL] = func(self, tok)
251
-			return node.new(EX.LIT, tok.value)
252
-		end,
253
-		[TOK.NAME] = func(self, tok)
254
-			--print('In TOK.NAME, tok is', tok)
255
-			res =  node.new(EX.REF, tok.value)
256
-			--print('In TOK.NAME, returning', res)
257
-			return res
258
-		end,
259
-		[TOK.QUOTE] = func(self, tok)
260
-			return self:parse_datum(tok.quoting)
261
-		end
262
-	},
263
-	SCALL_DISPATCH = {
264
-		define = func(self, args)
265
-			name = args[0]
266
-			if !(name.type == TOK.NAME) then error('Define: expected name as first argument') end
267
-			value = self:parse(args[1])
268
-			return node.new(EX.ASSIGN, {name=name.value, value=value})
269
-		end,
270
-		["if"] = func(self, args)
271
-			cond = self:parse(args[0])
272
-			ift = self:parse(args[1])
273
-			iff = self:parse(args[2])
274
-			return node.new(EX.IFELSE, {cond=cond, ift=ift, iff=iff})
275
-		end,
276
-		begin = func(self, args)
277
-			args:map(func(i) return self:parse(i) end)
278
-			return node.new(EX.LIST, args)
279
-		end,
280
-		lambda = func(self, args)
281
-			--print('Lambda args:', args)
282
-			params = args[0]
283
-			if !(type(params) == 'list') then error('Lambda: expected parameters as first argument (got '+tostring(params)+')') end
284
-			params:map(func(i)
285
-				if !(type(i) == 'map') then error('Lambda: expected name token in argument list (got sublist)') end
286
-				if !(i.type == TOK.NAME) then error('Lambda: expected name token in argument list (got '+(i:pretty())+')') end
287
-				return i.value
288
-			end)
289
-			body = args:copy()
290
-			body:remove(0)
291
-			--print('Lambda body:', body)
292
-			body:map(func(i) return self:parse(i) end)
293
-			return node.new(EX.FUNCDECL, {params=params, body=node.new(EX.LIST, body)})
294
-		end,
295
-		let = func(self, args)
296
-			defs = args[0]
297
-			if !(type(defs) == 'list') then error('Let: expected list of bindings are first argument') end
298
-			defs:map(func(i)
299
-				if !(type(i) == 'list') then error('Let: expected a binding entry') end
300
-				return self.SCALL_DISPATCH.define(self, i)
301
-			end)
302
-			body = args:copy()
303
-			body:remove(0)
304
-			body:map(func(i) return self:parse(i) end)
305
-			return node.new(EX.SCOPE, defs+body)
306
-		end,
307
-		letrec = func(self, args)
308
-			defs = args[0]
309
-			if !(type(defs) == 'list') then error('Let: expected list of bindings are first argument') end
310
-			defs:map(func(i)
311
-				if !(type(i) == 'list') then error('Let: expected a binding entry') end
312
-				return self.SCALL_DISPATCH.define(self, i)
313
-			end)
314
-			body = args:copy()
315
-			body:remove(0)
316
-			body:map(func(i) return self:parse(i) end)
317
-			return node.new(EX.LIST, defs+body)
318
-		end
319
-	}
320
-	parse_datum = func(self, tt)
321
-		if type(tt) == 'map' then
322
-			return self.TT_PARSE_DATUM_DISPATCH[tt.type](self, tt)
323
-		else
324
-			list = []
325
-			for tok in tt do
326
-				list:insert(#list, self:parse_datum(tok))
327
-			end
328
-			return node.new(EX.LIT, list)
329
-		end
330
-	end,
331
-	TT_PARSE_DATUM_DISPATCH = {
332
-		[TOK.INT] = func(self, tok)
333
-			return node.new(EX.LIT, tok.value)
334
-		end,
335
-		[TOK.BOOL] = func(self, tok)
336
-			return node.new(EX.LIT, tok.value)
337
-		end,
338
-		[TOK.NAME] = func(self, tok)
339
-			return node.new(EX.LIT, tok.value)
340
-		end,
341
-		[TOK.QUOTE] = func(self, tok)
342
-			return self:parse(tok.quoting)
343
-		end
344
-	},
345
-	run = func(self)
346
-		tt = self.ttgen:generate()
347
-		list = []
348
-		while 1 do
349
-			if None == tt then break end
350
-			--if type(tt) == 'list' then
351
-				list:insert(#list, self:parse(tt))
352
-			--end
353
-			tt = self.ttgen:generate()
354
-		end
355
-		--print('In run, list is', list)
356
-		return node.new(EX.LIST, list)
357
-	end
358
-}
359
-
360
-converter = {
361
-	new = func(p)
362
-		return {parser=p, __index = converter}
363
-	end,
364
-	make = func(self, node)
365
-		--print('In make, node is a', EX[node.type], 'of value', node)
366
-		res = self.MAKE_DISPATCH[node.type](self, node)
367
-		--print('In make, returning', res)
368
-		if type(res) == "astnode" then ast.print(res) end
369
-		return res
370
-	end,
371
-	MAKE_DISPATCH = {
372
-		[EX.CALL] = func(self, node)
373
-			e = parse('f()').stmtlist[0].expr
374
-			e.expr.ident = node.value.name
375
-			args = node.value.args:copy():map(func(i) return self:make(i) end)
376
-			--print('In EX.CALL, replacement args are', args)
377
-			e.args = args
378
-			--print('In EX.CALL, args are', e.args)
379
-			return e
380
-		end,
381
-		[EX.ASSIGN] = func(self, node)
382
-			e = parse('a = b').stmtlist[0].expr
383
-			e.ident = node.value.name
384
-			e.value = self:make(node.value.value)
385
-			return e
386
-		end,
387
-		[EX.FUNCDECL] = func(self, node)
388
-			e = parse('func() None None end').stmtlist[0].expr
389
-			params = node.value.params
390
-			--print('In EX.FUNCDECL, params are', params)
391
-			e.args = params
392
-			--print('In EX.FUNCDECL, args are', e.args)
393
-			e.body.stmtlist = self:make(node.value.body)
394
-			return e
395
-		end,
396
-		[EX.SCOPE] = func(self, node)
397
-			e = parse('(func() None None end)()').stmtlist[0].expr
398
-			node.type = EX.LIST
399
-			e.expr.body.stmtlist = self:make(node)
400
-			node.type = EX.SCOPE
401
-			return e
402
-		end,
403
-		[EX.IFELSE] = func(self, node)
404
-			e = parse('(func() if None then return None else return None end end)()').stmtlist[0].expr
405
-			e.expr.body.stmtlist[0].cond = self:make(node.value.cond)
406
-			e.expr.body.stmtlist[0].iftrue.stmtlist[0].ret = self:make(node.value.ift)
407
-			e.expr.body.stmtlist[0].iffalse.stmtlist[0].ret = self:make(node.value.iff)
408
-			return e
409
-		end,
410
-		[EX.DATUM] = func(self, node) error('EX.DATUM: Not implemented') end,
411
-		[EX.LIT] = func(self, node)
412
-			if type(node.value) == 'list' then
413
-				e = parse('[None]').stmtlist[0].expr
414
-				e.list = node.value:copy():map(func(i) return self:make(i) end)
415
-			else
416
-				e = parse('None').stmtlist[0].expr
417
-				if type(node.value) == "int" then
418
-					e.littype = ast.LIT_INT
419
-					e.ival = node.value
420
-				end
421
-				if type(node.value) == "string" then
422
-					e.littype = ast.LIT_STRING
423
-					e.str = node.value
424
-				end
425
-			end
426
-			return e
427
-		end,
428
-		[EX.REF] = func(self, node)
429
-			e = parse('a').stmtlist[0].expr
430
-			e.ident = node.value
431
-			return e
432
-		end,
433
-		[EX.LIST] = func(self, node)
434
-			e = parse('func() None end').stmtlist[0].expr
435
-			l = node.value:copy()
436
-			l:map(func(i)
437
-				s = parse('None').stmtlist[0]
438
-				s.expr = self:make(i)
439
-				return s
440
-			end)
441
-			lastidx = (#l) - 1
442
-			r = parse('return None').stmtlist[0]
443
-			r.ret = l[lastidx].expr
444
-			l[lastidx] = r
445
-			--print('In EX.LIST, e is now' e)
446
-			e.body.stmtlist = l
447
-			return e.body.stmtlist
448
-		end
449
-	},
450
-	run = func(self)
451
-		list = self:make(self.parser:run())
452
-		res = parse('(func() None None end)()')
453
-		--print('In run, list is', list)
454
-		--for i in list do ast.print(i) end
455
-		res.stmtlist[0].expr.expr.body.stmtlist = list
456
-		return res
457
-	end
458
-}
459
-
460
-_G = debug.globals()
461
-_G['+'] = func(a, b) return a + b end
462
-_G['-'] = func(a, b) return a - b end
463
-_G['*'] = func(a, b) return a * b end
464
-_G['/'] = func(a, b) return a / b end
465
-_G['<'] = func(a, b) return a < b end
466
-_G['>'] = func(a, b) return a > b end
467
-_G['<='] = func(a, b) return a <= b end
468
-_G['>='] = func(a, b) return a >= b end
469
-_G['=='] = func(a, b) return a == b end
470
-_G['eq'] = _G['==']
471
-_G['or'] = func(a, b) return a || b end
472
-_G['and'] = func(a, b) return a && b end

Loading…
Cancel
Save