hare

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

commit f6a55c2ba6268f8b7c2012a0e961f2acd1b2f2ef
parent 4f25cefce66e2a914f14cce07feea021efd16aaf
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 14 May 2022 13:28:24 +0200

ioctlgen: support type aliases

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mcmd/ioctlgen/main.ha | 32+++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/cmd/ioctlgen/main.ha b/cmd/ioctlgen/main.ha @@ -11,13 +11,16 @@ use regex; use strings; let ioctlre: regex::regex = regex::regex { ... }; +let typedefre: regex::regex = regex::regex { ... }; @init fn init() void = { ioctlre = regex::compile(`@(_IO[RW]*)\((.*)\)`)!; + typedefre = regex::compile(`^(export )?type `)!; }; @fini fn fini() void = { regex::regex_finish(&ioctlre); + regex::regex_finish(&typedefre); }; type dir = enum u64 { @@ -43,6 +46,13 @@ export fn main() void = { }; defer free(line); + if (regex::test(&typedefre, line)!) { + bufio::unreadrune(os::stdin, '\n'); + bufio::unread(os::stdin, strings::toutf8(line)); + loadtype(store); + continue; + }; + let groups = match (regex::find(&ioctlre, line)!) { case void => fmt::println(line)!; @@ -69,6 +79,26 @@ export fn main() void = { }; }; +fn loadtype(store: *types::typestore) void = { + const tee = io::tee(os::stdin, os::stdout); + const lex = lex::init(&tee, "<ioctl>"); + const decl = match (parse::decl(&lex)) { + case let err: parse::error => + fmt::fatal("Error parsing type declaration:", + parse::strerror(err)); + case let decl: ast::decl => + yield decl; + }; + + const tdecl = decl.decl as []ast::decl_type; + if (len(tdecl) != 1) { + fmt::fatal("Multiple type declarations are unsupported"); + }; + const tdecl = tdecl[0]; + const of = types::lookup(store, &tdecl._type)!; + types::createalias(store, tdecl.ident, of); +}; + fn parseioctl(store: *types::typestore, d: dir, params: str) ioctl = { const buf = bufio::fixed(strings::toutf8(params), io::mode::READ); const lex = lex::init(&buf, "<ioctl>"); @@ -93,7 +123,7 @@ fn parseioctl(store: *types::typestore, d: dir, params: str) ioctl = { case let err: types::error => fmt::fatal("Error:", types::strerror(err)); case types::deferred => - fmt::fatal("Error: this tool does not support reverse declarations"); + fmt::fatal("Error: this tool does not support forward references"); case let ty: const *types::_type => yield ty; };