From d97e7be98f9dc0bc07a98090ca304ed0d4cfb0bb Mon Sep 17 00:00:00 2001 From: Harrand Date: Sat, 30 Nov 2024 15:52:15 +0000 Subject: [PATCH] [parse] when initialising a constparam'd struct, the template arguments are now passed as expressions (such as in C++) instead of variable declarations, as this was confusing and verbose --- cpp/src2/ast.hpp | 4 +-- cpp/src2/parse_chords/parse_identifier.inl | 40 ++++++++++++++++++++-- samples/scratchpad.psy | 38 ++++---------------- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/cpp/src2/ast.hpp b/cpp/src2/ast.hpp index c6f4c55..90fd34b 100644 --- a/cpp/src2/ast.hpp +++ b/cpp/src2/ast.hpp @@ -562,11 +562,11 @@ namespace syntax struct struct_initialiser : public nodecomn { - struct_initialiser(identifier struct_name = {}, designated_initialiser_list constinits = {}, designated_initialiser_list inits = {}): + struct_initialiser(identifier struct_name = {}, expression_list constinits = {}, designated_initialiser_list inits = {}): struct_name(struct_name), constinits(constinits), inits(inits){} identifier struct_name; - designated_initialiser_list constinits; + expression_list constinits; designated_initialiser_list inits; std::string to_string() const diff --git a/cpp/src2/parse_chords/parse_identifier.inl b/cpp/src2/parse_chords/parse_identifier.inl index 882fba4..143de65 100644 --- a/cpp/src2/parse_chords/parse_identifier.inl +++ b/cpp/src2/parse_chords/parse_identifier.inl @@ -95,6 +95,25 @@ CHORD_BEGIN return {.t = result::type::reduce_success}; CHORD_END +// struct block +// create a struct with a single constparam +CHORD_BEGIN + STATE(TOKEN(keyword_struct), TOKEN(oanglebrack), NODE(variable_decl_list), TOKEN(canglebrack), NODE(block)) + + SETINDEX(2); + syntax::variable_decl_list constparams = GETNODE(variable_decl_list); + + SETINDEX(4); + auto blk = GETNODE(block); + + auto result_struct = syntax::capped_struct_decl{}; + result_struct.children.push_back(syntax::node{.payload = blk}); + result_struct.constparams = constparams; + + REDUCE_TO(expression, syntax::expression::type::struct_decl, result_struct); + return {.t = result::type::reduce_success}; +CHORD_END + // struct block // create a struct but omitting constparams CHORD_BEGIN @@ -129,16 +148,31 @@ CHORD_BEGIN return {.t = result::type::reduce_success}; CHORD_END -// iden{} +// iden{} // struct initialiser (no initialisers, one constinit) CHORD_BEGIN - STATE(NODE(identifier), TOKEN(oanglebrack), NODE(designated_initialiser), TOKEN(canglebrack), TOKEN(obrace), TOKEN(cbrace)) + STATE(NODE(identifier), TOKEN(oanglebrack), NODE(capped_expression), TOKEN(canglebrack), TOKEN(obrace), TOKEN(cbrace)) + + syntax::struct_initialiser structinit; + structinit.struct_name = GETNODE(identifier); + + SETINDEX(2); + structinit.constinits.exprs.push_back(GETNODE(capped_expression)); + + REDUCE_TO(expression, syntax::expression::type::struct_initialiser, structinit); + return {.t = result::type::reduce_success}; +CHORD_END + +// iden{} +// struct initialiser (no initialisers, multiple constinits) +CHORD_BEGIN + STATE(NODE(identifier), TOKEN(oanglebrack), NODE(expression_list), TOKEN(canglebrack), TOKEN(obrace), TOKEN(cbrace)) syntax::struct_initialiser structinit; structinit.struct_name = GETNODE(identifier); SETINDEX(2); - structinit.constinits.inits.push_back(GETNODE(designated_initialiser)); + structinit.constinits = GETNODE(expression_list); REDUCE_TO(expression, syntax::expression::type::struct_initialiser, structinit); return {.t = result::type::reduce_success}; diff --git a/samples/scratchpad.psy b/samples/scratchpad.psy index bbe2356..9e9639f 100644 --- a/samples/scratchpad.psy +++ b/samples/scratchpad.psy @@ -1,41 +1,15 @@ -[[entry]] -main ::= () -> u0 -{ - -} - -mystruct ::= struct +vec ::= struct { - x ::= 5@T; - mymethod ::= (val : i64)->i64 - { - return val + val; - } - - mynestedstruct ::= struct - { - mystr : string := "hello there"; - }; - nested : mynestedstruct; - - y ::= 7; + data ::= T{}; }; -myfn2 ::= main; -myfn3 ::= (red : i64, green : i64, blue : i64) -> u0 +[[entry]] +main ::= () -> u0 { - myval ::= mystruct - { - .x := 69, - .nested := mynestedstruct<.T := i64>{} - }; - myval.x; - myval.mymethod<"hello", 123>(1, 2, 3); + pos ::= vec{}; + pos.data = 1.0f; } -myfn4 ::= () -> u0{} - - == default : build == {