Example #1
0
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)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
 def evaluator(cls, node, environment):
     return Type.bool(node.value)
Example #5
0
 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)
Example #6
0
    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)