def test_abs(self):
     tokens = self._lexer.lex("ABS(-3-4)")
     self.assertEqual(7, len(tokens))
     expression = Expression()
     s = SymbolTable()
     s.put_symbol("ABS", "⌊", SymbolType.FUNCTION, arg=None)
     value = expression.eval(tokens, symbols=s)
     self.assertEqual(7, value)
 def test_tuples(self):
     expression = Expression()
     tokens = self._lexer.lex('1,2')
     self.assertEqual(3, len(tokens))
     expression = Expression()
     s = SymbolTable()
     value = expression.eval(tokens, symbols=s)
     self.assertEqual([1.0, 2.0], value)
 def test_eval7(self):
     tokens = self._lexer.lex(" 1 + A")
     self.assertEqual(3, len(tokens))
     expression = Expression()
     symbol_table = SymbolTable()
     symbol_table.put_symbol("A", 22, SymbolType.VARIABLE, arg=None)
     value = expression.eval(tokens, symbols=symbol_table)
     self.assertEqual(23, value)
 def test_eval8(self):
     tokens = self._lexer.lex('"AB" + A$')
     self.assertEqual(3, len(tokens))
     expression = Expression()
     symbol_table = SymbolTable()
     symbol_table.put_symbol("A$", "C", SymbolType.VARIABLE, arg=None)
     value = expression.eval(tokens, symbols=symbol_table)
     self.assertEqual('ABC', value)
 def test_eval9(self):
     # Currently, our basic is LEFT associative for everything. That seems to have been common
     # for BASICs, but seems to not be the mathematical standard.
     tokens = self._lexer.lex('2^3^2')
     self.assertEqual(5, len(tokens))
     expression = Expression()
     symbol_table = SymbolTable()
     value = expression.eval(tokens, symbols=symbol_table)
     self.assertEqual(64, value)
    def test_comma(self):
        tokens = self._lexer.lex('(1,2)')
        self.assertEqual(5, len(tokens))
        expression = Expression()
        value = expression.eval(tokens)
        self.assertEqual([1, 2], value)

        tokens = self._lexer.lex('(1,2,3,4,5)')
        self.assertEqual(11, len(tokens))
        expression = Expression()
        value = expression.eval(tokens)
        self.assertEqual([1, 2, 3, 4, 5], value)

        tokens = self._lexer.lex('(1,2),(3,4)')
        self.assertEqual(11, len(tokens))
        expression = Expression()
        value = expression.eval(tokens)
        self.assertEqual([1, 2, 3, 4], value)
 def test_example_x2(self):
     tokens = self._lexer.lex('E>10ORD(7)=0')
     self.assertEqual(10, len(tokens))
     s = SymbolTable()
     s.put_symbol("E", 3000, SymbolType.VARIABLE, None)
     s.put_symbol("D", [0, 0, 0, 0, 0, 0, 0, 0], SymbolType.ARRAY, None)
     expression = Expression()
     value = expression.eval(tokens, symbols=s)
     self.assertEqual(True, value)
Exemple #8
0
def stmt_if(executor, stmt):
    """
    An if statement works by skipping to the next line, if the THEN clause is false, otherwise
    it continues to execute the clauses after the THEN.
    :param executor:
    :param stmt:
    :return: None
    """
    e = Expression()
    result = e.eval(stmt._tokens, symbols=executor._symbols)
    if not result:
        executor.goto_next_line()
 def test_eval_parens1(self):
     tokens = self._lexer.lex("(7-3)")
     self.assertEqual(5, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(4, value)
 def test_eval_parens3(self):
     tokens = self._lexer.lex(" 1 +(7-3) * 2")
     self.assertEqual(9, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(9, value)
 def test_eval_unary_minus3(self):
     tokens = self._lexer.lex("10*-8")
     self.assertEqual(4, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(-80, value)
 def test_eval_unary_minus2(self):
     tokens = self._lexer.lex("-9*3")
     self.assertEqual(4, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(-27, value)
 def test_eval6(self):
     tokens = self._lexer.lex(" 1 + 2 * 3 ^ 4 - 3")
     self.assertEqual(9, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(160, value)
 def test_eval_unary_minus4(self):
     tokens = self._lexer.lex("-(8+3)")
     self.assertEqual(6, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(-11, value)
 def test_eval_unary_minus5(self):
     tokens = self._lexer.lex("-(2+3)*-(4+5)+-1")
     self.assertEqual(16, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(44, value)
 def test_eval2(self):
     tokens = self._lexer.lex('"ABC"')
     self.assertEqual(1, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual("ABC", value)
 def test_eval3(self):
     tokens = self._lexer.lex("1-3")
     self.assertEqual(3, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(-2.0, value)
 def test_eval_unary_minus6(self):
     tokens = self._lexer.lex("--9")
     self.assertEqual(3, len(tokens))
     expression = Expression()
     with self.assertRaises(BasicSyntaxError):
         value = expression.eval(tokens)
 def test_eval1(self):
     tokens = self._lexer.lex("1")
     self.assertEqual(1, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(1, value)
 def test_types_x(self):
     tokens = self._lexer.lex('"a" + "b" + "c"')
     self.assertEqual(5, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual("abc", value)
 def test_eval_unary_minus7(self):
     tokens = self._lexer.lex("(2+3)-1")
     self.assertEqual(7, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(4, value)
Exemple #22
0
def eval_expression(symbols, value):
    lexer = get_lexer()
    tokens = lexer.lex(value)
    e = Expression()
    result = e.eval(tokens, symbols=symbols)
    return result
 def test_eval5(self):
     tokens = self._lexer.lex(" 1 - 3 * 2 ")
     self.assertEqual(5, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(-5.0, value)
 def test_eval_parens5(self):
     tokens = self._lexer.lex("(7-3) * ((2*4) - (2+5))")
     self.assertEqual(19, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(4, value)
 def test_eval_unary_minus10(self):
     tokens = self._lexer.lex("(-7--3)")
     self.assertEqual(7, len(tokens))
     expression = Expression()
     value = expression.eval(tokens)
     self.assertEqual(-4, value)