Semantic Analysis v5
Semantic Analysis v5
Validate v5 programs with modules, imports/exports, and member access.
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
Modules
- Module names must be unique:
duplicate module: <name>. - A module can only import modules defined earlier in the program.
- Unknown module:
unknown module: <name> - Importing a later module:
import of future module: <name>
- Unknown module:
- The
mainmodule must exist and must be last:main module must be last.
Imports and exports
- Import aliases live in the module scope and must be unique.
exportonly applies tofnorlet/var/constdeclarations.- Exported names must exist and be unique per module.
Member access
m.namerequiresmto be an imported module alias.- Unknown module alias:
unknown module: <name>
- Unknown module alias:
- The member must be exported by that module:
unknown export: <module>.<name>
Existing v4 rules still apply
- Undefined name:
undefined name: <name>. - Redeclare in same scope:
redeclare in same scope: <name>. - Const requires initializer:
const requires initializer: <name>. - Cannot assign to const:
cannot assign to const: <name>. returnoutside function:return outside function.break/continueoutside loop.- Function call arity checks for identifier callees.
Notes
- 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.