Esempio n. 1
0
    def newInstanceContext(self, context):
        from prompto.declaration.SingletonCategoryDeclaration import SingletonCategoryDeclaration
        from prompto.expression.CategorySymbol import CategorySymbol
        from prompto.value.ConcreteInstance import ConcreteInstance
        from prompto.value.NativeInstance import NativeInstance
        from prompto.type.CategoryType import CategoryType
        from prompto.error.NullReferenceError import NullReferenceError

        value = self.parent.interpret(context)
        if value is None or value is NullValue.instance:
            raise NullReferenceError()
        if isinstance(value, TypeValue):
            typ = value.value
            if isinstance(typ, CategoryType):
                decl = typ.getDeclaration(context)
                if isinstance(decl, SingletonCategoryDeclaration):
                    value = context.loadSingleton(value.value)
        if isinstance(value, CategorySymbol):
            value = value.interpret(context)
        if isinstance(value, TypeValue):
            return context.newChildContext()
        elif isinstance(value, (ConcreteInstance, NativeInstance)):
            context = context.newInstanceContext(value, None)
            return context.newChildContext()
        else:
            context = context.newBuiltInContext(value)
            return context.newChildContext()
Esempio n. 2
0
 def interpret(self, context):
     o = self.parent.interpret(context)
     i = self.item.interpret(context)
     if o is None or o is NullValue.instance or \
             i is None or i is NullValue.instance:
         raise NullReferenceError()
     return o.getItem(context, i)
Esempio n. 3
0
    def interpret(self, context):
        resContext = context if isinstance(
            context, ResourceContext) else context.newResourceContext()
        o = self.resource.interpret(resContext)
        if o is None:
            raise NullReferenceError()
        if not isinstance(o, IResource):
            raise InternalError("Illegal write source: " + o)
        if not o.isWritable():
            raise InvalidResourceError("Not writable")
        text = self.content.interpret(resContext)
        try:
            if context is resContext:
                o.writeLine(text)
            elif self.thenWith is not None:

                def callback(text):
                    local = context.newChildContext()
                    local.registerValue(
                        Variable(self.thenWith.name, TextType.instance))
                    local.setValue(self.thenWith.name, TextValue(text))
                    self.thenWith.statements.interpret(local)

                o.writeFully(text, callback)
            else:
                o.writeFully(text)
            return None
        finally:
            if context is not resContext:
                o.close()
Esempio n. 4
0
 def interpret(self, context):
     # resolve parent to keep clarity
     parent = self.resolveParent(context)
     instance = parent.interpret(context)
     if instance is None or instance is NullValue.instance:
         raise NullReferenceError()
     else:
         return instance.getMemberValue(context, self.name, False)
Esempio n. 5
0
 def interpretParentInstance(self, context: Context):
     value = self.parent.interpret(context)
     if value is None or value == NullValue.instance:
         raise NullReferenceError()
     from prompto.expression.SuperExpression import SuperExpression
     if isinstance(self.parent, SuperExpression):
         return value.itype.getSuperType(context)
     else:
         return value.itype
Esempio n. 6
0
 def interpret(self, context):
     if not context.isWithResourceContext():
         raise SyntaxError("Not a resource context!")
     o = self.resource.interpret(context)
     if o is None:
         raise NullReferenceError()
     if not isinstance(o, IResource):
         raise InternalError("Illegal read source: " + o)
     if not o.isReadable():
         raise InvalidResourceError("Not readable")
     return o.readLine()
Esempio n. 7
0
 def interpret(self, context):
     context = context.newResourceContext()
     o = self.resource.interpret(context)
     if o is None:
         raise NullReferenceError()
     if not isinstance(o, IResource):
         raise InternalError("Illegal read source: " + o)
     if not o.isReadable():
         raise InvalidResourceError("Not readable")
     try:
         return BlobValue(o.readBinary())
     finally:
         o.close()
Esempio n. 8
0
 def interpret(self, context):
     itype = self.source.check(context)
     if not isinstance(itype, (ListType, TupleType, SetType)):
         raise SyntaxError("Unsupported type: " + itype)
     o = self.source.interpret(context)
     if o is None:
         raise NullReferenceError()
     if not isinstance(o, (ListValue, TupleValue, SetValue)):
         raise InternalError("Unexpected type:" + type(o).__name__)
     items = o.getIterator(context)
     getter = itype.itemType.getSortKeyReader(context, self.key)
     list = sorted(items, key=getter, reverse=self.desc)
     return ListValue(itype.itemType, items=list)
Esempio n. 9
0
 def interpretReference(self, context):
     # resolve parent to keep clarity
     parent = self.resolveParent(context)
     instance = parent.interpret(context)
     if instance is None or instance is NullValue.instance:
         raise NullReferenceError()
     elif isinstance(instance, IInstance):
         category = instance.getDeclaration()
         methods = category.getMemberMethodsMap(context, self.name)
         method = methods.getFirst() # TODO check prototype
         return ClosureValue(context.newInstanceContext(instance, None, True), MethodType(method))
     else:
         raise SyntaxError("Should never get here!")
Esempio n. 10
0
 def interpret(self, context):
     listType = self.source.check(context)
     if not isinstance(listType, IterableType):
         raise InternalError("Illegal source type: " + listType.typeName)
     itemType = listType.itemType
     items = self.source.interpret(context)
     if items is None:
         raise NullReferenceError()
     if not isinstance(items, IFilterable):
         raise InternalError("Illegal fetch source: " + str(items))
     arrow = self.predicate.toArrowExpression()
     xfilter = arrow.getFilter(context, itemType)
     return items.filter(xfilter)
Esempio n. 11
0
 def interpret(self, context):
     o = self.parent.interpret(context)
     if o is None:
         raise NullReferenceError()
     if isinstance(o, ISliceable):
         fi = None if self.first is None else self.first.interpret(context)
         if fi is not None and not isinstance(fi, IntegerValue):
             raise SyntaxError("Illegal sliced type: " + str(fi))
         li = None if self.last is None else self.last.interpret(context)
         if li is not None and not isinstance(li, IntegerValue):
             raise SyntaxError("Illegal sliced type: " + str(li))
         return o.slice(fi, li)
     else:
         raise SyntaxError("Illegal sliced object: " + self.parent)
Esempio n. 12
0
 def interpret(self, context):
     resContext = context if isinstance(
         context, ResourceContext) else context.newResourceContext()
     o = self.resource.interpret(resContext)
     if o is None:
         raise NullReferenceError()
     if not isinstance(o, IResource):
         raise InternalError("Illegal write source: " + o)
     if not o.isWritable():
         raise InvalidResourceError("Not writable")
     text = self.content.interpret(resContext)
     try:
         if context is resContext:
             o.writeLine(text)
         else:
             o.writeFully(text)
         return None
     finally:
         if context is not resContext:
             o.close()
Esempio n. 13
0
 def findMember(self, context, module):
     p = self.parent.interpret(context, module)
     if p is None:
         raise NullReferenceError()
     return getattr(p, self.name)
Esempio n. 14
0
 def interpretNative(self, context, returnType):
     try:
         return self.doInterpretNative(context, returnType)
     except ReferenceError:
         raise NullReferenceError()
Esempio n. 15
0
 def interpret(self, context):
     try:
         return self.doInterpret(context)
     except ReferenceError:
         raise NullReferenceError()