Пример #1
0
def evalComparison(self):
    if self.left is None:
        raise RuntimeException("Left mode missing", self)
    elif self.right is None:
        raise RuntimeException("Left mode missing", self)

    m1 = self.left.evaluate()
    m2 = self.right.evaluate()

    if m1.type == DataType.NIL or m2.type == DataType.NIL:
        return Data(False, DataType.BOOLEAN)

    if ((m1.type == DataType.STRING and m2.type == DataType.STRING) or
        (m1.type == DataType.BOOLEAN and m2.type == DataType.BOOLEAN) or
        (isNumberType(m1.type) and isNumberType(m2.type))):
        if self.token.type == TokenType.GREATER:
            return Data(m1.value > m2.value, DataType.BOOLEAN)
        if self.token.type == TokenType.GREATER_EQUAL:
            return Data(m1.value >= m2.value, DataType.BOOLEAN)
        if self.token.type == TokenType.LESS:
            return Data(m1.value < m2.value, DataType.BOOLEAN)
        if self.token.type == TokenType.LESS_EQUAL:
            return Data(m1.value <= m2.value, DataType.BOOLEAN)
    else:
        raise RuntimeException("Invalid data type for comparison", self)
Пример #2
0
def evalAddition(self):
    if self.left is None:
        raise RuntimeException("Left mode missing", self)
    elif self.right is None:
        raise RuntimeException("Left mode missing", self)

    m1 = self.left.evaluate()
    if not isNumberType(m1.type) and m1.type != DataType.STRING:
        raise RuntimeException("Invalid data type for operation", self.left)
    m2 = self.right.evaluate()
    if not isNumberType(m2.type) and m2.type != DataType.STRING:
        raise RuntimeException("Invalid data type for operation", self.right)

    if self.token.type == TokenType.PLUS and m1.type == DataType.INTEGER and m2.type == DataType.INTEGER:
        return Data(m1.value + m2.value, DataType.INTEGER)
    elif self.token.type == TokenType.PLUS and isNumberType(m1.type) and isNumberType(m2.type):
        return Data(float(m1.value + m2.value), DataType.DOUBLE)
    elif self.token.type == TokenType.PLUS and m1.type == DataType.STRING and isNumberType(m2.type):
        return Data(m1.value + str(m2.value), DataType.STRING)
    elif self.token.type == TokenType.PLUS and isNumberType(m1.type) and m2.type == DataType.STRING:
        return Data(str(m1.value) + m2.value, DataType.STRING)
    elif self.token.type == TokenType.PLUS and m1.type == DataType.STRING and m2.type == DataType.STRING:
        return Data(m1.value + m2.value, DataType.STRING)
    elif self.token.type == TokenType.MINUS and m1.type == DataType.INTEGER and m2.type == DataType.INTEGER:
        return Data(m1.value - m2.value, DataType.INTEGER)
    elif self.token.type == TokenType.MINUS and isNumberType(m1.type) and isNumberType(m2.type):
        return Data(m1.value - m2.value, DataType.DOUBLE)
    else:
        raise RuntimeException("Data type mismatch in addition/subtraction", self)
Пример #3
0
def evalAssignment(self):
    global currentBlockEnvironment

    if self.right is None:
        raise RuntimeException('No r-value for assignment', self)
    if self.left is None:
        raise RuntimeException('No l-value for assignment', self)
    data = self.right.evaluate()
    currentBlockEnvironment.set(self.left.token.text, data)
    return data
Пример #4
0
 def get(self, key):
     if key in self.environment:
         return self.environment[key]
     elif self.container is not None:
         return self.container.get(key)
     else:
         raise RuntimeException("Undefined variable '{0}'".format(key))
Пример #5
0
def evalIf(self):
    if self.condition is None:
        raise RuntimeException("No conditional given", self)
    b = isTrue(self.condition.evaluate())
    if b and self.left is not None:
        self.left.evaluate()
    elif not b and self.right is not None:
        self.right.evaluate()
Пример #6
0
 def set(self, key, data):
     if key in self.environment:
         self.environment[key] = data
         return True
     elif self.container is not None and self.container.set(key, data):
         return True
     else:
         raise RuntimeException("Cannot set undefined variable '{0}'".format(key))
