18
18
def ac_global_name (s ):
19
19
return Symbol ('_' + str (s ))
20
20
21
+ EXPOSED_FUNCTIONS = (car , cdr , caar , cadr , cdar , cddr , cons ,
22
+ list , last , append , reverse , nconc , nconc1 ,
23
+ map )
21
24
22
- arc_globals = dict ((ac_global_name (sym ), value ) for sym , value in [
23
- (t , t ),
24
- (Symbol ('car' ), car )
25
- ])
25
+ arc_globals = dict ((ac_global_name (Symbol (f .func_name )), f ) for f in EXPOSED_FUNCTIONS )
26
+ arc_globals [t ] = t
27
+ arc_globals [ac_global_name (Symbol ('+' ))] = lambda * args : sum (args )
26
28
27
29
28
30
class InterpretedFunction (object ):
@@ -52,8 +54,8 @@ def ac_eval(s, env):
52
54
return ac_if (cdr (s ), env )
53
55
elif xcar (s ) == Symbol ('fn' ):
54
56
return ac_fn (cadr (s ), cddr (s ), env )
55
- # elif xcar(s) == Symbol('assign'):
56
- # return ac_set(cdr(s), env)
57
+ elif xcar (s ) == Symbol ('assign' ):
58
+ return ac_set (cdr (s ), env )
57
59
elif acons (s ):
58
60
print 'DEBUG: funcall'
59
61
return ac_call (car (s ), cdr (s ), env )
@@ -67,12 +69,14 @@ def literal(x):
67
69
68
70
def ac_var_ref (s , env ):
69
71
assert isSymbol (s )
70
- print 'Referencing %s (type %s) in with keys' % (s , type (s )),
72
+ print 'DEBUG: Referencing %s (type %s) in env with keys' % (s , type (s )),
71
73
for key in env :
72
74
print key , type (key )
73
75
if s in env :
74
76
return env [s ]
75
- return arc_globals [ac_global_name (s )]
77
+ glo = arc_globals [ac_global_name (s )]
78
+ print 'DEBUG: returning global %s' % glo
79
+ return glo
76
80
77
81
78
82
# Should False Python objects be false eventually?
@@ -111,6 +115,38 @@ def ac_body(body, env):
111
115
return car (revmap (lambda x : ac_eval (x , env ), body ))
112
116
113
117
118
+ # Elided ac_setn; it's ac_set.
119
+ def ac_set (x , env ):
120
+ if x is nil :
121
+ return nil
122
+ return cons (ac_set1 (ac_macex (car (x )), cadr (x ), env ),
123
+ ac_set (cddr (x ), env ))
124
+
125
+
126
+ def ac_set1 (a , b1 , env ):
127
+ # Omitting dbname because it's done in ac_fn too.
128
+ if isSymbol (a ):
129
+ b = ac_eval (b1 , env )
130
+ if a is nil :
131
+ raise Exception ('Can\' t rebind nil' )
132
+ elif a is t :
133
+ raise Exception ('Can\' t rebind t' )
134
+ elif a in env :
135
+ env [a ] = b
136
+ else :
137
+ arc_globals [ac_global_name (a )] = b
138
+ else :
139
+ raise Exception ('First arg to set must be a symbol! (Given %s)' % a )
140
+
141
+
142
+ def ac_macex (x ):
143
+ # STUB. TODO.
144
+ if isSymbol (x ):
145
+ return x
146
+ else :
147
+ raise NotImplementedError ()
148
+
149
+
114
150
def ac_complex_args (args ):
115
151
'''Does a fn arg list use optional params or destructuring?'''
116
152
if args is nil or isSymbol (args ):
@@ -122,7 +158,7 @@ def ac_complex_args(args):
122
158
123
159
124
160
def ac_call (fn , args , env ):
125
- return ac_eval (fn , env )(* topylist (args ))
161
+ return ac_eval (fn , env )(* topylist (map ( lambda x : ac_eval ( x , env ), args ) ))
126
162
127
163
128
164
def tle ():
0 commit comments