-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheme_primitives.py
executable file
·798 lines (640 loc) · 19.5 KB
/
scheme_primitives.py
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
from operator import *
from math import floor, ceil
from scheme_utils import *
from scheme_tokens import symbol_escaped
from io import StringIO
try:
import turtle
except:
print("warning: could not import the turtle module.", file=sys.stderr)
class SchemeValue:
"""A value manipulated by a Scheme program."""
def __bool__(self):
"""All Scheme values other than #f are considered true in Python as
well as Scheme."""
return True
def type_name(self):
return "scheme value"
def __str__(self):
return "<{0}@{1}>".format(self.type_name(), hex(id(self)))
def _repr_(self):
return str(self)
def write(self, out):
"""Write a string representation of SELF on OUT, as for the write
procedure in Scheme."""
print(str(self), file=out, end="")
return UNSPEC
def display(self, out):
"""Write a string representation of SELF on OUT, without escapes,
as for the display procedure in Scheme."""
return self.write(out)
def eqvp(self, other):
return boolify(self is other)
def equalp(self, other):
return self.eqvp(other)
def atomp(self):
return TRUE
def numberp(self):
return FALSE
def integerp(self):
return FALSE
def booleanp(self):
return FALSE
def nullp(self):
return FALSE
def pairp(self):
return FALSE
def symbolp(self):
return FALSE
def procedurep(self):
return FALSE
def eof_objectp(self):
return FALSE
def length(self):
"""The length of SELF as a Python integer, assuming SELF is a list."""
raise SchemeError("value has no length")
def nth(self, k):
"""The Kth element of SELF, assuming it is a list."""
raise SchemeError("attempt to index a non-list")
def apply_step(self, args, evaluation):
"""Apply SELF to the arguments ARGS (a Python list of SchemeValues),
modifying EVALUATION to contain either the resulting value, or
the body of function and the environment in which it is to be
evaluated."""
raise SchemeError("attempt to call something other than a function: {0}"
.format(repr(self)))
class S_Expr(SchemeValue):
"""An S_Expr is a Scheme value that can be returned by the reader.
Other Scheme values can only result from the evaluation of functions or
forms (e.g., the values of lambda expressions)."""
def make_list(*args):
r = list(args)
result = NULL
while r:
result = Pair(r.pop(), result)
return result
class Pair(S_Expr):
def __init__(self, x, y):
self.car = x
self.cdr = y
def type_name(self):
return "pair"
def atomp(self):
return FALSE
def pairp(self):
return TRUE
def equalp(self, other):
if not other.pairp():
return FALSE
return self.car.equalp(other.car) and self.cdr.equalp(other.cdr)
def __repr__(self):
return "cons({0}, {1})".format(repr(self.car), repr(self.cdr))
def __str__(self):
out = StringIO()
self.display(out)
return out.getvalue()
def write(self, f):
print("(", file=f, end="")
self.car.write(f)
p = self.cdr
while p.pairp():
print(" ", file=f, end="")
p.car.write(f)
p = p.cdr
if not p.nullp():
print(" . ", file=f, end="")
p.write(f)
print(")", file=f, end="")
return UNSPEC
def display(self, f):
print("(", file=f, end="")
self.car.display(f)
p = self.cdr
while p.pairp():
print(" ", file=f, end="")
p.car.display(f)
p = p.cdr
if not p.nullp():
print(" . ", file=f, end="")
p.display(f)
print(")", file=f, end="")
return UNSPEC
def length(self):
n = 0
while self.pairp():
n += 1
self = self.cdr
if not self.nullp():
raise SchemeError("length attempted on improper list")
return n
def nth(self, k):
x = self
if k < 0:
raise SchemeError("negative index into list")
while True:
if x.nullp():
raise SchemeError("list index out of bounds")
elif not x.pairp():
raise SchemeError("ill-formed list")
if k == 0:
return x.car
x = x.cdr
k -= 1
class Null(S_Expr):
def __str__(self):
return "()"
def type_name(self):
return "null"
def nullp(self):
return TRUE
def atomp(self):
return TRUE
def length(self):
return 0
NULL = Null()
class Bool(S_Expr):
"""A boolean Scheme value (#t or #f). For convenience, this class is defined
so that TRUE (#t) is a true Python value as well and FALSE is a false
Python value."""
def __init__(self, is_true):
self.__truth = bool(is_true)
def __bool__(self):
"""As a Python value, SELF is True if it is #t and False otherwise."""
return self.__truth
def __str__(self):
return "#t" if self else "#f"
def type_name(self):
return "boolean"
def atomp(self):
return TRUE
def booleanp(self):
return TRUE
TRUE = Bool(True)
FALSE = Bool(False)
class Number(S_Expr):
def __init__(self, val):
self.num_val = val
def type_name(self):
return "integer" if type(self.num_val is int) else "real"
def atomp(self):
return TRUE
def numberp(self):
return TRUE
def integerp(self):
return boolify(type(self.num_val) is int)
def write(self, f):
print(self.num_val, file=f, end="")
def eqvp(self, other):
return boolify(other.integerp() and self.num_val == other.num_val)
def __str__(self):
return str(self.num_val)
class Symbol(S_Expr):
def __init__(self, ident):
# ident and escaped are strings representing the object
self.ident = ident
self.escaped = symbol_escaped(ident)
def type_name(self):
return "symbol"
def __str__(self):
return self.ident
def __repr__(self):
return "Symbol({0})".format(repr(self.ident))
def __hash__(self):
return hash(self.ident)
@staticmethod
def string_to_symbol(name):
"""The Symbol whose string value is NAME. Always returns the same
Symbol object when given the same string."""
result = Symbol.symbols.get(name, None)
if result is None:
result = Symbol.symbols[name] = Symbol(name)
return result
def atomp(self):
return TRUE
def symbolp(self):
return TRUE
def write(self, f):
print(self.escaped, file=f, end="")
def display(self, f):
print(self.ident, file=f, end="")
# The mapping of names to symbols.
symbols = {}
class Unspecified(SchemeValue):
"""A class whose sole instance is the "unspecified value", which is
returned to represent the value of a Scheme expressions whose value
is irrelevant and not specified by the Scheme report."""
def type_name(self):
return "unspecified value"
UNSPEC = Unspecified()
class Eof(SchemeValue):
"""A class whose sole instance is the "end-of-file object", which is
returned to represent an end-of-file condition when reading."""
def type_name(self):
return "eof object"
def eof_objectp(self):
return TRUE
THE_EOF_OBJECT = Eof()
def check_type(val, predicate, k, name):
"""Returns VAL. Raises a SchemeError if not PREDICATE(VAL), using
"argument K of NAME" to describe the offending value in any error message."""
if not predicate(val):
raise SchemeError("argument {0} of {1} has wrong type ({2})"
.format(k, name, val.type_name()))
return val
def string_to_atom(s):
"""The number or symbol denoted by S."""
try:
return Number(int(s))
except:
pass
try:
return Number(float(s))
except:
pass
return Symbol.string_to_symbol(s)
##
## Boolean operations
##
def scm_booleanp(x):
return x.booleanp()
def scm_not(x):
return boolify(not x)
def boolify(x):
"""Return the Scheme boolean value that corresponds to the "truthfulness"
of X in Python."""
return TRUE if x else FALSE
##
## Equivalence operations
##
def scm_eqp(x, y):
return boolify(x is y)
def scm_eqvp(x, y):
return x.eqvp(y)
def scm_equalp(x, y):
return x.equalp(y)
##
## Operations on lists and pairs.
##
def scm_pairp(x):
return x.pairp()
def scm_nullp(x):
return x.nullp()
def scm_listp(x):
p1 = x
while not x.nullp():
if not x.pairp():
return FALSE
if x.cdr.nullp():
return TRUE
if not x.cdr.pairp():
return FALSE
if p1 is x.cdr or p1 is x.cdr.cdr:
return FALSE
p1 = x.cdr
x = p1.cdr
return TRUE
def scm_length(x):
check_type(x, scm_listp, 0, 'length')
return Number(x.length())
def scm_cons(x, y):
return Pair(x, y)
def scm_car(x):
check_type(x, scm_pairp, 0, 'car')
return x.car
def scm_cdr(x):
check_type(x, scm_pairp, 0, 'cdr')
return x.cdr
def scm_set_car(x, y):
check_type(x, scm_pairp, 0, "set-car")
x.car = y
return UNSPEC
def scm_set_cdr(x, y):
check_type(x, scm_pairp, 0, "set-cdr")
x.cdr = y
return UNSPEC
def scm_list(*members):
result = NULL
for i in range(len(members)-1, -1, -1):
result = scm_cons(members[i], result)
return result
def scm_append(*vals):
if len(vals) == 0:
return NULL
result = vals[-1]
for i in range(len(vals)-2, -1, -1):
v = vals[i]
check_type(v, scm_listp, i, "append")
if not v.nullp():
r = p = scm_cons(v.car, result)
v = v.cdr
while v.pairp():
p.cdr = scm_cons(v.car, result)
p = p.cdr
v = v.cdr
result = r
return result
##
## Operations on symbols
##
def scm_symbolp(x):
return x.symbolp()
##
## Operations on integers
##
def scm_numberp(x):
return x.numberp()
def scm_integerp(x):
return x.integerp()
def _check_nums(*vals, pred = scm_numberp):
"""Check that all arguments in VALS satisfy PRED. TYPE_NAME is used
for error messages."""
for i in range(len(vals)):
if not vals[i].numberp():
msg = "an integer" if pred is scm_integerp else "a number"
raise SchemeError("operand #{0} is not {1}.".format(i, msg))
def _arith(op, init, vals):
"""Perform the OP operation on the integer values of VALS, with INIT as
the value when VALS is empty. Returns the result as a Scheme value."""
_check_nums(*vals)
s = init
for i in range(len(vals)):
s = op(s, vals[i].num_val)
return Number(s)
def scm_add(*vals):
return _arith(add, 0, vals)
def scm_sub(val0, *vals):
if len(vals) == 0:
return Number(-val0.num_val)
return _arith(sub, val0.num_val, vals)
def scm_mul(*vals):
return _arith(mul, 1, vals)
def scm_div(val0, val1):
_check_nums(val0, val1)
if val1.num_val == 0:
raise SchemeError("attempt to divide by zero!")
return Number(val0.num_val / val1.num_val)
def scm_quo(val0, val1):
_check_nums(val0, val1, pred = scm_integerp)
if val1.num_val == 0:
raise SchemeError("attempt to divide by zero!")
if (val0.num_val < 0) == (val1.num_val < 0):
return Number(val0.num_val // val1.num_val)
else:
return Number(-(abs(val0.num_val) // abs(val1.num_val)))
def scm_modulo(val0, val1):
_check_nums(val0, val1, pred = scm_integerp)
return Number(val0.num_val % val1.num_val)
def scm_remainder(val0, val1):
_check_nums(val0, val1, pred = scm_integerp)
x = val0.num_val
y = val1.num_val
return Number(x - (1 if (x<0) == (y<0) else -1) * (abs(x)//abs(y)) * y)
def scm_floor(val):
_check_nums(val)
return Number(floor(val.num_val))
def scm_ceil(val):
_check_nums(val)
return Number(floor(val.num_val))
def _numcomp(op, x, y):
_check_nums(x, y)
return boolify(op(x.num_val, y.num_val))
def scm_eq(x, y):
return _numcomp(eq, x, y)
def scm_lt(x, y):
return _numcomp(lt, x, y)
def scm_gt(x, y):
return _numcomp(gt, x, y)
def scm_le(x, y):
return _numcomp(le, x, y)
def scm_ge(x, y):
return _numcomp(ge, x, y)
##
## Other type tests
##
def scm_atomp(x):
return x.atomp()
def scm_procedurep(x):
return x.procedurep()
def scm_eof_objectp(x):
return x.eof_objectp()
##
## Output
##
def scm_display(val):
val.display(sys.stdout)
return UNSPEC
def scm_newline():
print()
sys.stdout.flush()
return UNSPEC
def scm_write(val):
val.write(sys.stdout)
return UNSPEC
##
## Other operations
##
def scm_error(msg = None):
if msg is None:
msg = ""
else:
check_type(msg, scm_symbolp, 0, "error")
msg = str(msg)
raise SchemeError(msg)
def scm_exit(code = None):
if code is None:
code = 0
else:
check_type(code, scm_integerp, 0, "exit")
code = code.num_val
sys.exit(code)
##
## Simply Scheme definitions (non-standard)
##
def sscm_word(*words):
"""The atom resulting from concatenating the representations of the atoms
in WORDS."""
result = ""
for w in words:
if scm_symbolp(w) or scm_numberp(w):
result += str(w)
else:
raise SchemeError("bad argument type to word: {0}"
.format(w.type_name()))
return string_to_atom(result)
def sscm_first(x):
"""If X is a list, its car. If X is an integer of symbol, the Scheme
value denoted by the first character of its printed representation."""
if x.symbolp() or x.numberp():
return string_to_atom(str(x)[0])
elif x.pairp():
return x.car
else:
raise SchemeError("bad argument to first")
def sscm_butfirst(x):
"""If X is a list, its cdr. If X is a symbol or integer, the Scheme
value denoted by all but the first character of its printed representation."""
if x.pairp():
return x.cdr
elif x.symbolp() or x.numberp():
return string_to_atom(str(x)[1:])
else:
raise SchemeError("bad argument to butfirst")
def sscm_last(x):
"""If X is a list, its last element. If it is a symbol or number, the
symbol or number denoted by the last character in its string value."""
if x.pairp():
while x.pairp() and not x.cdr.nullp():
x = x.cdr
if x.pairp():
return x.car
elif x.symbolp() or x.numberp():
return string_to_atom(str(x)[-1])
raise SchemeError("bad argument to last")
def sscm_butlast(x):
"""If X is a list, the list consisting of all but its last element.
If it is a symbol or number, the symbol or number denoted by all but
the last character in its string denotation."""
if x.pairp():
result = NULL
while x.pairp() and not x.cdr.nullp():
if result.nullp():
result = last = scm_cons(x.car, NULL)
else:
last.cdr = scm_cons(x.car, NULL)
last = last.cdr
x = x.cdr
if x.pairp():
return result
elif x.symbolp() or x.numberp():
return string_to_atom(str(x)[0:-1])
raise SchemeError("bad argument to butlast")
def sscm_sentence(*vals):
"""Creates a list out of the integers, symbols, and lists in VALS, treating
the atoms as single-element lists and concatenating the values together."""
result = NULL
for i in range(len(vals)-1, -1, -1):
v = vals[i]
if scm_listp(v):
result = scm_append(v, result)
elif v.integerp() or v.symbolp():
result = scm_cons(v, result)
else:
raise SchemeError("bad argument to sentence")
return result
##
## Turtle graphics (non-standard)
##
_turtle_screen_on = False
def _tscm_prep():
global _turtle_screen_on
if not _turtle_screen_on:
_turtle_screen_on = True
turtle.title("Scheme Turtles")
turtle.mode('logo')
def tscm_forward(n):
"""Move the turtle forward a distance N units on the current heading."""
_check_nums(n)
_tscm_prep()
turtle.forward(n.num_val)
return UNSPEC
def tscm_backward(n):
"""Move the turtle backward a distance N units on the current heading,
without changing direction."""
_check_nums(n)
_tscm_prep()
turtle.backward(n.num_val)
return UNSPEC
def tscm_left(n):
"""Rotate the turtle's heading N degrees counterclockwise."""
_check_nums(n)
_tscm_prep()
turtle.left(n.num_val)
return UNSPEC
def tscm_right(n):
"""Rotate the turtle's heading N degrees clockwise."""
_check_nums(n)
_tscm_prep()
turtle.right(n.num_val)
return UNSPEC
def tscm_circle(r, extent = None):
"""Draw a circle with center R units to the left of the turtle (i.e.,
right if N is negative. If EXTENT is not None, then draw EXTENT degrees
of the circle only. Draws in the clockwise direction if R is negative,
and otherwise counterclockwise, leaving the turtle facing along the
arc at its end."""
if extent is None:
_check_nums(r)
else:
_check_nums(r, extent)
_tscm_prep()
turtle.circle(r.num_val, extent and extent.num_val)
return UNSPEC
def tscm_setposition(x, y):
"""Set turtle's position to (X,Y), heading unchanged."""
_check_nums(x, y)
_tscm_prep()
turtle.setposition(x.num_val, y.num_val)
return UNSPEC
def tscm_setheading(h):
"""Set the turtle's heading H degrees clockwise from north (up)."""
_check_nums(h)
_tscm_prep()
turtle.setheading(h.num_val)
return UNSPEC
def tscm_penup():
"""Raise the pen, so that the turtle does not draw."""
_tscm_prep()
turtle.penup()
return UNSPEC
def tscm_pendown():
"""Lower the pen, so that the turtle starts drawing."""
_tscm_prep()
turtle.pendown()
return UNSPEC
def tscm_showturtle():
"""Make turtle visible."""
_tscm_prep()
turtle.showturtle()
return UNSPEC
def tscm_hideturtle():
"""Make turtle visible."""
_tscm_prep()
turtle.hideturtle()
return UNSPEC
def tscm_clear():
"""Clear the drawing, leaving the turtle unchanged."""
_tscm_prep()
turtle.clear()
return UNSPEC
def tscm_color(c):
"""Set the color to C, a symbol such as red or '#ffc0c0' (representing
hexadecimal red, green, and blue values."""
_tscm_prep()
check_type(c, scm_symbolp, 0, "color")
turtle.color(str(c))
return UNSPEC
def tscm_begin_fill():
"""Start a sequence of moves that outline a shape to be filled."""
_tscm_prep()
turtle.begin_fill()
return UNSPEC
def tscm_end_fill():
"""Fill in shape drawn since last begin_fill."""
_tscm_prep()
turtle.end_fill()
return UNSPEC
def tscm_exitonclick():
"""Wait for a click on the turtle window, and then close it."""
global _turtle_screen_on
if _turtle_screen_on:
turtle.exitonclick()
_turtle_screen_on = False
return UNSPEC
def tscm_speed(s):
"""Set the turtle's animation speed as indicated by S (an integer in
0-10, with 0 indicating no animation (lines draw instantly), and 1-10
indicating faster and faster movement."""
check_type(s, scm_integerp, 0, "speed")
_tscm_prep()
turtle.speed(s.num_val)
return UNSPEC