~torresjrjr/dc

5800671a4820e24aebfbc1103b9bbe22a530e1df — Byron Torres 6 months ago 07efabc
add n cmd
1 files changed, 47 insertions(+), 39 deletions(-)

M dc.ha
M dc.ha => dc.ha +47 -39
@@ 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)?;
		};