diff --git a/include/core.hpp b/include/core.hpp index edd3281..20e8b1e 100644 --- a/include/core.hpp +++ b/include/core.hpp @@ -73,6 +73,7 @@ namespace riddle * @return std::shared_ptr The int expression. */ [[nodiscard]] virtual std::shared_ptr new_int() = 0; + /** * @brief Create a new int expression with a value. * diff --git a/include/declaration.hpp b/include/declaration.hpp index 3fbb3cb..f476721 100644 --- a/include/declaration.hpp +++ b/include/declaration.hpp @@ -153,8 +153,8 @@ namespace riddle private: std::vector, id_token>> parameters; // the parameters of the constructor.. - std::vector inits; // the initializations of the fields.. - std::vector> body; // the body of the constructor.. + std::vector inits; // the initializations of the fields.. + std::vector> body; // the body of the constructor.. }; class method_declaration @@ -191,10 +191,10 @@ namespace riddle void refine(scope &scp) const; private: - std::vector return_type; // the return type of the method.. - id_token name; // the name of the method.. + std::vector return_type; // the return type of the method.. + id_token name; // the name of the method.. std::vector, id_token>> parameters; // the parameters of the method.. - std::vector> body; // the body of the method.. + std::vector> body; // the body of the method.. }; class predicate_declaration @@ -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, id_token>> parameters; // the type parameters of the class.. - std::vector> base_predicates; // the base predicates of the class.. - std::vector> body; // the body of the rule.. + std::vector> base_predicates; // the base predicates of the class.. + std::vector> body; // the body of the rule.. }; class class_declaration final : public type_declaration diff --git a/src/constructor.cpp b/src/constructor.cpp index 1a8ce25..de2a03d 100644 --- a/src/constructor.cpp +++ b/src/constructor.cpp @@ -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); } diff --git a/src/parser.cpp b/src/parser.cpp index 19f5fae..f6c530a 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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, id_token>> parameters; // the parameters of the constructor.. std::vector inits; // the initializations of the fields..