~torresjrjr/dc

9d3fefb466d402687dd335e210db9199908aa635 — Byron Torres 6 months ago 59378f1
write initial interface, add Makefile
4 files changed, 72 insertions(+), 0 deletions(-)

A .gitignore
A Makefile
M README.md
A dc.ha
A .gitignore => .gitignore +2 -0
@@ 0,0 1,2 @@
dc
tmp/

A Makefile => Makefile +13 -0
@@ 0,0 1,13 @@
.POSIX:
HARE=hare
HAREFLAGS=

all: dc

dc: dc.ha
	$(HARE) build $(HAREFLAGS) -o $@ $<

clean:
	rm -f dc

.PHONY: all clean

M README.md => README.md +8 -0
@@ 1,1 1,9 @@
# dc

An implementation of dc,
the desk calculator,
wriiten in Hare.

(pre-alpha)

Send patches to <b@torresjrjr.com>.

A dc.ha => dc.ha +49 -0
@@ 0,0 1,49 @@
use bufio;
use encoding::utf8;
use fmt;
use getopt;
use io;
use os;

export fn main() void = {
	const help: [_]getopt::help = [
		"desk calculator",
		"[file]",
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);

	if (len(cmd.args) > 1) {
		usage_exit(help);
	};

	let filename: (str | void) = if (len(cmd.args) == 1)
		switch (cmd.args[0]) {
		case "-" =>
			fmt::fatal("dc: invalid filename '-'");
		case "" =>
			fmt::fatal("dc: invalid filename ''");
		case =>
			yield cmd.args[0];
		}
	else
		void;

	for (true) {
		const r = match (bufio::scanrune(os::stdin)) {
		case utf8::invalid =>
			fmt::fatal("dc: invalid utf8 input");
		case io::error =>
			fmt::fatal("dc: IO error");
		case io::EOF =>
			break;
		case r: rune =>
			yield r;
		};
	};
};

@noreturn fn usage_exit(help: []getopt::help) void = {
	getopt::printusage(os::stderr, os::args[0], help);
	os::exit(1);
};