Skip to content

Commit

Permalink
function application
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraGrass committed Jul 20, 2019
1 parent 8bc2fc6 commit 4bcbf45
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
16 changes: 10 additions & 6 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
General
Rho for vars & funs, several statements
> Rho for vars & funs
several statements

b
> basic (BasicValue)
check if int
> check if int

x
> var (Variable)
Expand All @@ -12,7 +13,7 @@ x

(op1 e)
> uadd, usub, brkt (Other)
brackets don't need own nodes
> brackets don't need own nodes

( e1 op2 e2 )
> add, sub, mul, div
Expand All @@ -23,14 +24,17 @@ x

( e' e0 ... ek−1 )
> app
fun has to be known
> fun has to be known
> split e0 to ek-1 correctly?
convert "39 + 3" to "39+3"

( fun x0 ... xk−1 -> e )
> fun
differ between args and vars
> differ between args and vars

( let x1 = e1 in e0 )
> let
> differ between var and fun?

( let rec x1 = e1 and ... and xn = en in e0 )
> rec
rec
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
print(args.output)

# do actual work
print(parse_tree("let a = 17 in let f = fun b -> a + b in f 42"))
print(parse_tree("let a = 17 in let f = fun b -> a + b in f (39 + 2)"))
print()
print(parse_tree("if a then 3 else 4"))
print()
print(parse_tree("+ x3 "))
print(parse_tree("let x3 = 4 in + x3 "))
print()
print(parse_tree("((34))"))
print()
Expand Down
55 changes: 52 additions & 3 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import re

def mk_let(x, depth, dictio):
x1 = Variable("var", x['x1'], depth+1)
dictio[x['x1']] = "var"
tag = "fvar" if x['e1'].split(' ', 1)[0] == "fun" else "var"

x1 = Variable(tag, x['x1'], depth+1)
dictio[x['x1']] = tag

e1 = parse_tree(x['e1'], depth+1, dictio)
e0 = parse_tree(x['e0'], depth+1, dictio)
Expand All @@ -25,6 +27,37 @@ def mk_fun(x, depth, dictio):

return Other("fun", children, depth)

def split_expr(exprs):
print(exprs)

x = parse("({e1}) {e2}", exprs)
if(x != None):
tmp = [x['e1']]
tmp.extend(split_expr(x['e2']))
return tmp

x = parse("({e})", exprs)
if(x != None):
return [x['e']]

x = exprs.split(' ', 1)

if len(x) == 1:
return [x[0]]

else:
tmp = [x[0]]
tmp.extend(split_expr(x[1]))
return tmp

def mk_app(x, depth, dictio):
children = [Variable("fvar", x[0], depth+1)]

for expr in split_expr(x[1]):
children.append(parse_tree(expr, depth+1, dictio))

return Other("app", children, depth)

def mk_if(x, depth, dictio):
e0 = parse_tree(x['e0'], depth+1, dictio)
e1 = parse_tree(x['e1'], depth+1, dictio)
Expand Down Expand Up @@ -60,6 +93,9 @@ def mk_usub(x, depth, dictio):
e = parse_tree(x['e'], depth+1, dictio)
return Other("usub", [e], depth)

def is_known(expr, dictio):
return expr in dictio

def is_var(expr, dictio):
try:
return dictio[expr] == "var"
Expand All @@ -72,6 +108,12 @@ def is_arg(expr, dictio):
except KeyError:
return False

def is_fun(expr, dictio):
try:
return dictio[expr] == "fvar"
except KeyError:
return False

def is_int(expr):
try:
int(expr)
Expand All @@ -81,6 +123,10 @@ def is_int(expr):

def parse_tree(expr, depth=0, dictio={}):

# clear the dictionary
if(depth==0):
dictio.clear()

# remove unnecessary whitespace
expr = re.sub(' +', ' ',expr).strip()

Expand All @@ -98,7 +144,10 @@ def parse_tree(expr, depth=0, dictio={}):
return mk_fun(x, depth, dictio)

# e' e0 ... ek−1
# TODO
x = expr.split(' ', 1)
if(is_fun(x[0], dictio)):
print(x[0])
return mk_app(x, depth, dictio)

# if e0 then e1 else e2
x = parse("if {e0} then {e1} else {e2}", expr)
Expand Down

0 comments on commit 4bcbf45

Please sign in to comment.