From 103eca0b6bde9649786fa9aa54bdf3bf01c5d63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20Gra=C3=9F?= Date: Sun, 21 Jul 2019 08:49:54 +0200 Subject: [PATCH] check naming of variables --- TODO | 6 ++++-- main.py | 4 +++- tree_nodes.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index c58cb09..6a9d517 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,9 @@ General + Read from file! > Rho for vars & funs several statements - args really just in dictio in function body? + > args really just in dictio in function body? + parser doing proper separation? b > basic (BasicValue) @@ -9,7 +11,7 @@ b x > var (Variable) - one word, further specifications? -> check in constructor + > one word, further specifications? -> check in constructor global, local? (op1 e) diff --git a/main.py b/main.py index b30c069..da21adb 100644 --- a/main.py +++ b/main.py @@ -32,4 +32,6 @@ print() print(parse_tree("3 / 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")) \ No newline at end of file + 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")) \ No newline at end of file diff --git a/tree_nodes.py b/tree_nodes.py index 679214f..f29ec59 100644 --- a/tree_nodes.py +++ b/tree_nodes.py @@ -17,9 +17,27 @@ def __str__(self): super_str = super(BasicValue, self).__str__() return super_str + ": " + str(self.value) +def check_var(var): + for char in var: + if ord(char) < 48 or ord(char) > 57 and ord(char) < 65 or ord(char) > 91 and ord(char) < 97 or ord(char) > 122: + if not char == '_': + raise Exception('"{}" is not a valid variable name.'.format(var)) + + try: + int(var[0]) + raise Exception('"{}" is not a valid variable name.'.format(var)) + except ValueError: + pass + + if var[0] == '_' or var[-1] == '_': + raise Exception('"{}" is not a valid variable name.'.format(var)) + class Variable(Node): def __init__(self, tag, var, depth=0): + if(tag != "dummy"): + check_var(var) + self.var = var super(Variable, self).__init__(tag, depth)