Skip to content

Continue from the previous result when a line starts with an operator - #397

Open
ChrisJr404 wants to merge 1 commit into
printfn:mainfrom
ChrisJr404:continue-from-previous-result
Open

Continue from the previous result when a line starts with an operator#397
ChrisJr404 wants to merge 1 commit into
printfn:mainfrom
ChrisJr404:continue-from-previous-result

Conversation

@ChrisJr404

Copy link
Copy Markdown

This implements #306: if a line starts with a binary operator that can't begin an expression on its own, fend continues from the previous result, the way a pocket or on-screen calculator does.

> 5 * 10
50
> * 2
100
> to hex
64

Under the hood, when a previous result is available in _, an input whose first token is an operator that needs a left-hand operand (*, ^, mod, to/as, bit shifts, bitwise ops, ...) is rewritten to prepend _. Operators that also have a prefix form (+, -, /) are deliberately left alone, so -5 and /2 still mean negative five and one half. Nothing changes when there is no previous result yet, so a leading operator in a fresh context is still an error as before.

I added integration tests covering the rewritten operators, chaining (* 2 then ^ 2), the interaction with a leading @ attribute, the untouched -// cases, and the no-previous-result case, and documented the behaviour in the expressions chapter. cargo test, cargo fmt --check and cargo clippy --all-targets are all clean.

If a line begins with a binary operator that can't start an expression on
its own (like `* 2` or `to miles`), fend now treats it as continuing from
the previous result, so it becomes `_ * 2` or `_ to miles`. This matches how
pocket and on-screen calculators behave.

Operators with a prefix form (`+`, `-`, `/`) are left untouched so `-5` and
`/2` keep their existing meanings, and nothing changes when there is no
previous result to continue from.

Closes printfn#306
@ChrisJr404

Copy link
Copy Markdown
Author

The ubuntu job didn't fail on a test, it timed out in the "Test (Linux i686)" step while apt-get was installing gcc-i686-linux-gnu (the download of cpp-13-i686-linux-gnu stalled on the azure mirror and the job hit the 20 minute limit). clippy, fmt and the macos/windows builds all passed, so this looks like a transient mirror hiccup rather than anything in the change. Could you re-run that job when you get a chance?

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.

1 participant