~torresjrjr/dc

44c38c222b23f23563dc9c9b2d9399d89e32621f — Byron Torres 6 months ago 9d3fefb
add IO loop
1 files changed, 40 insertions(+), 0 deletions(-)

M dc.ha
M dc.ha => dc.ha +40 -0
@@ 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;
		};
	};
};