ParenExpr, better print formatting
This commit is contained in:
3
main.v
3
main.v
@@ -11,5 +11,8 @@ fn main() {
|
||||
pos: 0
|
||||
}
|
||||
statements := parser.parse_program()
|
||||
println("-- TOK --")
|
||||
print_toks(tokens)
|
||||
println("-- AST --")
|
||||
println(statements)
|
||||
}
|
||||
|
||||
19
parser.v
19
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'}
|
||||
|
||||
Reference in New Issue
Block a user