Skip to content

Commit

Permalink
conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraGrass committed Jul 21, 2019
1 parent 103eca0 commit 0e50d2e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 53 deletions.
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ x

( e1 op2 e2 )
> add, sub, mul, div
> gt, ge, lt, le, eq

( if e0 then e1 else e2 )
> if
conditions
> conditions

( e' e0 ... ek−1 )
> app
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# do actual work
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(parse_tree("if 1 then 3 else 4"))
print()
print(parse_tree("let x3 = 4 in + x3 "))
print()
Expand All @@ -34,4 +34,4 @@
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 test"))
print(parse_tree("let y_x = 4 in y_x"))
94 changes: 44 additions & 50 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,21 @@ def mk_if(x, depth, dictio):

return Other("if", [e0, e1, e2], depth)

def mk_add(x, depth, dictio):
def mk_op2(x, depth, dictio, tag):
e1 = parse_tree(x['e1'], depth+1, dictio)
e2 = parse_tree(x['e2'], depth+1, dictio)
return Other("add", [e1, e2], depth)
return Other(tag, [e1, e2], depth)

def mk_sub(x, depth, dictio):
e1 = parse_tree(x['e1'], depth+1, dictio)
e2 = parse_tree(x['e2'], depth+1, dictio)
return Other("sub", [e1, e2], depth)

def mk_mul(x, depth, dictio):
e1 = parse_tree(x['e1'], depth+1, dictio)
e2 = parse_tree(x['e2'], depth+1, dictio)
return Other("mul", [e1, e2], depth)

def mk_div(x, depth, dictio):
e1 = parse_tree(x['e1'], depth+1, dictio)
e2 = parse_tree(x['e2'], depth+1, dictio)
return Other("div", [e1, e2], depth)

def mk_uadd(x, depth, dictio):
e = parse_tree(x['e'], depth+1, dictio)
return Other("uadd", [e], depth)

def mk_usub(x, depth, dictio):
def mk_op1(x, depth, dictio, tag):
e = parse_tree(x['e'], depth+1, dictio)
return Other("usub", [e], depth)

def is_known(expr, dictio):
return expr in dictio
return Other(tag, [e], depth)

def is_var(expr, dictio):
try:
return dictio[expr] == "var"
except KeyError:
return False
def is_known(expr, dictio, tag=""):
if tag == "":
return expr in dictio

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

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

Expand Down Expand Up @@ -173,61 +142,86 @@ def parse_tree(expr, depth=0, dictio={}):

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

# if e0 then e1 else e2
x = parse("if {e0} then {e1} else {e2}", expr)
if(x != None):
return mk_if(x, depth, dictio)

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

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

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

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

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

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

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

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

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

# +e
x = parse("+{e}", expr)
if(x != None):
return mk_uadd(x, depth, dictio)
return mk_op1(x, depth, dictio, "uadd")

# -e
x = parse("-{e}", expr)
if(x != None):
return mk_usub(x, depth, dictio)
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_var(expr, dictio)):
if(is_known(expr, dictio, "var")):
return Variable("var", expr, depth)

# argument
if(is_arg(expr, dictio)):
if(is_known(expr, dictio, "arg")):
return Variable("arg", expr, depth)

# basic value
if(is_int(expr)):
return BasicValue("basic", int(expr), depth)

# default case
# raise Exception('"{}" is not a valid expression.'.format(expr))
return Variable("dummy", expr, depth)
raise Exception('"{}" is not a valid expression.'.format(expr))
# return Variable("dummy", expr, depth)

0 comments on commit 0e50d2e

Please sign in to comment.