-
Notifications
You must be signed in to change notification settings - Fork 5
/
hierarchy.py
116 lines (103 loc) · 3.07 KB
/
hierarchy.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Tampio Compiler
# Copyright (C) 2018 Iikka Hauhio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from fatal_error import typeError
classes = {}
def getClass(name):
if name in classes:
return classes[name]
else:
typeError("class not found: " + name)
def isClass(name):
return name in classes
def classSet():
return set(classes.values())
functions = {}
fields = {}
def getFunctions(name):
if name in functions:
return functions[name]
else:
return []
def getFields(name):
if name in fields:
return fields[name]
else:
return []
class Class:
def __init__(self, name, super_class):
self.name = name
self.super_class = super_class and getClass(super_class)
if self.super_class:
self.super_class.addSubclass(self)
self.subclasses = set()
self.fields = []
self.functions = []
self.methods = []
self.comparison_operators = []
def __repr__(self):
return "<Class " + self.name + ">"
def addSubclass(self, cl):
self.subclasses.add(cl)
if self.super_class:
self.super_class.addSubclass(cl)
def addField(self, name, plural):
f = Field(name, plural, self)
self.fields.append(f)
if name not in fields:
fields[name] = []
fields[name].append(f)
def addFunction(self, name, arg_form, expr):
f = Function(name, arg_form, expr, self)
if name+"_"+str(arg_form) not in functions:
functions[name+"_"+str(arg_form)] = []
functions[name+"_"+str(arg_form)].append(f)
self.functions.append(f)
def addMethod(self, name, arg_forms):
self.methods.append(Method(name, arg_forms))
def addComparisonOperator(self, name):
self.comparison_operators.append(ComparisonOperator(name))
def hasField(self, name):
return any([f.name == name for f in self.fields])
def hasFunction(self, name):
return name in [f.name for f in self.functions]
class Field:
def __init__(self, name, plural, cl):
self.name = name
self.plural = plural
self.cl = cl
def classes(self):
return set([self.cl]) | self.cl.subclasses
class Function:
def __init__(self, name, arg_form, expr, cl):
self.name = name
self.arg_form = arg_form
self.expr = expr
self.cl = cl
self.type = None
def inferType(self):
if self.type is None:
self.type = set() # rekursion vuoksi tyypin on oltava inferenssin aikana tyhjä
self.type = self.expr.inferType()
return self.type
def classes(self):
return set([self.cl]) | self.cl.subclasses
class Method:
def __init__(self, name, arg_forms):
self.name = name
self.arg_forms = arg_forms
class ComparisonOperator:
def __init__(self, name):
self.name = name