Skip to content

Commit

Permalink
Add typedef_declaration parsing in parser.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Jan 28, 2024
1 parent 7c79a7a commit 68b7f0b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions include/declaration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ namespace riddle
class typedef_declaration final : public type_declaration
{
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 ""; }

private:
id_token name;
id_token primitive_type;
std::unique_ptr<expression> expr;
};

class enum_declaration final : public type_declaration
Expand Down
40 changes: 39 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,45 @@ namespace riddle
return std::make_unique<compilation_unit>(std::move(types), std::move(methods), std::move(predicates), std::move(statements));
}

std::unique_ptr<typedef_declaration> parser::parse_typedef_declaration() {}
std::unique_ptr<typedef_declaration> parser::parse_typedef_declaration()
{
std::unique_ptr<id_token> primitive_type;
std::unique_ptr<expression> expr;

switch (tk->sym)
{
case BOOL_ID:
primitive_type = std::make_unique<id_token>(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string());
break;
case INT_ID:
primitive_type = std::make_unique<id_token>(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string());
break;
case REAL_ID:
primitive_type = std::make_unique<id_token>(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string());
break;
case TIME_ID:
primitive_type = std::make_unique<id_token>(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string());
break;
case STRING_ID:
primitive_type = std::make_unique<id_token>(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string());
break;
default:
error("expected either `bool` or `int` or `real` or `time` or `string`..");
}
tk = next_token();

expr = parse_expression();

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

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

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

return std::make_unique<typedef_declaration>(name, *primitive_type, std::move(expr));
}
std::unique_ptr<enum_declaration> parser::parse_enum_declaration() {}
std::unique_ptr<class_declaration> parser::parse_class_declaration() {}
std::unique_ptr<field_declaration> parser::parse_field_declaration() {}
Expand Down

0 comments on commit 68b7f0b

Please sign in to comment.