Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit 073fed7

Browse files
committed
stub in assignment (only works for simple assignment of symbols) and expose some functions from roots in a hacky manner.
1 parent a6de4f1 commit 073fed7

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

eval.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
def ac_global_name(s):
1919
return Symbol('_' + str(s))
2020

21+
EXPOSED_FUNCTIONS = (car, cdr, caar, cadr, cdar, cddr, cons,
22+
list, last, append, reverse, nconc, nconc1,
23+
map)
2124

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)
2628

2729

2830
class InterpretedFunction(object):
@@ -52,8 +54,8 @@ def ac_eval(s, env):
5254
return ac_if(cdr(s), env)
5355
elif xcar(s) == Symbol('fn'):
5456
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)
5759
elif acons(s):
5860
print 'DEBUG: funcall'
5961
return ac_call(car(s), cdr(s), env)
@@ -67,12 +69,14 @@ def literal(x):
6769

6870
def ac_var_ref(s, env):
6971
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)),
7173
for key in env:
7274
print key, type(key)
7375
if s in env:
7476
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
7680

7781

7882
# Should False Python objects be false eventually?
@@ -111,6 +115,38 @@ def ac_body(body, env):
111115
return car(revmap(lambda x: ac_eval(x, env), body))
112116

113117

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+
114150
def ac_complex_args(args):
115151
'''Does a fn arg list use optional params or destructuring?'''
116152
if args is nil or isSymbol(args):
@@ -122,7 +158,7 @@ def ac_complex_args(args):
122158

123159

124160
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)))
126162

127163

128164
def tle():

roots.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,38 @@
33
'''
44
from symbol import *
55

6+
# Functions we'll expose eventually need to be defined with def or they
7+
# won't have names.
8+
69
# We can't use tuples here because we need mutable cons cells.
710
# Objects are big and slow.
8-
cons = lambda x, y: [x,y]
9-
car = lambda x: x[0]
10-
cdr = lambda x: x[1]
11-
12-
caar = lambda x: x[0][0]
13-
cadr = lambda x: x[1][0] # car of cdr.
14-
cdar = lambda x: x[0][1]
15-
cddr = lambda x: x[1][1]
11+
def cons(x,y):
12+
return [x, y]
13+
def car(x):
14+
return x[0]
15+
def cdr(x):
16+
return x[1]
17+
18+
def caar(x):
19+
return x[0][0]
20+
def cadr(x):
21+
return x[1][0]
22+
def cdar(x):
23+
return x[0][1]
24+
def cddr(x):
25+
return x[1][1]
1626

1727
pylist = list
1828

1929
# Atom is a problem: we're going to expose Python 2-tuples in our Lisp
2030
# eventually, and those are probably atoms. Let's just get it working
2131
# and then we'll worry about the distinction; the associated car/cdr
2232
# implementations are bad too.
23-
acons = lambda x: isinstance(x, pylist) and len(x) == 2
24-
atom = lambda x: not acons(x)
33+
def acons(x):
34+
return isinstance(x, pylist) and len(x) == 2
35+
36+
def atom(x):
37+
return not acons(x)
2538

2639
# Readable types:
2740
# - lists
@@ -53,8 +66,8 @@ def revappend(x, y):
5366
# return y
5467
# return revappend(cdr(x), cons(car(x), y))
5568
while x is not nil:
56-
x = cdr(x)
5769
y = cons(car(x), y)
70+
x = cdr(x)
5871
return y
5972

6073
def reverse(x):

0 commit comments

Comments
 (0)