while loops
This commit is contained in:
28
parser.v
28
parser.v
@@ -122,7 +122,9 @@ fn (mut s SymbolTable) is_in_global_scope() bool {
|
||||
|
||||
// ------------------------------------------- Expressions
|
||||
|
||||
type Expr = VoidExpr | UnaryExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | StringLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr | PrintExpr | FnCall | ClassMember | ClassInstantiation | MemberAccess
|
||||
type Expr = VoidExpr | UnaryExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | StringLiteral |
|
||||
Variable | TypeExpr | Function | TypeCast | ParenExpr | PrintExpr | FnCall | ClassMember |
|
||||
ClassInstantiation | MemberAccess
|
||||
|
||||
struct VoidExpr {}
|
||||
|
||||
@@ -204,7 +206,8 @@ struct FnCall {
|
||||
|
||||
// ------------------------------------------- Statements
|
||||
|
||||
type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | IfStmt | ElseStmt | ElifStmt | FuncDecl | Param | ClassDecl
|
||||
type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | IfStmt | ElseStmt | ElifStmt | FuncDecl | Param |
|
||||
ClassDecl | WhileLoop
|
||||
|
||||
struct VarDecl {
|
||||
name string
|
||||
@@ -251,6 +254,11 @@ struct ElifStmt {
|
||||
block Block
|
||||
}
|
||||
|
||||
struct WhileLoop {
|
||||
guard Expr
|
||||
block Block
|
||||
}
|
||||
|
||||
struct Param {
|
||||
name string
|
||||
type string
|
||||
@@ -529,6 +537,10 @@ fn (mut p Parser) get_expr_is_immutable(expr Expr) bool {
|
||||
memberinfo := classinfo.members_info[expr.member] or {parse_error("Undefined member ${expr.member}")}
|
||||
memberinfo.is_immutable
|
||||
}
|
||||
Variable {
|
||||
varinfo := p.symbols.lookup_var(expr.name) or {parse_error("Undefined variable ${expr.name}")}
|
||||
varinfo.is_immutable
|
||||
}
|
||||
else {true}
|
||||
}
|
||||
}
|
||||
@@ -616,6 +628,7 @@ fn (mut p Parser) parse_statement() Stmt {
|
||||
.kw_if {return p.parse_if()}
|
||||
.kw_else {return p.parse_else()}
|
||||
.kw_elif {return p.parse_elif()}
|
||||
.kw_while {return p.parse_while()}
|
||||
else {return p.parse_expr_stmt()}
|
||||
}
|
||||
}
|
||||
@@ -768,6 +781,17 @@ fn (mut p Parser) parse_return_stmt() ReturnStmt {
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut p Parser) parse_while() WhileLoop {
|
||||
p.expect(.kw_while)
|
||||
cond := p.parse_expr(.base)
|
||||
if p.get_expr_type(cond) != 'bool' {
|
||||
parse_error('While loop guard must be of type bool')
|
||||
}
|
||||
block := p.parse_block(false)
|
||||
|
||||
return WhileLoop {cond, block}
|
||||
}
|
||||
|
||||
fn (mut p Parser) parse_if() IfStmt {
|
||||
p.expect(.kw_if)
|
||||
cond := p.parse_expr(.base)
|
||||
|
||||
Reference in New Issue
Block a user