Esempio n. 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)
Esempio n. 2
0
    def stringEvaluator(cls, left, operator, right):
        if operator.value == "+":
            return Type.string(left.value + right.value)

        if operator.value == "-":
            raise RuntimeException(
                f"Operator {operator.value} is not supported by string types",
                operator.pos)

        raise RuntimeError("This line should never be reached")
Esempio n. 3
0
def getTuning(config):
    key = Type.string("tuning")
    if key in config.value:
        tuning = config.value[key]
        if not tuning.type in [Type.INTEGER, Type.FLOAT] or tuning.value < 0:
            raise RuntimeException("The 'tuning' property must be non-negative integer or float", None)

        return tuning.value

    return DEFAULT_TUNING
Esempio n. 4
0
def getAttack(config):
    key = Type.string("attack")
    if key in config.value:
        attack = config.value[key]
        if not attack.type in [Type.INTEGER, Type.FLOAT] or attack.value < 0:
            raise RuntimeException("The 'attack' property must be non-negative integer or float", None)

        return attack.value

    return DEFAULT_ATTACK
Esempio n. 5
0
def getDecay(config):
    key = Type.string("decay")
    if key in config.value:
        decay = config.value[key]
        if not decay.type in [Type.INTEGER, Type.FLOAT] or decay.value < 0:
            raise RuntimeException("The 'decay' property must be non-negative integer or float", None)

        return decay.value

    return DEFAULT_DECAY
Esempio n. 6
0
def getBpm(config):
    key = Type.string("bpm")
    if key in config.value:
        bpm = config.value[key]
        if bpm.type != Type.INTEGER or bpm.value <= 0:
            raise RuntimeException("The 'bpm' property must be positive integer", None)

        return bpm.value

    return DEFAULT_BPM
Esempio n. 7
0
    def evaluator(cls, node, environment):
        map = {}
        keyEvaluator = Evaluator.oneOf(
            Evaluator.forNodes(lambda node, environment: EvaluationResult.OK(Type.string(node.value)), Identifier),
            expressionEvaluator(doAssert=True)
        )
        for entry in node.children:
            key = keyEvaluator(entry.key, environment).value
            if key in map:
                raise RuntimeException(f"Duplicated key '{key.stringify()}' found in map", entry.pos)
            map[key] = expressionEvaluator(doAssert=True)(entry.value, environment).value

        return Type.map(map)
Esempio n. 8
0
def getOvertones(config):
    key = Type.string("overtones")
    if key in config.value:
        overtones = config.value[key]
        rawOvertones = [overtone.value for overtone in overtones.value]
        if overtones.type != Type.LIST or not all(overtone.type in [Type.FLOAT, Type.INTEGER] for overtone in overtones.value):
            raise RuntimeException("The 'overtones' property must be list of floats", None)

        if len(rawOvertones) < 1:
            raise RuntimeException("The 'overtones' property must contain one overtone at least", None)

        if any(overtone < 0 for overtone in rawOvertones):
            raise RuntimeException("The 'overtones' property mustn't contain negative values", None)

        if sum(rawOvertones) > 1.0:
            raise RuntimeException("The 'overtones' property must contain overtones which sum is not greater than 1.0", None)

        return rawOvertones

    return DEFAULT_OVERTONES
Esempio n. 9
0
 def evaluator(cls, node, environment):
     return Type.string(node.value)
Esempio n. 10
0
def _function(env, object):
    return Type.string(object.stringify())
Esempio n. 11
0
 def evaluateForString(cls, value):
     return Type.string(value[::-1])
Esempio n. 12
0
def _function2(env, prompt):
    print(prompt.value, end="")
    value = input()
    return Type.string(value)
Esempio n. 13
0
def _function1(env):
    value = input()
    return Type.string(value)