dc

Tiny reverse polish desk calculator
Log | Files | Refs | README | LICENSE

commit 9d3fefb466d402687dd335e210db9199908aa635
parent 59378f18dba3c1ff12f70650be06c439bacef385
Author: Byron Torres <b@torresjrjr.com>
Date:   Mon, 22 Nov 2021 09:31:44 +0000

write initial interface, add Makefile

Diffstat:
A.gitignore | 2++
AMakefile | 13+++++++++++++
MREADME.md | 8++++++++
Adc.ha | 49+++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +dc +tmp/ diff --git a/Makefile b/Makefile @@ -0,0 +1,13 @@ +.POSIX: +HARE=hare +HAREFLAGS= + +all: dc + +dc: dc.ha + $(HARE) build $(HAREFLAGS) -o $@ $< + +clean: + rm -f dc + +.PHONY: all clean diff --git a/README.md b/README.md @@ -1 +1,9 @@ # dc + +An implementation of dc, +the desk calculator, +wriiten in Hare. + +(pre-alpha) + +Send patches to <b@torresjrjr.com>. diff --git a/dc.ha b/dc.ha @@ -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); +};