summaryrefslogtreecommitdiffstats
path: root/src/front
diff options
context:
space:
mode:
Diffstat (limited to 'src/front')
-rw-r--r--src/front/scope.py20
-rw-r--r--src/front/symbol.py34
-rw-r--r--src/front/symbols.py30
3 files changed, 54 insertions, 30 deletions
diff --git a/src/front/scope.py b/src/front/scope.py
new file mode 100644
index 0000000..2cc3018
--- /dev/null
+++ b/src/front/scope.py
@@ -0,0 +1,20 @@
+class Scope(object, DictMixin):
+ def __init__(self, parent):
+ self.parent = parent
+ self.__symbols = {}
+
+ def __getitem__(self, key):
+ try:
+ return self.__symbols[key]
+ except KeyError:
+ return self.parent[key]
+
+ def __setitem__(self, key, item):
+ self.__symbols[key] = item
+
+ def keys(self):
+ keys = self.__symbols.keys()
+ for key in self.parent.keys():
+ if key not in keys:
+ keys.append(key)
+ return keys
diff --git a/src/front/symbol.py b/src/front/symbol.py
new file mode 100644
index 0000000..879403f
--- /dev/null
+++ b/src/front/symbol.py
@@ -0,0 +1,34 @@
+class SymbolClass(object):
+ def __init__(self, name):
+ self.name = name
+
+ def __str__(self):
+ return self.name
+
+ def __repr__(self):
+ return "<SymbolClass: %s>" % self
+
+SymbolClass.CONST = SymbolClass("CONST")
+SymbolClass.VAR = SymbolClass("VAR")
+SymbolClass.FUNC = SymbolClass("FUNC")
+
+class SymbolType(object):
+ def __init__(self, name):
+ self.name = name
+
+ def __str__(self):
+ return self.name
+
+ def __repr__(self):
+ return "<SymbolType: %s>" % self
+
+SymbolType.INT = SymbolType("INT")
+SymbolType.BOOL = SymbolType("BOOL")
+
+class Symbol(object):
+ __slots__ = ['cls', 'type', 'value']
+
+ def __init__(self, value, type = SymbolType.INT, cls = SymbolClass.VAR):
+ self.value = value
+ self.type = type
+ self.cls = cls
diff --git a/src/front/symbols.py b/src/front/symbols.py
deleted file mode 100644
index 0f98d83..0000000
--- a/src/front/symbols.py
+++ /dev/null
@@ -1,30 +0,0 @@
-class SymbolTable:
- def __init__(self):
- self.symbols = {}
- return
-
- def put(self, token, id = None):
- try:
- id = int(id)
- except TypeError:
- try:
- id = max(self.symbols.values()) + 1
- except ValueError:
- id = 0
- except ValueError:
- raise ValueError("only digits as id possible. '%s' is not a number" % id)
-
- self.symbols[token] = id
- return id
-
- def get(self, token):
- try:
- return self.symbols[token]
- except KeyError:
- return None
-
- def getOrPut(self, token):
- try:
- return self.symbols[token]
- except KeyError:
- return self.put(token)