Пример #7
0
def evalMultiplication(self):
    if self.left is None:
        raise RuntimeException("Left mode missing", self)
    elif self.right is None:
        raise RuntimeException("Right mode missing", self)

    m1 = self.left.evaluate()
    if m1.type != DataType.INTEGER and m1.type != DataType.DOUBLE:
        raise RuntimeException("Invalid data type for operation", self.left)
    m2 = self.right.evaluate()
    if m2.type != DataType.INTEGER and m2.type != DataType.DOUBLE:
        raise RuntimeException("Invalid data type for operation", self.right)

    if self.token.type == TokenType.STAR and m1.type == DataType.INTEGER and m2.type == DataType.INTEGER:
        return Data(m1.value * m2.value, DataType.INTEGER)
    elif self.token.type == TokenType.STAR:
        return Data(float(m1.value * m2.value), DataType.DOUBLE)
    elif self.token.type == TokenType.SLASH:
        return Data(m1.value / m2.value, DataType.DOUBLE)
Пример #8
0
def evalUnary(self):
    if self.right is None:
        error("No right node for unary operator")

    v = self.right.evaluate()
    if self.token.type == TokenType.MINUS and isNumberType(v.type):
        return Data(v.value * -1, v.type)
    elif self.token.type == TokenType.BANG and v.type == DataType.BOOLEAN:
        return Data(not v.value, v.type)
    else:
        raise RuntimeException("Invalid data type for unary operator", self.right)
Пример #9
0
def evalVariableDecl(self):
    global currentBlockEnvironment

    if self.left is None:
        raise RuntimeException("No left node on var declaration", self)
    if self.right is not None:
        data = self.right.evaluate()
        currentBlockEnvironment.declare(self.left.token.text)
        currentBlockEnvironment.set(self.left.token.text, data)
    else:
        currentBlockEnvironment.declare(self.left.token.text)
Пример #10
0
def evalEquality(self):
    if self.left is None:
        raise RuntimeException("Left mode missing", self)
    elif self.right is None:
        raise RuntimeException("Left mode missing", self)

    m1 = self.left.evaluate()
    m2 = self.right.evaluate()

    if m1.type == DataType.NIL or m2.type == DataType.NIL:
        return Data(False, DataType.BOOLEAN)

    if ((m1.type == DataType.STRING and m2.type == DataType.STRING) or
            (m1.type == DataType.BOOLEAN and m2.type == DataType.BOOLEAN) or
            (isNumberType(m1.type) and isNumberType(m2.type))):
        if self.token.type == TokenType.EQUAL_EQUAL:
            return Data(m1.value == m2.value, DataType.BOOLEAN)
        if self.token.type == TokenType.BANG_EQUAL:
            return Data(m1.value != m2.value, DataType.BOOLEAN)
    else:
        raise RuntimeException("Invalid data type for equality", self)
Пример #11
0
    def call(self, env):
        aData = env.get("a")
        if aData is None:
            raise RuntimeException(
                "'a' parameter is not defined in call to mod")
        bData = env.get("b")
        if bData is None:
            raise RuntimeException(
                "'b' parameter is not defined in call to mod")

        if aData.type != DataType.DOUBLE and aData.type != DataType.INTEGER:
            raise RuntimeException(
                "'a' argument for call to mod of invalid type")
        if bData.type != DataType.DOUBLE and bData.type != DataType.INTEGER:
            raise RuntimeException(
                "'b' argument for call to mod of invalid type")

        if aData.type == DataType.INTEGER and bData.type == DataType.INTEGER:
            return Data(aData.value % bData.value, DataType.INTEGER)
        else:
            return Data(aData.value % bData.value, DataType.DOUBLE)
Пример #12
0
def evalPrint(self):
    if self.right is None:
        raise RuntimeException("Invalid statement, missing expression", self)

    m1 = self.right.evaluate()
    if m1.type == DataType.BOOLEAN and m1.value:
        print("true")
    elif m1.type == DataType.BOOLEAN and not m1.value:
        print("false")
    elif m1.type == DataType.NIL:
        print("nil")
    else:
        print(m1.value)
Пример #13
0
def confirmArityMatch(func, args):
    if (args is None and len(func.params) > 0) or (args is not None and len(args) != len(func.params)):
        raise RuntimeException('For call to "{0}": expected {1} arguments, found {2}'.format(func.value, len(func.params), len(args)))
Пример #14
0
def evalExpr(self):
    if self.left is not None:
        RuntimeException("Left on EXPRESSION is not none!  What do I do?", self)
    if self.right is not None:
        return self.right.evaluate()