Skip to content

Commit

Permalink
start partial lambda application
Browse files Browse the repository at this point in the history
  • Loading branch information
hoosierEE committed Jul 30, 2024
1 parent c123e23 commit 4da52e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
19 changes: 11 additions & 8 deletions prototype/Eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
from .Semantic import Val,Name
import operator as op

type Expr = float|int|list|str
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()|str(): return e[x]
case Val(): return Eval(x.t(x.v),e)
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 (':',a,b):
Expand All @@ -23,8 +25,9 @@ def Eval(x:Expr,e:dict) -> Expr:
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 (op,*xs) if op in e:
proc = e[op]
args = [Eval(a,e) for a in xs]
return proc(*args)
case list(): return Eval([Eval(i,e) for i in x],e)
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)
28 changes: 20 additions & 8 deletions prototype/Semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
from .Builtin import ASSIGN,VERB
from dataclasses import dataclass

class Name(str): pass
class Builtin(str): pass
class Name(str): pass
class Nil(object): pass
class Sym(str): pass

# type Builtin = str
# type Name = str
# type Nil = None
# type Sym = str

def ty(x:str) -> type:
'''infer types of literals'''
if x[0] in '`"': return (str,Sym)[x[0]=='`']
Expand Down Expand Up @@ -85,7 +80,24 @@ 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 4da52e6

Please sign in to comment.