def getValueAccordingToType(value, type): try: if type.value == Type.STRING: return Type.string(value) if type.value == Type.INTEGER: return Type.integer(int(value)) if type.value == Type.BOOL: consumedChars, token = boolTokenizer(value, 0, 0) if consumedChars > 0: return Type.bool(token.value) return ValueError() if type.value == Type.NOTE: consumedChars, token = noteTokenizer(value, 0, 0) if consumedChars > 0: return Type.note(token.value) raise ValueError() raise RuntimeException( f"Type {type.value.name.lower()} is not supported", None) except ValueError: raise RuntimeException( f"Invalid value '{value}' for type {type.value.name.lower()}", None)
def evaluator(cls, node, environment): value = expressionEvaluator(doAssert=True)(node.value, environment).value if value.type != Type.BOOL: raise RuntimeException(f"Operator '{node.operator.value}' is supported only by {Type.BOOL.name.lower()} type", node.value.pos) return Type.bool(not value.value)
def otherRelationOperatorsEvaluator(cls, left, operator, right): if left.type in [Type.INTEGER, Type.FLOAT ] and right.type in [Type.INTEGER, Type.FLOAT]: if operator.value == ">": return Type.bool(left.value > right.value) if operator.value == ">=": return Type.bool(left.value >= right.value) if operator.value == "<": return Type.bool(left.value < right.value) if operator.value == "<=": return Type.bool(left.value <= right.value) raise RuntimeException( f"Operator {operator.value} is not supported by {left.type.name.lower()} and {right.type.name.lower()} types", operator.pos)
def evaluator(cls, node, environment): return Type.bool(node.value)
def evaluator(cls, node, environment): left = expressionEvaluator(doAssert=True)(node.left, environment).value right = expressionEvaluator(doAssert=True)(node.right, environment).value return Type.bool(left.value and right.value)
def notEqualOperatorEvaluator(cls, left, operator, right): if left.type in [Type.INTEGER, Type.FLOAT ] and right.type in [Type.INTEGER, Type.FLOAT]: return Type.bool(left.value != right.value) return Type.bool(left.type != right.type or left.value != right.value)