9c6b516453
- Reorganize repository to Rust-style layout: compiler/bootstrap/ compiler/selfhost/ compiler/tests/ library/std/ library/runtime/ tests/ tools/ - Add buxs/ Windows-compatible project root - Add borrow checker tests and implement: - Alias analysis (double mutable borrow detection) - Use-after-move detection for own T - Expand standard library: - Std::Os: Args, Env, Cwd, Chdir - Std::Time: NowMs, NowUs, SleepMs - Std::Process: Run, Output - Std::Io: PrintInt64 (fixes 32-bit truncation bug) - Add examples: os_time.bux, process.bux - Fix PrintInt to use int64_t in C runtime
45 lines
1019 B
Nim
45 lines
1019 B
Nim
import std/tables
|
|
import types, ast, source_location
|
|
|
|
type
|
|
SymbolKind* = enum
|
|
skVar
|
|
skFunc
|
|
skType
|
|
skConst
|
|
skModule
|
|
|
|
Symbol* = ref object
|
|
kind*: SymbolKind
|
|
name*: string
|
|
typ*: Type
|
|
decl*: Decl ## optional back-reference to AST decl
|
|
isMutable*: bool
|
|
isPublic*: bool
|
|
|
|
Scope* = ref object
|
|
parent*: Scope
|
|
table*: Table[string, Symbol] ## O(1) lookup via hash table
|
|
|
|
proc newScope*(parent: Scope = nil): Scope =
|
|
result = Scope(parent: parent)
|
|
|
|
proc define*(scope: Scope, sym: Symbol): bool =
|
|
## Returns false if name already exists in this scope
|
|
if scope.table.hasKey(sym.name):
|
|
return false
|
|
scope.table[sym.name] = sym
|
|
return true
|
|
|
|
proc lookup*(scope: Scope, name: string): Symbol =
|
|
var cur = scope
|
|
while cur != nil:
|
|
if cur.table.hasKey(name):
|
|
return cur.table[name]
|
|
cur = cur.parent
|
|
return nil
|
|
|
|
proc lookupLocal*(scope: Scope, name: string): Symbol =
|
|
if scope.table.hasKey(name):
|
|
return scope.table[name]
|
|
return nil |