예제 #1
0
    def semantic(self, scope):
        # HACK: check scope.symbols directly, because we do not care if outer
        # scopes have a symbol with the same name, only if two symbols have
        # the same name, and are at the same level.

        if self.__wasChecked: return self
        self.__wasChecked = True

        if scope.symbols.get(self.name, self) is not self:
            raise error.NameError, "A symbol named '%s' already exists in the current scope." % self.name

        scope[self.name] = self

        self.__checkBases(scope)
        self.__checkConcreteness(scope)

        childScope = Scope(parent=scope)
        childScope.klass = self

        # If the class has no constructors, define a default ctor
        from ast.ctor import Ctor, DefaultCtor
        hasCtor = bool([d for d in self.body.decls if isinstance(d, Ctor)])

        if not hasCtor:
            self.body.decls.append(DefaultCtor())

        self.body = self.body.semantic(childScope)

        return self
예제 #2
0
    def semantic2(self, scope):
        childScope = Scope(parent=scope)
        childScope.klass = self

        self.body = self.body.semantic2(childScope)

        return self
예제 #3
0
    def semantic(self, scope):
        if scope.symbols.get(self.name, self) is not self:
            raise error.NameError, "A symbol named '%s' already exists in the current scope." % self.name

        scope[self.name] = self

        childScope = Scope(parent=scope)
        childScope.klass = self
        self.body = self.body.semantic(childScope)
        return self
예제 #4
0
    def testNestedResolve(self):
        scope2 = Scope(parent=self.scope)
        self.scope.addSymbol('foo', foo)
        scope2.addSymbol('bar', bar)

        result = scope2.resolveSymbol('bar')
        self.assertEqual(bar, result)

        result = self.scope.resolveSymbol('bar')
        self.assertEqual(None, result)
예제 #5
0
    def semantic(self, scope):
        from nine.scope import Scope

        arg = self.arg.semantic(scope)
        t = arg.getType()

        if t != vartypes.BooleanType:
            raise TypeError, "While condition must be of type boolean, not %r" % t

        childScope = Scope(parent=scope)
        childScope.innerLoop = self

        block = self.block.semantic(childScope)

        return WhileStatement(arg, block)
예제 #6
0
class ScopeTest(unittest.TestCase):
    def setUp(self):
        self.scope = Scope(parent=None)

    def testCtor(self):
        # If execution passes the setUp phase, the test has already passed.
        pass

    def testAddSymbol(self):
        self.scope.addSymbol('foo', foo)

    def testBadAddSymbol(self):
        self.scope.addSymbol('foo', foo)

        self.assertRaises(
            AssertionError,
            lambda: self.scope.addSymbol('foo', foo)
        )

    def testSimpleResolve(self):
        self.scope.addSymbol('foo', foo)
        self.assertEqual(foo, self.scope.resolveSymbol('foo'))

    def testNestedResolve(self):
        scope2 = Scope(parent=self.scope)
        self.scope.addSymbol('foo', foo)
        scope2.addSymbol('bar', bar)

        result = scope2.resolveSymbol('bar')
        self.assertEqual(bar, result)

        result = self.scope.resolveSymbol('bar')
        self.assertEqual(None, result)
예제 #7
0
 def setUp(self):
     self.scope = Scope(parent=None)