view python/c3/semantics.py @ 148:e5263f74b287

Added c3 language frontend initial parser
author Windel Bouwman
date Fri, 01 Mar 2013 10:24:01 +0100
parents
children 74241ca312cc
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 getType(self, name):
      t = self.getSymbol(name)
      print(t)
      assert isinstance(t, astnodes.Type)
      return t
   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):
   scope.addSymbol(astnodes.BaseType('int'))

class Semantics:
   """ This class constructs the AST from parser input """
   def __init__(self, diag):
      self.diag = diag
   def handlePackage(self, name, loc):
      self.mod = astnodes.Package(name)
      self.mod.loc = loc
      self.mod.scope = self.curScope = Scope()
      createBuiltins(self.curScope)
   def handleBinop(self, lhs, op, rhs):
      pass
   def actOnLocal(self, t, name, ival):
      s = astnodes.Variable(name, t, False)
      self.curScope.addSymbol(s)
   def actOnType(self, tok):
      # Try to lookup type, in case of failure return void
      pass