Skip to content

Commit

Permalink
Calculator: allow the use of p in any context
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-develop committed Jul 27, 2024
1 parent 56ca902 commit b82acfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Since 0.19.0-beta, we use [this changelog format](https://keepachangelog.com). I
### Added
* Added the `loop` loop, which is an infinite loop.

### Calculator
#### Changed
* The `p` instruction can be used in any command (e.g. things like `1 2 + p` are now possible)

## 1.0.0 (2024-07-08)
*As this change does not affect public API, this is released as 1.0.0*

Expand Down
29 changes: 21 additions & 8 deletions examples/calculator.noug
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ while:main_loop True then
if calcul in ["reseti", "ri"] then; var increment = 0; continue; end
if calcul in ["printi"] then; print(increment); continue; end
if calcul in ["p"] then
# although the 'p' instruction can be used inside 'commands', having only a 'p' as the input should not
# increase the 'i' value
if len(stack) == 0 then print(0) \
else print(stack(-1))
continue
Expand Down Expand Up @@ -167,18 +169,26 @@ while:main_loop True then
replace(tokens, -1, tokens(-1)*10 + int(char))
else # float
var floating_point += 1
replace(tokens, -1, round(tokens(-1) + float(char)/round(10 ^ floating_point),\
floating_point)\
)
replace(\
tokens, -1,\
round(\
tokens(-1) + float(char)/round(10 ^ floating_point),\
floating_point\
)\
)
end
else
if is_int(tokens(-1)) then
replace(tokens, -1, tokens(-1)*10 - int(char))
else # float
var floating_point += 1
replace(tokens, -1, round(tokens(-1) - float(char)/(10 ^ floating_point),\
floating_point)\
)
replace(\
tokens, -1,\
round(\
tokens(-1) - float(char)/(10 ^ floating_point),\
floating_point\
)\
)
end
end
else
Expand Down Expand Up @@ -270,10 +280,13 @@ while:main_loop True then
var stack = list(old_stack)
continue:main_loop
end
elif token == "p" then
if len(stack) == 0 then print(0) \
else print(stack(-1))
elif token == "pi" then
append(stack, math.pi)
append(stack, math.pi)
elif token == "tau" then
append(stack, math.tau)
append(stack, math.tau)
elif token == "ans" then
append(stack, answer)
elif token == "mod" and len(stack) > 1 and is_num(stack(-1)) and is_num(stack(-2)) then
Expand Down

0 comments on commit b82acfc

Please sign in to comment.