Skip to content

Commit

Permalink
Refactor dynamic_cast to use pointer instead of value in expression.c…
Browse files Browse the repository at this point in the history
…pp, constructor.cpp, and statement.cpp
  • Loading branch information
riccardodebenedictis committed Apr 16, 2024
1 parent c89fc15 commit f033d9a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace riddle
}

if (auto c = tp->get_constructor(arg_types))
instance->items.emplace(init.get_name().id, c.value().get().invoke(std::move(arguments)));
instance->items.emplace(init.get_name().id, c->get().invoke(std::move(arguments)));
else
throw std::runtime_error("Cannot find constructor for class " + init.get_name().id);
}
Expand All @@ -77,7 +77,7 @@ namespace riddle
}

if (auto c = (*st).get().get_constructor(arg_types))
instance->items.emplace(init.get_name().id, c.value().get().invoke(std::move(arguments)));
instance->items.emplace(init.get_name().id, c->get().invoke(std::move(arguments)));
else
throw std::runtime_error("Cannot find supertype " + init.get_name().id + ".");
}
Expand All @@ -104,7 +104,7 @@ namespace riddle
else if (auto c_tp = dynamic_cast<component_type *>(&f->get_type()))
{
if (auto c = c_tp->get_constructor({xpr->get_type()}))
instance->items.emplace(f_name, c.value().get().invoke({xpr}));
instance->items.emplace(f_name, c->get().invoke({xpr}));
else
throw std::runtime_error("Cannot find constructor for class " + f->get_type().get_name());
}
Expand All @@ -125,7 +125,7 @@ namespace riddle
if (auto c_tp = dynamic_cast<component_type *>(&f->get_type()))
{
if (auto c = c_tp->get_constructor(arg_types))
instance->items.emplace(f_name, c.value().get().invoke(std::move(arguments)));
instance->items.emplace(f_name, c->get().invoke(std::move(arguments)));
else
throw std::runtime_error("Cannot find constructor for class " + f->get_type().get_name());
}
Expand Down
36 changes: 18 additions & 18 deletions src/declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ namespace riddle
}
void enum_declaration::refine(scope &scp) const
{
auto et = static_cast<enum_type *>(&scp.get_type(name.id).value().get()); // cast is safe because the type was declared in the same scope
auto et = static_cast<enum_type *>(&scp.get_type(name.id)->get()); // cast is safe because the type was declared in the same scope
for (const auto &er : enum_refs)
{
auto t = scp.get_type(er[0].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(er[0].start_line) + ", " + std::to_string(er[0].start_pos) + "] type `" + er[0].id + "` not found");
for (size_t i = 1; i < er.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(er[i].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(er[i].start_line) + ", " + std::to_string(er[i].start_pos) + "] type `" + er[i].id + "` not found");
}
else
throw std::invalid_argument("[" + std::to_string(er[i].start_line) + ", " + std::to_string(er[i].start_pos) + "] `" + er[i].id + "` is not a component type");
if (auto c_et = dynamic_cast<enum_type *>(&t.value().get()))
if (auto c_et = dynamic_cast<enum_type *>(&t->get()))
et->enums.emplace_back(*c_et);
else
throw std::invalid_argument("[" + std::to_string(er[er.size() - 1].start_line) + ", " + std::to_string(er[er.size() - 1].start_pos) + "] `" + er[er.size() - 1].id + "` is not an enum type");
Expand All @@ -60,7 +60,7 @@ namespace riddle
if (!t)
throw std::invalid_argument("[" + std::to_string(type_ids[0].start_line) + ", " + std::to_string(type_ids[0].start_pos) + "] type `" + type_ids[0].id + "` not found");
for (size_t i = 1; i < type_ids.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(type_ids[i].id);
if (!t)
Expand All @@ -71,7 +71,7 @@ namespace riddle

for (const auto &init : inits)
{ // we create the field and add it to the scope..
auto f = std::make_unique<field>(t.value().get(), init.get_name().id, init.get_args());
auto f = std::make_unique<field>(t->get(), init.get_name().id, init.get_args());
if (auto ct = dynamic_cast<component_type *>(&scp))
ct->add_field(std::move(f));
else if (auto cr = dynamic_cast<core *>(&scp))
Expand All @@ -91,15 +91,15 @@ namespace riddle
if (!t)
throw std::invalid_argument("[" + std::to_string(tp_ids[0].start_line) + ", " + std::to_string(tp_ids[0].start_pos) + "] type `" + tp_ids[0].id + "` not found");
for (size_t i = 1; i < tp_ids.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(tp_ids[i].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(tp_ids[i].start_line) + ", " + std::to_string(tp_ids[i].start_pos) + "] type `" + tp_ids[i].id + "` not found");
}
else
throw std::invalid_argument("[" + std::to_string(tp_ids[i].start_line) + ", " + std::to_string(tp_ids[i].start_pos) + "] `" + tp_ids[i].id + "` is not a component type");
args.push_back(std::make_unique<field>(t.value().get(), id.id));
args.push_back(std::make_unique<field>(t->get(), id.id));
}

// we create the constructor and add it to the scope..
Expand All @@ -119,15 +119,15 @@ namespace riddle
if (!t)
throw std::invalid_argument("[" + std::to_string(return_type[0].start_line) + ", " + std::to_string(return_type[0].start_pos) + "] type `" + return_type[0].id + "` not found");
for (size_t i = 1; i < return_type.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(return_type[i].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(return_type[i].start_line) + ", " + std::to_string(return_type[i].start_pos) + "] type `" + return_type[i].id + "` not found");
}
else
throw std::invalid_argument("[" + std::to_string(return_type[i].start_line) + ", " + std::to_string(return_type[i].start_pos) + "] `" + return_type[i].id + "` is not a component type");
rt = t.value().get();
rt = t->get();
}

std::vector<std::unique_ptr<field>> pars; // the method's arguments
Expand All @@ -138,15 +138,15 @@ namespace riddle
if (!t)
throw std::invalid_argument("[" + std::to_string(tp_ids[0].start_line) + ", " + std::to_string(tp_ids[0].start_pos) + "] type `" + tp_ids[0].id + "` not found");
for (size_t i = 1; i < tp_ids.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(tp_ids[i].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(tp_ids[i].start_line) + ", " + std::to_string(tp_ids[i].start_pos) + "] type `" + tp_ids[i].id + "` not found");
}
else
throw std::invalid_argument("[" + std::to_string(tp_ids[i].start_line) + ", " + std::to_string(tp_ids[i].start_pos) + "] `" + tp_ids[i].id + "` is not a component type");
pars.push_back(std::make_unique<field>(t.value().get(), id.id));
pars.push_back(std::make_unique<field>(t->get(), id.id));
}

auto m = std::make_unique<method>(scp, rt, name.id, std::move(pars), std::move(body));
Expand All @@ -170,7 +170,7 @@ namespace riddle
}
void predicate_declaration::refine(scope &scp) const
{
auto p = static_cast<predicate *>(&scp.get_predicate(name.id).value().get()); // cast is safe because the predicate was declared in the same scope
auto p = static_cast<predicate *>(&scp.get_predicate(name.id)->get()); // cast is safe because the predicate was declared in the same scope

// the predicate's arguments
for (const auto &[tp_ids, id] : parameters)
Expand All @@ -179,15 +179,15 @@ namespace riddle
if (!t)
throw std::invalid_argument("[" + std::to_string(tp_ids[0].start_line) + ", " + std::to_string(tp_ids[0].start_pos) + "] type `" + tp_ids[0].id + "` not found");
for (size_t i = 1; i < tp_ids.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(tp_ids[i].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(tp_ids[i].start_line) + ", " + std::to_string(tp_ids[i].start_pos) + "] type `" + tp_ids[i].id + "` not found");
}
else
throw std::invalid_argument("[" + std::to_string(tp_ids[i].start_line) + ", " + std::to_string(tp_ids[i].start_pos) + "] `" + tp_ids[i].id + "` is not a component type");
p->add_field(std::make_unique<field>(t.value().get(), id.id));
p->add_field(std::make_unique<field>(t->get(), id.id));
}
}

