Skip to content

Commit

Permalink
[ast] ast node to_string rework to no longer contain noisy labels - i…
Browse files Browse the repository at this point in the history
…nfact now allowed to be directly used to convert an expression to a string for example
  • Loading branch information
harrand committed Nov 30, 2024
1 parent d97e7be commit f0e2513
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cpp/src2/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace syntax

std::string expression::to_string() const
{
return std::format("expr-{}({}{})", expression::type_names[static_cast<int>(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):
Expand Down
29 changes: 13 additions & 16 deletions cpp/src2/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -110,7 +110,7 @@ namespace syntax

std::string to_string() const
{
return std::format("integer-literal({})", this->val);
return std::format("{}", this->val);
}
};

Expand All @@ -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
{
Expand All @@ -138,7 +138,7 @@ namespace syntax

std::string to_string() const
{
return std::format("char-literal('{}' (dec {})", this->val, static_cast<int>(this->val));
return std::format("'{}'", this->val, static_cast<int>(this->val));
}
};

Expand All @@ -162,7 +162,7 @@ namespace syntax

std::string to_string() const
{
return std::format("string-literal(\"{}\")", this->val);
return std::format("\"{}\"", this->val);
}
};

Expand All @@ -173,7 +173,7 @@ namespace syntax

std::string to_string() const
{
return "null-literal()";
return "null";
}
};

Expand All @@ -187,7 +187,7 @@ namespace syntax

std::string to_string() const
{
return std::format("identifier({})", this->iden);
return this->iden;
}
};

Expand Down Expand Up @@ -308,7 +308,7 @@ namespace syntax
contents += ", ";
}
}
return std::format("expr-list({})", contents);
return std::format("{}", contents);
}
};

Expand All @@ -328,7 +328,7 @@ namespace syntax
contents += ", ";
}
}
return std::format("annotations({})", contents);
return std::format("{}", contents);
}
};

Expand Down Expand Up @@ -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()));
}
};

Expand Down Expand Up @@ -408,7 +406,7 @@ namespace syntax
contents += ", ";
}
}
return std::format("variable-decl-list({})", contents);
return std::format("{}", contents);
}
};

Expand All @@ -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" : "");
}
};

Expand All @@ -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());
}
};

Expand Down
47 changes: 44 additions & 3 deletions cpp/src2/parse_chords/parse_identifier.inl
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
// iden : iden
// iden : iden<expr>
// 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<expr-list>
// 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
Expand Down
4 changes: 2 additions & 2 deletions cpp/src2/parse_chords/parse_token.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -44,7 +44,7 @@ CHORD_BEGIN
std::vector<syntax::variable_decl> 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

Expand Down
5 changes: 5 additions & 0 deletions samples/scratchpad.psy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ vec ::= struct<T : type, S : u64>
data ::= T{};
};

len ::= (vector : vec<f32, 3>, multiplier : f32) -> f32
{

}

[[entry]]
main ::= () -> u0
{
Expand Down

0 comments on commit f0e2513

Please sign in to comment.