Skip to content

Commit

Permalink
some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoosierEE committed Aug 19, 2024
1 parent be3ea6a commit 800d0b9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion proto2/lisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@ def Parse(x:list)->list:

def Eval(e:dict,x:list|int|str)->int|list:#Eval({},Parse(Split('(if 1 42 43)')))
match x:
case ('lam',[a,*b],c): return ['lam',a,Eval(e,['lam',b,c])]
case ('lam',a,b): return x
case int()|('lam',_,_):return x
case str(): return e[x]
case ('-',a,b): return Eval(e,a)-Eval(e,b)
case ('let',a,b,c): return Eval({**e,a:Eval(e,b)},c)
case ('if',a,b,c): return Eval(e,b) if Eval(e,a) else Eval(e,c)
case (('lam',a,b),*c): return Eval({**e,**dict(zip(a,(Eval(e,i) for i in c)))},b)
case (('lam',a,b),*c): return Eval({**e,**dict(zip(a,(Eval(e,i) for i in c)))},b) # FIXME: ((lam (a b) b) 1)
case list(): return Eval(e,[Eval(e,i) for i in x])

if __name__=="__main__":#unit tests
x = [a.strip() for a in '''
42 ⇒ 42
(lam (a b) b) ⇒ ['lam', 'a', ['lam', 'b', 'b']]
(- 1 4) ⇒ -3
(let x 1 (- 43 x)) ⇒ 42
(lam x (- 0 x)) ⇒ ['lam', 'x', ['-', 0, 'x']]
(if (- 3 3) 1 2) ⇒ 2
(if 42 1 2) ⇒ 1
((lam x x) 3) ⇒ 3
(let f (lam x 3) (f 1)) ⇒ 3
(let a 1 ((lam _ a) 2)) ⇒ 1
(let a 3 (let f (let a 1 (lam _ a)) (f 2))) ⇒ 1
'''.splitlines()[1:-1]]
# # failing tests:
# (let f (let a 1 (lam _ a)) (f 2)) ⇒ 1
# (if (let x 1 x) x 2) ⇒ 1
# ((lam (a b) (- a b)) (5 3)) ⇒ 2
sz = max(map(len,x))
for t in x:
i,o = t.split(' ⇒ ')
Expand Down

0 comments on commit 800d0b9

Please sign in to comment.