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

View file

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

View file

@ -41,6 +41,6 @@ General backlog:
* Empty array expression * Empty array expression
* Calling functions as methods * Calling functions as methods
* Replace std::runtime_error with appropriate custom exception types * 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!!! * TEST COVERAGE!!!
* Separate actual AST from pre-AST (the one before indentation & scoping is resolved) * Separate actual AST from pre-AST (the one before indentation & scoping is resolved)