-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme51.lua
4081 lines (3980 loc) · 181 KB
/
scheme51.lua
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local input, input_file = {}
local write = write
if not table.pack then
table.pack = function(...)
return { n = select('#', ...), ... }
end
end
if not table.unpack then
table.unpack = unpack
end
if not math.type then
function math.type(x)
if type(x) == 'number' then
return select(2, math.modf(x)) ~= 0 and 'float' or 'integer'
end
return nil
end
end
if not load then
function _G.load(string, source, mode, env)
return setfenv(loadstring(string), env)
end
end
if not write then
function write(x)
return io.write(tostring(x))
end
end
function getchar()
if #input >= 1 then
local c = table.remove(input, 1)
return c
elseif input_file then
return input_file:read(1)
elseif not _CC_DEFAULT_SETTINGS then
return io.read(1)
else
while true do
local ev, c = coroutine.yield()
if ev == 'key' and c == keys.enter then
write('\n')
return '\n'
elseif ev == 'char' then
write(c)
return c
end
end
end
end
local function ungetchar(ch)
table.insert(input, 1, ch)
end
local function peek()
local ch = getchar()
ungetchar(ch)
return ch
end
local function peekp(c)
return peek() == c
end
local function eat(chs, i)
local i = i or 1
if i > #chs then
return
elseif peekp(chs:sub(i, i)) and i <= #chs then
getchar()
return eat(chs, i + 1)
else
return error("unexpected character " .. getchar())
end
end
local function skip_spaces()
local ch = getchar()
if ch == ' ' or ch == '\t' or ch == '\n' then
return skip_spaces()
elseif ch == ';' then
while ch ~= '\n' do
ch = getchar()
end
return skip_spaces()
end
return ch
end
local function gcd(a, b)
if b == 0 then
return math.abs(a)
else
return gcd(b, a % b)
end
end
function rational(a, b)
assert(type(a) == 'number' and type(b) == 'number')
local c = gcd(a, b)
local d = math.floor(b/c)
if d == 0 then
return error("division by zero")
else
return {[0]=rational, math.floor(a/c), math.floor(b/c)}
end
end
local function insist_delim(x)
local ch = peek()
if ch == ' ' or ch == '\t' or ch == '\n' or ch == ')' or ch == ']' or ch == '}' then
return x
else
return error("expected delimiter, got " .. getchar())
end
end
local function is_char_base(radix)
if radix <= 10 then
local upper = string.char(string.byte('9') - (10 - radix))
return function(ch)
return '0' <= ch and ch <= upper
end
elseif radix <= 26 then
local upper = string.char(string.byte('z') - (26 - radix))
return function(ch)
return ('0' <= ch and ch <= '9') or ('a' <= ch and ch <= upper)
end
elseif radix <= 52 then
local upper = string.char(string.byte('Z') - (52 - radix))
return function(ch)
return ('0' <= ch and ch <= '9')
or ('a' <= ch and ch <= 'z')
or ('A' <= ch and ch <= upper)
end
end
end
local function read_number(acc, radix, allow_dot)
local allow_dot = allow_dot == nil and true or false
local p = is_char_base(radix or 10)
local ch = getchar()
if p(ch) then
return insist_delim(read_number(acc .. ch, radix))
elseif ch == '.' and allow_dot then
local dec = read_number('', radix, false)
return insist_delim(read_number(acc .. ch .. tostring(dec)))
elseif ch == '/' and allow_dot then
local denom = read_number('', radix, false)
return insist_delim(rational(tonumber(acc), tonumber(denom)))
else
ungetchar(ch)
return insist_delim(tonumber(acc or '0', radix))
end
end
local symbol_chars = {
['+'] = true, ['-'] = true, ['?'] = true, ['!'] = true, ['='] = true,
['>'] = true, ['<'] = true, ['*'] = true, ['/'] = true
}
local function symbol_carp(ch)
return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') or symbol_chars[ch]
end
local symbol_table, symbol = {}, {}
local function mksymbol(x, iskeyword)
if symbol_table[x] then
return symbol_table[x]
else
symbol_table[x] = {kw=iskeyword,[0]=symbol,x}
return symbol_table[x]
end
end
_G.symbol_table = symbol_table
local _lambda = mksymbol('lambda')
local _if = mksymbol('if')
local _quote = mksymbol('quote')
local _quasiquote = mksymbol('quasiquote')
local _unquote = mksymbol('unquote')
local _unquotes = mksymbol('unquote-splicing')
local _define = mksymbol('define')
local _set = mksymbol('set!')
local function read_symbol(acc, k)
local ch = getchar()
if symbol_carp(ch) or (ch <= '9' and ch >= '0') or ch == '/' then
return read_symbol(acc .. ch, k)
else
ungetchar(ch)
return (k or mksymbol)(acc)
end
end
local read_sexpr
local function read_character()
local ch = getchar()
if ch == 's' then
if peekp('p') then
eat('pace')
return ' '
end
return insist_delim(s)
elseif ch == 'n' then
if peekp('e') then
eat('ewline')
return '\n'
elseif peekp('u') then
eat('ull')
return '\0'
end
return insist_delim('n')
elseif ch == 't' then
if peekp('a') then
eat('ab')
return '\t'
end
return insist_delim('t')
elseif ch == 'b' then
if peekp('a') then
eat('ackspace')
return '\08'
end
return insist_delim('b')
elseif ch == 'e' then
if peekp('s') then
eat('scape')
return '\x1b'
end
return insist_delim('e')
elseif ch == 'd' then
if peekp('e') then
eat('elete')
return '\x7f'
end
return insist_delim('d')
elseif ch == 'x' then
local acc = '0x'
while tonumber(peek(), 16) do
acc = acc .. getchar()
end
return insist_delim(utf8.char(tonumber(acc)))
else
return ch
end
end
local scm_nil, scm_eof, eval, callproc = {}, {}, {}, {}
local function read_special_atom()
local ch = getchar()
if ch == 't' then
return insist_delim(true)
elseif ch == 'f' then
return insist_delim(false)
elseif ch == '\\' then
return read_character()
elseif ch == ';' then
read_sexpr()
return true
elseif ch == ':' then
return insist_delim(read_symbol('', function(s)
return {[0] = symbol, kw = true, s}
end))
elseif ch == 'e' then
eat('of')
return insist_delim(scm_eof)
elseif ch == 'o' then
return read_number('', 8)
elseif ch == 'x' then
return read_number('', 16)
elseif ch == 'b' then
return read_number('', 2)
else
return error('unexpected special atom ' .. ch)
end
end
local function cons(a,b)
return {a,b}
end
local delims = { [']'] = true, ['}'] = true, [')'] = true, ['['] = ']', ['('] = ')', ['{'] = '}' }
local function read_sexpr_list(delim, can_dot)
local ch = skip_spaces()
if ch == delim then
return scm_nil
elseif type(delims[ch]) == 'boolean' then
return error("delimiter mismatch: expected " .. delim .. " but got " .. ch)
elseif ch == '.' and can_dot then -- improper pair
local e = read_sexpr()
if not peekp(delim) then
return error("expected delimiter after cdr of improper list")
end
getchar()
return e
elseif not ch then
return error("expected expression in list, but got #eof")
else
ungetchar(ch)
return {read_sexpr(), read_sexpr_list(delim, true)}
end
end
local function read_string(acc)
local ch = getchar()
if ch == '\\' then
local ch = getchar()
if ch == 'n' then
return read_string(acc .. '\n')
elseif ch == 't' then
return read_string(acc .. '\t')
elseif ch == '"' then
return read_string(acc .. '"')
elseif ch == '\\' then
return read_string(acc .. '\\')
else
error("unknown escape sequence " .. ch)
end
elseif ch == '"' then
return acc
elseif not ch then
error(("unterminated string literal: %q"):format(acc))
else
return read_string(acc .. ch)
end
end
function read_sexpr()
local ch = skip_spaces()
if not ch then
return scm_eof
elseif ch == '(' or ch == '[' or ch == '{' then
return read_sexpr_list(delims[ch], false)
elseif ch == '\'' then
return {_quote, {read_sexpr(), scm_nil}}
elseif ch == '`' then
return {_quasiquote, {read_sexpr(), scm_nil}}
elseif ch == ',' then
if peekp('@') then
getchar()
return {_unquotes, {read_sexpr(), scm_nil}}
end
return {_unquote, {read_sexpr(), scm_nil}}
elseif ch <= '9' and ch >= '0' then
return read_number(ch)
elseif ch == '-' then
local ch2 = peek()
if ch2 <= '9' and ch2 >= '0' then
getchar()
local num = read_number(ch2)
if type(num) == 'table' then
return rational(-num[1], num[2])
else
return -num
end
else
return read_symbol(ch)
end
elseif ch == '#' then
return read_special_atom()
elseif symbol_carp(ch) then
return read_symbol(ch)
elseif ch == '"' then
return read_string("")
else
return error("lexical error at character: " .. ch)
end
end
local function symbolp(e)
return type(e) == 'table' and e[0] == symbol
end
local function consp(e)
return type(e) == 'table' and #e == 2 and e[0] ~= rational
end
local function scm_print(e, display, seen)
seen = seen or {}
if type(e) == 'table' then
if seen[e] then
write("#<cycle>")
return
end
seen[e] = true
if symbolp(e) then
write(e[1])
elseif e[0] == eval then
write '(lambda '
scm_print(e[1], display, seen)
write ' '
scm_print(e[3], display, seen)
write ')'
elseif consp(e) then
write '('
repeat
scm_print(e[1], display, seen)
if consp(e[2]) then
write ' '
end
e = e[2]
until not consp(e)
if e ~= scm_nil then
write ' . '
scm_print(e, display, seen)
end
write ')'
elseif e == scm_nil then
write ('\'()')
elseif e[0] == callproc then
write '<procedure>'
elseif e[0] == rational then
write (tostring(e[1]))
write '/'
write (tostring(e[2]))
elseif e == scm_eof then
write '#eof'
elseif e.kw then
write '#:'
write(e[1])
elseif getmetatable(e) and getmetatable(e).__call then
write(tostring(getmetatable(e).__call))
else
write '(make-hash-table '
for k, v in pairs(e) do
write '('
scm_print(k, display, seen)
write ' . '
scm_print(v, display, seen)
write ')'
if next(e, k) ~= nil then
write ' '
end
end
write ')'
end
elseif type(e) == 'string' then
if display then
write(e)
else
write(('%q'):format(e))
end
elseif e == true then
write('#t')
elseif e == false then
write('#f')
else
write(tostring(e))
end
end
local function is_self_eval(e)
return type(e) == 'number'
or type(e) == 'string'
or type(e) == 'function'
or type(e) == 'boolean'
or (type(e) == 'table' and (e[0] == eval or e[0] == rational))
or e == scm_nil
or e == scm_eof
or e == nil
end
local function num2rat(x)
if type(x) == 'table' then
return x
else
return rational(x, 1)
end
end
local function bothnums(a, b)
return (type(a) == 'number' or (type(a) == 'table' and a[0] == rational))
and (type(b) == 'number' or (type(b) == 'table' and b[0] == rational))
end
local function scm_eq(a, b)
if a == b then
return true
elseif consp(a) and consp(b) then
return scm_eq(a[1]) and scm_eq(a[2])
elseif type(a) == 'table' and (a[0] == eval or a[0] == callproc) then
return false
elseif type(a) == 'table' and a.kw and type(b) == 'table' and b.kw then
return a[1] == b[1]
elseif bothnums(a, b) then
local a = num2rat(a)
local b = num2rat(b)
return a[1] * b[2] == b[1] * a[2]
end
return false
end
local function add_rat(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x + y
elseif type(x) == 'table' and math.type(y) == 'float' then
return (y * x[2] + x[1]) / x[2]
elseif type(y) == 'table' and math.type(x) == 'float' then
return (x * y[2] + y[1]) / y[2]
end
local x = num2rat(x)
local y = num2rat(y)
return rational(x[1] * y[2] + y[1] * x[2], x[2] * y[2])
end
local function minus_rat(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x - y
elseif type(x) == 'table' and math.type(y) == 'float' then
return (y * x[2] - x[1]) / x[2]
elseif type(y) == 'table' and math.type(x) == 'float' then
return (x * y[2] - y[1]) / y[2]
end
local x = num2rat(x)
local y = num2rat(y)
return rational(x[1] * y[2] - y[1] * x[2], x[2] * y[2])
end
local function times_rat(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x * y
elseif type(x) == 'table' and math.type(y) == 'float' then
return (y * x[1]) / x[2]
elseif type(y) == 'table' and math.type(x) == 'float' then
return (x * y[1]) / y[2]
end
local x = num2rat(x)
local y = num2rat(y)
return rational(x[1] * y[1], x[2] * y[2])
end
local function over_rat(x, y)
if type(x) == 'number' and type(y) == 'number' then
if math.type(x) == 'integer' and math.type(x) == 'integer' then
return rational(x, y)
end
return x / y
elseif type(x) == 'table' and math.type(y) == 'float' then
return x[1] / (x[2] * y)
elseif type(y) == 'table' and math.type(x) == 'float' then
return x / (y[1] * y[2])
end
local x = num2rat(x)
local y = num2rat(y)
return rational(x[1] * y[2], x[2] * y[1])
end
local gensym_counter = 0
local scm_env = {
}
scm_env.read = read_sexpr
local handles = {}
function _G.input_from_file(p, t)
if not p then
error('not p', 2)
end
if type(p) == 'string' then
local h = io.open(p, 'r')
if not h then
return error('failed to open ' .. p .. ' for reading')
end
p = h
end
local i = input_file
input_file = p
local r = t()
input_file = i
return r
end
function _G.output_to_file(p, t)
if not p then
error('not p', 2)
end
if type(p) == 'string' then
local h = io.open(p, 'w')
if not h then
return error('failed to open ' .. p .. ' for write')
end
local old_write = write
function write(x)
h:write(x)
end
local r = t()
write = old_write
h:close()
return r
else
error("argument must be a path", 2)
end
end
-- return repl()
if select(1, ...) == 'boot' then
scm_env.booting = true
else
scm_env.booting = false
end
function scm_env.environment()
return _ENV
end
function _G.scm_set_helper(x, v, n)
if not x then
error("can't set! " .. n)
end
return v
end
function _G.var(x, n)
if x ~= nil then
return x
end
return error("no binding for symbol " .. n, 2)
end
function ignore(x)
return true
end
local symbol = mksymbol
_G.add_rat = add_rat
_G.over_rat = over_rat
_G.times_rat = times_rat
_G.minus_rat = minus_rat
_G._read = read_sexpr
_G.scm_nil = scm_nil
_G.scm_eof = scm_eof
_G.symbol = mksymbol
function _G.keyword(s)
return {[0]=symbol,kw=true,s}
end
_G._symbolS63 = symbolp
_G._eqS63 = scm_eq
function _G._write(...)
local t = table.pack(...)
for i = 1, t.n do
scm_print(t[i])
end
return true
end
function _G._display(...)
local t = table.pack(...)
for i = 1, t.n do
scm_print(t[i], true)
end
return true
end
function _S43(...)
local t = table.pack(...)
local r = 0
for i = 1, t.n do
r = add_rat(r, t[i])
end
return r
end
function _S42(...)
local t = table.pack(...)
local r = 1
for i = 1, t.n do
r = times_rat(r, t[i])
end
return r
end
function _S45(...)
local t = table.pack(...)
if t.n == 0 then
error("not enough arguments for operator (-)")
elseif t.n == 1 then
return minus_rat(0, t[1])
else
local r = t[1]
for i = 2, t.n do
r = minus_rat(r, t[i])
end
return r
end
end
function _S47(...)
local t = table.pack(...)
if t.n < 1 then
error("not enough arguments for operator (/)")
elseif t.n == 1 then
return over_rat(rational(1, 1), t[1])
else
local r = t[1]
for i = 2, t.n do
r = over_rat(r, t[i])
end
return r
end
end
function _callS47native(s, ...)
if _symbolS63(s) then
return _G[s[1]](...)
else
local o, path = _G, "_G"
repeat
o = o[s[1][1]]
path = path .. "." .. s[1][1]
s = s[2]
until not _pairS63(s)
if not o then
error("No such procedure: " .. path)
end
return o(...)
end
end
function _apply(f, t)
assert(_pairS63(t) or t == scm_nil, "values to be applied must be a list")
local args, n, i = {}, 0, 1
while _pairS63(t) do
args[i] = t[1]
i, n, t = i + 1, n + 1, t[2]
end
local r = table.pack(pcall(f, table.unpack(args, 1, n)))
if not r[1] then
error(r[2], 2)
else
return table.unpack(r, 2, r.n)
end
end
local gensym_counter = gensym_counter or 0
function _gensym()
gensym_counter = gensym_counter + 1
return symbol('#.' .. gensym_counter)
end
function _error(...)
return error(list(...))
end
function _catch(thunk, handler)
local ok, err = pcall(thunk)
if not ok then
return handler(err)
else
return err
end
end
function _pairS63(p)
return type(p) == 'table' and #p == 2 and p[0] ~= rational
end
local function rat_lte(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x <= y
end
local x = num2rat(x)
local y = num2rat(y)
return x[1]*y[2] <= y[1]*x[2]
end
function _S62(...)
local t = table.pack(...)
if t.n < 2 then
error("not enough arguments for operator (>)")
else
for i = 1, t.n do
if t[i + 1] and rat_lte(t[i], t[i + 1]) then
return false
end
end
return true
end
end
local function rat_gte(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x >= y
end
local x = num2rat(x)
local y = num2rat(y)
return x[1]*y[2] >= y[1]*x[2]
end
function _S60(...)
local t = table.pack(...)
if t.n < 2 then
error("not enough arguments for operator (<)")
else
for i = 1, t.n do
if t[i + 1] and rat_gte(t[i], t[i + 1]) then
return false
end
end
return true
end
end
local function rat_lt(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x < y
end
local x = num2rat(x)
local y = num2rat(y)
return x[1]*y[2] < y[1]*x[2]
end
function _S62S61(...)
local t = table.pack(...)
if t.n < 2 then
error("not enough arguments for operator (>=)")
else
for i = 1, t.n do
if t[i + 1] and rat_lt(t[i], t[i + 1]) then
return false
end
end
return true
end
end
local function rat_gt(x, y)
if type(x) == 'number' and type(y) == 'number' then
return x > y
end
local x = num2rat(x)
local y = num2rat(y)
return x[1]*y[2] > y[1]*x[2]
end
function _S60S61(...)
local t = table.pack(...)
if t.n < 2 then
error("not enough arguments for operator (<=)")
else
for i = 1, t.n do
if t[i + 1] and rat_gt(t[i], t[i + 1]) then
return false
end
end
return true
end
end
function var(x, n)
if x ~= nil then return x end
return error('no binding for symbol ' .. n, 2)
end
function list(car, ...)
if car ~= nil then
return { car, list(...) }
else
return scm_nil
end
end
function scm_set_helper(var, val, name)
assert(var ~= nil, 'no previous binding for symbol ' .. name);
return val
end
if not _ENV then _ENV = _G end
function _environment() return _ENV end
function _setS45carS33(_cell , _val ) _cell[1] = _val; return true end
function _car(_cell ) return _cell[1] end
function _setS45cdrS33(_cell , _val ) _cell[2] = _val; return true end
function _cdr(_cell ) return _cell[2] end
function _nullS63(_p ) return _p == scm_nil or _p == nil end
function _numberS63(_p ) return type(_p) == 'number' or (type(_p) == 'table' and _p[0] == rational) end
function _stringS63(_p ) return type(_p) == 'string' end
function _keywordS63(_p ) return _symbolS63(_p) and _p.kw ~= nil end
function _procedureS63(_p ) return type(_p) == 'function' or (type(_p) == 'table' and getmetatable(_p) and type(getmetatable(_p).__call) == 'function') end
function _charS63(_p ) return type(_p) == 'string' and #_p == 1 end
function _cons(_a , _b ) return {_a,_b} end
function _hashS45ref(_t , _k , _def ) if _t[_k] ~= nil then return _t[_k] else return _def or false end end
function _callS45withS45values(_pro , _con ) return _con(_pro()) end
function _values(...)
return ...
end
function _hashS45forS45each(_hash , _func ) local n = 0
for k, v in pairs(_hash) do
_func(k, v)
n = n + 1
end
return n end
function _rationalS63(_p ) return type(_p) == 'table' and _p[0] == rational end
_platform = 'Scheme 51'
function _withS45inputS45fromS45file(_path , _thunk ) return input_from_file(_path, _thunk) end
function _withS45outputS45toS45file(_path , _thunk ) return output_to_file(_path, _thunk) end
_booting = false
local function ignore(x) end
ignore((function(_S35S46893)
if _S35S46893 then
return (function(_S35S46894)
if _S35S46894 then
return (function()
var(_display, "display")("loading pre-built Scheme...", "\
" );
var(_callS47native, "call/native")(symbol('dofile'), "scheme51.lua" );
_display("loading startup file...", "\
" );
return _callS47native(symbol('load'), "return _load('emaccs/startup.ss')" )()
end)() else
return false
end
end)(var(_callS47native, "call/native")({symbol('fs'),{symbol('exists'),scm_nil}}, "scheme51.lua" )) else
return false
end
end)(var(_eqS63, "eq?")(var(_platform, "platform"), symbol('computercraft') )))_not = setmetatable({args={symbol('b'),scm_nil},doc="Negate the boolean ,b."}, { __call = function(_S35S46895 , _b )
if var(_b, "b") then return false else return true end
end });
ignore(_not)_S61 = _eqS63;
ignore(_S61)_charS61S63 = _S61;
ignore(_charS61S63)_caar = (function(_x )
return ((var(_x, "x"))[1])[1]
end);
ignore(_caar)_cadr = (function(_x )
return ((var(_x, "x"))[2])[1]
end);
ignore(_cadr)_cdar = (function(_x )
return ((var(_x, "x"))[1])[2]
end);
ignore(_cdar)_cddr = (function(_x )
return ((var(_x, "x"))[2])[2]
end);
ignore(_cddr)_list = (function(...)
local _x = list(...);
return var(_x, "x")
end);
ignore(_list)_makeS45hashS45table = setmetatable({args=scm_nil,doc="Create a new hash table. This procedure is internal: Shouldn't this\
have arguments?"}, { __call = function(_S35S46897 )
return _callS47native(symbol('load'), "return {}" )()
end });
ignore(_makeS45hashS45table)_hashS45setS33 = setmetatable({args={symbol('table'),{symbol('key'),{symbol('value'),scm_nil}}},doc="Associate the ,key in ,table with the given ,value."}, { __call = function(_S35S46898 , _table , _key , _value )
return _callS47native(symbol('rawset'), var(_table, "table"), var(_key, "key"), var(_value, "value") )
end });
ignore(_hashS45setS33)_copyS45hashS45table = setmetatable({args={symbol('table'),scm_nil},doc="Copy the hash table ,table. Since hash tables are mutable, this is\
important if you want to maintain persistence."}, { __call = function(_S35S46899 , _table )
local _x;
_x = _makeS45hashS45table();
var(_hashS45forS45each, "hash-for-each")(var(_table, "table"), (function(_k , _v )
return _hashS45setS33(var(_x, "x"), var(_k, "k"), var(_v, "v") )
end) );
return var(_x, "x")
end });
ignore(_copyS45hashS45table)_macros = _makeS45hashS45table();
ignore(_macros)_pushS45macroS33 = setmetatable({args={symbol('name'),{symbol('expander'),scm_nil}},doc="Associate the macro ,name with the expansion function ,expander.\
Prefer 'define-syntax instead."}, { __call = function(_S35S46900 , _name , _expander )
_hashS45setS33(_macros, (var(_name, "name"))[1], var(_expander, "expander") );
return true
end });
ignore(_pushS45macroS33)_lookupS45macroS45in = setmetatable({args={symbol('symbol'),{symbol('table'),scm_nil}},doc="Look up the macro called ,symbol in the table ,table."}, { __call = function(_S35S46901 , _symbol , _table )
return var(_hashS45ref, "hash-ref")(var(_table, "table"), (var(_symbol, "symbol"))[1] )
end });
ignore(_lookupS45macroS45in)_makeS45lambda = setmetatable({args={symbol('args'),{symbol('body'),scm_nil}},doc="Make a 'lambda expression with the given ,args and ,body."}, { __call = function(_S35S46902 , _args , _body )
return {symbol('lambda'), {var(_args, "args"), var(_body, "body")}}
end });
ignore(_makeS45lambda)_map = setmetatable({args={symbol('f'),{symbol('x'),scm_nil}},doc="Map the procedure ,f over the list ,x, collecting the results in a\