hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit f801f22349998f60268e328bd062f47e2e4a076d
parent 185f6ae0c9fc5da288cbf44b9fd360a5b95d9209
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 27 Mar 2021 09:35:53 -0400

cmd/harec: implement option parsing

Diffstat:
Mcmd/harec/main.ha | 53++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 42 insertions(+), 11 deletions(-)

diff --git a/cmd/harec/main.ha b/cmd/harec/main.ha @@ -1,4 +1,5 @@ use fmt; +use getopt; use hare::ast; use hare::lex; use hare::parse; @@ -7,17 +8,47 @@ use io; use os; export fn main() void = { - let input = os::open(os::args[1]) as *io::stream; - defer io::close(input); + let cmd = getopt::parse(os::args, + "compiles Hare programs", + ('D', "ident:type=value", "defines a constant"), + ('o', "path", "set the output file"), + ('T', "tags...", "sets build tags"), + ('t', "path", "write typedefs to a file"), + ('N', "ident", "set the namespace for unit"), + "files..." + ); + defer getopt::finish(&cmd); - let lexer = lex::init(input, os::args[1]); - let su = match (parse::subunit(&lexer)) { - err: parse::error => { - printerr(err); - os::exit(1); - }, - u: ast::subunit => u, + for (let i = 0z; i < len(cmd.opts); i += 1) { + let opt = cmd.opts[i]; + switch (opt.0) { + 'D' => abort(), // TODO + 'o' => abort(), // TODO + 'T' => abort(), // TODO + 't' => abort(), // TODO + 'N' => abort(), // TODO + * => abort(), + }; + }; + + for (let i = 0z; i < len(cmd.args); i += 1) { + let input = match (os::open(cmd.args[i])) { + f: *io::stream => f, + err: io::error => fmt::fatal("Error opening {}: {}", + cmd.args[i], io::errstr(err)), + }; + defer io::close(input); + + let lexer = lex::init(input, cmd.args[i]); + let su = match (parse::subunit(&lexer)) { + err: parse::error => { + printerr(err); + os::exit(1); + }, + u: ast::subunit => u, + }; + defer ast::subunit_free(su); + + unparse::subunit(os::stdout, su); }; - defer ast::subunit_free(su); - unparse::subunit(os::stdout, su); };