From: Brendan Hansen Date: Tue, 11 Aug 2020 18:57:23 +0000 (-0500) Subject: changed UFC operator to |> for now; fixed bug with nested UFCs X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=0b883de3de69dbc53d97dfbf0f2ffb6e5f89aee2;p=onyx.git changed UFC operator to |> for now; fixed bug with nested UFCs --- diff --git a/include/onyxlex.h b/include/onyxlex.h index e1c23c11..614eee80 100644 --- a/include/onyxlex.h +++ b/include/onyxlex.h @@ -34,6 +34,7 @@ typedef enum TokenType { Token_Type_Right_Arrow, Token_Type_Left_Arrow, Token_Type_Empty_Block, + Token_Type_Pipe, Token_Type_Greater_Equal, Token_Type_Less_Equal, diff --git a/onyx b/onyx index e19764a5..4353307f 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/stack_based.onyx b/progs/stack_based.onyx index f07194e3..95d3efa7 100644 --- a/progs/stack_based.onyx +++ b/progs/stack_based.onyx @@ -8,7 +8,7 @@ package main use package printing use package memory -sort :: proc (arr: [N]i32, cmp: proc (i32, i32) -> i32) { +sort :: proc (arr: [N]i32, cmp: proc (i32, i32) -> i32) -> [N]i32 { for i: 0, N { smallest_idx := i; @@ -18,6 +18,8 @@ sort :: proc (arr: [N]i32, cmp: proc (i32, i32) -> i32) { arr[i] = arr[smallest_idx]; arr[smallest_idx] = tmp; } + + return arr; } ret_val :: proc (x: i32, y: i32) -> i32 { @@ -58,6 +60,12 @@ vec_add :: proc (v: Vec3, u: Vec3, use out: ^Vec3) { z = v.z + u.z; } +clamp :: proc (v: i32, lo: i32, hi: i32) -> i32 { + if v < lo do return lo; + if v > hi do return hi; + return v; +} + some_value := 20 + 30 * 4 + 15 / 5; start :: proc #export { @@ -85,7 +93,7 @@ start :: proc #export { arr[1][3] = 40; arr[1][4] = 50; arr[1][9] = 123; - print(arr[1] 'sumN()); + print(arr[1]|>sumN()); print(summing(cast(^i32) arr[1])); v1: Vec3; @@ -104,9 +112,9 @@ start :: proc #export { print(v3.y); print(v3.z); - print(v3'mag_squared()); + print(v3|>mag_squared()); - print(10'ret_val(4)); + print(10 |> ret_val(4)); for i: 0, N do print(arr[1][i]); @@ -127,7 +135,10 @@ start :: proc #export { for i: 0, N do something[i] = N - i; for i: 0, N do print(something[i]); - something'sort(proc (a: i32, b: i32) -> i32 { return a - b; }); + val := something + |> sort(proc (a: i32, b: i32) -> i32 { return a - b; }) + |> sumN(); + print(val); - for i: 0, N do print(something[i]); + for i: 0, N do print(something[i] |> clamp(3, 6)); } \ No newline at end of file diff --git a/src/onyxlex.c b/src/onyxlex.c index 3f7fbe78..153606e4 100644 --- a/src/onyxlex.c +++ b/src/onyxlex.c @@ -32,6 +32,7 @@ static const char* token_type_names[] = { "->", "<-", "---", + "|>", ">=", "<=", @@ -172,6 +173,7 @@ OnyxToken* onyx_get_token(OnyxTokenizer* tokenizer) { LITERAL_TOKEN("->", 0, Token_Type_Right_Arrow); LITERAL_TOKEN("<-", 0, Token_Type_Right_Arrow); LITERAL_TOKEN("---", 0, Token_Type_Empty_Block); + LITERAL_TOKEN("|>", 0, Token_Type_Pipe); LITERAL_TOKEN("&&", 0, Token_Type_And_And); LITERAL_TOKEN("||", 0, Token_Type_Or_Or); LITERAL_TOKEN(">>>=", 0, Token_Type_Sar_Equal); diff --git a/src/onyxparser.c b/src/onyxparser.c index c58469b7..89148789 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -415,13 +415,23 @@ static AstTyped* parse_factor(OnyxParser* parser) { break; } - case '\'': { + case Token_Type_Pipe: { AstUfc* ufc_node = make_node(AstUfc, Ast_Kind_Ufc); - ufc_node->token = expect_token(parser, '\''); + ufc_node->token = expect_token(parser, Token_Type_Pipe); ufc_node->object = retval; - ufc_node->call = parse_factor(parser); - retval = (AstTyped *) ufc_node; + AstTyped* right = parse_factor(parser); + + if (right->kind == Ast_Kind_Ufc) { + ufc_node->call = ((AstUfc *) right)->object; + ((AstUfc *) right)->object = (AstTyped *) ufc_node; + retval = right; + + } else { + ufc_node->call = right; + retval = (AstTyped *) ufc_node; + } + break; } diff --git a/src/onyxsymres.c b/src/onyxsymres.c index c6432ad0..262e3b40 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -223,6 +223,10 @@ static void symres_field_access(AstFieldAccess** fa) { } static void symres_ufc(AstUfc** ufc) { + AstCall* call_node = (AstCall *) (*ufc)->call; + symres_expression((AstTyped **) &call_node); + symres_expression(&(*ufc)->object); + if ((*ufc)->call->kind != Ast_Kind_Call) { onyx_message_add(Msg_Type_Literal, (*ufc)->token->pos, @@ -230,23 +234,18 @@ static void symres_ufc(AstUfc** ufc) { return; } - symres_expression(&(*ufc)->object); if ((*ufc)->object == NULL) return; - AstCall* call_node = (AstCall *) (*ufc)->call; - AstArgument* implicit_arg = onyx_ast_node_new(semstate.node_allocator, sizeof(AstArgument), Ast_Kind_Argument); implicit_arg->value = (*ufc)->object; implicit_arg->next = (AstNode *) call_node->arguments; - + call_node->arguments = implicit_arg; call_node->arg_count++; call_node->next = (*ufc)->next; - symres_expression((AstTyped **) &call_node); - // NOTE: Not a UFC node *ufc = (AstUfc *) call_node; }