From d363be0eef4bf07f024b16d6cc343f6f2215cb17 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 21 Mar 2026 22:09:33 +0300 Subject: [PATCH] Replace some usages of ostringstream with std::format --- libs/ast/source/type_check.cpp | 10 ++-------- libs/interpreter/source/eval.cpp | 19 ++++--------------- plans.txt | 2 +- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/libs/ast/source/type_check.cpp b/libs/ast/source/type_check.cpp index bb68879..5dca68b 100644 --- a/libs/ast/source/type_check.cpp +++ b/libs/ast/source/type_check.cpp @@ -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) diff --git a/libs/interpreter/source/eval.cpp b/libs/interpreter/source/eval.cpp index 14129b5..52d7f12 100644 --- a/libs/interpreter/source/eval.cpp +++ b/libs/interpreter/source/eval.cpp @@ -8,6 +8,7 @@ #include #include +#include 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 args; for (auto const & expression : function_call.arguments) diff --git a/plans.txt b/plans.txt index c31087b..49f3cbf 100644 --- a/plans.txt +++ b/plans.txt @@ -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)