Skip to content

Commit

Permalink
ast: merge Scope.add_symbol && Scope.add_symbol_with_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 30, 2024
1 parent e39b59d commit 9d0c69c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
25 changes: 10 additions & 15 deletions compiler/ast/Scope.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,18 @@ pub fn (sc &Scope) derive() &Scope {
}
}

pub fn (mut sc Scope) add_symbol_with_lookup(sym Symbol) ! {
if other := sc.lookup(sym.name) {
m := if other is Variable && other.is_arg {
'${sym.type_of()} `${sym.name}` has the same name as an argument'
} else if other.type_of() == sym.type_of() {
'duplicate ${sym.type_of()} `${sym.name}`'
} else {
'another symbol exists with the same name as `${sym.name}`'
}
return error(m)
}
sc.syms << sym
@[params]
pub struct AddSymbolParams {
pub:
lookup bool
}

pub fn (mut sc Scope) add_symbol(sym Symbol) ! {
if other := sc.find(sym.name) {
m := if other.type_of() == sym.type_of() {
pub fn (mut sc Scope) add_symbol(sym Symbol, params AddSymbolParams) ! {
func := if params.lookup { sc.lookup } else { sc.find }
if other := func(sym.name) {
m := if (other is Variable && other.is_arg) && (sym is Variable && !sym.is_arg) {
'${sym.type_of()} `${sym.name}` has the same name as an argument'
} else if other.type_of() == sym.type_of() {
'duplicate ${sym.type_of()} `${sym.name}`'
} else {
'another symbol exists with the same name as `${sym.name}`'
Expand Down
12 changes: 2 additions & 10 deletions compiler/sema/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,8 @@ fn (mut sema Sema) while_stmt(mut stmt ast.WhileStmt) {
fn (mut sema Sema) let_stmt(mut stmt ast.LetStmt) {
if sema.first_pass {
for var in stmt.lefts {
if var.is_local {
// local variables
sema.scope.add_symbol_with_lookup(var) or {
context.error(err.msg(), var.pos, context.note('inside ${sema.sym.type_of()} `${sema.sym.name}`'))
}
} else {
// variables declared at module scope
sema.scope.add_symbol(var) or {
context.error(err.msg(), var.pos, context.note('inside ${sema.sym.type_of()} `${sema.sym.name}`'))
}
sema.scope.add_symbol(var, lookup: var.is_local) or {
context.error(err.msg(), var.pos, context.note('inside ${sema.sym.type_of()} `${sema.sym.name}`'))
}
}
}
Expand Down

0 comments on commit 9d0c69c

Please sign in to comment.