~subol-hackers/subol/safelisp-rpython

« back to all changes in this revision

Viewing changes to safelisp/builtins.py

  • Committer: Christopher Armstrong
  • Date: 2007-01-17 00:06:13 UTC
  • Revision ID: radix@twistedmatrix.com-20070117000613-978rwvg03ql0gy6u
safelisp.builtinsĀ isĀ unused

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import sys, operator
2
 
 
3
 
from zope.interface import implements
4
 
 
5
 
from safelisp import objects as O
6
 
from safelisp.wrapper import PythonFunctionWrapper
7
 
from safelisp.isafelisp import ISLObject, ISLCallable
8
 
 
9
 
## Builtin Functions ##
10
 
 
11
 
def func_println(*args):
12
 
    func_print(*args)
13
 
    sys.stdout.write('\n')
14
 
 
15
 
def func_print(*args):
16
 
    sys.stdout.write(str(O.simplify(args[0])))
17
 
    for x in args[1:]:
18
 
        sys.stdout.write(' ')
19
 
        sys.stdout.write(str(O.simplify(x)))
20
 
 
21
 
 
22
 
def func_index(o, i):
23
 
    return o.sl_index(i)
24
 
 
25
 
 
26
 
def func_slice(seq, begin, end):
27
 
    return seq.sl_slice(begin, end)
28
 
 
29
 
 
30
 
def func_vec(*args):
31
 
    return O.List(list(args))
32
 
 
33
 
# whooaoh, bad name. Wait, why is it a bad name? maybe because 'dict'
34
 
# should be a type object.
35
 
 
36
 
def func_dict(*args):
37
 
    assert (len(args) % 2) == 0, "need even number of args"
38
 
    d = {}
39
 
    args = list(args)
40
 
    while args:
41
 
        k = args.pop(0)
42
 
        v = args.pop(0)
43
 
        d[k] = v
44
 
    return O.Dict(d)
45
 
 
46
 
 
47
 
def func_getattr(o, name):
48
 
    name = name.pyvalue
49
 
    if not hasattr(o, 'sl_getAttribute'):
50
 
        raise ValueError("%r is not an SLObject." % o)
51
 
    return o.sl_getAttribute(name)
52
 
 
53
 
 
54
 
def func_setattr(o, name, value):
55
 
    name = name.pyvalue
56
 
    if not hasattr(o, 'sl_setAttribute'):
57
 
        raise ValueError("%r is not an SLObject." % o)
58
 
    return o.sl_setAttribute(name, value)
59
 
 
60
 
 
61
 
# funcs that will have names that aren't valid python identifiers: see
62
 
# builtins assignment below
63
 
 
64
 
def add(*args):
65
 
    v = ISLObject(reduce(operator.add, [x.pyvalue for x in args]))
66
 
    return v
67
 
 
68
 
def sub(*args):
69
 
    v = ISLObject(reduce(operator.sub, [x.pyvalue for x in args]))
70
 
    return v
71
 
 
72
 
def mul(*args):
73
 
    v = ISLObject(reduce(operator.mul, [x.pyvalue for x in args]))
74
 
    return v
75
 
 
76
 
def div(*args):
77
 
    v = ISLObject(reduce(operator.div, [x.pyvalue for x in args]))
78
 
    return v
79
 
 
80
 
def eq(o1, o2):
81
 
    return O.simplify(o1) == O.simplify(o2)
82
 
 
83
 
 
84
 
class TracebackPrinter(object):
85
 
    implements(ISLCallable)
86
 
 
87
 
    def callFunc(self, interp, args):
88
 
        if interp.frames:
89
 
            sys.stderr.write("""\
90
 
==========================================================
91
 
An error occured. Traceback follows, innermost frame last.
92
 
----------------------------------------------------------
93
 
""")
94
 
        for frame in interp.frames:
95
 
            if not frame.curform:
96
 
                sys.stderr.write("    While trying to call %s\n" 
97
 
                                 % (frame.name,))
98
 
            else:
99
 
                sys.stderr.write("    In %s, line %s\n" 
100
 
                                 % (frame.name, 
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))
105
 
 
106
 
 
107
 
 
108
 
def func_make_object():
109
 
    return O.SLObject()
110
 
 
111
 
 
112
 
 
113
 
builtins = {
114
 
    '+': PythonFunctionWrapper(add),
115
 
    '-': PythonFunctionWrapper(sub),
116
 
    '*': PythonFunctionWrapper(mul),
117
 
    '/': PythonFunctionWrapper(div),
118
 
    '==': PythonFunctionWrapper(eq),
119
 
    '*exc-handler*': TracebackPrinter(),
120
 
    'None': None,
121
 
    # XXX maybe this in a separate cap -- maybe setattr in a separate cap
122
 
    'make-object': PythonFunctionWrapper(func_make_object),
123
 
    }
124
 
 
125
 
 
126
 
 
127
 
def getFunctions():
128
 
    return {
129
 
        '+': PythonFunctionWrapper(add),
130
 
        '-': PythonFunctionWrapper(sub),
131
 
        '*': PythonFunctionWrapper(mul),
132
 
        '/': PythonFunctionWrapper(div),
133
 
        '==': PythonFunctionWrapper(eq),
134
 
        'None': None,
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,
138
 
        "print": func_print,
139
 
        "index": func_index,
140
 
        "slice": func_slice,
141
 
        "vec": func_vec,
142
 
        "dict": func_dict,
143
 
        "getattr": func_getattr,
144
 
        "setattr": func_setattr,
145
 
        "make_object": func_make_object,
146
 
        '*exc-handler*': TracebackPrinter(),
147
 
        }
148
 
 
149
 
 
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)
155
 
#     return d
156