ParenExpr, better print formatting
This commit is contained in:
3
main.v
3
main.v
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
19
parser.v
19
parser.v
@@ -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'}
|
||||||
|
|||||||
Reference in New Issue
Block a user