commit 07efabcc63e26356c5ba94913cd1357aeefd3bbc
parent 82e950c3bfb9b1a1b95b14ec775c534a68eac0f7
Author: Byron Torres <b@torresjrjr.com>
Date: Tue, 23 Nov 2021 22:49:40 +0000
add negative numbers and stack control commands
Diffstat:
M | dc.ha | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- |
1 file changed, 53 insertions(+), 14 deletions(-)
diff --git a/dc.ha b/dc.ha
@@ -9,7 +9,7 @@ use os;
use strconv;
use strings;
-let stack: []f64 = [];
+let S: []f64 = [];
export fn main() void = {
const help: [_]getopt::help = [
@@ -53,14 +53,18 @@ export fn main() void = {
if (ascii::isdigit(r)) {
bufio::unreadrune(os::stdin, r);
- const el = scan_number();
- push(el);
+ push(scan_number());
+ continue;
+ };
+
+ if (r == '_') {
+ push(-scan_number());
continue;
};
switch (r) {
case '+' =>
- if (len(stack) < 2) {
+ if (len(S) < 2) {
fmt::errorln("dc: stack has too few elements")?;
continue;
};
@@ -68,7 +72,7 @@ export fn main() void = {
const b = pop();
push(a + b);
case '-' =>
- if (len(stack) < 2) {
+ if (len(S) < 2) {
fmt::errorln("dc: stack has too few elements")?;
continue;
};
@@ -76,7 +80,7 @@ export fn main() void = {
const b = pop();
push(a - b);
case '*' =>
- if (len(stack) < 2) {
+ if (len(S) < 2) {
fmt::errorln("dc: stack has too few elements")?;
continue;
};
@@ -84,7 +88,7 @@ export fn main() void = {
const b = pop();
push(a * b);
case '/' =>
- if (len(stack) < 2) {
+ if (len(S) < 2) {
fmt::errorln("dc: stack has too few elements")?;
continue;
};
@@ -92,7 +96,7 @@ export fn main() void = {
const b = pop();
push(a / b);
case '%' =>
- if (len(stack) < 2) {
+ if (len(S) < 2) {
fmt::errorln("dc: stack has too few elements")?;
continue;
};
@@ -100,17 +104,46 @@ export fn main() void = {
const b = pop();
push(math::modf64(a, b));
case 'p' =>
- if (len(stack) == 0) {
+ 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)?;
- continue;
};
};
};
@@ -155,15 +188,21 @@ fn scan_number() f64 = {
};
fn pop() f64 = {
- const a = stack[len(stack) - 1];
- delete(stack[len(stack) - 1]);
+ const a = S[len(S) - 1];
+ delete(S[len(S) - 1]);
return a;
};
fn push(el: f64) void = {
- append(stack, el);
+ append(S, el);
};
fn peek() f64 = {
- return stack[len(stack) - 1];
+ return S[len(S) - 1];
+};
+
+fn clear() void = {
+ for (len(S) != 0) {
+ delete(S[len(S) - 1]);
+ };
};