Exemplo n.º 1
0
 def interpret(self, context):
     value = self.expression.interpret(context)
     if value is not None:
         target = getTargetType(context, self.itype, self.mutable)
         if isinstance(value,
                       IntegerValue) and target == DecimalType.instance:
             value = DecimalValue(value.DecimalValue())
         elif isinstance(value,
                         DecimalValue) and target == IntegerType.instance:
             return IntegerValue(value.IntegerValue())
         elif target.isMoreSpecificThan(context, value.itype):
             value.itype = self.itype
     return value
Exemplo n.º 2
0
 def interpret(self, context):
     value = self.expression.interpret(context)
     if value is not None and value != NullValue.instance:
         target = getTargetType(context, self.itype, self.mutable)
         if target != value.itype:
             if isinstance(value, IntegerValue) and target == DecimalType.instance:
                 value = DecimalValue(value.DecimalValue())
             elif isinstance(value, DecimalValue) and target == IntegerType.instance:
                 return IntegerValue(value.IntegerValue())
             elif value.itype.isAssignableFrom(context, target):
                 value.itype = self.itype
             elif not target.isAssignableFrom(context, value.itype):
                 raise SyntaxError("Cannot cast " + str(value.itype) + " to " + str(self.itype))
     return value
Exemplo n.º 3
0
 def Subtract(self, context, value):
     if isinstance(value, IntegerValue):
         return IntegerValue(self.IntegerValue() - value.IntegerValue())
     elif isinstance(value, DecimalValue):
         return DecimalValue(self.DecimalValue() - value.DecimalValue())
     else:
         raise SyntaxError("Illegal: Integer - " + type(value).__name__)
Exemplo n.º 4
0
 def Add(self, context, value):
     if isinstance(value, IntegerValue):
         return IntegerValue(self.IntegerValue() + value.IntegerValue())
     elif isinstance(value, DecimalValue):
         return DecimalValue(value.DecimalValue() + self.value)
     else:
         raise SyntaxError("Illegal: Integer + " + type(value).__name__)
Exemplo n.º 5
0
 def Divide(self, context, value):
     if isinstance(value, INumber):
         if value.DecimalValue() == 0.0:
             raise DivideByZeroError()
         else:
             return DecimalValue(self.DecimalValue() / value.DecimalValue())
     else:
         raise SyntaxError("Illegal: Integer / " + type(value).__name__)
Exemplo n.º 6
0
 def interpretPromotion(self, item):
     if item is None:
         return item
     if DecimalType.instance == self.itemType and item.itype == IntegerType.instance:
         return DecimalValue(item.DecimalValue())
     elif TextType.instance == self.itemType and item.itype == CharacterType.instance:
         return TextValue(item.value)
     else:
         return item
Exemplo n.º 7
0
 def Multiply(self, context, value):
     if isinstance(value, IntegerValue):
         return IntegerValue(self.IntegerValue() * value.IntegerValue())
     elif isinstance(value, DecimalValue):
         return DecimalValue(value.DecimalValue() * self.IntegerValue())
     elif isinstance(value, IMultiplyable):
         return value.Multiply(context, self)
     else:
         raise SyntaxError("Illegal: Integer * " + type(value).__name__)
Exemplo n.º 8
0
 def interpret(self, context):
     val = self.expression.interpret(context)
     if isinstance(val, IntegerValue):
         return IntegerValue(-val.IntegerValue())
     elif isinstance(val, DecimalValue):
         return DecimalValue(-val.DecimalValue())
     elif isinstance(val, PeriodValue):
         return val.inverse()
     else:
         raise SyntaxError("Illegal: - " + type(val).__name__)
Exemplo n.º 9
0
def convert(obj):
    if obj is None:
        return NullValue.instance
    elif inspect.isgenerator(obj):
        return convert(list(obj))
    elif isinstance(obj, list):
        return convertList(obj)
    elif isinstance(obj, dict):
        return convertDocument(obj)
    elif isinstance(obj, int):
        return IntegerValue(obj)
    elif isinstance(obj, float):
        return DecimalValue(obj)
    elif isinstance(obj, bool):
        return BooleanValue(obj)
    elif isinstance(obj, str):
        return TextValue(obj)
    else:
        raise ReadWriteError("Cannot convert: " + type(obj).__name__)
Exemplo n.º 10
0
 def readJSONField(self, context, node, parts):
     if node is None:
         return NullValue.instance
     elif isinstance(node, bool):
         return BooleanValue.ValueOf(node)
     elif isinstance(node, int):
         return IntegerValue(node)
     elif isinstance(node, float):
         return DecimalValue(node)
     elif isinstance(node, str):
         return TextValue(node)
     elif isinstance(node, list):
         raise Exception("list")
     elif isinstance(node, dict):
         raise Exception("dict")
     elif isinstance(node, object):
         raise Exception("object")
     else:
         raise Exception(str(type(node)))
Exemplo n.º 11
0
 def convertPythonValueToPromptoValue(self, context, value, returnType):
     if isinstance(value, Number):
         return DecimalValue(float(value))
     else:
         return super(DecimalType, self).convertPythonValueToPromptoValue(
             context, value, returnType)  # TODO for now
Exemplo n.º 12
0
 def __init__(self, text):
     super(DecimalLiteral, self).__init__(text, DecimalValue.Parse(text))
Exemplo n.º 13
0
 def autocast(self, decl, value):
     if isinstance(value, IntegerValue) and decl.getType() is DecimalType.instance:
         value = DecimalValue(value.DecimalValue())
     return value
Exemplo n.º 14
0
 def autocast(self, name, value):
     if isinstance(value, IntegerValue):
         actual = self.instances.get(name)
         if actual.getType(self) is DecimalType.instance:
             value = DecimalValue(value.DecimalValue())
     return value
Exemplo n.º 15
0
def toDecimal(node):
    return DecimalValue(node)