Replace some usages of ostringstream with std::format

This commit is contained in:
Nikita Lisitsa 2026-03-21 22:09:33 +03:00
parent 357baa9652
commit d363be0eef
3 changed files with 7 additions and 24 deletions

View file

@ -571,11 +571,7 @@ namespace pslang::ast
if (!node.arguments.empty())
{
if (node.arguments.size() != struct_node.fields.size())
{
std::ostringstream os;
os << "Cannot create struct " << named_type->name << ": expected " << struct_node.fields.size() << " arguments, but got " << node.arguments.size();
throw type_error(os.str(), node.location);
}
throw type_error(std::format("Cannot create struct {}: expected {} arguments, but got {}", named_type->name, struct_node.fields.size(), node.arguments.size()), node.location);
for (std::size_t i = 0; i < node.arguments.size(); ++i)
{
@ -688,9 +684,7 @@ namespace pslang::ast
}
}
std::ostringstream os;
os << "Struct \"" << named_type->name << "\" has no field named \"" << node.field_name << "\"";
throw type_error(os.str(), node.location);
throw type_error(std::format("Struct \"{}\" has no field named \"{}\"", named_type->name, node.field_name), node.location);
}
void apply(expression_ptr const & node)

View file

@ -8,6 +8,7 @@
#include <sstream>
#include <optional>
#include <format>
namespace pslang::interpreter
{
@ -67,21 +68,13 @@ namespace pslang::interpreter
if (index_unsigned)
{
if (*index_unsigned >= size)
{
std::ostringstream os;
os << "Array index " << *index_unsigned << " out of bounds " << size;
throw runtime_error(os.str(), location);
}
throw runtime_error(std::format("Array index {} out of bounds {}", *index_unsigned, size), location);
return *index_unsigned;
}
else // if (index_signed)
{
if (*index_signed < 0 || *index_signed >= size)
{
std::ostringstream os;
os << "Array index " << *index_signed << " out of bounds " << size;
throw runtime_error(os.str(), location);
}
throw runtime_error(std::format("Array index {} out of bounds {}", *index_signed, size), location);
return *index_signed;
}
}
@ -522,11 +515,7 @@ namespace pslang::interpreter
if (fcommon)
{
if (fcommon->arguments.size() != function_call.arguments.size())
{
std::ostringstream os;
os << "Cannot call function: expected " << fcommon->arguments.size() << " arguments, got " << function_call.arguments.size();
throw internal_error(os.str(), function_call.location);
}
throw internal_error(std::format("Cannot call function: expected {} arguments, got {}", fcommon->arguments.size(), function_call.arguments.size()), function_call.location);
std::vector<value> args;
for (auto const & expression : function_call.arguments)

View file

@ -41,6 +41,6 @@ General backlog:
* Empty array expression
* Calling functions as methods
* Replace std::runtime_error with appropriate custom exception types
* Replace std::ostringstream with std::format
* Replace std::ostringstream with std::format (need support for std::format in type/ast printing)
* TEST COVERAGE!!!
* Separate actual AST from pre-AST (the one before indentation & scoping is resolved)