Semantic Analysis v3

hard · compilers, semantics, scopes, closures

Semantic Analysis v3

Validate v3 programs with nested functions, arrays, maps, and indexing.

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

Check these 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

  • const declarations must have an initializer: const requires initializer: <name>.
  • Assigning to a const (or function) binding is an error: cannot assign to const: <name>.

Returns

  • return is only valid inside a function: return outside function.

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 in their scope.
  • Keep the analyzer readable and straightforward.

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.

Run tests to see results
No issues detected
    Join Discord