Semantic Analysis v4
Semantic Analysis v4
Validate v4 programs with loops, break/continue, and short-circuit logic.
Function signature
func Analyze(prog *Program) []string
Return a slice of error strings (in the order discovered). If there are no errors, return nil or an empty slice.
Rules
Names and scopes
- Using an undefined name is an error:
undefined name: <name>. - Redeclaring a name in the same scope is an error:
redeclare in same scope: <name>. - Scopes nest for blocks and functions.
Const rules
constdeclarations must have an initializer:const requires initializer: <name>.- Assigning to a const (or function) binding is an error:
cannot assign to const: <name>.
Returns
returnis only valid inside a function:return outside function.
Loops
breakoutside a loop is an error:break outside loop.continueoutside a loop is an error:continue outside loop.forintroduces a new scope for its init.
Calls (dynamic)
- Always analyze the callee expression and all argument expressions.
- If the callee is an identifier bound to a function declaration, check arity:
arity mismatch: <name> expects <n>.
Notes
- Functions are statements and can appear inside blocks.
- Treat function declarations as const bindings.
- Keep the analyzer readable and straightforward.
Function and loop boundaries
A loop body is a block nested inside the for initializer scope. Body locals may shadow initializer bindings and are not visible in the post clause. Entering a nested function resets loop context for that function; restore the outer context afterward. Break/continue cannot target a loop in a different function, even when the function declaration appears inside that loop.
Before implementation
Use the module’s worked reconstruction and independent debugging lab to predict an intermediate artifact first. Preserve one failing counterexample while implementing the stated API. The lesson’s transfer task uses a fresh case after the guided trace.