@@ 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;
+ };
};
};