Expand All @@ -207,15 +207,15 @@ namespace riddle
}
void class_declaration::refine(scope &scp) const
{
auto c_ct = static_cast<component_type *>(&scp.get_type(name.id).value().get()); // cast is safe because the type was declared in the same scope
auto c_ct = static_cast<component_type *>(&scp.get_type(name.id)->get()); // cast is safe because the type was declared in the same scope

for (const auto &tp : base_classes)
{
auto t = scp.get_type(tp[0].id);
if (!t)
throw std::invalid_argument("[" + std::to_string(tp[0].start_line) + ", " + std::to_string(tp[0].start_pos) + "] type `" + tp[0].id + "` not found");
for (size_t i = 1; i < tp.size(); ++i)
if (auto ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto ct = dynamic_cast<component_type *>(&t->get()))
{
t = ct->get_type(tp[i].id);
if (!t)
Expand All @@ -224,7 +224,7 @@ namespace riddle
else
throw std::invalid_argument("[" + std::to_string(tp[i].start_line) + ", " + std::to_string(tp[i].start_pos) + "] `" + tp[i].id + "` is not a component type");

if (auto c_ct = dynamic_cast<component_type *>(&t.value().get()))
if (auto c_ct = dynamic_cast<component_type *>(&t->get()))
c_ct->parents.emplace_back(*c_ct);
else
throw std::invalid_argument("[" + std::to_string(tp[tp.size() - 1].start_line) + ", " + std::to_string(tp[tp.size() - 1].start_pos) + "] `" + tp[tp.size() - 1].id + "` is not a component type");
Expand All @@ -248,7 +248,7 @@ namespace riddle
}
void class_declaration::refine_predicates(scope &scp) const
{
auto c_ct = static_cast<component_type *>(&scp.get_type(name.id).value().get()); // cast is safe because the type was declared in the same scope
auto c_ct = static_cast<component_type *>(&scp.get_type(name.id)->get()); // cast is safe because the type was declared in the same scope
// we refine the predicates..
for (const auto &p : predicates)
p->refine(*c_ct);
Expand Down
8 changes: 4 additions & 4 deletions src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ namespace riddle
auto tp_opt = scp.get_type(instance_type.front().id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + instance_type.front().id);
auto tp = dynamic_cast<component_type *>(&tp_opt.value().get());
auto tp = dynamic_cast<component_type *>(&tp_opt->get());
if (!tp)
throw std::runtime_error("Class " + instance_type.front().id + " is not a component type");
for (auto it = instance_type.begin() + 1; it != instance_type.end(); it++)
{
tp_opt = tp->get_type(it->id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + it->id);
tp = dynamic_cast<component_type *>(&tp_opt.value().get());
tp = dynamic_cast<component_type *>(&tp_opt->get());
if (!tp)
throw std::runtime_error("Class " + it->id + " is not a component type");
}
Expand All @@ -47,7 +47,7 @@ namespace riddle
}

if (auto c = tp->get_constructor(arg_types))
return c.value().get().invoke(std::move(arguments));
return c->get().invoke(std::move(arguments));
else
throw std::runtime_error("Cannot find constructor for class " + instance_type.front().id);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace riddle
if (!method_opt)
throw std::runtime_error("Cannot find method " + function_name.id);

return method_opt.value().get().invoke(c_env, std::move(arguments));
return method_opt->get().invoke(c_env, std::move(arguments));
}

std::shared_ptr<item> id_expression::evaluate(const scope &, std::shared_ptr<env> ctx) const
Expand Down
12 changes: 6 additions & 6 deletions src/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace riddle
auto tp_opt = scp.get_type(field_type.front().id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + field_type.front().id);
auto tp = &tp_opt.value().get();
auto tp = &tp_opt->get();
if (!tp)
throw std::runtime_error("Class " + field_type.front().id + " is not a component type");
for (auto it = field_type.begin() + 1; it != field_type.end(); it++)
Expand All @@ -20,7 +20,7 @@ namespace riddle
tp_opt = cmp_tp->get_type(it->id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + it->id);
tp = &tp_opt.value().get();
tp = &tp_opt->get();
}

