From f0e25132f2ed69bbb5500b9d4eb39ff3aa3be6cc Mon Sep 17 00:00:00 2001 From: Harrand Date: Sat, 30 Nov 2024 16:27:53 +0000 Subject: [PATCH] [ast] ast node to_string rework to no longer contain noisy labels - infact now allowed to be directly used to convert an expression to a string for example --- cpp/src2/ast.cpp | 2 +- cpp/src2/ast.hpp | 29 ++++++------- cpp/src2/parse_chords/parse_identifier.inl | 47 ++++++++++++++++++++-- cpp/src2/parse_chords/parse_token.inl | 4 +- samples/scratchpad.psy | 5 +++ 5 files changed, 65 insertions(+), 22 deletions(-) diff --git a/cpp/src2/ast.cpp b/cpp/src2/ast.cpp index 3e96b2c..0f9a242 100644 --- a/cpp/src2/ast.cpp +++ b/cpp/src2/ast.cpp @@ -21,7 +21,7 @@ namespace syntax std::string expression::to_string() const { - return std::format("expr-{}({}{})", expression::type_names[static_cast(this->t)], this->expr->to_string(), this->extra->has_value() ? std::format(", {}", this->extra->to_string()) : ""); + return this->expr->to_string(); } if_statement::if_statement(expression cond, block blk, bool is_static): diff --git a/cpp/src2/ast.hpp b/cpp/src2/ast.hpp index 90fd34b..46a0b29 100644 --- a/cpp/src2/ast.hpp +++ b/cpp/src2/ast.hpp @@ -85,7 +85,7 @@ namespace syntax std::string to_string() const { - return std::format("token({})", tok.lexeme); + return std::format("'{}'", tok.lexeme); } const char* name() const @@ -110,7 +110,7 @@ namespace syntax std::string to_string() const { - return std::format("integer-literal({})", this->val); + return std::format("{}", this->val); } }; @@ -122,7 +122,7 @@ namespace syntax std::string to_string() const { - return std::format("decimal-literal({})", this->val); + return std::format("{}", this->val); } const char* name() const { @@ -138,7 +138,7 @@ namespace syntax std::string to_string() const { - return std::format("char-literal('{}' (dec {})", this->val, static_cast(this->val)); + return std::format("'{}'", this->val, static_cast(this->val)); } }; @@ -162,7 +162,7 @@ namespace syntax std::string to_string() const { - return std::format("string-literal(\"{}\")", this->val); + return std::format("\"{}\"", this->val); } }; @@ -173,7 +173,7 @@ namespace syntax std::string to_string() const { - return "null-literal()"; + return "null"; } }; @@ -187,7 +187,7 @@ namespace syntax std::string to_string() const { - return std::format("identifier({})", this->iden); + return this->iden; } }; @@ -308,7 +308,7 @@ namespace syntax contents += ", "; } } - return std::format("expr-list({})", contents); + return std::format("{}", contents); } }; @@ -328,7 +328,7 @@ namespace syntax contents += ", "; } } - return std::format("annotations({})", contents); + return std::format("{}", contents); } }; @@ -374,11 +374,9 @@ namespace syntax mutable bool impl_should_add_to_current_scope = true; mutable bool impl_is_defined_before_parent_block = false; - - std::string to_string() const { - return std::format("variable-decl({}{} : {}{})", this->annotations.exprs.size() ? std::format(" [{}] ", this->annotations.to_string()) : "", this->var_name.to_string(), this->type_name.to_string(), expr.is_null() ? "" : std::format(" := {}", expr.to_string())); + return std::format("{}{} : {}{}", this->annotations.exprs.size() ? std::format("[[{}]] ", this->annotations.to_string()) : "", this->var_name.to_string(), this->type_name.to_string(), expr.is_null() ? "" : std::format(" := {}", expr.to_string())); } }; @@ -408,7 +406,7 @@ namespace syntax contents += ", "; } } - return std::format("variable-decl-list({})", contents); + return std::format("{}", contents); } }; @@ -426,7 +424,7 @@ namespace syntax std::string to_string() const { - return std::format("function-decl({}<{}>({}) -> {}{})", this->annotations.exprs.size() ? std::format(" [{}] ", this->annotations.to_string()) : "", this->constparams.to_string(), this->params.to_string(), this->return_type_name.to_string(), this->is_extern ? ":= extern" : ""); + return std::format("func{}<{}>({}) -> {}{}", this->annotations.exprs.size() ? std::format(" [{}] ", this->annotations.to_string()) : "", this->constparams.to_string(), this->params.to_string(), this->return_type_name.to_string(), this->is_extern ? ":= extern" : ""); } }; @@ -449,11 +447,10 @@ namespace syntax 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->constparams.to_string(), this->params.to_string()); + return std::format("{}<{}>({})", this->func_name.to_string(), this->constparams.to_string(), this->params.to_string()); } }; diff --git a/cpp/src2/parse_chords/parse_identifier.inl b/cpp/src2/parse_chords/parse_identifier.inl index 143de65..0d72811 100644 --- a/cpp/src2/parse_chords/parse_identifier.inl +++ b/cpp/src2/parse_chords/parse_identifier.inl @@ -1,11 +1,52 @@ -// iden : iden +// iden : iden +// explicitly-typed variable declaration (of a type with constparams) with no initialiser +CHORD_BEGIN + STATE(NODE(identifier), TOKEN(col), NODE(identifier), TOKEN(oanglebrack), NODE(capped_expression), TOKEN(canglebrack)) + + syntax::identifier name = GETNODE(identifier); + SETINDEX(2); + syntax::identifier struct_name = GETNODE(identifier); + SETINDEX(4); + syntax::expression constparam = GETNODE(capped_expression); + + syntax::identifier type_name; + type_name.iden = std::format("{}<{}>", struct_name.iden, constparam.expr->to_string()); + type_name.loc = constparam.loc; + REDUCE_TO(variable_decl, name, type_name, syntax::expression{}); + return {.t = result::type::reduce_success}; +CHORD_END + +// iden : iden +// explicitly-typed variable declaration (of a type with constparams) with no initialiser +CHORD_BEGIN + STATE(NODE(identifier), TOKEN(col), NODE(identifier), TOKEN(oanglebrack), NODE(expression_list), TOKEN(canglebrack)) + + syntax::identifier name = GETNODE(identifier); + SETINDEX(2); + syntax::identifier struct_name = GETNODE(identifier); + SETINDEX(4); + syntax::expression_list constparams = GETNODE(expression_list); + + syntax::identifier type_name; + type_name.iden = std::format("{}<{}>", struct_name.iden, constparams.to_string()); + type_name.loc = constparams.loc; + REDUCE_TO(variable_decl, name, type_name, syntax::expression{}); + return {.t = result::type::reduce_success}; +CHORD_END + +// iden : expr // explicitly-typed variable declaration with no initialiser. CHORD_BEGIN - STATE(NODE(identifier), TOKEN(col), NODE(identifier)) + STATE(NODE(identifier), TOKEN(col), NODE(capped_expression)) syntax::identifier name = GETNODE(identifier); SETINDEX(2); - syntax::identifier type_name = GETNODE(identifier); + syntax::capped_expression expr = GETNODE(capped_expression); + + syntax::identifier type_name; + type_name.loc = expr.loc; + type_name.iden = expr.expr->to_string(); + REDUCE_TO(variable_decl, name, type_name, syntax::expression{}); return {.t = result::type::reduce_success}; CHORD_END diff --git a/cpp/src2/parse_chords/parse_token.inl b/cpp/src2/parse_chords/parse_token.inl index dde5963..f3ed216 100644 --- a/cpp/src2/parse_chords/parse_token.inl +++ b/cpp/src2/parse_chords/parse_token.inl @@ -28,7 +28,7 @@ CHORD_BEGIN SETINDEX(4); syntax::identifier return_type_name = GETNODE(identifier); - REDUCE_TO(function_decl, params, syntax::variable_decl_list{}, return_type_name); + REDUCE_TO(function_decl, syntax::variable_decl_list{}, params, return_type_name); return {.t = result::type::reduce_success}; CHORD_END @@ -44,7 +44,7 @@ CHORD_BEGIN std::vector params; params.push_back(param); - REDUCE_TO(function_decl, params, syntax::variable_decl_list{}, return_type_name); + REDUCE_TO(function_decl, syntax::variable_decl_list{}, params, return_type_name); return {.t = result::type::reduce_success}; CHORD_END diff --git a/samples/scratchpad.psy b/samples/scratchpad.psy index 9e9639f..bad5ef0 100644 --- a/samples/scratchpad.psy +++ b/samples/scratchpad.psy @@ -3,6 +3,11 @@ vec ::= struct data ::= T{}; }; +len ::= (vector : vec, multiplier : f32) -> f32 +{ + +} + [[entry]] main ::= () -> u0 {