Skip to content

Commit

Permalink
check naming of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraGrass committed Jul 21, 2019
1 parent a608c8e commit 103eca0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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)
> check if int

x
> var (Variable)
one word, further specifications? -> check in constructor
> one word, further specifications? -> check in constructor
global, local?

(op1 e)
Expand Down
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
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"))
18 changes: 18 additions & 0 deletions tree_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 103eca0

Please sign in to comment.