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
1) No duplicate declarations in the same scope:
redeclare in same scope: <name>
2) Names must be declared before use:
undefined name: <name>
3) Const rules:
constmust have an initializer
const requires initializer: <name>
- assignment to
constis not allowed
cannot assign to const: <name>
4) Return placement:
returnis only valid inside a function
return outside function
5) 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.
Run tests to see results
No issues detected