view python/c3/scope.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents b28a11c01dbe
children e023d3ce1d63
line wrap: on
line source

from . import astnodes

class Scope:
   """ A scope contains all symbols in a scope """
   def __init__(self, parent=None):
      self.symbols = {}
      self.parent = parent
   def __iter__(self):
      return iter(self.symbols.values())
   def getSymbol(self, name):
      if name in self.symbols:
         return self.symbols[name]
      # Look for symbol:
      if self.parent:
         return self.parent.getSymbol(name)
      raise CompilerException("Symbol {0} not found".format(name), name.loc)
   def hasSymbol(self, name):
      if name in self.symbols:
         return True
      if self.parent:
         return self.parent.hasSymbol(name)
      return False
   def addSymbol(self, sym):
      self.symbols[sym.name] = sym

# buildin types:
intType = astnodes.BaseType('int')
doubleType = astnodes.BaseType('double')
voidType = astnodes.BaseType('void')
boolType = astnodes.BaseType('void')

def createBuiltins(scope):
   for tn in ['u64', 'u32', 'u16', 'u8']:
      scope.addSymbol(astnodes.BaseType(tn))
   for t in [intType, doubleType, voidType, boolType]:
      scope.addSymbol(t)

topScope = Scope()
createBuiltins(topScope)