dc

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

commit 44c38c222b23f23563dc9c9b2d9399d89e32621f
parent 9d3fefb466d402687dd335e210db9199908aa635
Author: Byron Torres <b@torresjrjr.com>
Date:   Mon, 22 Nov 2021 14:26:05 +0000

add IO loop

Diffstat:
Mdc.ha | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/dc.ha b/dc.ha @@ -4,6 +4,9 @@ use fmt; use getopt; use io; use os; +use strconv; + +let stack: []f64 = []; export fn main() void = { const help: [_]getopt::help = [ @@ -40,6 +43,43 @@ export fn main() void = { case r: rune => yield r; }; + + switch (r) { + case '1' => + const el = 1f64; + append(stack, el); + case '+' => + if (len(stack) < 2) { + fmt::errorln("dc: stack has too few elements")?; + continue; + }; + const a = stack[len(stack) - 2]; + const b = stack[len(stack) - 1]; + const sum = a + b; + delete(stack[len(stack) - 1]); + delete(stack[len(stack) - 1]); + append(stack, sum); + case '-' => + if (len(stack) < 2) { + fmt::errorln("dc: stack has too few elements")?; + continue; + }; + const a = stack[len(stack) - 2]; + const b = stack[len(stack) - 1]; + const diff = a - b; + delete(stack[len(stack) - 1]); + delete(stack[len(stack) - 1]); + append(stack, diff); + case 'p' => + if (len(stack) == 0) { + fmt::errorln("dc: stack has no elements")?; + continue; + }; + const el = stack[len(stack) - 1]; + fmt::println(el)?; + case => + continue; + }; }; };