31d Defining behaviour: Container types and assignment#104
31d Defining behaviour: Container types and assignment#104ngjunsiang wants to merge 6 commits intomainfrom
Conversation
|
Along the way we fix a little bug in error reporting: In the scanning phase, |
Designing container typesHow would this data go through our execution pipeline?
This means that:
We need clearer separation of responsibility here. |
Design draftIntention: The resolver should only be responsible for checking the logic of operations. It should not be using up memory bringing Containers into actual existence; at most its work would be done in the typesystem to register custom types. The interpreter is responsible for actually instantiating containers. For the resolver to still do its job without bringing Arrays and Objects into existence, it must have access to array metadata, without relying on the Declare Expr sticking around after that. The logical place to stash this metadata is in the TypedValue. That means, in addition to the |
The new TypedValueSo ... what is a TypedValue exactly?
In our new design, the TypedValue must still be able to serve these purposes without actually holding an Array or Object (remember that those will only be instantiated in the interpreter). That means the new TypedValue's
|
|
A quick prototype of the *Metadata classes and the TypedValue's metadata attribute: |
|
Once I comment the line, the code fails; there is already code that relies on |
|
On second thought, I'd rather lose |
Hear me out: what if containers had types?
Suppse every container had a
typeattribute. A Student object would haveStudent.type == "Student". An Array might have typeARRAY, orARRAY[1:3] OF INTEGER.I imagine the Container protocol would look something like this:
https://github.com/nyjc-computing/pseudo-9608/blob/3bdf489b985911caa08996a570ed59d90a1d6190/pseudocode/lang/object.py#L146-L159
Why do we want this, or even need this? I'm looking at the part of the 9608 pseudocode reference that requires support for statements like these:
tl;dr Pseudo needs to support Array and Object assignments. Currently, this works:
We lean heavily on Python's object model to make this work; internally, within the global frame, the
Pupil1andPupil2names point to the same Object, while theNoughtsAndCrossesandSavedGamenames point to the same Array.But when we change the code a little:
We get this:
That's not quite how things are supposed to work in Pseudo. DECLAREing a name means that memory is set aside for its data, so Array and Object assignment should result in copying, not referencing of data. After the assignment, mutations to
Pupil1andNoughtsAndCrossesshould not be reflected in the copies.Not to mention that array assignment for arrays with different sizes is not supported in Pseudo.
We'll fix all this in this chapter.