v2: fix for position and check

pull/3779/head
Alexander Medvednikov 2020-02-19 11:06:36 +01:00
parent 042bce4b6c
commit bc11419ff9
3 changed files with 15 additions and 13 deletions

View File

@ -341,6 +341,7 @@ pub:
cond Expr cond Expr
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
is_inf bool // `for {}`
} }
pub struct ForInStmt { pub struct ForInStmt {

View File

@ -273,7 +273,7 @@ fn (c mut Checker) stmt(node ast.Stmt) {
typ := c.expr(it.cond) typ := c.expr(it.cond)
// typ_sym := c.table.get_type_symbol(typ) // typ_sym := c.table.get_type_symbol(typ)
// if typ_sym.kind != .bool { // if typ_sym.kind != .bool {
if table.type_idx(typ) != table.bool_type_idx { if !it.is_inf && table.type_idx(typ) != table.bool_type_idx {
c.error('non-bool used as for condition', it.pos) c.error('non-bool used as for condition', it.pos)
} }
for stmt in it.stmts { for stmt in it.stmts {

View File

@ -91,7 +91,6 @@ pub fn parse_file(path string, table &table.Table) ast.File {
for p.tok.kind == .key_import { for p.tok.kind == .key_import {
imports << p.import_stmt() imports << p.import_stmt()
} }
// TODO: import only mode // TODO: import only mode
for { for {
// res := s.scan() // res := s.scan()
@ -893,11 +892,13 @@ fn (p mut Parser) for_statement() ast.Stmt {
// defer { p.close_scope() } // defer { p.close_scope() }
// Infinite loop // Infinite loop
if p.tok.kind == .lcbr { if p.tok.kind == .lcbr {
pos := p.tok.position()
stmts := p.parse_block() stmts := p.parse_block()
p.close_scope() p.close_scope()
return ast.ForStmt{ return ast.ForStmt{
stmts: stmts stmts: stmts
pos: p.tok.position() pos: pos
is_inf: true
} }
} }
else if p.tok.kind == .key_mut { else if p.tok.kind == .key_mut {