From: Brendan Hansen Date: Mon, 20 Jul 2020 22:11:45 +0000 (-0500) Subject: Added 'drop' instruction for ignored function return values X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=46fefbcf15c754e2cc0dcb499b881501a475e124;p=onyx.git Added 'drop' instruction for ignored function return values --- diff --git a/include/onyxtypes.h b/include/onyxtypes.h index f7feda57..1f7ff864 100644 --- a/include/onyxtypes.h +++ b/include/onyxtypes.h @@ -104,5 +104,6 @@ StructMember type_struct_lookup_member(Type* type, char* member); b32 type_is_pointer(Type* type); b32 type_is_struct(Type* type); b32 type_is_bool(Type* type); +b32 type_results_in_void(Type* type); #endif // #ifndef ONYX_TYPES diff --git a/onyx b/onyx index 91945452..b260910c 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/structs.onyx b/progs/structs.onyx index ad96f3ce..1bf7f3e2 100644 --- a/progs/structs.onyx +++ b/progs/structs.onyx @@ -105,12 +105,17 @@ link_create :: proc (data: i32, parent: ^^Link) { *parent = link; } -link_print :: proc (start: ^Link) { +link_print :: proc (start: ^Link) -> i32 { + count := 0; + walker := start; while (walker as i32) != 0 { print(walker.data); walker = walker.next; + count += 1; } + + return count; } link_test :: proc #export "main2" { diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 604989c3..7fe714b4 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -380,6 +380,8 @@ CHECK(binaryop, AstBinaryOp* binop, b32 assignment_is_ok) { if (binop->operation >= Binary_Op_Equal && binop->operation <= Binary_Op_Greater_Equal) { binop->type = &basic_types[Basic_Kind_Bool]; + } else if (binop_is_assignment(binop)) { + binop->type = &basic_types[Basic_Kind_Void]; } else { binop->type = binop->left->type; } @@ -575,16 +577,17 @@ CHECK(global, AstGlobal* global) { CHECK(statement, AstNode* stmt) { switch (stmt->kind) { + case Ast_Kind_Break: return 0; + case Ast_Kind_Continue: return 0; + case Ast_Kind_Return: return check_return((AstReturn *) stmt); case Ast_Kind_If: return check_if((AstIf *) stmt); case Ast_Kind_While: return check_while((AstWhile *) stmt); case Ast_Kind_For: return check_for((AstFor *) stmt); - case Ast_Kind_Call: return check_call((AstCall *) stmt); case Ast_Kind_Block: return check_block((AstBlock *) stmt); - case Ast_Kind_Binary_Op: return check_binaryop((AstBinaryOp *) stmt, 1); - - case Ast_Kind_Break: return 0; - case Ast_Kind_Continue: return 0; + case Ast_Kind_Binary_Op: + stmt->flags |= Ast_Flag_Expr_Ignored; + return check_binaryop((AstBinaryOp *) stmt, 1); default: stmt->flags |= Ast_Flag_Expr_Ignored; diff --git a/src/onyxtypes.c b/src/onyxtypes.c index f23b8802..6b10c3c9 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -277,3 +277,11 @@ b32 type_is_struct(Type* type) { b32 type_is_bool(Type* type) { return type != NULL && type->kind == Type_Kind_Basic && type->Basic.kind == Basic_Kind_Bool; } + +b32 type_results_in_void(Type* type) { + return (type == NULL) + || (type->kind == Type_Kind_Basic && type->Basic.kind == Basic_Kind_Void) + || ( (type->kind == Type_Kind_Function) + && (type->Function.return_type->kind == Type_Kind_Basic) + && (type->Function.return_type->Basic.kind == Basic_Kind_Void)); +} diff --git a/src/onyxwasm.c b/src/onyxwasm.c index cfeb97b9..c5d9cc7e 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -913,6 +913,10 @@ COMPILE_FUNC(expression, AstTyped* expr) { assert(0); } + if (expr->flags & Ast_Flag_Expr_Ignored && + !type_results_in_void(expr->type)) + WI(WI_DROP); + *pcode = code; }