Module: compiler.symbol_table

Source: ./compiler/symbol_table.reef


Overview

compiler/symbol_table - Scoped Symbol Table for Compilers

Provides scope-aware symbol management with O(1) lookups using hash tables. Supports nested scopes with proper shadowing semantics (inner scope shadows outer).

Usage: import compiler.symbol_table

let st = symbol_table.create_int_table() symbol_table.define_int(st, "x", 42) symbol_table.scope_push(st) symbol_table.define_int(st, "x", 100) // shadows outer x let val = symbol_table.lookup_int(st, "x") // returns 100 symbol_table.scope_pop(st) let val2 = symbol_table.lookup_int(st, "x") // returns 42

Design:

  • Each scope level has its own hash map for O(1) lookup within scope
  • Lookup searches from current scope upward (shadowing semantics)
  • scope_pop clears symbols in current scope
  • Maximum 32 scope levels (sufficient for typical nesting)

Factory functions (type-specific due to Reef's generic limitations): create_int_table() -> IntSymbolTable create_string_table() -> StringSymbolTable create_ptr_table() -> PtrSymbolTable


Types

IntScope

Each scope stores symbols in a hash map (arrays for keys/values/states)

Fields:

Name Type
keys [string]
values [int]
states [int]
size int

IntSymbolTable

Fields:

Name Type
scopes [IntScope]
depth int
default_value int

StringScope

Fields:

Name Type
keys [string]
values [string]
states [int]
size int

StringSymbolTable

Fields:

Name Type
scopes [StringScope]
depth int
default_value string

PtrScope

Fields:

Name Type
keys [string]
values [pointer]
states [int]
size int

PtrSymbolTable

Fields:

Name Type
scopes [PtrScope]
depth int
default_value pointer

Functions

fn max_scope_depth(): int

Maximum scope depth (32 levels is plenty for typical code)

fn scope_capacity(): int

Capacity per scope (hash map size)

fn hash_string(s: string): int

fn create_int_table(): IntSymbolTable

Create a new IntSymbolTable

fn scope_depth_int(st: IntSymbolTable): int

Get current scope depth

fn lookup_int_in_scope(scope: IntScope, name: string): int

Lookup helper for a specific scope

fn has_int_in_scope(scope: IntScope, name: string): bool

Check if name exists in specific scope

fn lookup_int(st: IntSymbolTable, name: string): int

Lookup a symbol (searches all scopes from innermost to outermost)

fn has_int(st: IntSymbolTable, name: string): bool

Check if symbol exists in any scope

fn has_current_int(st: IntSymbolTable, name: string): bool

Check if symbol exists in current scope only

fn create_string_table(): StringSymbolTable

fn scope_depth_str(st: StringSymbolTable): int

fn has_str_in_scope(scope: StringScope, name: string): bool

fn lookup_str_in_scope(scope: StringScope, name: string): string

fn lookup_str(st: StringSymbolTable, name: string): string

fn has_str(st: StringSymbolTable, name: string): bool

fn has_current_str(st: StringSymbolTable, name: string): bool

fn create_ptr_table(): PtrSymbolTable

fn scope_depth_ptr(st: PtrSymbolTable): int

fn has_ptr_in_scope(scope: PtrScope, name: string): bool

fn lookup_ptr_in_scope(scope: PtrScope, name: string): pointer

fn lookup_ptr(st: PtrSymbolTable, name: string): pointer

fn has_ptr(st: PtrSymbolTable, name: string): bool

fn has_current_ptr(st: PtrSymbolTable, name: string): bool


Procedures

proc init_int_scope(st: IntSymbolTable, depth: int)

Initialize a scope at given depth

proc scope_push_int(st: IntSymbolTable)

Push a new scope

proc scope_pop_int(st: IntSymbolTable)

Pop current scope

proc define_int(st: IntSymbolTable, name: string, value: int)

Define a symbol in current scope

proc init_str_scope(st: StringSymbolTable, depth: int)

proc scope_push_str(st: StringSymbolTable)

proc scope_pop_str(st: StringSymbolTable)

proc define_str(st: StringSymbolTable, name: string, value: string)

proc init_ptr_scope(st: PtrSymbolTable, depth: int)

proc scope_push_ptr(st: PtrSymbolTable)

proc scope_pop_ptr(st: PtrSymbolTable)

proc define_ptr(st: PtrSymbolTable, name: string, value: pointer)


Generated by reefc doc