-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_nodes.py
56 lines (41 loc) · 1.57 KB
/
tree_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Node(object):
def __init__(self, tag, depth=0):
self.tag = tag
self.depth = depth
def __str__(self):
return " " * self.depth + self.tag
class BasicValue(Node):
def __init__(self, tag, value, depth=0):
self.value = value
super(BasicValue, self).__init__(tag, depth)
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)
def __str__(self):
super_str = super(Variable, self).__str__()
return super_str + ": " + self.var
class Other(Node):
def __init__(self, tag, children, depth=0):
self.children = children
super(Other, self).__init__(tag, depth)
def __str__(self):
super_str = super(Other, self).__str__()
return super_str + ":\n" + '\n'.join(map(str, self.children))