-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.cpp
More file actions
358 lines (297 loc) · 8.85 KB
/
Copy pathoutput.cpp
File metadata and controls
358 lines (297 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "math.h"
#include "output.h"
#include "vm.h"
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
void NumberOutput::init() {
memset(vm.number_output_data, BL, NUMBER_OUTPUT_SZ);
start();
}
void NumberOutput::start() {
vm.number_output_ptr = NUMBER_OUTPUT_SZ;
}
void NumberOutput::add_digit() {
// compute digit
udint value = dpop();
// can use % instead of f_mod because value is assumed positive
int digit = static_cast<int>(value % vm.user->BASE);
value /= vm.user->BASE;
dpush(value);
// output it
if (digit < 10) {
add_char('0' + digit);
}
else {
add_char('A' + digit - 10);
}
}
void NumberOutput::add_digits() {
dint value;
do {
add_digit();
value = dpeek();
}
while (value != 0);
}
void NumberOutput::add_char(char c) {
if (vm.number_output_ptr < 1) {
error(Error::PicturedNumericOutputStringOverflow);
}
else {
vm.number_output_data[--vm.number_output_ptr] = c;
}
}
void NumberOutput::add_sign(int sign) {
if (sign < 0) {
add_char('-');
}
}
void NumberOutput::add_string(const std::string& str) {
add_string(str.c_str(), static_cast<uint>(str.size()));
}
void NumberOutput::add_string(const char* str, uint size) {
for (int i = size - 1; i >= 0; --i) {
add_char(str[i]);
}
}
void NumberOutput::end() const {
dpop(); // drop number
push(mem_addr(vm.number_output_data + vm.number_output_ptr));
push(NUMBER_OUTPUT_SZ - vm.number_output_ptr);
}
void NumberOutput::end_print() const {
dpop(); // drop number
const char* str = vm.number_output_data + vm.number_output_ptr;
uint size = NUMBER_OUTPUT_SZ - vm.number_output_ptr;
print_string(str, size);
}
static std::string print_dint_uint(int sign) {
vm.number_output.start();
vm.number_output.add_char(BL);
vm.number_output.add_digits();
vm.number_output.add_sign(sign);
vm.number_output.end();
uint size = pop();
uint addr = pop();
char* str = mem_char_ptr(addr, size);
return std::string(str, str + size);
}
static std::string print_dint_uint_aligned(int width, int sign) {
vm.number_output.start();
dint d;
do {
vm.number_output.add_digit();
width--;
d = dpeek();
}
while (d != 0);
if (sign < 0) {
vm.number_output.add_sign(sign);
width--;
}
while (width-- > 0) {
vm.number_output.add_char(BL);
}
vm.number_output.end();
uint size = pop();
uint addr = pop();
char* str = mem_char_ptr(addr, size);
return std::string(str, str + size);
}
std::string char_to_string(char c) {
std::ostringstream oss;
oss << c;
return oss.str();
}
void print_char(char c) {
std::cout << char_to_string(c);
}
void print_string(const std::string& str) {
for (char c : str) {
print_char(c);
}
}
std::string spaces_to_string(int count) {
if (count < 1) {
return "";
}
else {
return std::string(count, BL);
}
}
void print_spaces(int count) {
print_string(spaces_to_string(count));
}
std::string string_to_string(uint addr, uint size) {
return string_to_string(mem_char_ptr(addr, size), size);
}
std::string string_to_string(const char* str, uint size) {
std::ostringstream oss;
for (uint i = 0; i < size; ++i) {
oss << str[i];
}
return oss.str();
}
void print_string(uint addr, uint size) {
print_string(mem_char_ptr(addr, size), size);
}
void print_string(const char* str, uint size) {
print_string(string_to_string(str, size));
}
std::string number_to_string(int value) {
dpush(std::abs(static_cast<dint>(value)));
int sign = value;
return print_dint_uint(sign);
}
void print_number(int value) {
print_string(number_to_string(value));
}
std::string number_to_string(dint value) {
dpush(std::abs(value));
int sign = value < 0 ? -1 : 1;
return print_dint_uint(sign);
}
void print_number(dint value) {
print_string(number_to_string(value));
}
std::string number_to_string(double value) {
std::ostringstream oss;
oss << std::setprecision(vm.precision - 1)
<< (std::fabs(value) < EPSILON ? 0 : value);
return oss.str();
}
void print_number(double value) {
print_string( number_to_string(value));
}
std::string number_fixed_to_string(double value) {
std::ostringstream oss;
oss << std::fixed << std::setprecision(vm.precision - 1)
<< (std::fabs(value) < EPSILON ? 0 : value);
std::string number = oss.str();
if (number.find('.') == std::string::npos) {
number.push_back('.');
}
// Remove trailing zeros
number.erase(number.find_last_not_of('0') + 1);
number.push_back(BL);
return number;
}
void print_number_fixed(double value) {
print_string(number_fixed_to_string(value));
}
std::string number_engineering_to_string(double value) {
if (std::fabs(value) < EPSILON) {
return "0. ";
}
else {
int exponent = static_cast<int>(std::floor(std::log10(std::fabs(value))));
int eng_exponent = exponent >= 0
? exponent - (exponent % 3)
: exponent - ((exponent % 3 + 3) % 3);
double significand = value / std::pow(10.0, eng_exponent);
std::ostringstream oss;
oss << std::fixed << std::setprecision(vm.precision - 1)
<< significand;
// Trim trailing zeros and decimal point
std::string significand_str = oss.str();
significand_str.erase(significand_str.find_last_not_of('0') + 1);
if (significand_str.back() == '.') {
significand_str.pop_back();
}
std::ostringstream result;
result << significand_str << "E" << eng_exponent << BL;
return result.str();
}
}
void print_number_engineering(double value) {
print_string(number_engineering_to_string(value));
}
std::string number_scientific_to_string(double value) {
if (std::fabs(value) < EPSILON) {
return "0E+0 ";
}
else {
int exponent = static_cast<int>(std::floor(std::log10(std::fabs(value))));
double significand = value / std::pow(10.0, exponent);
std::ostringstream oss;
oss << std::fixed << std::setprecision(vm.precision - 1)
<< significand;
// Trim trailing zeros and decimal point
std::string significand_str = oss.str();
significand_str.erase(significand_str.find_last_not_of('0') + 1);
if (significand_str.back() == '.') {
significand_str.pop_back();
}
std::ostringstream result;
result << significand_str << "E" << (exponent >= 0 ? "+" : "") << exponent <<
" ";
return result.str();
return std::string();
}
}
void print_number_scientific(double value) {
print_string(number_scientific_to_string(value));
}
std::string number_dot_to_string(dint value) {
std::string number = number_to_string(value, 0);
number.push_back('.');
number.push_back(BL);
return number;
}
void print_number_dot(dint value) {
print_string(number_dot_to_string(value));
}
std::string number_e_to_string(double value) {
std::ostringstream oss;
oss << std::setprecision(vm.precision - 1)
<< (std::fabs(value) < EPSILON ? 0 : value);
std::string number = oss.str();
if (number.find('e') == std::string::npos &&
number.find('E') == std::string::npos) {
number.push_back('e');
}
number.push_back(BL);
return number;
}
void print_number_e(double value) {
print_string(number_e_to_string(value));
}
std::string number_to_string(int value, int width) {
dpush(std::abs(value));
return print_dint_uint_aligned(width, value);
}
void print_number(int value, int width) {
print_string(number_to_string(value, width));
}
std::string number_to_string(dint value, int width) {
dpush(std::abs(value));
int sign = value < 0 ? -1 : 1;
return print_dint_uint_aligned(width, sign);
}
void print_number(dint value, int width) {
print_string(number_to_string(value, width));
}
std::string unsigned_number_to_string(uint value) {
push(value); // lo
push(0); // hi
return print_dint_uint(+1);
}
void print_unsigned_number(uint value) {
print_string(unsigned_number_to_string(value));
}
std::string unsigned_number_to_string(uint value, int width) {
push(value);
push(0);
return print_dint_uint_aligned(width, 1);
}
void print_unsigned_number(uint value, int width) {
print_string(unsigned_number_to_string(value, width));
}