From: Brendan Hansen Date: Sun, 29 Aug 2021 01:31:54 +0000 (-0500) Subject: bugfixes with expression macro substitutions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=80ec63be80018c16e6955685c454c5efd8060c64;p=onyx.git bugfixes with expression macro substitutions --- diff --git a/bin/onyx b/bin/onyx index a124e137..30b2a890 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/docs/todo b/docs/todo index a3445f3e..ef21b982 100644 --- a/docs/todo +++ b/docs/todo @@ -109,7 +109,7 @@ Language Cohesion: [ ] Functions should return the correct value in all branches. - [ ] Add macros. + [X] Add macros. [ ] enum #flags should be able to be used as so: ``` diff --git a/examples/08_enums.onyx b/examples/08_enums.onyx index 0262ea2d..63449417 100644 --- a/examples/08_enums.onyx +++ b/examples/08_enums.onyx @@ -54,7 +54,7 @@ main :: (args: [] cstr) { // you can leave out the enum name from the lookup and just use a unary '.'. // For example, // - + val := SimpleEnum.Value2; // Here .Value2 can be resolved from the context of "val". diff --git a/examples/11_map.onyx b/examples/11_map.onyx index 94e9d984..bba7b9e4 100644 --- a/examples/11_map.onyx +++ b/examples/11_map.onyx @@ -2,7 +2,7 @@ use package core -main :: (args: [] cstr) { +main :: (args: [] cstr) { // Onyx does not have map type built into the language semantics, // but using polymorphic structs and overloaded procedures, it does // provide an 'any-to-any' hash map in it's core libraries. diff --git a/examples/12_varargs.onyx b/examples/12_varargs.onyx index 34d9948e..dde67ff1 100644 --- a/examples/12_varargs.onyx +++ b/examples/12_varargs.onyx @@ -20,7 +20,7 @@ use package core main :: (args: [] cstr) { - + // This is how you specify a fixed type variadic procedure. 'args' can be thought // of as a slice of integers, with data and count fields. You can iterate over the // varargs using a for-loop like any other array-like structure. Note, do not diff --git a/examples/14_overloaded_procs.onyx b/examples/14_overloaded_procs.onyx index ad91d8f3..fd500a50 100644 --- a/examples/14_overloaded_procs.onyx +++ b/examples/14_overloaded_procs.onyx @@ -14,7 +14,7 @@ // multiple times. Instead, to do the above in Onyx, you create a explicitly // overloaded procedure, // -// foo :: proc { +// foo :: #match { // (x: i32) { ... }, // (y: str) { ... }, // } diff --git a/examples/17_operator_overload.onyx b/examples/17_operator_overload.onyx index 49f5e303..96cac6a3 100644 --- a/examples/17_operator_overload.onyx +++ b/examples/17_operator_overload.onyx @@ -16,7 +16,7 @@ use package core // a binary operator, you cannot pass a custom allocator. #operator + (x: str, y: str) -> str { - return string.concat(x, y); + return string.concat(x, y); } main :: (args: [] cstr) { @@ -35,7 +35,7 @@ main :: (args: [] cstr) { // As a side note, '==' is already overloaded for strings in the // standard library, so you can do this: - + if array.contains(cast([] str) strings, "is ") { println("The array contains 'is '."); } else { diff --git a/modules/ui/ui.onyx b/modules/ui/ui.onyx index 327efcc9..9b41a7e1 100644 --- a/modules/ui/ui.onyx +++ b/modules/ui/ui.onyx @@ -196,7 +196,7 @@ has_active_animation :: () -> bool { // Utilities -get_site_hash :: (site: CallSite, increment := 0) -> UI_Id { +get_site_hash :: macro (site: CallSite, increment := 0) -> UI_Id { hash :: package core.hash file_hash := hash.to_u32(site.file); @@ -221,7 +221,7 @@ move_towards :: macro (value: ^$T, target: T, step: T) { if *value > target - step && *value < target + step do *value = target; } -#private color_lerp :: (t: f32, c1: gfx.Color4, c2: gfx.Color4) -> gfx.Color4 { +#private color_lerp :: macro (t: f32, c1: gfx.Color4, c2: gfx.Color4) -> gfx.Color4 { return .{ r = c1.r * (1 - t) + c2.r * t, g = c1.g * (1 - t) + c2.g * t, diff --git a/src/onyxchecker.c b/src/onyxchecker.c index a2ff9458..ce564b16 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -810,11 +810,11 @@ CheckStatus check_binaryop_assignment(AstBinaryOp* binop) { else if (binop->operation == Binary_Op_Assign_Sar) operation = Binary_Op_Sar; AstBinaryOp* new_right = make_binary_op(context.ast_alloc, operation, binop->left, binop->right); - new_right->token = binop->token; - CHECK(binaryop, &new_right); - binop->right = (AstTyped *) new_right; + new_right->token = binop->token; binop->operation = Binary_Op_Assign; + + CHECK(binaryop, (AstBinaryOp **) &binop->right); } if (binop->right->type == NULL) { @@ -1182,7 +1182,7 @@ CheckStatus check_struct_literal(AstStructLiteral* sl) { char* err_msg = NULL; if (!fill_in_arguments(&sl->args, (AstNode *) sl, &err_msg)) { onyx_report_error(sl->token->pos, err_msg); - + bh_arr_each(AstTyped *, value, sl->args.values) { if (*value == NULL) { i32 member_idx = value - sl->args.values; // Pointer subtraction hack @@ -1434,10 +1434,10 @@ CheckStatus check_subscript(AstSubscript** psub) { } } - if (!type_is_array_accessible(sub->addr->type)) - ERROR_(sub->token->pos, - "Expected pointer or array type for left of array access, got '%s'.", - node_get_type_name(sub->addr)); + if (!type_is_array_accessible(sub->addr->type)) { + report_bad_binaryop((AstBinaryOp *) sub); + return Check_Error; + } if (types_are_compatible(sub->expr->type, builtin_range_type_type)) { Type *of = NULL; @@ -1448,6 +1448,7 @@ CheckStatus check_subscript(AstSubscript** psub) { else { // FIXME: Slice creation should be allowed for slice types and dynamic array types, like it // is below, but this code doesn't look at that. + report_bad_binaryop((AstBinaryOp *) sub); ERROR(sub->token->pos, "Invalid type for left of slice creation."); } @@ -1461,6 +1462,7 @@ CheckStatus check_subscript(AstSubscript** psub) { resolve_expression_type(sub->expr); if (sub->expr->type->kind != Type_Kind_Basic || (sub->expr->type->Basic.kind != Basic_Kind_I32 && sub->expr->type->Basic.kind != Basic_Kind_U32)) { + report_bad_binaryop((AstBinaryOp *) sub); ERROR_(sub->token->pos, "Expected type u32 or i32 for index, got '%s'.", node_get_type_name(sub->expr)); @@ -1486,6 +1488,7 @@ CheckStatus check_subscript(AstSubscript** psub) { sub->type = sub->addr->type->Pointer.elem; } else { + report_bad_binaryop((AstBinaryOp *) sub); ERROR(sub->token->pos, "Invalid type for left of array access."); }