Skip to content

Commit

Permalink
Add to_string() implementation for typedef_declaration and enum_decla…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
riccardodebenedictis committed Jan 28, 2024
1 parent 68b7f0b commit 1f763d5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
35 changes: 33 additions & 2 deletions include/declaration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace riddle
public:
typedef_declaration(const id_token &name, const id_token &primitive_type, std::unique_ptr<expression> &&expr) : name(name), primitive_type(primitive_type), expr(std::move(expr)) {}

std::string to_string() const override { return ""; }
std::string to_string() const override { return "typedef " + primitive_type.to_string() + " " + name.to_string() + " = " + expr->to_string() + ";"; }

private:
id_token name;
Expand All @@ -30,7 +30,38 @@ namespace riddle
class enum_declaration final : public type_declaration
{
public:
std::string to_string() const override { return ""; }
enum_declaration(const id_token &name, std::vector<string_token> &&values, std::vector<std::vector<id_token>> &&enum_refs) : name(name), values(std::move(values)), enum_refs(std::move(enum_refs)) {}

std::string to_string() const override
{
std::string result = "enum " + name.to_string() + " {";
if (!values.empty())
{
result += '\"' + values[0].to_string() + '\"';
for (size_t i = 1; i < values.size(); ++i)
result += ", \"" + values[i].to_string() + '\"';
if (!enum_refs.empty())
result += ", ";
}
if (!enum_refs.empty())
{
result += enum_refs[0][0].to_string();
for (size_t i = 1; i < enum_refs[0].size(); ++i)
result += "." + enum_refs[0][i].to_string();
for (size_t i = 1; i < enum_refs.size(); ++i)
{
result += ", " + enum_refs[i][0].to_string();
for (size_t j = 1; j < enum_refs[i].size(); ++j)
result += "." + enum_refs[i][j].to_string();
}
}
return result + "}";
}

private:
id_token name;
std::vector<string_token> values;
std::vector<std::vector<id_token>> enum_refs;
};

class class_declaration final : public type_declaration
Expand Down
60 changes: 59 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,65 @@ namespace riddle

return std::make_unique<typedef_declaration>(name, *primitive_type, std::move(expr));
}
std::unique_ptr<enum_declaration> parser::parse_enum_declaration() {}
std::unique_ptr<enum_declaration> parser::parse_enum_declaration()
{
std::vector<string_token> values;
std::vector<std::vector<id_token>> enum_refs;

if (!match(ENUM_ID))
error("expected `enum`..");

if (!match(ID_ID))
error("expected identifier..");

auto name = *static_cast<const id_token *>(tokens[pos - 2].get());

do
{
switch (tk->sym)
{
case LBRACE_ID:
{
tk = next_token();
if (!match(StringLiteral_ID))
error("expected string literal..");
values.emplace_back(*static_cast<const string_token *>(tokens[pos - 2].get()));

while (match(COMMA_ID))
{
if (!match(StringLiteral_ID))
error("expected string literal..");
values.emplace_back(*static_cast<const string_token *>(tokens[pos - 2].get()));
}

if (!match(RBRACE_ID))
error("expected `}`..");
break;
}
case ID_ID:
{
std::vector<id_token> ids;
ids.emplace_back(*static_cast<const id_token *>(tk));
tk = next_token();
while (match(DOT_ID))
{
if (!match(ID_ID))
error("expected identifier..");
ids.emplace_back(*static_cast<const id_token *>(tokens[pos - 2].get()));
}
enum_refs.emplace_back(std::move(ids));
break;
}
default:
error("expected either `{` or identifier..");
}
} while (match(BAR_ID));

if (!match(SEMICOLON_ID))
error("expected `;`..");

return std::make_unique<enum_declaration>(name, std::move(values), std::move(enum_refs));
}
std::unique_ptr<class_declaration> parser::parse_class_declaration() {}
std::unique_ptr<field_declaration> parser::parse_field_declaration() {}
std::unique_ptr<method_declaration> parser::parse_method_declaration() {}
Expand Down

0 comments on commit 1f763d5

Please sign in to comment.