Skip to content

Add identifier not found error handling to evaluator - #389

Open
DeveloperC286 wants to merge 1 commit into
mainfrom
claude/undefined-identifier-errors-10v33s
Open

Add identifier not found error handling to evaluator#389
DeveloperC286 wants to merge 1 commit into
mainfrom
claude/undefined-identifier-errors-10v33s

Conversation

@DeveloperC286

Copy link
Copy Markdown
Owner

Summary

This PR implements proper error handling for undefined identifiers in the evaluator. Previously, accessing an undefined identifier would return Null, but now it correctly raises an "Identifier not found" error.

Key Changes

  • Modified Environment.get() method to return Option<Object> instead of Object, allowing distinction between a variable that exists with a Null value and a variable that doesn't exist at all
  • Added identifier expression evaluation with a new evaluate_identifier_expression() method that returns an error when an identifier is not found in the environment
  • Updated expression evaluation to use the new identifier evaluation method
  • Added comprehensive test suite with 10 test cases covering various scenarios:
    • Simple undefined identifiers
    • Undefined identifiers in expressions and function calls
    • Typos in function names
    • Undefined variables in function bodies
    • Undefined identifiers in conditionals and return statements
    • Scope-related cases (accessing function parameters outside their scope)

Implementation Details

  • The Environment.get() method now properly propagates None through the environment chain when a variable is not found, rather than defaulting to Null
  • Error messages follow the format: "Identifier not found: {identifier}."
  • All test cases include lexical analysis, syntax analysis, and evaluation error snapshots
  • One existing test case was updated to reflect the corrected behavior (identifier_expression_case_8 now correctly returns False instead of Null)

https://claude.ai/code/session_01Htq6Br9uauDWFGr9zPxTyc

Environment::get returned Object::Null when a name was not bound anywhere
in the environment chain, so a typo silently produced a null value. The
mistake then surfaced further downstream as a confusing type mismatch,
pointing at the operator rather than at the undefined name.

Environment::get now returns Option<Object>, and identifier evaluation
moves into its own module which bails with "Identifier not found: <name>."
when the lookup misses.

identifier_expression_case_8 enshrined the old behaviour, evaluating
"let is_directory = false;\nis_file" and asserting Ok(Null). It now
references the identifier it binds, and the undefined-identifier code it
used to cover moves to the new identifier_not_found evaluation error
suite alongside nine other cases.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Htq6Br9uauDWFGr9zPxTyc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants