Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hoosierEE committed Aug 5, 2024
1 parent 46523a5 commit 494eb28
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
23 changes: 12 additions & 11 deletions prototype/Eval.py
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
24 changes: 4 additions & 20 deletions prototype/Semantic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .Ast import Ast
from .Parser import NIL
from .Builtin import ASSIGN,VERB
from dataclasses import dataclass

class LengthError(Exception): pass
class Builtin(str): pass
class Name(str): pass
class Nil(object): pass
Expand Down Expand Up @@ -80,24 +81,7 @@ def formalize(a:Ast) -> Ast:
return Ast(a.node, Ast('[',*(map(Ast,filter(str,xyz)))), *map(formalize,a.children))#insert (prg x y z)
return Ast(a.node, *map(formalize,a.children))

def par(a:Ast) -> Ast:
'''
convert partial applications to new lambdas with fewer args
(after formalize)
{x,y}[;2] ⇒ {x,2}
{x,y,z}[;2] ⇒ {x,2,y}
{x,y,z}[;;3] ⇒ {x,y,3}
{z}[3] ⇒ {y}
{y}[2] ⇒ {x}
{z}[;4] ⇒ {x}
{z}[;;2] ⇒ 2
'''
match a.node:
case 'app' if a.children[0].node=='{':
return ... #TODO
return Ast(a.node, *map(par,a.children))

def Sema(a:Ast) -> Ast|Val:
'''wrapper function for all the semantic analysis passes, in the right order'''
# return infer(formalize(lamc(lamp(a))))
return (formalize(lamc(lamp(a))))
return infer(formalize(lamc(lamp(a))))
# return formalize(lamc(lamp(a)))

0 comments on commit 494eb28

Please sign in to comment.