-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
16 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
from .Ast import Ast | ||
from .Semantic import Val,Name | ||
import operator as op | ||
|
||
type Expr = float|int|list|Name|Nil | ||
|
||
def Env() -> dict: | ||
return { | ||
'-': op.sub, | ||
'+': op.add, | ||
# 'app': lambda x,y: x(*y), | ||
} | ||
|
||
def Eval(x:Expr,e:dict) -> Expr: | ||
# print(dict(set(e.items()-set(Env().items()))),x) | ||
match x: | ||
case Name(): return e[x] | ||
case Val() if isinstance(x.v,str): return x.t(x.v) | ||
case Val() if isinstance(x.v,Ast): return [x.t(v.node) for v in x.v.children] | ||
case float()|int()|('{',_,_): return x | ||
case Ast(): return Eval([x.node,*x.children],e) | ||
case Val() if isinstance(x.v,Ast): return [x.t(v.node) for v in x.v[1]] | ||
case Val() if x.t == Name: return e[x.v] | ||
case Val(): return x.t(x.v) | ||
case Ast(): return Eval([x.node,*x[1]],e) | ||
case (':',a,b): | ||
k = Eval(a,e) if a.v in e else a.v | ||
e[k] = Eval(b,e) | ||
return e[k] | ||
case ('(',*xs): return [Eval(x,e) for x in xs] | ||
case (';',*xs): return Eval([Eval(x,e) for x in xs][-1],e) | ||
case ('app',a,b): | ||
(an,ac),(bn,bc) = a.t,b.t | ||
if an+bn=='{[': # FIXME - handle projection | ||
args = [x.v for x in ac[0][1]] | ||
given = [Eval(i,e) for i in bc] | ||
return Eval(ac[1],{**e,**dict(zip(args,given))}) | ||
return 42 | ||
case (f,*xs) if f in e: return e[f](*(Eval(a,e) for a in xs)) | ||
case (f,*xs) if f=='app': | ||
# TODO | ||
return 0 | ||
case list(): return [Eval(i,e) for i in x] | ||
case _: print('wat?',x) | ||
case _: return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters