dc

Tiny reverse polish desk calculator
Log | Files | Refs | README | LICENSE

commit 5800671a4820e24aebfbc1103b9bbe22a530e1df
parent 07efabcc63e26356c5ba94913cd1357aeefd3bbc
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue, 23 Nov 2021 22:57:36 +0000

add n cmd

Diffstat:
Mdc.ha | 86+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 47 insertions(+), 39 deletions(-)

diff --git a/dc.ha b/dc.ha @@ -63,6 +63,53 @@ export fn main() void = { }; switch (r) { + // misc + case 'q' => + os::exit(0); + // printing + case 'p' => + if (len(S) == 0) { + fmt::errorln("dc: stack has no elements")?; + continue; + }; + fmt::println(peek())?; + case 'n' => + if (len(S) == 0) { + fmt::errorln("dc: stack has no elements")?; + continue; + }; + fmt::println(pop())?; + case 'f' => + for (let i = len(S) - 1; i < len(S); i -= 1) { + fmt::println(S[i])?; + }; + // stack control + case 'c' => + clear(); + case 'd' => + push(peek()); + case 'r' => + const a = pop(); + const b = pop(); + push(a); + push(b); + case 'R' => + let n = pop(): int; + if (n > 1) { + const l = len(S): int; + const n = if (n < l) n else l; + const i = l - n; + const a = S[i]; + delete(S[i]); + append(S, a); + } else if (n < 1) { + const l = len(S): int; + const n = if (-n < l) -n else l; + const i = l - n; + const a = pop(); + insert(S[i], a); + }; + // arithmetic case '+' => if (len(S) < 2) { fmt::errorln("dc: stack has too few elements")?; @@ -103,45 +150,6 @@ export fn main() void = { const a = pop(); const b = pop(); push(math::modf64(a, b)); - case 'p' => - if (len(S) == 0) { - fmt::errorln("dc: stack has no elements")?; - continue; - }; - const el = peek(); - fmt::println(el)?; - case 'f' => - for (let i = len(S) - 1; i < len(S); i -= 1) { - const el = S[i]; - fmt::println(el)?; - }; - case 'c' => - clear(); - case 'd' => - push(peek()); - case 'r' => - const a = pop(); - const b = pop(); - push(a); - push(b); - case 'R' => - let n = pop(): int; - if (n > 1) { - const l = len(S): int; - const n = if (n < l) n else l; - const i = l - n; - const a = S[i]; - delete(S[i]); - append(S, a); - } else if (n < 1) { - const l = len(S): int; - const n = if (-n < l) -n else l; - const i = l - n; - const a = pop(); - insert(S[i], a); - }; - case 'q' => - os::exit(0); case => fmt::errorfln("dc: unimplemented '{}'", r)?; };