Skip to content

Commit

Permalink
Refactor code to use string_view instead of string for field and item…
Browse files Browse the repository at this point in the history
… names in id_token constructor
  • Loading branch information
riccardodebenedictis committed Oct 9, 2024
1 parent 3eea508 commit 74653f8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace riddle
* @return std::shared_ptr<arith_item> The int expression.
*/
[[nodiscard]] virtual std::shared_ptr<arith_item> new_int() = 0;

/**
* @brief Create a new int expression with a value.
*
Expand Down
16 changes: 8 additions & 8 deletions include/declaration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ namespace riddle

private:
std::vector<std::pair<std::vector<id_token>, id_token>> parameters; // the parameters of the constructor..
std::vector<init_element> inits; // the initializations of the fields..
std::vector<std::unique_ptr<statement>> body; // the body of the constructor..
std::vector<init_element> inits; // the initializations of the fields..
std::vector<std::unique_ptr<statement>> body; // the body of the constructor..
};

class method_declaration
Expand Down Expand Up @@ -191,10 +191,10 @@ namespace riddle
void refine(scope &scp) const;

private:
std::vector<id_token> return_type; // the return type of the method..
id_token name; // the name of the method..
std::vector<id_token> return_type; // the return type of the method..
id_token name; // the name of the method..
std::vector<std::pair<std::vector<id_token>, id_token>> parameters; // the parameters of the method..
std::vector<std::unique_ptr<statement>> body; // the body of the method..
std::vector<std::unique_ptr<statement>> body; // the body of the method..
};

class predicate_declaration
Expand Down Expand Up @@ -238,10 +238,10 @@ namespace riddle
void refine(scope &scp) const;

private:
id_token name; // the name of the class..
id_token name; // the name of the class..
std::vector<std::pair<std::vector<id_token>, id_token>> parameters; // the type parameters of the class..
std::vector<std::vector<id_token>> base_predicates; // the base predicates of the class..
std::vector<std::unique_ptr<statement>> body; // the body of the rule..
std::vector<std::vector<id_token>> base_predicates; // the base predicates of the class..
std::vector<std::unique_ptr<statement>> body; // the body of the rule..
};

class class_declaration final : public type_declaration
Expand Down
6 changes: 4 additions & 2 deletions src/constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ namespace riddle
arguments.push_back(xpr);
}

if (auto c = tp->get_constructor(arg_types))
instance->items.emplace(init.get_name().id, c->get().invoke(std::move(arguments)));
if (arg_types.size() == 1 && f->get().get_type().is_assignable_from(arg_types.at(0)))
instance->items.emplace(init.get_name().id, arguments.at(0)); // we assign the argument to the field
else if (auto c = tp->get_constructor(arg_types))
instance->items.emplace(init.get_name().id, c->get().invoke(std::move(arguments))); // we invoke the constructor and assign the result to the field
else
throw std::runtime_error("Cannot find constructor for class " + init.get_name().id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ namespace riddle
error("expected either `typedef` or `enum` or `class` or `predicate` or `void` or identifier..");
}

if (constructors.empty())
if (constructors.empty()) // if there are no constructors, we add the default constructor..
{
std::vector<std::pair<std::vector<id_token>, id_token>> parameters; // the parameters of the constructor..
std::vector<init_element> inits; // the initializations of the fields..
Expand Down

0 comments on commit 74653f8

Please sign in to comment.