3
from zope.interface import implements
5
from safelisp import objects as O
6
from safelisp.wrapper import PythonFunctionWrapper
7
from safelisp.isafelisp import ISLObject, ISLCallable
9
## Builtin Functions ##
11
def func_println(*args):
13
sys.stdout.write('\n')
15
def func_print(*args):
16
sys.stdout.write(str(O.simplify(args[0])))
19
sys.stdout.write(str(O.simplify(x)))
26
def func_slice(seq, begin, end):
27
return seq.sl_slice(begin, end)
31
return O.List(list(args))
33
# whooaoh, bad name. Wait, why is it a bad name? maybe because 'dict'
34
# should be a type object.
37
assert (len(args) % 2) == 0, "need even number of args"
47
def func_getattr(o, name):
49
if not hasattr(o, 'sl_getAttribute'):
50
raise ValueError("%r is not an SLObject." % o)
51
return o.sl_getAttribute(name)
54
def func_setattr(o, name, value):
56
if not hasattr(o, 'sl_setAttribute'):
57
raise ValueError("%r is not an SLObject." % o)
58
return o.sl_setAttribute(name, value)
61
# funcs that will have names that aren't valid python identifiers: see
62
# builtins assignment below
65
v = ISLObject(reduce(operator.add, [x.pyvalue for x in args]))
69
v = ISLObject(reduce(operator.sub, [x.pyvalue for x in args]))
73
v = ISLObject(reduce(operator.mul, [x.pyvalue for x in args]))
77
v = ISLObject(reduce(operator.div, [x.pyvalue for x in args]))
81
return O.simplify(o1) == O.simplify(o2)
84
class TracebackPrinter(object):
85
implements(ISLCallable)
87
def callFunc(self, interp, args):
90
==========================================================
91
An error occured. Traceback follows, innermost frame last.
92
----------------------------------------------------------
94
for frame in interp.frames:
96
sys.stderr.write(" While trying to call %s\n"
99
sys.stderr.write(" In %s, line %s\n"
101
frame.curform and frame.curform.lineno))
102
sys.stderr.write(" %s\n" % O.form2code(frame.curform))
103
kl, inst = sys.exc_info()[:2]
104
sys.stderr.write( "%s: %s\n" % (kl.__name__, inst))
108
def func_make_object():
114
'+': PythonFunctionWrapper(add),
115
'-': PythonFunctionWrapper(sub),
116
'*': PythonFunctionWrapper(mul),
117
'/': PythonFunctionWrapper(div),
118
'==': PythonFunctionWrapper(eq),
119
'*exc-handler*': TracebackPrinter(),
121
# XXX maybe this in a separate cap -- maybe setattr in a separate cap
122
'make-object': PythonFunctionWrapper(func_make_object),
129
'+': PythonFunctionWrapper(add),
130
'-': PythonFunctionWrapper(sub),
131
'*': PythonFunctionWrapper(mul),
132
'/': PythonFunctionWrapper(div),
133
'==': PythonFunctionWrapper(eq),
135
# XXX maybe this in a separate cap -- maybe setattr in a separate cap
136
'make-object': PythonFunctionWrapper(func_make_object),
137
"println": func_println,
143
"getattr": func_getattr,
144
"setattr": func_setattr,
145
"make_object": func_make_object,
146
'*exc-handler*': TracebackPrinter(),
150
# return builtins + {}
151
# d = builtins.copy()
152
# for name,obj in globals().items():
153
# if name.startswith('func_'):
154
# d[name[5:]] = PythonFunctionWrapper(obj)