Skip to content

Commit

Permalink
small optimization?
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Nov 24, 2023
1 parent 9949e8c commit 304f87e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/aro/SymbolStack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn popScope(s: *SymbolStack) void {
}

pub fn findTypedef(s: *SymbolStack, p: *Parser, name: StringId, name_tok: TokenIndex, no_type_yet: bool) !?Symbol {
const prev = s.lookup(name, .vars) orelse s.lookup(name, .tags) orelse return null;
const prev = s.lookup(name, .both) orelse return null;
switch (prev.kind) {
.typedef => return prev,
.@"struct" => {
Expand Down Expand Up @@ -130,25 +130,32 @@ const ScopeKind = enum {
tags,
/// everything else
vars,
/// both tags and vars
both,
};

/// Return the Symbol for `name` (or null if not found) in the innermost scope
pub fn get(s: *SymbolStack, name: StringId, kind: ScopeKind) ?Symbol {
pub fn get(s: *SymbolStack, name: StringId, comptime kind: ScopeKind) ?Symbol {
return switch (kind) {
.vars => s.scopes.items[s.active_len - 1].vars.get(name),
.tags => s.scopes.items[s.active_len - 1].tags.get(name),
.both => @compileError("don't get both"),
};
}

/// Return the Symbol for `name` (or null if not found) in the nearest active scope,
/// starting at the innermost.
fn lookup(s: *SymbolStack, name: StringId, kind: ScopeKind) ?Symbol {
fn lookup(s: *SymbolStack, name: StringId, comptime kind: ScopeKind) ?Symbol {
var i = s.active_len;
while (i > 0) {
i -= 1;
switch (kind) {
.vars => if (s.scopes.items[i].vars.get(name)) |sym| return sym,
.tags => if (s.scopes.items[i].tags.get(name)) |sym| return sym,
.both => {
if (s.scopes.items[i].vars.get(name)) |sym| return sym;
if (s.scopes.items[i].tags.get(name)) |sym| return sym;
},
}
}
return null;
Expand Down Expand Up @@ -246,10 +253,11 @@ pub fn defineSymbol(

/// Get a pointer to the named symbol in the innermost scope.
/// Asserts that a symbol with the name exists.
pub fn getPtr(s: *SymbolStack, name: StringId, kind: ScopeKind) *Symbol {
pub fn getPtr(s: *SymbolStack, name: StringId, comptime kind: ScopeKind) *Symbol {
return switch (kind) {
.tags => s.scopes.items[s.active_len - 1].tags.getPtr(name).?,
.vars => s.scopes.items[s.active_len - 1].vars.getPtr(name).?,
.both => @compileError("don't get both"),
};
}

Expand Down

0 comments on commit 304f87e

Please sign in to comment.