Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraGrass committed Jul 21, 2019
1 parent 0e50d2e commit 916e721
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ General
> Rho for vars & funs
several statements
> args really just in dictio in function body?
parser doing proper separation?
parser doing proper separation? -> nope

b
> basic (BasicValue)
Expand Down
22 changes: 20 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
print(args.input)
print(args.output)

# do actual work
# own examples
print(parse_tree("let a = 17 in let f = fun b -> a + b in f (39 + 2)"))
print()
print(parse_tree("if 1 then 3 else 4"))
Expand All @@ -34,4 +34,22 @@
print()
print(parse_tree("let rec f = fun x y -> if y <= 1 then x else f ( x * y ) ( y - 1 ) in f 1"))
print()
print(parse_tree("let y_x = 4 in y_x"))
print(parse_tree("let y_x = 4 in y_x"))
print()

# from lecture slides
print(parse_tree("let rec fac = fun x -> if x <= 1 then 1 else x * fac (x-1) in fac 7")) # 102
print()

# TODO: for let f, first "in" is chosen (a in b)
# print(parse_tree("let c = 5 in let f = fun a -> let b = a * a in b + c in f c")) # 114
# print()

print(parse_tree("let a = 19 in let b = a * a in a + b")) # 131
print()

print(parse_tree("let a = 17 in let f = fun b -> a + b in f 42")) # 140
print()

print(parse_tree("let rec f = fun x y -> if y <= 1 then x else f ( x * y ) ( y - 1 ) in f 1")) # 159
print()
30 changes: 15 additions & 15 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def parse_tree(expr, depth=0, dictio={}):
# remove unnecessary whitespace
expr = re.sub(' +', ' ',expr).strip()

# (e)
x = parse("({e})", expr)
if(x != None):
return parse_tree(x['e'], depth, dictio)

# let rec x1 = e1 and ... and xn = en in e0
x = parse("let rec {defs} in {e0}", expr)
if(x != None):
Expand Down Expand Up @@ -175,16 +180,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, "add")

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

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

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

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

# +e
x = parse("+{e}", expr)
if(x != None):
Expand All @@ -205,11 +210,6 @@ def parse_tree(expr, depth=0, dictio={}):
if(x != None):
return mk_op1(x, depth, dictio, "usub")

# (e)
x = parse("({e})", expr)
if(x != None):
return parse_tree(x['e'], depth, dictio)

# variable
if(is_known(expr, dictio, "var")):
return Variable("var", expr, depth)
Expand Down

0 comments on commit 916e721

Please sign in to comment.