diff --git a/include/declaration.hpp b/include/declaration.hpp index 8059f30..8afa282 100644 --- a/include/declaration.hpp +++ b/include/declaration.hpp @@ -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 &&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 expr; }; class enum_declaration final : public type_declaration diff --git a/src/parser.cpp b/src/parser.cpp index 86300d3..174bacd 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -71,7 +71,45 @@ namespace riddle return std::make_unique(std::move(types), std::move(methods), std::move(predicates), std::move(statements)); } - std::unique_ptr parser::parse_typedef_declaration() {} + std::unique_ptr parser::parse_typedef_declaration() + { + std::unique_ptr primitive_type; + std::unique_ptr expr; + + switch (tk->sym) + { + case BOOL_ID: + primitive_type = std::make_unique(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string()); + break; + case INT_ID: + primitive_type = std::make_unique(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string()); + break; + case REAL_ID: + primitive_type = std::make_unique(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string()); + break; + case TIME_ID: + primitive_type = std::make_unique(tk->start_line, tk->start_pos, tk->end_line, tk->end_pos, tk->to_string()); + break; + case STRING_ID: + primitive_type = std::make_unique(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(tokens[pos - 2].get()); + + if (!match(SEMICOLON_ID)) + error("expected `;`.."); + + return std::make_unique(name, *primitive_type, std::move(expr)); + } std::unique_ptr parser::parse_enum_declaration() {} std::unique_ptr parser::parse_class_declaration() {} std::unique_ptr parser::parse_field_declaration() {}