ParenExpr, better print formatting

This commit is contained in:
uan
2026-02-04 17:48:16 +01:00
parent 56d873d9d3
commit 6525fa711d
3 changed files with 19 additions and 6 deletions

3
main.v
View File

@@ -11,5 +11,8 @@ fn main() {
pos: 0 pos: 0
} }
statements := parser.parse_program() statements := parser.parse_program()
println("-- TOK --")
print_toks(tokens)
println("-- AST --")
println(statements) println(statements)
} }

View File

@@ -70,8 +70,7 @@ fn (mut s SymbolTable) is_in_global_scope() bool {
// ------------------------------------------- Expressions // ------------------------------------------- Expressions
type Expr = VoidExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast type Expr = VoidExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr
type LiteralExpr = IntegerLiteral | RealLiteral | BoolLiteral
struct VoidExpr {} struct VoidExpr {}
@@ -106,8 +105,12 @@ struct Function {
} }
struct TypeCast { struct TypeCast {
expr Expr
type string type string
expr Expr
}
struct ParenExpr {
expr Expr
} }
// ------------------------------------------- Statements // ------------------------------------------- Statements
@@ -116,8 +119,8 @@ type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | FuncDecl
struct VarDecl { struct VarDecl {
name string name string
value Expr
type string type string
value Expr
} }
struct FuncDecl { struct FuncDecl {
@@ -201,6 +204,7 @@ fn (mut p Parser) parse_primary() Expr {
.identifier {Variable{token.text}} .identifier {Variable{token.text}}
.kw_fn {Function{token.text}} .kw_fn {Function{token.text}}
.type {p.parse_type(token.text)} .type {p.parse_type(token.text)}
.lparen {p.parse_paren()}
else {parse_error("Unexpected Token")} else {parse_error("Unexpected Token")}
} }
} }
@@ -244,8 +248,15 @@ fn (mut p Parser) parse_type(type string) Expr {
return TypeExpr {name: type} 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 { fn (mut p Parser) get_expr_type(expr Expr) string {
return match expr { return match expr {
ParenExpr {p.get_expr_type(expr.expr)}
IntegerLiteral {'int'} IntegerLiteral {'int'}
RealLiteral {'real'} RealLiteral {'real'}
BoolLiteral {'bool'} BoolLiteral {'bool'}

View File

@@ -1,2 +1 @@
let a real = 5.1; let a int = int((5.1 + 2.2) * 5.1);
let x real = 7.0 + real(int(a) + int(7.0));