fix: report bare identifier assignment as a compile error#8
Merged
Conversation
`x be value` on an already-declared (or undeclared) bare identifier was parsed permissively and the assignment silently dropped, compiling to a bare expression statement (`x;`). Code that looked valid ran with stale values and no diagnostics. Per the RFC (6.4 Assignment), bare identifier assignment is not supported by design. This change makes the compiler say so: - parser: collect diagnostics in a new `Parser::errors` array; bare identifier assignment pushes a descriptive error suggesting const/let or compound assignment - cmd: `purus build` / `purus check` print collected errors and exit 1 - pkg: `compile()` / `check()` surface the compiler's stderr/stdout in the thrown Error instead of a generic "Command failed" message Property, index, slice, destructuring, and compound assignments are unaffected. Fixes #5
This file contains hidden or 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
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.
Summary
x be valueon a bare identifier was parsed permissively and the assignment silently dropped — it compiled tox;, so code ran with stale values and no diagnostics.Per the RFC (6.4 Assignment), bare identifier assignment is not supported by design (it prevents accidental implicit globals). This PR makes the compiler actually say so instead of silently discarding the code.
Changes
Parser::errorsarray collects diagnostics; bare identifier assignment pushes a descriptive error:purus build/purus checkprint collected errors and exit 1compile()/check()now surface the compiler's output in the thrownErrorinstead of a generic "Command failed" messageProperty (
obj.x be), index (arr[\0] be), slice, destructuring ([a; b] be), and compound (x add be 1) assignments are unaffected.Tests
moon test --target js: 320 passedx be 10and for reassignment afterlet; no errors for property/index/compound assignmentscompile('let y be 10\ny be 20')now throws with the message above; valid code still compilesNote for docs
The website docs currently contradict the RFC on this point (
operators.mdxshowsy be 20;control-flow.mdxusesi be i add 1in while examples — those examples infinite-loop today). They should be updated to use compound assignment; I can follow up with a puruslang/docs PR.Fixes #5