for (const auto &field : fields)
Expand Down Expand Up @@ -112,15 +112,15 @@ namespace riddle
auto tp_opt = scp.get_type(enum_type.front().id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + enum_type.front().id);
auto tp = dynamic_cast<component_type *>(&tp_opt.value().get());
auto tp = dynamic_cast<component_type *>(&tp_opt->get());
if (!tp)
throw std::runtime_error("Class " + enum_type.front().id + " is not a component type");
for (auto it = enum_type.begin() + 1; it != enum_type.end(); it++)
{
tp_opt = tp->get_type(it->id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + it->id);
tp = dynamic_cast<component_type *>(&tp_opt.value().get());
tp = dynamic_cast<component_type *>(&tp_opt->get());
if (!tp)
throw std::runtime_error("Class " + it->id + " is not a component type");
}
Expand Down Expand Up @@ -165,15 +165,15 @@ namespace riddle
if (!pred_opt)
throw std::runtime_error("Cannot find predicate " + predicate_name.id);

auto &pred = pred_opt.value().get();
auto &pred = pred_opt->get();

std::map<std::string, std::shared_ptr<item>> args;
for (const auto &arg : arguments)
{
auto tp_opt = pred.get_field(arg.get_id().id);
if (!tp_opt)
throw std::runtime_error("Cannot find field " + arg.get_id().id);
auto &tp = tp_opt.value().get().get_type();
auto &tp = tp_opt->get().get_type();
auto val = arg.get_expression()->evaluate(scp, ctx);
if (tp.is_assignable_from(val->get_type())) // the target type is a superclass of the assignment..
args.emplace(arg.get_id().id, val);
Expand Down

0 comments on commit f033d9a

Please sign in to comment.