Skip to content

Commit

Permalink
[parse] remove aliases as they are no longer needed, and partial supp…
Browse files Browse the repository at this point in the history
…ort for constparams for structs
  • Loading branch information
harrand committed Nov 30, 2024
1 parent b25218c commit f65a18f
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 188 deletions.
44 changes: 21 additions & 23 deletions cpp/src2/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,32 +444,29 @@ namespace syntax

struct function_call : public nodecomn
{
function_call(identifier func_name = {}, expression_list params = {}): func_name(func_name), params(params){}
function_call(identifier func_name = {}, expression_list constparams = {}, expression_list params = {}): func_name(func_name), params(params){}

identifier func_name;
expression_list constparams;
expression_list params;


std::string to_string() const
{
return std::format("function-call({}({}))", this->func_name.to_string(), this->params.to_string());
return std::format("function-call({}<{}>({}))", this->func_name.to_string(), this->constparams.to_string(), this->params.to_string());
}
};

struct meta_region : public nodecomn
{
enum class type
{
name_space,
static_if,
build,
_unknown,
_count
};
constexpr static std::array<const char*, int(type::_count)> type_names
{
"namespace",
"static_if",
"build",
};

Expand Down Expand Up @@ -498,20 +495,6 @@ namespace syntax
}
};

struct alias : public nodecomn
{
alias(identifier alias_name = {}, expression type_value_expr = {}): alias_name(alias_name), type_value_expr(type_value_expr){}

identifier alias_name;
expression type_value_expr;


std::string to_string() const
{
return std::format("alias({} ::= {})", this->alias_name.iden, type_value_expr.to_string());
}
};

struct struct_decl : public nodecomn
{
struct_decl(variable_decl_list constparams = {}, bool capped = false): capped(capped){}
Expand All @@ -522,7 +505,7 @@ namespace syntax

std::string to_string() const
{
return std::format("struct()");
return std::format("struct(<{}>)", constparams.to_string());
}

const char* name() const
Expand Down Expand Up @@ -577,6 +560,21 @@ namespace syntax
}
};

struct struct_initialiser : public nodecomn
{
struct_initialiser(identifier struct_name = {}, designated_initialiser_list constinits = {}, designated_initialiser_list inits = {}):
struct_name(struct_name), constinits(constinits), inits(inits){}

identifier struct_name;
designated_initialiser_list constinits;
designated_initialiser_list inits;

std::string to_string() const
{
return std::format("struct-init({}<{}> {{{}}})", struct_name.iden, constinits.to_string(), inits.to_string());
}
};

struct if_statement : public nodecomn
{
if_statement(expression cond = {}, block blk = {}, bool is_static = false);
Expand Down Expand Up @@ -632,11 +630,11 @@ namespace syntax
function_call,
meta_region,
capped_meta_region,
alias,
struct_decl,
capped_struct_decl,
designated_initialiser,
designated_initialiser_list,
struct_initialiser,
if_statement,
else_statement
>;
Expand Down Expand Up @@ -668,11 +666,11 @@ namespace syntax
"function call",
"meta region",
"capped meta region",
"alias",
"struct declaration",
"capped struct declaration",
"designated initialiser",
"designated initialiser list",
"struct initialiser",
"if statement",
"else statement"
};
Expand Down
2 changes: 0 additions & 2 deletions cpp/src2/lex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ namespace lex
keyword_build,
keyword_namespace,
keyword_struct,
keyword_alias,
keyword_return,
keyword_if,
keyword_else,
Expand Down Expand Up @@ -110,7 +109,6 @@ namespace lex
trait{.name = "build"},
trait{.name = "namespace"},
trait{.name = "struct"},
trait{.name = "alias"},
trait{.name = "return"},
trait{.name = "if"},
trait{.name = "else"},
Expand Down
45 changes: 0 additions & 45 deletions cpp/src2/parse_chords/parse_expression.inl
Original file line number Diff line number Diff line change
Expand Up @@ -199,49 +199,4 @@ CHORD_BEGIN
auto expr2 = GETNODE(expression);
REDUCE_TO(expression, syntax::expression::type::neqcompare, expr, expr2);
return {.t = result::type::reduce_success};
CHORD_END

