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
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
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__)
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__)
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__)
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
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__)
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__)
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__)
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)))
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
def __init__(self, text): super(DecimalLiteral, self).__init__(text, DecimalValue.Parse(text))
def autocast(self, decl, value): if isinstance(value, IntegerValue) and decl.getType() is DecimalType.instance: value = DecimalValue(value.DecimalValue()) return value
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
def toDecimal(node): return DecimalValue(node)