hare

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

commit e5ecf11005e3e9d1bc9b1cd6a8dfef884a18e9e2
parent 05991cae3e628268fb2436feebd81254fe4353c3
Author: Sebastian <sebastian@sebsite.pw>
Date:   Tue, 17 Oct 2023 22:30:27 -0400

hare::ast: store ptrs in decl_func and decl_type

decl_const and decl_global store pointers, whereas decl_type and
decl_func stored values (for decl_type's type, and for decl_func's
prototype and body). For consistency, I changed the latter two to store
pointers instead of values.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mcmd/haredoc/doc/sort.ha | 2+-
Mcmd/ioctlgen/main.ha | 2+-
Mhare/ast/decl.ha | 35+++++++++++++----------------------
Mhare/parse/decl.ha | 11++++++-----
Mhare/unit/process.ha | 8++++----
Mhare/unit/scan.ha | 2+-
Mhare/unparse/decl.ha | 22+++++++++++-----------
7 files changed, 37 insertions(+), 45 deletions(-)

diff --git a/cmd/haredoc/doc/sort.ha b/cmd/haredoc/doc/sort.ha @@ -33,7 +33,7 @@ export fn sort_decls(decls: []ast::decl) summary = { symbol = f.symbol, ident = f.ident, prototype = f.prototype, - body = void, + body = null, attrs = f.attrs, }, docs = decl.docs, diff --git a/cmd/ioctlgen/main.ha b/cmd/ioctlgen/main.ha @@ -103,7 +103,7 @@ fn loadtype(store: *types::typestore) void = { fmt::fatal("Multiple type declarations are unsupported"); }; const tdecl = tdecl[0]; - const of = types::lookup(store, &tdecl._type)!; + const of = types::lookup(store, tdecl._type)!; types::newalias(store, tdecl.ident, of); }; diff --git a/hare/ast/decl.ha b/hare/ast/decl.ha @@ -30,7 +30,7 @@ export type decl_global = struct { // type foo = int; export type decl_type = struct { ident: ident, - _type: _type, + _type: *_type, }; // Attributes applicable to a function declaration. @@ -47,8 +47,8 @@ export type fndecl_attrs = enum { export type decl_func = struct { symbol: str, ident: ident, - prototype: _type, - body: (expr | void), + prototype: *_type, + body: nullable *expr, attrs: fndecl_attrs, }; @@ -81,18 +81,17 @@ export fn decl_finish(d: decl) void = { case let t: []decl_type => for (let i = 0z; i < len(t); i += 1) { ident_free(t[i].ident); - type_finish(&t[i]._type); + type_finish(t[i]._type); + free(t[i]._type); }; free(t); case let f: decl_func => free(f.symbol); ident_free(f.ident); - type_finish(&f.prototype); - match (f.body) { - case let e: expr => - expr_finish(&e); - case void => void; - }; + type_finish(f.prototype); + free(f.prototype); + expr_finish(f.body); + free(f.body); case let c: []decl_const => for (let i = 0z; i < len(c); i += 1) { ident_free(c[i].ident); @@ -103,17 +102,9 @@ export fn decl_finish(d: decl) void = { }; free(c); case let e: assert_expr => - match (e.cond) { - case let e: *expr => - expr_finish(e); - free(e); - case null => void; - }; - match (e.message) { - case let e: *expr => - expr_finish(e); - free(e); - case null => void; - }; + expr_finish(e.cond); + free(e.cond); + expr_finish(e.message); + free(e.message); }; }; diff --git a/hare/parse/decl.ha b/hare/parse/decl.ha @@ -118,7 +118,7 @@ fn decl_type(lexer: *lex::lexer) ([]ast::decl_type | error) = { let btok = try(lexer, ltok::COMMA)?; append(decl, ast::decl_type { ident = ident, - _type = _type, + _type = alloc(_type), }); if (btok is void) { break; @@ -167,21 +167,22 @@ fn decl_func(lexer: *lex::lexer) (ast::decl_func | error) = { len(params[i].name) > 0, "Expected parameter name in function declaration")?; }; - yield expr(lexer)?; + yield alloc(expr(lexer)?); case ltok::SEMICOLON => - yield lex::unlex(lexer, tok); + lex::unlex(lexer, tok); + yield null; case => abort(); // unreachable }; return ast::decl_func { symbol = sym, ident = ident, - prototype = ast::_type { + prototype = alloc(ast::_type { start = proto_start, end = proto_end, flags = 0, repr = prototype, - }, + }), body = body, attrs = attr, }; diff --git a/hare/unit/process.ha b/hare/unit/process.ha @@ -54,16 +54,16 @@ fn process_func( ) (decl | error) = { assert(func.attrs & ast::fndecl_attrs::TEST == 0); // TODO const afndecl = adecl.decl as ast::decl_func; - const prototype = types::lookup(ctx.store, &func.prototype)!; + const prototype = types::lookup(ctx.store, func.prototype)!; const fntype = prototype.repr as types::func; assert(fntype.variadism == types::variadism::NONE); // TODO assert(len(fntype.params) == 0); // TODO ctx.fntype = &fntype; const body: nullable *expr = match (afndecl.body) { - case let abody: ast::expr => - yield process_expr(ctx, &abody)?; - case void => + case let abody: *ast::expr => + yield process_expr(ctx, abody)?; + case null => yield null; }; diff --git a/hare/unit/scan.ha b/hare/unit/scan.ha @@ -51,7 +51,7 @@ fn scan_func( func: *ast::decl_func, ) (void | types::deferred | error) = { assert(func.attrs & ast::fndecl_attrs::TEST == 0); // TODO - const fntype = match (types::lookup(ctx.store, &func.prototype)) { + const fntype = match (types::lookup(ctx.store, func.prototype)) { case let err: types::error => return err; case types::deferred => diff --git a/hare/unparse/decl.ha b/hare/unparse/decl.ha @@ -99,7 +99,7 @@ export fn decl( n += space(&ctx)?; n += syn(&ctx, "=", synkind::OPERATOR)?; n += space(&ctx)?; - n += __type(&ctx, syn, &t[i]._type)?; + n += __type(&ctx, syn, t[i]._type)?; if (i + 1 < len(t)) { n += syn(&ctx, ",", synkind::PUNCTUATION)?; n += space(&ctx)?; @@ -107,7 +107,7 @@ export fn decl( }; case let f: ast::decl_func => ctx.stack = &stack { - cur = &f.prototype, + cur = f.prototype, up = ctx.stack, ... }; @@ -146,12 +146,12 @@ export fn decl( const fntype = f.prototype.repr as ast::func_type; n += prototype(&ctx, syn, &fntype)?; match (f.body) { - case void => void; - case let e: ast::expr => + case null => void; + case let e: *ast::expr => n += space(&ctx)?; n += syn(&ctx, "=", synkind::OPERATOR)?; n += space(&ctx)?; - n += _expr(&ctx, syn, &e)?; + n += _expr(&ctx, syn, e)?; }; case let e: ast::assert_expr => n += assert_expr(&ctx, syn, &e)?; @@ -275,11 +275,11 @@ fn decl_test(d: *ast::decl, expected: str) bool = { d.decl = [ ast::decl_type { ident = ["foo"], - _type = type_int, + _type = &type_int, }, ast::decl_type { ident = ["bar"], - _type = type_int, + _type = &type_int, }, ]; assert(decl_test(&d, "type foo = int, bar = int;")); @@ -287,8 +287,8 @@ fn decl_test(d: *ast::decl, expected: str) bool = { d.decl = ast::decl_func { symbol = "foo", ident = ["foo"], - prototype = type_fn, - body = void, + prototype = &type_fn, + body = null, attrs = ast::fndecl_attrs::FINI, }; assert(decl_test(&d, "@fini @symbol(\"foo\") fn foo(foo: int, bar: int...) int;")); @@ -307,8 +307,8 @@ fn decl_test(d: *ast::decl, expected: str) bool = { d.decl = ast::decl_func { symbol = "", ident = ["foo"], - prototype = type_fn, - body = expr_void, + prototype = &type_fn, + body = &expr_void, attrs = 0, }; assert(decl_test(&d, "fn foo(int) int = void;"));