Skip to content

Commit

Permalink
need to handle indexing on lists like vec
Browse files Browse the repository at this point in the history
  • Loading branch information
hoosierEE committed May 8, 2024
1 parent a2fe7a9 commit 97764d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
24 changes: 17 additions & 7 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,28 @@ Some [[https://github.com/doctest/doctest/tree/master/doc/markdown#reference][do
- [-] prototype implementation
- [X] lex/scan/tokenize
- [X] parse
- [ ] semantic analysis
- [ ] type inference
- [-] semantic analysis
- [ ] verbs
- [ ] iterators
- [ ] type checking
- [ ] name binding
- [ ] other errors (arity, unused)
- [-] type inference
- [X] (int|float|string|symbol)
- [X] vec of (int|float|string|symbol)
- [X] list
- [ ] tensor
- [ ] dict
- [ ] table
- [X] name binding
- [X] function application
- [X] variable names and lexical scope
- [X] composition/projection =(2+)1=
- [X] projection ⇒ lambda
- [ ] composition ⇒ lambda
- [ ] errors (mutable, rank, unused, ...)
- [-] codegen
- [X] tree-walk interpreter
- [X] simple arithmetic =1+2=
- [X] array arithmetic =1 2+3 4=
- [ ] type inference
- [ ] variable names and lexical scope
- [ ] composition/projection =(2+)1=
- [ ] iterators =+/1 2 3=
- [ ] structural functions =3#"hi""world"=
- [-] hardware accelerated implementation
Expand Down
16 changes: 12 additions & 4 deletions prototype/Eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def v2(op:str,a,b):

def evl(x:Ast,e=None) -> Val:
e = e or {}
x = formalize(lift_prj(x))
print(x)
return Eval(e,formalize(lift_prj(x)))

def Eval(e,x) -> Val:
Expand All @@ -86,7 +88,12 @@ def Eval(e,x) -> Val:
e[n] = r#NOTE: reference semantics make this update visible to caller
return r

if x.node in ('cmp','prj','{'): return x
if x.node in [*'(;']:#list or sequence
ks = [Eval(e,i) for i in x.children]
if x.node == ';': return ks[-1]#return the last of the sequence (progn)
return Val('L',ks)

if x.node in ('{','cmp','prj'): return x#defer eval of lambda, cmp, prj until/if they are applied.

if x.node in ('@','app'):#"app" always has 2 children TODO: what about "@"?
b = Eval(e,x.children[0])#body (...should it use {**e} instead of e?)
Expand All @@ -95,12 +102,13 @@ def Eval(e,x) -> Val:
newenv = {a.node:v for a,v in zip(b.children[0].children,args)}
return Eval({**e,**newenv},b.children[1])
if type(b)==Val:
if b.t.isupper() and a.t=='a':
return v2('@',b,a)
#FIXME: "a" not defined
# a = args[0] # not quite...
if b.t.isupper() and a.t=='a': return v2('@',b,a)

raise RuntimeError(f'nyi: {x}')

if x.node[0] in VERB:
if type(x.node)==str and x.node[0] in VERB:
k = [Eval(e,c) for c in x.children[::(-1,1)[x.node in '(;']]]
return (0,v1,v2)[len(x.children)](x.node,*k[::-1])
else:
Expand Down
7 changes: 4 additions & 3 deletions prototype/Semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

def lift_prj(a:Ast) -> Ast:
'''projection ⇒ lambda'''
ax,ay = Ast('x'),Ast('y')
match a.node,len(a.children):
case 'prj',1:
if (v:=a.children[0].node)[0] in VERB and v.endswith(':'):
return Ast('{',Ast('[',Ast('x')), Ast(a.children[0],Ast('x')))
return Ast('{',Ast('[',*map(Ast,'xy')), Ast(a.children[0],*map(Ast,'xy')))
return Ast('{',Ast('[',ax), Ast(v,ax))
return Ast('{',Ast('[',ax,ay), Ast(v,ax,ay))
case 'prj',2:
return Ast('{',Ast('[',Ast('x')), Ast(a.children[0],Ast('x'),lift_prj(a.children[1])))
return Ast('{',Ast('[',ax), Ast(a.children[0].node,lift_prj(a.children[1]),ax))
return Ast(a.node, *map(lift_prj,a.children))

def get_params(a:Ast) -> str:
Expand Down

0 comments on commit 97764d0

Please sign in to comment.