diff --git a/main.v b/main.v index 9c413dd..8516d44 100644 --- a/main.v +++ b/main.v @@ -11,5 +11,8 @@ fn main() { pos: 0 } statements := parser.parse_program() + println("-- TOK --") + print_toks(tokens) + println("-- AST --") println(statements) } diff --git a/parser.v b/parser.v index db1fe2f..33367ca 100644 --- a/parser.v +++ b/parser.v @@ -70,8 +70,7 @@ fn (mut s SymbolTable) is_in_global_scope() bool { // ------------------------------------------- Expressions -type Expr = VoidExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast -type LiteralExpr = IntegerLiteral | RealLiteral | BoolLiteral +type Expr = VoidExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr struct VoidExpr {} @@ -106,8 +105,12 @@ struct Function { } struct TypeCast { - expr Expr type string + expr Expr +} + +struct ParenExpr { + expr Expr } // ------------------------------------------- Statements @@ -116,8 +119,8 @@ type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | FuncDecl struct VarDecl { name string - value Expr type string + value Expr } struct FuncDecl { @@ -201,6 +204,7 @@ fn (mut p Parser) parse_primary() Expr { .identifier {Variable{token.text}} .kw_fn {Function{token.text}} .type {p.parse_type(token.text)} + .lparen {p.parse_paren()} else {parse_error("Unexpected Token")} } } @@ -244,8 +248,15 @@ fn (mut p Parser) parse_type(type string) Expr { return TypeExpr {name: type} } +fn (mut p Parser) parse_paren() ParenExpr { + expr := p.parse_expr() + p.expect(.rparen) + return ParenExpr{expr: expr} +} + fn (mut p Parser) get_expr_type(expr Expr) string { return match expr { + ParenExpr {p.get_expr_type(expr.expr)} IntegerLiteral {'int'} RealLiteral {'real'} BoolLiteral {'bool'} diff --git a/test.one b/test.one index 3a9582b..dfc3894 100644 --- a/test.one +++ b/test.one @@ -1,2 +1 @@ -let a real = 5.1; -let x real = 7.0 + real(int(a) + int(7.0)); +let a int = int((5.1 + 2.2) * 5.1);