view python/c3/scope.py @ 153:e05b2b216bfc

Added common
author Windel Bouwman
date Sat, 02 Mar 2013 09:58:31 +0100
parents 4ae0e02599de
children b28a11c01dbe
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

def createBuiltins(scope):
   for tn in ['int', 'u32', 'u16', 'double']:
      scope.addSymbol(astnodes.BaseType(tn))

topScope = Scope()

createBuiltins(topScope)