def test_assignment_complex(self): parser = Parser() tree = parser.run("x = y = 3") self.assertEqual(tree.op, Token(lexems.ASSIGNMENT, lexems.ASSIGNMENT)) self.assertEqual(tree.right.op, Token(lexems.ASSIGNMENT, lexems.ASSIGNMENT)) self.assertEqual(tree.left.val, Token(lexems.LETTER, 'x'))
def test_fn_def_add(self): parser = Parser() tree = parser.run("fn add x y => x + z") self.assertEqual(Token(lexems.LETTER, "add"), tree.name) self.assertEqual( [Token(lexems.LETTER, "x"), Token(lexems.LETTER, "y")], tree.arguments) self.assertEqual(Token(lexems.PLUS, lexems.PLUS), tree.definition.op)
def test_fn_def(self): parser = Parser() tree = parser.run("fn avg x y => (x + y) / 2") self.assertEqual(Token(lexems.LETTER, "avg"), tree.name) self.assertEqual( [Token(lexems.LETTER, "x"), Token(lexems.LETTER, "y")], tree.arguments) self.assertEqual(Token(lexems.DIVIDE, lexems.DIVIDE), tree.definition.op)
def test_example_operation(self): tokens = self.lexer.parse("x + 6") result = [ Token(lexems.LETTER, 'x'), Token(lexems.PLUS, lexems.PLUS), Token(lexems.DIGIT, 6.0), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_fn_def_echo_call_no_def(self): parser = Parser() tree = parser.run("fn echo x => x") self.assertEqual(Token(lexems.LETTER, "echo"), tree.name) self.assertEqual([Token(lexems.LETTER, "x")], tree.arguments) self.assertEqual(Token(lexems.LETTER, "x"), tree.definition.val) with self.assertRaises(Exception) as context: tree = parser.run("echo1 11")
def test_example_assignment(self): tokens = self.lexer.parse("x = 7") result = [ Token(lexems.LETTER, 'x'), Token(lexems.ASSIGNMENT, lexems.ASSIGNMENT), Token(lexems.DIGIT, 7.0), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_example_fn_call_inc(self): tokens = self.lexer.parse("a = inc a") result = [ Token(lexems.LETTER, "a"), Token(lexems.ASSIGNMENT, lexems.ASSIGNMENT), Token(lexems.LETTER, 'inc'), Token(lexems.LETTER, 'a'), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_example_assignment_chain_hard(self): tokens = self.lexer.parse("x = 13 + (y = 3)") result = [ Token(lexems.LETTER, 'x'), Token(lexems.ASSIGNMENT, lexems.ASSIGNMENT), Token(lexems.DIGIT, 13.0), Token(lexems.PLUS, lexems.PLUS), Token(lexems.OPEN_BRACKET, lexems.OPEN_BRACKET), Token(lexems.LETTER, 'y'), Token(lexems.ASSIGNMENT, lexems.ASSIGNMENT), Token(lexems.DIGIT, 3.0), Token(lexems.CLOSE_BRACKET, lexems.CLOSE_BRACKET), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def run(self, line): self.clear() self.tokens = self.lexer.parse(line) res = self.expr() if self.next() != Token(EOF, ""): raise (Exception("Error: Invalid input")) return res
def test_example_fn_definition_error(self): tokens = self.lexer.parse("fn add x y => x + z") result = [ Token(lexems.FN_KEYWORD, lexems.FN_KEYWORD), Token(lexems.LETTER, "add"), Token(lexems.LETTER, 'x'), Token(lexems.LETTER, 'y'), Token(lexems.FN_OPERATOR, lexems.FN_OPERATOR), Token(lexems.LETTER, 'x'), Token(lexems.PLUS, lexems.PLUS), Token(lexems.LETTER, 'z'), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_example_fn_definition_inc(self): tokens = self.lexer.parse("fn inc x=>x+1") result = [ Token(lexems.FN_KEYWORD, lexems.FN_KEYWORD), Token(lexems.LETTER, "inc"), Token(lexems.LETTER, 'x'), Token(lexems.FN_OPERATOR, lexems.FN_OPERATOR), Token(lexems.LETTER, 'x'), Token(lexems.PLUS, lexems.PLUS), Token(lexems.DIGIT, 1.0), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_example_fn_definition_echo(self): tokens = self.lexer.parse("fn echo x => x") result = [ Token(lexems.FN_KEYWORD, lexems.FN_KEYWORD), Token(lexems.LETTER, "echo"), Token(lexems.LETTER, 'x'), Token(lexems.FN_OPERATOR, lexems.FN_OPERATOR), Token(lexems.LETTER, 'x'), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_bad_fn_def(self): parser = Parser() tree = parser.run("fn avg => (x + y) / 2") self.assertEqual(Token(lexems.LETTER, "avg"), tree.name)
def test_brackets(self): parser = Parser() tree = parser.run("(1 + 7) * 5") self.assertEqual(tree.op, Token(lexems.MULTIPLY, lexems.MULTIPLY)) self.assertEqual(tree.right.val, Token(lexems.DIGIT, 5.0))
def test_example_fn_definition(self): tokens = self.lexer.parse("fn avg => (x + y) / 2") result = [ Token(lexems.FN_KEYWORD, lexems.FN_KEYWORD), Token(lexems.LETTER, "avg"), Token(lexems.FN_OPERATOR, lexems.FN_OPERATOR), Token(lexems.OPEN_BRACKET, lexems.OPEN_BRACKET), Token(lexems.LETTER, 'x'), Token(lexems.PLUS, lexems.PLUS), Token(lexems.LETTER, 'y'), Token(lexems.CLOSE_BRACKET, lexems.CLOSE_BRACKET), Token(lexems.DIVIDE, lexems.DIVIDE), Token(lexems.DIGIT, 2.0), Token(lexems.EOF, "") ] self.assertEqual(tokens, result)
def test_fn_def_echo(self): parser = Parser() tree = parser.run("fn echo x => x") self.assertEqual(Token(lexems.LETTER, "echo"), tree.name) self.assertEqual([Token(lexems.LETTER, "x")], tree.arguments) self.assertEqual(Token(lexems.LETTER, "x"), tree.definition.val)
def test_adding(self): parser = Parser() tree = parser.run("1 + 7") self.assertEqual(tree.op, Token(lexems.PLUS, lexems.PLUS)) self.assertEqual(tree.left.val, Token(lexems.DIGIT, 1)) self.assertEqual(tree.right.val, Token(lexems.DIGIT, 7))
def test_fn_def_echo_call_no_args(self): parser = Parser() tree = parser.run("fn echo => 2") self.assertEqual(Token(lexems.LETTER, "echo"), tree.name)
def get_token(self, position): if position >= len(self.tokens): return Token(EOF, "") return self.tokens[position]
def test_multiply(self): parser = Parser() tree = parser.run("1 + 7 * 5") self.assertEqual(tree.op, Token(lexems.PLUS, lexems.PLUS)) self.assertEqual(tree.left.val, Token(lexems.DIGIT, 1.0))