Semantic Analysis v6

hard · compilers, semantics, types

Semantic Analysis v6

Validate v6 programs with modules and static types.

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.

New type rules

Variable declarations

  • If a type annotation is present, the initializer (if any) must be assignable.
  • If no annotation is present, the initializer must exist and be inferable.
  • Empty arrays/maps require an annotation.

Function declarations

  • Parameter types are required.
  • Return type is required.
  • return expressions must be assignable to the declared return type.

Expressions

  • + works on numbers or strings only.
  • - * / work on numbers only.
  • && || ! work on bools only.
  • < <= > >= work on numbers only.
  • == != require compatible types.
  • Arrays and maps must be homogeneous.
  • Map keys must be number/bool/string.

Assignability

  • Types must match structurally.
  • nil is assignable to any type.

Module rules (from v5)

  • Unique module names.
  • main exists and is last.
  • Imports refer to earlier modules.
  • Member access requires exported names.

Notes

  • Keep error messages simple and consistent.
Run tests to see results
No issues detected