// expr{}
// struct initialiser (no initialisers)
CHORD_BEGIN
STATE(NODE(expression), TOKEN(obrace), TOKEN(cbrace))
auto struct_name = GETNODE(expression);
if(struct_name.t != syntax::expression::type::identifier)
{
return {.t = result::type::error, .errmsg = std::format("pattern: {}{{}} is invalid, the preceding token(s) should instead constitute an identifier", syntax::expression::type_names[static_cast<int>(struct_name.t)])};
}
std::vector<syntax::designated_initialiser> inits = {};
REDUCE_TO(expression, syntax::expression::type::struct_initialiser, struct_name, syntax::designated_initialiser_list{inits});
return {.t = result::type::reduce_success};
CHORD_END

// expr{desig-init}
// struct initialiser (single initialiser)
CHORD_BEGIN
STATE(NODE(expression), TOKEN(obrace), NODE(designated_initialiser), TOKEN(cbrace))
auto struct_name = GETNODE(expression);
if(struct_name.t != syntax::expression::type::identifier)
{
return {.t = result::type::error, .errmsg = std::format("pattern: {}{{}} is invalid, the preceding token(s) should instead constitute an identifier (i.e `my_struct_name`)", syntax::expression::type_names[static_cast<int>(struct_name.t)])};
}
SETINDEX(2);
std::vector<syntax::designated_initialiser> inits = {};
inits.push_back(GETNODE(designated_initialiser));
REDUCE_TO(expression, syntax::expression::type::struct_initialiser, struct_name, syntax::designated_initialiser_list{inits});
return {.t = result::type::reduce_success};
CHORD_END

// expr{desig-init-list}
// struct initialiser (multiple initialisers)
CHORD_BEGIN
STATE(NODE(expression), TOKEN(obrace), NODE(designated_initialiser_list), TOKEN(cbrace))
auto struct_name = GETNODE(expression);
if(struct_name.t != syntax::expression::type::identifier)
{
return {.t = result::type::error, .errmsg = std::format("pattern: {}{{}} is invalid, the preceding token(s) should instead constitute an identifier (i.e `my_struct_name`)", syntax::expression::type_names[static_cast<int>(struct_name.t)])};
}
SETINDEX(2);
auto inits = GETNODE(designated_initialiser_list);
REDUCE_TO(expression, syntax::expression::type::struct_initialiser, struct_name, inits);
return {.t = result::type::reduce_success};
CHORD_END
101 changes: 69 additions & 32 deletions cpp/src2/parse_chords/parse_identifier.inl
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,31 @@ CHORD_BEGIN
return {.t = result::type::reduce_success};
CHORD_END

// iden<expr-list>(expr-list)
// function call (multiple arguments, multiple constparams)
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(oanglebrack), NODE(expression_list), TOKEN(canglebrack), TOKEN(oparen), NODE(expression_list), TOKEN(cparen))

syntax::identifier name = GETNODE(identifier);
SETINDEX(2);
syntax::expression_list constparams = GETNODE(expression_list);
SETINDEX(5);
syntax::expression_list params = GETNODE(expression_list);

REDUCE_TO(function_call, name, constparams, params);
return {.t = result::type::reduce_success};
CHORD_END

// iden(expr-list)
// function call (multiple arguments)
// function call (multiple arguments, constparams omitted)
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(oparen), NODE(expression_list), TOKEN(cparen))

syntax::identifier name = GETNODE(identifier);
SETINDEX(2);
syntax::expression_list params = GETNODE(expression_list);

REDUCE_TO(function_call, name, params);
REDUCE_TO(function_call, name, syntax::expression_list{}, params);
return {.t = result::type::reduce_success};
CHORD_END

Expand All @@ -45,7 +60,7 @@ CHORD_BEGIN
std::vector<syntax::expression> params = {};
params.push_back(GETNODE(expression));

REDUCE_TO(function_call, name, params);
REDUCE_TO(function_call, name, syntax::expression_list{}, params);
return {.t = result::type::reduce_success};
CHORD_END

Expand All @@ -56,27 +71,27 @@ CHORD_BEGIN

syntax::identifier name = GETNODE(identifier);

REDUCE_TO(function_call, name, syntax::expression_list{});
REDUCE_TO(function_call, name, syntax::expression_list{}, syntax::expression_list{});
return {.t = result::type::reduce_success};
CHORD_END

// iden :: namespace
// namespace meta region
// struct <variable-decl> block
// create a struct with a single constparam
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(colcol), TOKEN(keyword_namespace))
syntax::identifier name = GETNODE(identifier);
REDUCE_TO(meta_region, name, syntax::meta_region::type::name_space);
return {.t = result::type::reduce_success};
CHORD_END
STATE(TOKEN(keyword_struct), TOKEN(oanglebrack), NODE(capped_variable_decl), TOKEN(canglebrack), NODE(block))

