Skip to content

Commit

Permalink
fixed wrong let parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraGrass committed Jul 27, 2019
1 parent cecf893 commit 0b02f1b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ General
several statements
> args really just in dictio in function body?
parser doing proper separation? -> nope
change imports in main

b
> basic (BasicValue)
Expand Down
58 changes: 47 additions & 11 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ def mk_rec(x, depth, dictio):

return Other("rec", children, depth)

def parse_let(expr):

expr = expr.split()
if (len(expr) == 0 or expr[0] != "let"):
return None

counter = 0
ind = -1

for i in range(len(expr)):
if (expr[i] == "let"):
counter += 1
elif (expr[i] == "in"):
counter -= 1

if(counter == 0):
ind = i
break

if (ind == -1):
return None

tmp1 = expr[1:ind]
e0 = expr[ind+1:]

prs = parse("{x1} = {e1}", " ".join(tmp1))
if(prs is None):
return None

x = dict()
x['x1'] = prs['x1']
x['e1'] = prs['e1']
x['e0'] = " ".join(e0)

return x

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

Expand Down Expand Up @@ -136,7 +172,7 @@ def parse_tree(expr, depth=0, dictio={}):
return mk_rec(x, depth, dictio)

# let x1 = e1 in e0
x = parse("let {x1} = {e1} in {e0}", expr)
x = parse_let(expr)
if(x != None):
return mk_let(x, depth, dictio)

Expand Down Expand Up @@ -180,16 +216,6 @@ def parse_tree(expr, depth=0, dictio={}):
if(x != None):
return mk_op2(x, depth, dictio, "eq")

# e1 * e2
x = parse("{e1}*{e2}", expr)
if(x != None):
return mk_op2(x, depth, dictio, "mul")

# e1 / e2
x = parse("{e1}/{e2}", expr)
if(x != None):
return mk_op2(x, depth, dictio, "div")

# e1 + e2
x = parse("{e1}+{e2}", expr)
if(x != None):
Expand All @@ -200,6 +226,16 @@ def parse_tree(expr, depth=0, dictio={}):
if(x != None):
return mk_op2(x, depth, dictio, "sub")

# e1 * e2
x = parse("{e1}*{e2}", expr)
if(x != None):
return mk_op2(x, depth, dictio, "mul")

# e1 / e2
x = parse("{e1}/{e2}", expr)
if(x != None):
return mk_op2(x, depth, dictio, "div")

# +e
x = parse("+{e}", expr)
if(x != None):
Expand Down

0 comments on commit 0b02f1b

Please sign in to comment.