Skip to content

Commit

Permalink
[parse] when initialising a constparam'd struct, the template argumen…
Browse files Browse the repository at this point in the history
…ts are now passed as expressions (such as in C++) instead of variable declarations, as this was confusing and verbose
  • Loading branch information
harrand committed Nov 30, 2024
1 parent f65a18f commit d97e7be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cpp/src2/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 37 additions & 3 deletions cpp/src2/parse_chords/parse_identifier.inl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ CHORD_BEGIN
return {.t = result::type::reduce_success};
CHORD_END

// struct <variable-decl-list> 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
Expand Down Expand Up @@ -129,16 +148,31 @@ CHORD_BEGIN
return {.t = result::type::reduce_success};
CHORD_END

// iden<desig-init>{}
// iden<expr>{}
// 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<expr-list>{}
// 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};
Expand Down
38 changes: 6 additions & 32 deletions samples/scratchpad.psy
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
[[entry]]
main ::= () -> u0
{

}

mystruct ::= struct<T : type>
vec ::= struct<T : type, S : u64>
{
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 ::= <size : u64, T : type>(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<f32, 3>{};
pos.data = 1.0f;
}

myfn4 ::= <size ::= 64>() -> u0{}


== default : build ==
{

Expand Down

0 comments on commit d97e7be

Please sign in to comment.