def test_call(self): """Ensures function calls are parsed properly.""" code = "print('hello')" tokens = self.lexer.parse(code) parsed = self.parser.parse_literal(tokens)[0] test = CallExpression(name=IdentLiteral(name='print'), args=[StringLiteral('hello')]) assert pickle(parsed) == pickle( test), "Issue in function call parsing."
def test_math(self): """Ensures arithmetic is parsed properly.""" code = "5 + 5 / 2" tokens = self.lexer.parse(code) parsed = self.parser.parse_literal(tokens)[0] test = BinaryOperator(operator='+', left=IntegerLiteral(5), right=BinaryOperator(operator='/', left=IntegerLiteral(5), right=IntegerLiteral(2))) assert pickle(parsed) == pickle(test), "Issue in arithmetic parsing."
def test_conditional_1(self): """Ensures conditionals are parsed properly.""" code = "if (1 == 1) return 1" tokens = self.lexer.parse(code) parsed = self.parser.parse_literal(tokens)[0] test = Conditional('if', expression=BinaryOperator(operator='==', left=IntegerLiteral(1), right=IntegerLiteral(1)), body=[Return(IntegerLiteral(1))], else_body=[]) assert pickle(parsed) == pickle( test), "Issue in inline conditional parsing."
def test_assignment_1(self): """Ensures variable declaration and assignment are parsed properly.""" code = "let test = 5 * 5 / 5" tokens = self.lexer.parse(code) parsed = self.parser.parse_literal(tokens)[0] test = Assignment(_type='let', ident=IdentLiteral(name='test'), value=BinaryOperator(operator='/', left=BinaryOperator( operator='*', left=IntegerLiteral(5), right=IntegerLiteral(5)), right=IntegerLiteral(5))) assert pickle(parsed) == pickle(test), "Issue in assignment parsing."
def parse(self, tokens, verbose=False): self.tokens = FutureIter(tokens) ast = [] while self.tokens.current is not None: if self.tokens.current.type != TokenType.NEWLINE: ast.append(self.parse_top()) else: self.tokens._next() if verbose: print("AST:", PRNTLINE, json.dumps(pickle(ast), indent=2), PRNTLINE) return ast