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

Operational Assignments #23

Closed
faultyserver opened this issue Nov 5, 2017 · 1 comment
Closed

Operational Assignments #23

faultyserver opened this issue Nov 5, 2017 · 1 comment
Labels
feature-request Any request for a new feature of the language. Includes both syntax and library features. semantics Generic label for semantics issues. All semantics issues should have this tag. syntax Any issue relating to the syntax of Myst.
Milestone

Comments

@faultyserver
Copy link
Member

Operational assignments are common shorthands for "dual expressions", where an operation is performed, and then the result is re-assigned to the same left hand value used in the operation. For example:

a += 1
# is equivalent to
a = a + 1

For non-logical operations, this is essentially a simple syntax rewrite that takes any expression in the form a {{op}}= b and rewrites it as a = a {{op}} b. This works for all of the arithmetic and comparative operators.

However, ||= and &&= are generally handled differently, something that the semantics of which are surprisingly complex. I won't bother going into those semantics here, as it could easily take up multiple articles on its own.

In any case, the parser is already capable of parsing these statements, and has the corresponding OpAssign node to represent them. However, the interpreter does not actually support visiting these nodes, and will instead raise an error when encountered.

I would recommend a two-step approach to this, unless the second step becomes more simple.

  1. Implement OpAssign for the arithmetic and comparative operators, but raise an UnsupportedError or similar when encountering ||= or &&=.

  2. Go back and flesh out ||= and &&= properly once assignment methods are implemented (rewrites of obj.method = value to be equivalent to obj.method=(value), where method= is a method name.

@faultyserver faultyserver added feature-request Any request for a new feature of the language. Includes both syntax and library features. semantics Generic label for semantics issues. All semantics issues should have this tag. syntax Any issue relating to the syntax of Myst. labels Nov 5, 2017
@faultyserver faultyserver added this to the v0.2.0 milestone Nov 5, 2017
@faultyserver
Copy link
Member Author

This should be fully possible now that #24 has been implemented.

faultyserver added a commit that referenced this issue Nov 6, 2017
The interpreter now nows how to visit `OpAssign` nodes, including the conditional assignment variants, `||=` and `&&=`. For normal cases, the assignments are rewritten from `a op= b` to `a = a op b`. For conditional assignments, the expansion is closest to `a op a = b`, but with some safeguarding to avoid raising errors when `a` does not exist (it will be initialized to `nil`).
faultyserver added a commit that referenced this issue Nov 10, 2017
The interpreter now nows how to visit `OpAssign` nodes, including the conditional assignment variants, `||=` and `&&=`. For normal cases, the assignments are rewritten from `a op= b` to `a = a op b`. For conditional assignments, the expansion is closest to `a op a = b`, but with some safeguarding to avoid raising errors when `a` does not exist (it will be initialized to `nil`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Any request for a new feature of the language. Includes both syntax and library features. semantics Generic label for semantics issues. All semantics issues should have this tag. syntax Any issue relating to the syntax of Myst.
Projects
None yet
Development

No branches or pull requests

1 participant