From 8e5da8dd58ba1663d1f11a134b1c0c68d5f5b9c3 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 27 Aug 2022 16:12:01 -0500 Subject: [PATCH] removed "as" keyword --- core/container/avl_tree.onyx | 5 ++++- core/container/iter.onyx | 2 +- core/container/map.onyx | 2 +- core/container/set.onyx | 2 +- core/hash.onyx | 2 +- examples/13_use_keyword.onyx | 2 +- examples/22_interfaces.onyx | 6 +++--- include/lex.h | 1 - src/lex.c | 2 -- src/parser.c | 29 ++++++++++++++++++++++------- tests/complicated_polymorph.onyx | 2 +- tests/interfaces.onyx | 8 ++++---- 12 files changed, 39 insertions(+), 24 deletions(-) diff --git a/core/container/avl_tree.onyx b/core/container/avl_tree.onyx index 554ea44e..ff409417 100644 --- a/core/container/avl_tree.onyx +++ b/core/container/avl_tree.onyx @@ -63,7 +63,10 @@ contains :: (tree: ^AVL_Tree, data: tree.T) -> bool { } print :: (tree: ^AVL_Tree) { - use package core {print as std_print, printf} + use core { + std_print :: print, + printf + } if tree == null { std_print("_ "); diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 829db3b6..33222bc1 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -29,7 +29,7 @@ package core.iter as_iterator :: #match {} #local __iterable_test :: macro (i: Iterator($V)) {} -Iterable :: interface (T as t) { +Iterable :: interface (t: $T) { as_iterator(t) |> __iterable_test(); } diff --git a/core/container/map.onyx b/core/container/map.onyx index df66e8c1..e12ebace 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -11,7 +11,7 @@ package core.map } #local { - ValidKey :: interface (T as t) { + ValidKey :: interface (t: $T) { // In order to use a certain type as a key in a Map, you must // provide an implementation of core.hash.to_u32() for that type, // and you must provide an operator overload for ==. diff --git a/core/container/set.onyx b/core/container/set.onyx index dd429e14..c758f0ad 100644 --- a/core/container/set.onyx +++ b/core/container/set.onyx @@ -7,7 +7,7 @@ package core.set math :: package core.math } -#local SetValue :: interface (T as t) { +#local SetValue :: interface (t: $T) { { hash.to_u32(t) } -> u32; { t == t } -> bool; } diff --git a/core/hash.onyx b/core/hash.onyx index 3d85ed1a..baa67125 100644 --- a/core/hash.onyx +++ b/core/hash.onyx @@ -14,6 +14,6 @@ to_u32 :: #match { (key: type_expr) -> u32 do return to_u32(cast(u32) key); } -Hashable :: interface (T as t) { +Hashable :: interface (t: $T) { { to_u32(t) } -> u32; } diff --git a/examples/13_use_keyword.onyx b/examples/13_use_keyword.onyx index b3576401..cb30eac0 100644 --- a/examples/13_use_keyword.onyx +++ b/examples/13_use_keyword.onyx @@ -28,7 +28,7 @@ main :: (args: [] cstr) { use package core.alloc // You can also restrict and partially rename the symbols that collide - use package core.string { free as string_free } + use package core.string { string_free :: free } } { diff --git a/examples/22_interfaces.onyx b/examples/22_interfaces.onyx index 7d5709d1..1bf958af 100644 --- a/examples/22_interfaces.onyx +++ b/examples/22_interfaces.onyx @@ -32,7 +32,7 @@ add_example :: () { // expressions do not type check correctly, then the set of parameters does not // satisfy the interface. - CanAdd :: interface (T as t) { + CanAdd :: interface (t: $T) { t + t; } @@ -59,7 +59,7 @@ add_example :: () { // These have to be defined out here because #operator and #match are only allowed // as top level expressions currently. -NumberLike :: interface (T as t) { +NumberLike :: interface (t: $T) { // The constraints here are a little stricter than before. This syntax // allows you to specify the expected type of the expression. If the type // of the expression and the type after the arrow do not match, then the @@ -101,7 +101,7 @@ overloaded_procedure_example :: () { // when the are combined with overloaded procedures. // Take this example. Here is the same CanAdd interface from before. - CanAdd :: interface (T as t) { + CanAdd :: interface (t: $T) { t + t; } diff --git a/include/lex.h b/include/lex.h index f1395b72..df684476 100644 --- a/include/lex.h +++ b/include/lex.h @@ -23,7 +23,6 @@ typedef enum TokenType { Token_Type_Keyword_Elseif, Token_Type_Keyword_Return, Token_Type_Keyword_Global, - Token_Type_Keyword_As, Token_Type_Keyword_Cast, Token_Type_Keyword_While, Token_Type_Keyword_For, diff --git a/src/lex.c b/src/lex.c index c34cc951..eba315ed 100644 --- a/src/lex.c +++ b/src/lex.c @@ -21,7 +21,6 @@ static const char* token_type_names[] = { "elseif", "return", "global", - "as", "cast", "while", "for", @@ -342,7 +341,6 @@ whitespace_skipped: switch (curr) { case 'a': LITERAL_TOKEN("alignof", 1, Token_Type_Keyword_Alignof); - LITERAL_TOKEN("as", 1, Token_Type_Keyword_As); break; case 'b': LITERAL_TOKEN("break", 1, Token_Type_Keyword_Break); diff --git a/src/parser.c b/src/parser.c index f41fe093..e98ffd16 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1398,12 +1398,13 @@ static AstNode* parse_use_stmt(OnyxParser* parser) { if (parser->hit_unexpected_token) return NULL; QualifiedUse qu; - qu.symbol_name = expect_token(parser, Token_Type_Symbol); + qu.as_name = expect_token(parser, Token_Type_Symbol); + qu.symbol_name = qu.as_name; - if (consume_token_if_next(parser, Token_Type_Keyword_As)) - qu.as_name = expect_token(parser, Token_Type_Symbol); - else - qu.as_name = qu.symbol_name; + if (consume_token_if_next(parser, ':')) { + expect_token(parser, ':'); + qu.symbol_name = expect_token(parser, Token_Type_Symbol); + } bh_arr_push(use_node->only, qu); @@ -2151,9 +2152,10 @@ static AstInterface* parse_interface(OnyxParser* parser) { if (parser->hit_unexpected_token) return interface; InterfaceParam ip; - ip.type_token = expect_token(parser, Token_Type_Symbol); - expect_token(parser, Token_Type_Keyword_As); ip.value_token = expect_token(parser, Token_Type_Symbol); + expect_token(parser, ':'); + expect_token(parser, '$'); + ip.type_token = expect_token(parser, Token_Type_Symbol); bh_arr_push(interface->params, ip); @@ -3112,6 +3114,19 @@ static void parse_top_level_statement(OnyxParser* parser) { goto submit_binding_to_entities; } + case '(': { + AstTyped *retval = NULL; + if (parse_possible_function_definition(parser, &retval)) { + ENTITY_SUBMIT(retval); + return; + } + if (parse_possible_quick_function_definition(parser, &retval)) { + ENTITY_SUBMIT(retval); + return; + } + break; + } + case '#': { if (next_tokens_are(parser, 2, '#', Token_Type_Keyword_If)) { AstIf* static_if = parse_static_if_stmt(parser, 0); diff --git a/tests/complicated_polymorph.onyx b/tests/complicated_polymorph.onyx index aaa9c558..754edbc4 100644 --- a/tests/complicated_polymorph.onyx +++ b/tests/complicated_polymorph.onyx @@ -14,7 +14,7 @@ main :: (args: [] cstr) { return g(f(a)); } - Number :: interface (T as t) { + Number :: interface (t: $T) { { t * t } -> T; } diff --git a/tests/interfaces.onyx b/tests/interfaces.onyx index 6ac73902..897f903a 100644 --- a/tests/interfaces.onyx +++ b/tests/interfaces.onyx @@ -2,7 +2,7 @@ use package core -Hashable :: interface (T as t) { +Hashable :: interface (t :$T) { { hash.to_u32(t) } -> u32; } @@ -16,7 +16,7 @@ try_hash :: #match { }, } -CanCastTo :: interface (T as t, D as d) { +CanCastTo :: interface (t: $T, d: $D) { { cast(D) t } -> D; } @@ -37,7 +37,7 @@ do_math :: macro (x, y: $T) -> T where SemiRing(T) { return x + y + x * y; } -SemiRing :: interface (T as t) { +SemiRing :: interface (t: $T) { {t + t} -> T; {t * t} -> T; } @@ -46,7 +46,7 @@ bit_math :: (x, y: $T) -> T where BitField(T) { return (x & y) | (x ^ y); } -BitField :: interface (T as t) { +BitField :: interface (t: $T) { { ~t } -> T; { t & t } -> T; { t | t } -> T; -- 2.25.1