Skip to content

Commit

Permalink
Merge pull request #22 from contour-terminal/improvement/cleanup
Browse files Browse the repository at this point in the history
Refactor IRGenerator::createCallArgs to be more code-reusing
  • Loading branch information
christianparpart authored Jan 20, 2024
2 parents faf1bc6 + c9b9836 commit 9790115
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/shell/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,37 +233,32 @@ export class IRGenerator final: public CoreVM::IRBuilder, public ast::Visitor

CoreVM::Value* toBool(CoreVM::Value* value) { return createNCmpEQ(value, get(CoreVM::CoreNumber(0))); }

std::vector<CoreVM::Constant*> createCallArgs(std::vector<std::unique_ptr<ast::Expr>> const& args)
static std::vector<CoreVM::Constant*> createArray(std::vector<std::unique_ptr<ast::Expr>> const& expressions)
{
TRACE_SCOPE("createCallArgs");
auto callArguments = std::vector<CoreVM::Constant*> {};
for (auto const& arg: args)
auto irArray = std::vector<CoreVM::Constant*> {};
for (auto const& expr: expressions)
{
TRACE_SCOPE(fmt::format("Parameter: ", ast::ASTPrinter::print(*arg)));
auto* value = codegen(arg.get());
if (auto* constant = dynamic_cast<CoreVM::Constant*>(value); constant != nullptr)
callArguments.push_back(constant);
TRACE_SCOPE(fmt::format("Parameter: ", ast::ASTPrinter::print(*expr)));
if (auto* constant = dynamic_cast<CoreVM::Constant*>(expr.get()); constant != nullptr)
irArray.push_back(constant);
else
fmt::print("Warning: non-constant argument passed to builtin function\n");
assert(!"TODO");
}
return callArguments;
return irArray;
}

std::vector<CoreVM::Constant*> createCallArgs(std::vector<std::unique_ptr<ast::Expr>> const& args)
{
TRACE_SCOPE("createCallArgs");
return IRGenerator::createArray(args);
}

std::vector<CoreVM::Constant*> createCallArgs(std::string const& programName,
std::vector<std::unique_ptr<ast::Expr>> const& args)
{
TRACE_SCOPE("createCallArgs");
auto callArguments = std::vector<CoreVM::Constant*> {};
callArguments.push_back(get(programName));
for (auto const& arg: args)
{
TRACE_SCOPE(fmt::format("Parameter: ", ast::ASTPrinter::print(*arg)));
auto* value = codegen(arg.get());
if (auto* constant = dynamic_cast<CoreVM::Constant*>(value); constant != nullptr)
callArguments.push_back(constant);
else
fmt::print("Warning: non-constant argument passed to builtin function\n");
}
auto callArguments = IRGenerator::createArray(args);
callArguments.insert(callArguments.begin(), get(programName));
return callArguments;
}

Expand Down

0 comments on commit 9790115

Please sign in to comment.