hare

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

commit 2a4622c2c8f6ab4c7b6402d42c9989b0a1bf65e6
parent cb4abcbad7c204d2ab84f34970f1ffd57cc786c8
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 21 Aug 2021 13:08:43 +0200

hare::unit: partially implement constant exprs

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

Diffstat:
Ahare/types/lookup.ha | 14++++++++++++++
Ahare/unit/+test.ha | 24++++++++++++++++++++++++
Mhare/unit/process.ha | 37++++++++++++++++++++++++++++++++++++-
Mscripts/gen-stdlib | 11+++++++++--
Mstdlib.mk | 9++++++---
5 files changed, 89 insertions(+), 6 deletions(-)

diff --git a/hare/types/lookup.ha b/hare/types/lookup.ha @@ -0,0 +1,14 @@ +use hare::ast; + +// Looks up a built-in type. +export fn lookup_builtin( + store: *typestore, + _type: ast::builtin_type, +) const *_type = { + // TODO: &ast::type { ... } + const atype = ast::_type { + _type = _type, + ... + }; + return lookup(store, &atype)!; +}; diff --git a/hare/unit/+test.ha b/hare/unit/+test.ha @@ -0,0 +1,24 @@ +use bufio; +use hare::ast; +use hare::lex; +use hare::parse; +use hare::types; +use io; +use strings; + +fn parse_expr(src: str) *ast::expr = { + const stream = bufio::fixed(strings::toutf8(src), io::mode::READ); + defer io::close(stream); + const lexer = lex::init(stream, "<test>"); + return alloc(parse::expression(&lexer)!); +}; + +fn mktestctx() context = context { + store = types::store(types::x86_64, null, null), + scope = alloc(scope { ... }), +}; + +fn freetestctx(ctx: *context) void = { + // TODO: Some of this should be in -test + types::store_free(ctx.store); +}; diff --git a/hare/unit/process.ha b/hare/unit/process.ha @@ -1,4 +1,5 @@ use hare::ast; +use hare::lex; use hare::types; fn process(ctx: *context, subunits: const []ast::subunit) (unit | error) = { @@ -83,7 +84,7 @@ fn process_expr( ast::break_expr => abort(), // TODO ast::call_expr => abort(), // TODO ast::cast_expr => abort(), // TODO - ast::constant_expr => abort(), // TODO + ast::constant_expr => process_constant(ctx, expr), ast::continue_expr => abort(), // TODO ast::defer_expr => abort(), // TODO ast::delete_expr => abort(), // TODO @@ -101,3 +102,37 @@ fn process_expr( ast::switch_expr => abort(), // TODO ast::unarithm_expr => abort(), // TODO }; + +fn process_constant(ctx: *context, aexpr: *ast::expr) (*expr | error) = { + const constexpr = aexpr.expr as ast::constant_expr; + const er = match (constexpr) { + void => ( + types::lookup_builtin(ctx.store, ast::builtin_type::VOID), + void: constant_expr, + ), + bool => abort(), // TODO + ast::_null => abort(), // TODO + lex::value => abort(), // TODO + ast::array_constant => abort(), // TODO + ast::struct_constant => abort(), // TODO + ast::tuple_constant => abort(), // TODO + }; + return alloc(expr { + start = aexpr.start, + end = aexpr.end, + result = er.0, + expr = er.1, + ... + }); +}; + +@test fn constant() void = { + const ctx = mktestctx(); + defer freetestctx(&ctx); + const aexpr = parse_expr("void"); + defer ast::expr_free(aexpr); + const expr = process_constant(&ctx, aexpr)!; + assert(expr.result._type as types::builtin == types::builtin::VOID); + const constexpr = expr.expr as constant_expr; + assert(constexpr is void); +}; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -368,6 +368,7 @@ gensrcs_hare_types() { gen_srcs hare::types \ arch.ha \ hash.ha \ + lookup.ha \ store.ha \ types.ha \ $* @@ -396,8 +397,14 @@ gensrcs_hare_unit() { } hare_unit() { - gensrcs_hare_unit - gen_ssa hare::unit hare::ast hare::types hash hash::fnv strings hare::lex + if [ $testing -eq 1 ] + then + gensrcs_hare_unit +test.ha + else + gensrcs_hare_unit + fi + gen_ssa hare::unit hare::ast hare::types hash hash::fnv strings hare::lex \ + bufio hare::parse } hare_unparse() { diff --git a/stdlib.mk b/stdlib.mk @@ -697,6 +697,7 @@ $(HARECACHE)/hare/parse/hare_parse.ssa: $(stdlib_hare_parse_srcs) $(stdlib_rt) $ stdlib_hare_types_srcs= \ $(STDLIB)/hare/types/arch.ha \ $(STDLIB)/hare/types/hash.ha \ + $(STDLIB)/hare/types/lookup.ha \ $(STDLIB)/hare/types/store.ha \ $(STDLIB)/hare/types/types.ha @@ -716,7 +717,7 @@ stdlib_hare_unit_srcs= \ $(STDLIB)/hare/unit/scope.ha \ $(STDLIB)/hare/unit/unit.ha -$(HARECACHE)/hare/unit/hare_unit.ssa: $(stdlib_hare_unit_srcs) $(stdlib_rt) $(stdlib_hare_ast) $(stdlib_hare_types) $(stdlib_hash) $(stdlib_hash_fnv) $(stdlib_strings) $(stdlib_hare_lex) +$(HARECACHE)/hare/unit/hare_unit.ssa: $(stdlib_hare_unit_srcs) $(stdlib_rt) $(stdlib_hare_ast) $(stdlib_hare_types) $(stdlib_hash) $(stdlib_hash_fnv) $(stdlib_strings) $(stdlib_hare_lex) $(stdlib_bufio) $(stdlib_hare_parse) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/hare/unit @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nhare::unit \ @@ -1932,6 +1933,7 @@ $(TESTCACHE)/hare/parse/hare_parse.ssa: $(testlib_hare_parse_srcs) $(testlib_rt) testlib_hare_types_srcs= \ $(STDLIB)/hare/types/arch.ha \ $(STDLIB)/hare/types/hash.ha \ + $(STDLIB)/hare/types/lookup.ha \ $(STDLIB)/hare/types/store.ha \ $(STDLIB)/hare/types/types.ha \ $(STDLIB)/hare/types/+test.ha @@ -1950,9 +1952,10 @@ testlib_hare_unit_srcs= \ $(STDLIB)/hare/unit/process.ha \ $(STDLIB)/hare/unit/scan.ha \ $(STDLIB)/hare/unit/scope.ha \ - $(STDLIB)/hare/unit/unit.ha + $(STDLIB)/hare/unit/unit.ha \ + $(STDLIB)/hare/unit/+test.ha -$(TESTCACHE)/hare/unit/hare_unit.ssa: $(testlib_hare_unit_srcs) $(testlib_rt) $(testlib_hare_ast) $(testlib_hare_types) $(testlib_hash) $(testlib_hash_fnv) $(testlib_strings) $(testlib_hare_lex) +$(TESTCACHE)/hare/unit/hare_unit.ssa: $(testlib_hare_unit_srcs) $(testlib_rt) $(testlib_hare_ast) $(testlib_hare_types) $(testlib_hash) $(testlib_hash_fnv) $(testlib_strings) $(testlib_hare_lex) $(testlib_bufio) $(testlib_hare_parse) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/hare/unit @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nhare::unit \