Example #1
0
 def test_constructor(self):
     buf = """
     fn constructorTest() -> int {
       let foo = new Foo(name, age);
       return 0;
     }
     """
     exprs = [
         ast.Function(
             "constructorTest",
             [],
             [
                 ast.LetStatement(
                     "foo",
                     ast.Constructor(
                         "Foo",
                         [ast.VariableRef("name"),
                          ast.VariableRef("age")]),
                 ),
                 ast.ReturnStatement(ast.Number(0)),
             ],
             ast.Type(ast.TypeKind.INT),
         )
     ]
     self._test_parse_impl(buf, exprs)
Example #2
0
 def test_struct_with_member_functions(self):
     buf = """
     struct Foo {
       string name;
       int age;
       fn memberFunc() -> int {
         return age;
       }
     };
     """
     exprs = [
         ast.Structure(
             "Foo",
             [
                 ast.Member("name", ast.Type(ast.TypeKind.STRING)),
                 ast.Member("age", ast.Type(ast.TypeKind.INT)),
             ],
             [
                 ast.Function(
                     "memberFunc",
                     [("this", ast.Type(ast.TypeKind.USER, "Foo"))],
                     [ast.ReturnStatement(ast.VariableRef("age"))],
                     ast.Type(ast.TypeKind.INT),
                 )
             ],
         )
     ]
     self._test_parse_impl(buf, exprs)
Example #3
0
 def test_while_loop(self):
     buf = """
     fn loopTest(int x) -> int {
       while (x < 5) {
         print("blah");
       }
       return 0;
     }
     """
     exprs = [
         ast.Function(
             "loopTest",
             [("x", ast.Type(ast.TypeKind.INT))],
             [
                 ast.WhileLoop(
                     ast.BinaryOp(ast.VariableRef("x"), ast.Number(5),
                                  Token(TokenType.LESS_THAN, "<")),
                     [
                         ast.ExprStatement(
                             ast.FunctionCall("print",
                                              [ast.String("blah")]))
                     ],
                 ),
                 ast.ReturnStatement(ast.Number(0)),
             ],
             ast.Type(ast.TypeKind.INT),
         )
     ]
     self._test_parse_impl(buf, exprs)
Example #4
0
 def test_member_access(self):
     buf = """
     fn foo() -> int {
       return bar.age;
     }
     """
     exprs = [
         ast.Function(
             "foo",
             [],
             [
                 ast.ReturnStatement(
                     ast.MemberAccess(ast.VariableRef("bar"), "age"))
             ],
             ast.Type(ast.TypeKind.INT),
         )
     ]
     self._test_parse_impl(buf, exprs)
Example #5
0
 def test_let_statement(self):
     buf = """
     fn letFunc() -> int {
       let x = 1;
       return x;
     }
     """
     exprs = [
         ast.Function(
             "letFunc",
             [],
             [
                 ast.LetStatement("x", ast.Number(1)),
                 ast.ReturnStatement(ast.VariableRef("x"))
             ],
             ast.Type(ast.TypeKind.INT),
         )
     ]
     self._test_parse_impl(buf, exprs)
Example #6
0
 def _parse_primary_expr(self):
     value = self.cur_tok.value
     expr = None
     if self._consume_token(TokenType.NUMBER_LITERAL):
         expr = ast.Number(int(value))
     elif self._consume_token(TokenType.STRING_LITERAL):
         expr = ast.String(value)
     elif self._consume_token(TokenType.L_PAREN):
         expr = self._parse_vector()
     elif self._consume_token(TokenType.L_BRACE):
         expr = self._parse_map()
     elif self._consume_token(TokenType.NEW):
         expr = self._parse_constructor()
     elif self._consume_token(TokenType.IDENTIFIER):
         if self._consume_token(TokenType.L_BRACKET):
             return self._parse_function_call(value)
         return ast.VariableRef(value)
     else:
         raise RuntimeError("unrecognised primary expression: Token=({0})".format(self.cur_tok))
     return expr