diff --git a/TODO b/TODO index 9026908..16c3050 100644 --- a/TODO +++ b/TODO @@ -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) @@ -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 @@ -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 \ No newline at end of file + rec \ No newline at end of file diff --git a/main.py b/main.py index fe2a475..3f9fc5b 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/parser.py b/parser.py index 475ff30..925e1b6 100644 --- a/parser.py +++ b/parser.py @@ -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) @@ -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) @@ -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" @@ -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) @@ -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() @@ -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)