Skip to content

Commit ad9e42d

Browse files
committed
Runtime: implemented %d printf formatting
1 parent 664853d commit ad9e42d

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/runtime/PyString.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "NotImplementedError.hpp"
66
#include "PyBool.hpp"
77
#include "PyDict.hpp"
8+
#include "PyFloat.hpp"
89
#include "PyInteger.hpp"
910
#include "PyList.hpp"
1011
#include "PyNone.hpp"
@@ -15,11 +16,13 @@
1516
#include "runtime/PyBytes.hpp"
1617
#include "runtime/PyObject.hpp"
1718
#include "runtime/Value.hpp"
19+
#include "runtime/forward.hpp"
1820
#include "types/api.hpp"
1921
#include "types/builtin.hpp"
2022
#include "utilities.hpp"
2123

2224
#include <algorithm>
25+
#include <cstddef>
2326
#include <limits>
2427
#include <optional>
2528
#include <span>
@@ -1262,6 +1265,18 @@ PyResult<std::string> PyString::FormatSpec::apply(PyObject *obj) const
12621265
case 'r': {
12631266
return obj->repr().and_then([](PyString *str) { return Ok(str->value()); });
12641267
} break;
1268+
case 'd': {
1269+
if (obj->type()->issubclass(types::integer())) {
1270+
const auto &big_int = static_cast<const PyInteger &>(*obj).as_big_int();
1271+
return Ok(big_int.get_str(10));
1272+
} else if (obj->type()->issubclass(types::float_())) {
1273+
const auto &f = static_cast<const PyFloat &>(*obj).as_f64();
1274+
return Ok(BigIntType{ f }.get_str(10));
1275+
} else {
1276+
return Err(
1277+
type_error("%d format: a real number is required, not {}", obj->type()->name()));
1278+
}
1279+
} break;
12651280
default:
12661281
return Err(
12671282
not_implemented_error("printf conversion type '{}' not implemented", *conversion_type));

0 commit comments

Comments
 (0)