Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dictionary #36

Merged
merged 14 commits into from Apr 1, 2024
Merged

Implement dictionary #36

merged 14 commits into from Apr 1, 2024

Conversation

quephird
Copy link
Owner

@quephird quephird commented Mar 26, 2024

The following changes were made in this PR for initial support of dictionaries in Lox:

  • Dictionary literals in Lox are like the ones in Swift, where commas delimit pairs and the colon delimits each key and value, e.g. ["a": 1, "b": 2]
  • Because any LoxValue can be a dictionary key, it needed to be Hashable since LoxDictionary uses Swift dictionary as a backend for managing key/value pairs
  • That meant that I needed a sensible strategy for uniquely identifying all cases of LoxValue including those associated with UserDefinedFunctions. And so I introduced an objectId property with a UUID type that accomplishes the task
  • I needed to add explicit implementation for == for Expression and ResolvedExpression because lists of tuples are not automatically Equatable. Equatable conformance for expressions and statements, and their resolved counterparts, is necessary only for unit tests
  • Moved standard library into its own file to reduce the amount of noise in Interpreter
  • Lox dictionaries have a native properties, count, keys, and values, as well as native methods removeValue() and merge()
  • Like lists, dictionaries also support a subscript syntax for retrieving and values associated with keys
  • There is a utility method, Interpreter.makeDictionary() that is used both in the context of the interpreter as well as for unit tests

@quephird quephird merged commit 2aa42b9 into main Apr 1, 2024
@quephird quephird deleted the implement_dictionary branch April 1, 2024 00:09
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.

None yet

1 participant