Semantic Analysis
Semantic Analysis
Implement semantic analysis for the expanded language (v2).
You are given a parsed Program AST. Your job is to check scoping and rule violations, returning a list of error messages.
Function signature
func Analyze(prog *Program) []string
Return a slice of error strings. If there are no errors, return an empty slice.
Rules to enforce
- No duplicate declarations in the same scope:
redeclare in same scope: <name>
- Names must be declared before use:
undefined name: <name>
- Const rules:
constmust have an initializer
const requires initializer: <name>
- assignment to
constis not allowed
cannot assign to const: <name>
- Return placement:
returnis only valid inside a function
return outside function
- Function calls:
- Calling a non-function name is an error
call of non-function: <name>
- Arity must match the declared function parameter count
arity mismatch: <name> expects <n>
Notes
- The language is dynamically typed; do not type-check expressions.
- Functions are top-level declarations, but the analyzer should still behave sensibly if they appear elsewhere.
- Errors should be reported in the order encountered during a left-to-right traversal.
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