Semantic Analysis
- parsing gives shape; analysis validates meaning
- names, binding kinds, scope, and call legality before runtime
Scope policy
- explicit scope stack: current → parent → root
- duplicate declarations forbidden in same scope
- inner declaration shadows outer
Function strategy
- two-phase often needed: 1) register function declarations 2) validate function bodies
- enables recursion and arity checks at call sites
Assignment rules
constbinding may not be reassignedvarmay be updated in nearest enclosing scope- unresolved targets are
undefined nameerrors
Control-flow validation
- track
inFunctionDepth returnvalid only when inside function
Diagnostic quality
- deterministic issue order
- messages include location + concrete symbol/call count
- examples:
undefined name: xredeclare in same scope: xcannot assign to const: pireturn outside functionarity mismatch: add expects 2, got 1
Why good analysis
- catches failures early
- aligns compiler and VM error model
- keeps runtime traces meaningful
Quick checks
- same-name in one scope -> declare error
- same-name in child scope -> shadow allowed
- recursion accepted after declaration pass
- call arity mismatch flagged before compile emit
- valid call path may still error at runtime if types mismatch
1 / 1