0dade151d2
Bootstrap compiler improvements: - Extract findStdlibDir() to remove hardcoded path and deduplicate - Extract prepareProject()/mergeProject() to share code between cmdCheck/cmdBuild - Fix C backend to emit warning on unknown types instead of silent 'int' - Clean up all unused imports across 7 modules - Makefile: add strip, debug target, clean-all, selfhost strip QBE removal: - Remove vendor/qbe/ (74K lines) - Remove compiler/selfhost/qbe_backend.bux, nim_backend.bux Stdlib improvements: - Add Result.bux + Option.bux modules - Fix Json.bux memory leak (removed double String_Copy) - Add missing imports in Crypto.bux (Alloc/Free) and Test.bux (PrintLine/PrintInt) - Clean stale buxc_debug binary
46 lines
1.0 KiB
Nim
46 lines
1.0 KiB
Nim
import std/tables
|
|
import types, ast
|
|
|
|
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
|
|
isOwn*: bool ## true if declared as own T
|
|
|
|
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 |