// iden :: alias := expr;
// type alias
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(colcol), TOKEN(keyword_alias), TOKEN(col), TOKEN(eq), NODE(expression), TOKEN(semicol))
syntax::identifier name = GETNODE(identifier);
SETINDEX(5);
syntax::expression expr = GETNODE(expression);
REDUCE_TO(alias, name, expr);
SETINDEX(2);
syntax::variable_decl_list constparams;
constparams.decls.push_back(GETNODE(capped_variable_decl));

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

Expand Down Expand Up @@ -104,35 +119,57 @@ CHORD_BEGIN
CHORD_END

// iden{}
// struct initialiser (no initialisers)
// struct initialiser (no initialisers, constinits omitted)
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(obrace), TOKEN(cbrace))
auto struct_name = GETNODE(identifier);
std::vector<syntax::designated_initialiser> inits = {};
REDUCE_TO(expression, syntax::expression::type::struct_initialiser, struct_name, syntax::designated_initialiser_list(inits));
syntax::struct_initialiser structinit;
structinit.struct_name = GETNODE(identifier);

REDUCE_TO(expression, syntax::expression::type::struct_initialiser, structinit);
return {.t = result::type::reduce_success};
CHORD_END

// iden<desig-init>{}
// struct initialiser (no initialisers, one constinit)
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(oanglebrack), NODE(designated_initialiser), 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));

REDUCE_TO(expression, syntax::expression::type::struct_initialiser, structinit);
return {.t = result::type::reduce_success};
CHORD_END

// iden{desig-init}
// struct initialiser (single initialiser)
// struct initialiser (single initialiser, constinits omitted)
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(obrace), NODE(designated_initialiser), TOKEN(cbrace))
auto struct_name = GETNODE(identifier);

syntax::struct_initialiser structinit;
structinit.struct_name = GETNODE(identifier);

SETINDEX(2);
std::vector<syntax::designated_initialiser> inits = {};
inits.push_back(GETNODE(designated_initialiser));
REDUCE_TO(expression, syntax::expression::type::struct_initialiser, struct_name, syntax::designated_initialiser_list{inits});
structinit.inits.inits.push_back(GETNODE(designated_initialiser));

REDUCE_TO(expression, syntax::expression::type::struct_initialiser, structinit);
return {.t = result::type::reduce_success};
CHORD_END

// iden{desig-init-list}
// struct initialiser (multiple initialisers)
// struct initialiser (multiple initialisers, constinits omitted)
CHORD_BEGIN
STATE(NODE(identifier), TOKEN(obrace), NODE(designated_initialiser_list), TOKEN(cbrace))
auto struct_name = GETNODE(identifier);

syntax::struct_initialiser structinit;
structinit.struct_name = GETNODE(identifier);
SETINDEX(2);
auto inits = GETNODE(designated_initialiser_list);
REDUCE_TO(expression, syntax::expression::type::struct_initialiser, struct_name, inits);
structinit.inits = GETNODE(designated_initialiser_list);

REDUCE_TO(expression, syntax::expression::type::struct_initialiser, structinit);
return {.t = result::type::reduce_success};
CHORD_END

Expand Down
10 changes: 0 additions & 10 deletions cpp/src2/parse_chords/parse_token.inl
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,6 @@ CHORD_BEGIN
return {.t = result::type::reduce_success};
CHORD_END

// { alias
// starts an unfinished block
CHORD_BEGIN
STATE(TOKEN(obrace), NODE(alias))
SETINDEX(1);
auto al = GETNODE(alias);
REDUCE_TO(unfinished_block, al);
return {.t = result::type::reduce_success};
CHORD_END

// { if-statement
// starts an unfinished block
CHORD_BEGIN
Expand Down
9 changes: 0 additions & 9 deletions cpp/src2/parse_chords/parse_unfinished_block.inl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ CHORD_BEGIN
return {.t = result::type::reduce_success};
CHORD_END

// add an alias to the end of the unfinished block.
CHORD_BEGIN
STATE(NODE(unfinished_block), NODE(alias))
auto blk = GETNODE(unfinished_block);
blk.extend(GETNODE(alias));
REDUCE_TO(unfinished_block, std::move(blk));
return {.t = result::type::reduce_success};
CHORD_END

// add an if-statement to the end of the unfinished block.
CHORD_BEGIN
STATE(NODE(unfinished_block), NODE(if_statement))
Expand Down
Loading

0 comments on commit f65a18f

Please sign in to comment.