importing python metacircular evaluator into git
[boa.git] / boa2 / boa / primitives.py
blob8ff515a5c3f371deb8a26cddf8e76fbc10ad81fb
1 """Standard Lisp primitives
3 Lisp type Python representation
4 --------- ---------------------
5 pair class cons
6 '() None
7 symbol class symbol
10 """
12 from error import *
14 __all__ = ["cons", "car", "cdr", "null_p", "pair_p", "pylist",
15 "cadr", "caar", "cddr", "caddr", "caadr", "cdadr", "cadddr",
16 "set_car", "set_cdr", "symbol", "list", "map",
17 "true", "false", "eq_p", "number_p", "symbol_p", "string_p",
18 "display", "newline", "symbol_to_string",
19 "+", "-"]
21 # pair
22 ######
24 class cons(object):
25 __slots__ = ('car', 'cdr')
26 def __init__(self, car, cdr):
27 self.car = car
28 self.cdr = cdr
29 def __eq__(self, o):
30 return pair_p(o) and self.car == o.car and self.cdr == o.cdr
32 def pair_p(p):
33 return type(p) is cons
35 def null_p(o):
36 return o is None
38 def car(p):
39 if pair_p(p): return p.car
40 else: error("CAR: expects a non-empty <pair>; given %s", p)
42 def cdr(p):
43 if pair_p(p): return p.cdr
44 else: error("CDR: expects a non-empty <pair>; given %s", p)
46 def set_car(p,v):
47 if pair_p(p): p.car = v
48 else: error("CAR: expects a non-empty <pair>; given %s", p)
50 def set_cdr(p,v):
51 if pair_p(p): p.cdr = v
52 else: error("CDR: expects a non-empty <pair>; given %s", p)
54 # TODO: complete the combinations
55 def cadr(x): return car(cdr(x))
56 def caar(x): return car(car(x))
57 def cddr(x): return cdr(cdr(x))
58 def caddr(x): return car(cdr(cdr(x)))
59 def caadr(x): return car(car(cdr(x)))
60 def cadar(x): return car(cdr(car(x)))
61 def cdadr(x): return cdr(car(cdr(x)))
62 def caddr(x): return car(cdr(cdr(x)))
63 def cadddr(x): return car(cdr(cdr(cdr(x))))
65 def list(*args):
66 l = None
67 for a in reversed(args):
68 l = cons(a, l)
69 return l
71 def map(function, ls):
72 result = []
73 while not null_p(ls):
74 result.append(function(car(ls)))
75 ls = cdr(ls)
76 return list(*result)
78 def pylist(ls):
79 result = []
80 while not null_p(ls):
81 result.append(car(ls))
82 ls = cdr(ls)
83 return result
85 # symbol
86 ########
88 class symbol(object):
89 __slot__ = ('string')
90 def __init__(self, string):
91 self.string = string
92 def __hash__(self):
93 return hash(self.string)
94 def __eq__(self, o):
95 return type(o) is symbol and self.string == o.string
96 def __str__(self):
97 return "<symbol: %s>" % self.string
98 __repr__ = __str__
100 def symbol_p(sym):
101 return type(sym) is symbol
103 def symbol_to_string(sym):
104 if symbol_p(sym): return sym.string
105 else: error("symbol->string: expets a <symbol>; given %s", sym)
107 def string_to_symbol(s):
108 return symbol(s)
110 # misc
111 ######
113 true = True
114 false = False
116 def eq_p(x, y):
117 return x == y
119 def number_p(n):
120 return type(n) in (int, float)
122 def string_p(s):
123 return type(s) is str
125 def display(s):
126 print s
127 return symbol("ok")
129 def newline():
130 print
131 return symbol("ok")
133 # math
134 ######
136 globals()["+"] = lambda *args: sum(args)
137 globals()["-"] = lambda a,b: a-b