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 lists #11
Merged
Merged
Implement lists #11
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…nstance to `LoxList`.
quephird
force-pushed
the
implement_lists
branch
from
March 15, 2024 01:10
2ede86b
to
b436274
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The following functionality is introduced in this PR:
[1, 2, 3]
foo[2] = 42
count
propertyfoo.append(42)
andfoo.deleteAt(1)
In order to achieve this, the following significant changes were made:
LoxList
that subclasses fromLoxInstance
. Unlike otherLoxInstance
s, it disallows setting of any properties, and exposes only a few builtin properties, such ascount
List
class for the time being, that is processed upon instantiatingInterpreter
. The two functions in the builtinList
class,append()
anddeleteAt()
, call two new functions defined in theNativeFunction
enum where the work is actually doneInterpreter.prepareCode()
, which is called in the initializer, rather than outside of it.Lox
class now callsInterpreter.interpret()
orInterpreter.interpretRepl()
, passing in the raw source code, rather than instantiating the rest of the pipeline before instantiatingInterpreter
.InterpreterTests
have their inputs expressed as raw Lox source code strings instead ofResolvedStatement
arrays, making them far easier to read.My brilliant girlfriend, with some help from myself, came up with the strategy above for appending to and deleting from lists because we needed a way of somehow passing a reference to the
LoxList
instance from its property handler (namely theget()
method) to somewhere in the interpreter so that that could mutate its elements. If we tried doing so inLoxList.get()
without any other machinery, we would not have any access to the environment, and thus be able to get a handle tothis
and then mutate its elements. Likewise, if we tried havingInterpreter.handleGetExpression()
construct and return aUserDefinedFunction
instance, we would not be able to express, in Lox statements, the ability to append to or delete from a list, which is what we're trying to implement in the first place!