Skip to content

Commit

Permalink
sema: early return inside file stmts with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 27, 2024
1 parent 3da65e7 commit 3160aa6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion compiler/ast/Stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub:
name_pos FilePos
args []FnArg
return_type Type
stmts []Stmt
pub mut:
stmts []Stmt
}

pub struct FnArg {
Expand Down
7 changes: 6 additions & 1 deletion compiler/context/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@ pub fn (mut ctx CContext) load_primitive_types() {
}
}

@[inline]
pub fn (ctx &CContext) code_has_errors() bool {
return ctx.report.errors > 0
}

pub fn (ctx &CContext) abort_if_errors() {
if ctx.report.errors > 0 {
if ctx.code_has_errors() {
reason := if ctx.report.errors == 1 {
'aborting due to previous error'
} else {
Expand Down
19 changes: 15 additions & 4 deletions compiler/sema/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,24 @@ fn (mut sema Sema) check_file(mut file ast.File) {
sema.file.scope = ast.Scope.new(sema.ctx.universe, sema.sym)
sema.scope = sema.file.scope

sema.first_pass = true
sema.stmts(mut sema.file.stmts)
sema.file_stmts(true)
if sema.ctx.code_has_errors() {
return
}

sema.first_pass = false
sema.stmts(mut sema.file.stmts)
sema.file_stmts(false)
if sema.ctx.code_has_errors() {
return
}

sema.ctx.files << sema.file
}

fn (mut sema Sema) file_stmts(first_pass bool) {
sema.first_pass = first_pass
sema.stmts(mut sema.file.stmts)
}

fn (mut sema Sema) stmts(mut stmts []ast.Stmt) {
for mut stmt in stmts {
sema.stmt(mut stmt)
Expand All @@ -73,5 +82,7 @@ fn (mut sema Sema) fn_stmt(mut stmt ast.FnStmt) {
args: stmt.args
node: unsafe { stmt }
}) or { context.error(err.msg(), stmt.name_pos) }
sema.stmts(mut stmt.stmts)
return
}
}

0 comments on commit 3160aa6

Please sign in to comment.