~sircmpwn/hare-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH harec] eval_expr: move expression type and result assignment from individual functions

Details
Message ID
<20220628194136.4001390-1-tb46305@gmail.com>
DKIM signature
pass
Download raw message
Patch: +8 -22
previously some of these result types diverged from check, causing
assertion failures.

Signed-off-by: Armin Weigl <tb46305@gmail.com>
---
 src/eval.c             | 26 ++++----------------------
 tests/10-binarithms.ha |  4 ++++
 2 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/src/eval.c b/src/eval.c
index 89567e9..2aa9195 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -17,9 +17,6 @@ eval_access(struct context *ctx, struct expression *in, struct expression *out)
	struct expression tmp = {0};
	enum eval_result r;

	out->type = EXPR_CONSTANT;
	out->result = in->result;

	switch (in->access.type) {
	case ACCESS_IDENTIFIER:
		return EVAL_INVALID; // &ident handled in eval_unarithm
@@ -328,8 +325,6 @@ eval_binarithm(struct context *ctx, struct expression *in, struct expression *ou
		}
		break;
	}
	out->type = EXPR_CONSTANT;
	out->result = in->result;
	if (type_is_float(in->result)) {
		out->constant.fval = ftrunc(in->result, fval);
	} else if (type_is_signed(in->result)) {
@@ -345,8 +340,6 @@ eval_binarithm(struct context *ctx, struct expression *in, struct expression *ou
enum eval_result
eval_const(struct context *ctx, struct expression *in, struct expression *out)
{
	out->type = EXPR_CONSTANT;
	out->result = in->result;
	enum type_storage storage = type_dealias(out->result)->storage;
	if (storage == STORAGE_ENUM) {
		storage = type_dealias(out->result)->alias.type->storage;
@@ -449,14 +442,12 @@ eval_cast(struct context *ctx, struct expression *in, struct expression *out)
	// The STORAGE_ARRAY exception is to make sure we handle expandable
	// arrays at this point.
	if (to->storage == from->storage && to->storage != STORAGE_ARRAY) {
		*out = val;
		out->constant = val.constant;
		return EVAL_OK;
	}

	// XXX: We should also be able to handle expressions which use
	// symbols/identifiers
	out->type = EXPR_CONSTANT;
	out->result = in->result;

	const struct type *subtype;
	switch (to->storage) {
@@ -554,8 +545,6 @@ enum eval_result
eval_measurement(struct context *ctx, struct expression *in, struct expression *out)
{
	assert(in->type == EXPR_MEASURE);
	out->type = EXPR_CONSTANT;
	out->result = &builtin_type_size;
	struct expression obj = {0};
	enum eval_result res;
	switch (in->measure.op) {
@@ -721,7 +710,6 @@ eval_struct(struct context *ctx, struct expression *in, struct expression *out)
	assert(in->type == EXPR_STRUCT);
	assert(type_dealias(in->result)->storage != STORAGE_UNION); // TODO
	const struct type *type = type_dealias(in->result);
	out->type = EXPR_CONSTANT;

	size_t n = count_struct_fields(type);
	assert(n > 0);
@@ -756,7 +744,6 @@ eval_struct(struct context *ctx, struct expression *in, struct expression *out)
	}

	out->constant._struct = fields[0];
	out->result = in->result;
	free(fields);
	return EVAL_OK;
}
@@ -766,8 +753,6 @@ eval_tuple(struct context *ctx, struct expression *in, struct expression *out)
{
	assert(in->type == EXPR_TUPLE);
	const struct type *type = type_dealias(in->result);
	out->type = EXPR_CONSTANT;


	struct tuple_constant *out_tuple_start, *out_tuple;
	out_tuple_start = out_tuple = xcalloc(1, sizeof(struct tuple_constant));
@@ -790,7 +775,6 @@ eval_tuple(struct context *ctx, struct expression *in, struct expression *out)
	}

	out->constant.tuple = out_tuple_start;
	out->result = in->result;
	return EVAL_OK;
}

@@ -798,8 +782,6 @@ eval_tuple(struct context *ctx, struct expression *in, struct expression *out)
static enum eval_result
eval_unarithm(struct context *ctx, struct expression *in, struct expression *out)
{
	out->type = EXPR_CONSTANT;

	if (in->unarithm.op == UN_ADDRESS) {
		assert(in->unarithm.operand->type == EXPR_ACCESS);
		// TODO other access types
@@ -807,8 +789,6 @@ eval_unarithm(struct context *ctx, struct expression *in, struct expression *out
		if (in->unarithm.operand->access.object->otype != O_DECL) {
			return EVAL_INVALID;
		}
		out->result = type_store_lookup_pointer(
			ctx->store, in->unarithm.operand->result, 0);
		out->constant.object = in->unarithm.operand->access.object;
		out->constant.ival = 0;
		return EVAL_OK;
@@ -820,7 +800,6 @@ eval_unarithm(struct context *ctx, struct expression *in, struct expression *out
		return r;
	}

	out->result = lvalue.result;
	switch (in->unarithm.op) {
	case UN_ADDRESS:
		assert(0); // handled above
@@ -850,6 +829,9 @@ eval_unarithm(struct context *ctx, struct expression *in, struct expression *out
enum eval_result
eval_expr(struct context *ctx, struct expression *in, struct expression *out)
{
	out->result = in->result;
	out->type = EXPR_CONSTANT;

	switch (in->type) {
	case EXPR_ACCESS:
		return eval_access(ctx, in, out);
diff --git a/tests/10-binarithms.ha b/tests/10-binarithms.ha
index 0536d1a..44e1897 100644
--- a/tests/10-binarithms.ha
+++ b/tests/10-binarithms.ha
@@ -1,3 +1,5 @@
use testmod;

fn error() bool = {
	abort();
};
@@ -91,11 +93,13 @@ fn sar_shr() void = {
def FLOAT: f64 = 6.0 * 7.0;
def I8: i8 = 127 * 2;
def U8: u8 = 128 * 2;
def ALIAS: testmod::enum_alias = 1: testmod::_enum: testmod::enum_alias + 1: testmod::enum_alias;

fn eval() void = {
	assert(FLOAT == 42.0);
	assert(I8 == 126);
	assert(U8 == 0);
	assert(ALIAS == 2);
};

export fn main() void = {
-- 
2.35.1

[harec/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<CL20VQ1B666G.2KUWSOVWIJM5Q@cirno2>
In-Reply-To
<20220628194136.4001390-1-tb46305@gmail.com> (view parent)
DKIM signature
missing
Download raw message
harec/patches: SUCCESS in 54s

[eval_expr: move expression type and result assignment from individual functions][0] from [Armin Weigl][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/33361
[1]: tb46305@gmail.com

✓ #789998 SUCCESS harec/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/789998
✓ #789999 SUCCESS harec/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/789999
Details
Message ID
<CL51ZLZ1RMQZ.309Q5GU5LG9N5@taiga>
In-Reply-To
<20220628194136.4001390-1-tb46305@gmail.com> (view parent)
DKIM signature
pass
Download raw message
Thanks!

To git@git.sr.ht:~sircmpwn/harec
   53f2eb1..8da848e  master -> master
Reply to thread Export thread (mbox)