SMTStabilizer API
Public API documentation for SMTStabilizer
Loading...
Searching...
No Matches
value.cpp
Go to the documentation of this file.
1/* -*- Source -*-
2 *
3 * The Util Functions
4 *
5 * Author: Fuqi Jia <jiafq@ios.ac.cn>
6 *
7 * Copyright (C) 2025 Fuqi Jia
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27
28// Modified by Xiang Zhang, 2026
29// Additional changes licensed under the MIT License
30#include "value.h"
31
32#include <algorithm>
33#include <cctype>
34#include <stdexcept>
35
36namespace stabilizer::parser {
37// Default constructor
38Value::Value() : value_type(UNKNOWN) {}
39
40// Copy constructor
41Value::Value(const Value &other)
42 : string_value(other.string_value), number_value(other.number_value), interval_value(other.interval_value), boolean_value(other.boolean_value), value_type(other.value_type) {}
43
44// Assignment operator
46 if (this != &other) {
51 value_type = other.value_type;
52 }
53 return *this;
54}
55
56// Destructor
58
59// Constructors for different types
60Value::Value(const std::string &string_value)
61 : string_value(string_value), value_type(STRING) {}
62
63Value::Value(const Number &number_value)
64 : number_value(number_value), value_type(NUMBER) {}
65
66Value::Value(const Interval &interval_value)
67 : interval_value(interval_value), value_type(INTERVAL) {}
68
69Value::Value(const bool &boolean_value)
70 : boolean_value(boolean_value), value_type(BOOLEAN) {}
71
72Value::Value(const ValueType &value_type) : value_type(value_type) {}
73
74// Setter methods
75void Value::setValue(const std::string &string_value) {
76 this->string_value = string_value;
77 this->value_type = STRING;
78}
79
80void Value::setValue(const Number &number_value) {
81 this->number_value = number_value;
82 this->value_type = NUMBER;
83}
84
85void Value::setValue(const Interval &interval_value) {
86 this->interval_value = interval_value;
87 this->value_type = INTERVAL;
88}
89
90void Value::setValue(const bool &boolean_value) {
91 this->boolean_value = boolean_value;
92 this->value_type = BOOLEAN;
93}
94
95// Getter methods
97
98std::string Value::getStringValue() const {
99 if (value_type != STRING) {
100 throw std::runtime_error("Value is not a string");
101 }
102 return string_value;
103}
104
106 if (value_type != NUMBER) {
107 throw std::runtime_error("Value is not a number");
108 }
109 return number_value;
110}
111
113 if (value_type != INTERVAL) {
114 throw std::runtime_error("Value is not an interval");
115 }
116 return interval_value;
117}
118
120 if (value_type != BOOLEAN) {
121 throw std::runtime_error("Value is not a boolean");
122 }
123 return boolean_value;
124}
125
126// Assignment operators for different types
127Value &Value::operator=(const std::string &string_value) {
128 this->string_value = string_value;
129 this->value_type = STRING;
130 return *this;
131}
132
133Value &Value::operator=(const Number &number_value) {
134 this->number_value = number_value;
135 this->value_type = NUMBER;
136 return *this;
137}
138
139Value &Value::operator=(const Interval &interval_value) {
140 this->interval_value = interval_value;
141 this->value_type = INTERVAL;
142 return *this;
143}
144
145Value &Value::operator=(const bool &boolean_value) {
146 this->boolean_value = boolean_value;
147 this->value_type = BOOLEAN;
148 return *this;
149}
150
151// toString method
152std::string Value::toString() const {
153 switch (value_type) {
154 case STRING:
155 return string_value;
156 case NUMBER:
157 return number_value.toString();
158 case INTERVAL:
159 return interval_value.toString();
160 case BOOLEAN:
161 return boolean_value ? "true" : "false";
162 default:
163 return "unknown";
164 }
165}
166
167// Factory functions to create shared_ptr<Value>
168std::shared_ptr<Value> newValue(const std::string &string_value) {
169 return std::make_shared<Value>(string_value);
170}
171
172std::shared_ptr<Value> newValue(const Number &number_value) {
173 return std::make_shared<Value>(number_value);
174}
175
176std::shared_ptr<Value> newValue(const Interval &interval_value) {
177 return std::make_shared<Value>(interval_value);
178}
179
180std::shared_ptr<Value> newValue(const bool &boolean_value) {
181 return std::make_shared<Value>(boolean_value);
182}
183
184std::shared_ptr<Value> newValue(const ValueType &value_type) {
185 return std::make_shared<Value>(value_type);
186}
187
188std::shared_ptr<Value> newValue(const int &integer_value) {
189 return std::make_shared<Value>(Number(integer_value));
190}
191
192std::shared_ptr<Value> newValue(const double &double_value) {
193 return std::make_shared<Value>(Number(double_value));
194}
195
196std::shared_ptr<Value> newValue(const float &float_value) {
197 return std::make_shared<Value>(Number(static_cast<double>(float_value)));
198}
199
200std::shared_ptr<Value> newValue(const long &long_value) {
201 return std::make_shared<Value>(Number(static_cast<int>(long_value)));
202}
203
204std::shared_ptr<Value> newValue(const short &short_value) {
205 return std::make_shared<Value>(Number(static_cast<int>(short_value)));
206}
207
208std::shared_ptr<Value> newValue(const char &char_value) {
209 return std::make_shared<Value>(Number(static_cast<int>(char_value)));
210}
211
212// Comparison operators
213bool Value::operator==(const Value &other) const {
214 if (value_type != other.value_type) {
215 return false; // Different types are not equal
216 }
217
218 switch (value_type) {
219 case STRING:
220 return string_value == other.string_value;
221 case NUMBER:
222 return number_value == other.number_value;
223 case INTERVAL:
224 return interval_value == other.interval_value;
225 case BOOLEAN:
226 return boolean_value == other.boolean_value;
227 default:
228 return false;
229 }
230}
231
232bool Value::operator!=(const Value &other) const { return !(*this == other); }
233
234bool Value::operator<(const Value &other) const {
235 if (value_type != other.value_type) {
236 throw std::runtime_error("Cannot compare values of different types");
237 }
238
239 switch (value_type) {
240 case STRING:
241 return string_value < other.string_value;
242 case NUMBER:
243 return number_value < other.number_value;
244 case INTERVAL:
245 return interval_value < other.interval_value;
246 case BOOLEAN:
247 return boolean_value < other.boolean_value;
248 default:
249 throw std::runtime_error("Cannot compare values of this type");
250 }
251}
252
253bool Value::operator<=(const Value &other) const {
254 return (*this < other) || (*this == other);
255}
256
257bool Value::operator>(const Value &other) const { return !(*this <= other); }
258
259bool Value::operator>=(const Value &other) const { return !(*this < other); }
260
261// Arithmetic operators
262Value Value::operator+(const Value &other) const {
263 // Type checking and conversion
264 if (value_type == NUMBER && other.value_type == NUMBER) {
265 return Value(number_value + other.number_value);
266 }
267 else if (value_type == INTERVAL && other.value_type == INTERVAL) {
268 return Value(interval_value + other.interval_value);
269 }
270 else if (value_type == STRING && other.value_type == STRING) {
271 return Value(string_value + other.string_value); // String concatenation
272 }
273 else {
274 throw std::runtime_error("Cannot add values of these types");
275 }
276}
277
278Value Value::operator-(const Value &other) const {
279 // Type checking
280 if (value_type == NUMBER && other.value_type == NUMBER) {
281 return Value(number_value - other.number_value);
282 }
283 else if (value_type == INTERVAL && other.value_type == INTERVAL) {
284 return Value(interval_value - other.interval_value);
285 }
286 else {
287 throw std::runtime_error("Cannot subtract values of these types");
288 }
289}
290
291Value Value::operator*(const Value &other) const {
292 // Type checking
293 if (value_type == NUMBER && other.value_type == NUMBER) {
294 return Value(number_value * other.number_value);
295 }
296 else if (value_type == INTERVAL && other.value_type == INTERVAL) {
297 return Value(interval_value * other.interval_value);
298 }
299 else {
300 throw std::runtime_error("Cannot multiply values of these types");
301 }
302}
303
304Value Value::operator/(const Value &other) const {
305 // Type checking and division by zero
306 if (value_type == NUMBER && other.value_type == NUMBER) {
307 return Value(number_value / other.number_value);
308 }
309 else if (value_type == INTERVAL && other.value_type == INTERVAL) {
310 return Value(interval_value / other.interval_value);
311 }
312 else {
313 throw std::runtime_error("Cannot divide values of these types");
314 }
315}
316
317Value Value::operator%(const Value &other) const {
318 // Type checking
319 if (value_type == NUMBER && other.value_type == NUMBER) {
320 return Value(number_value % other.number_value);
321 }
322 else if (value_type == INTERVAL && other.value_type == INTERVAL) {
323 return Value(interval_value.mod(other.interval_value));
324 }
325 else {
326 throw std::runtime_error("Cannot perform modulo on values of these types");
327 }
328}
329
330// Mathematical functions
332 switch (value_type) {
333 case NUMBER:
334 return Value(number_value.sin());
335 case INTERVAL:
336 return Value(interval_value.sin());
337 default:
338 throw std::runtime_error("Cannot compute sine of this type");
339 }
340}
341
343 switch (value_type) {
344 case NUMBER:
345 return Value(number_value.cos());
346 case INTERVAL:
347 return Value(interval_value.cos());
348 default:
349 throw std::runtime_error("Cannot compute cosine of this type");
350 }
351}
352
354 switch (value_type) {
355 case NUMBER:
356 return Value(number_value.tan());
357 case INTERVAL:
358 return Value(interval_value.tan());
359 default:
360 throw std::runtime_error("Cannot compute tangent of this type");
361 }
362}
363
365 switch (value_type) {
366 case NUMBER:
367 return Value(number_value.sqrt());
368 case INTERVAL:
369 return Value(interval_value.sqrt());
370 default:
371 throw std::runtime_error("Cannot compute square root of this type");
372 }
373}
374
376 switch (value_type) {
377 case NUMBER:
378 return Value(number_value.exp());
379 case INTERVAL:
380 return Value(interval_value.exp());
381 default:
382 throw std::runtime_error("Cannot compute exponential of this type");
383 }
384}
385
387 switch (value_type) {
388 case NUMBER:
389 return Value(number_value.ln());
390 case INTERVAL:
391 return Value(interval_value.ln());
392 default:
393 throw std::runtime_error("Cannot compute natural logarithm of this type");
394 }
395}
396
398 switch (value_type) {
399 case NUMBER:
400 return Value(number_value.abs());
401 case INTERVAL:
402 return Value(interval_value.abs());
403 default:
404 throw std::runtime_error("Cannot compute absolute value of this type");
405 }
406}
407
408Value Value::pow(const Value &other) const {
409 // Type checking
410 if (value_type == NUMBER && other.value_type == NUMBER) {
411 return Value(number_value.pow(other.number_value));
412 }
413 else if (value_type == INTERVAL && other.value_type == INTERVAL) {
414 return Value(interval_value.pow(other.interval_value));
415 }
416 else if (value_type == INTERVAL && other.value_type == NUMBER) {
417 return Value(interval_value.pow(other.number_value));
418 }
419 else {
420 throw std::runtime_error("Cannot compute power with these types");
421 }
422}
423
424// Logical operators
425bool Value::operator&&(const Value &other) const {
426 if (value_type != BOOLEAN || other.value_type != BOOLEAN) {
427 throw std::runtime_error("Logical AND requires boolean operands");
428 }
429 return boolean_value && other.boolean_value;
430}
431
432bool Value::operator||(const Value &other) const {
433 if (value_type != BOOLEAN || other.value_type != BOOLEAN) {
434 throw std::runtime_error("Logical OR requires boolean operands");
435 }
436 return boolean_value || other.boolean_value;
437}
438
439bool Value::operator!() const {
440 if (value_type != BOOLEAN) {
441 throw std::runtime_error("Logical NOT requires a boolean operand");
442 }
443 return !boolean_value;
444}
445
446// Assignment arithmetic operators
448 *this = *this + other;
449 return *this;
450}
451
453 *this = *this - other;
454 return *this;
455}
456
458 *this = *this * other;
459 return *this;
460}
461
463 *this = *this / other;
464 return *this;
465}
466
468 *this = *this % other;
469 return *this;
470}
471
472// Bitwise operators
473Value Value::operator^(const Value &other) const {
474 if (value_type != NUMBER || other.value_type != NUMBER) {
475 throw std::runtime_error("Bitwise XOR requires number operands");
476 }
477 throw std::runtime_error(
478 "Bitwise XOR operation not implemented for Number class");
479}
480
481Value Value::operator&(const Value &other) const {
482 if (value_type != NUMBER || other.value_type != NUMBER) {
483 throw std::runtime_error("Bitwise AND requires number operands");
484 }
485 throw std::runtime_error(
486 "Bitwise AND operation not implemented for Number class");
487}
488
489Value Value::operator|(const Value &other) const {
490 if (value_type != NUMBER || other.value_type != NUMBER) {
491 throw std::runtime_error("Bitwise OR requires number operands");
492 }
493 throw std::runtime_error(
494 "Bitwise OR operation not implemented for Number class");
495}
496
497Value Value::operator<<(const Value &other) const {
498 if (value_type != NUMBER || other.value_type != NUMBER) {
499 throw std::runtime_error("Left shift requires number operands");
500 }
501 throw std::runtime_error(
502 "Left shift operation not implemented for Number class");
503}
504
505Value Value::operator>>(const Value &other) const {
506 if (value_type != NUMBER || other.value_type != NUMBER) {
507 throw std::runtime_error("Right shift requires number operands");
508 }
509 throw std::runtime_error(
510 "Right shift operation not implemented for Number class");
511}
512
513// Increment and decrement
515 if (value_type != NUMBER && value_type != INTERVAL) {
516 throw std::runtime_error("Increment requires a number or interval");
517 }
518 if (value_type == NUMBER) {
520 }
521 else {
522 interval_value = interval_value.operator++();
523 }
524 return *this;
525}
526
528 if (value_type != NUMBER && value_type != INTERVAL) {
529 throw std::runtime_error("Decrement requires a number or interval");
530 }
531 if (value_type == NUMBER) {
533 }
534 else {
535 interval_value = interval_value.operator--();
536 }
537 return *this;
538}
539
541 Value old = *this;
542 ++(*this);
543 return old;
544}
545
547 Value old = *this;
548 --(*this);
549 return old;
550}
551
552// Unary operators
554 if (value_type == NUMBER) {
555 throw std::runtime_error(
556 "Bitwise NOT operation not implemented for Number class");
557 }
558 else if (value_type == INTERVAL) {
559 return Value(interval_value.operator~());
560 }
561 else {
562 throw std::runtime_error("Bitwise NOT requires a number or interval");
563 }
564}
565
567 if (value_type == NUMBER) {
568 return Value(-number_value);
569 }
570 else if (value_type == INTERVAL) {
571 return Value(interval_value.negate());
572 }
573 else {
574 throw std::runtime_error("Negation requires a number or interval");
575 }
576}
577
578// Additional mathematical functions
580 switch (value_type) {
581 case NUMBER:
582 return Value(number_value.safeSqrt());
583 case INTERVAL:
584 return Value(interval_value.safeSqrt());
585 default:
586 throw std::runtime_error("Cannot compute safe square root of this type");
587 }
588}
589
591 switch (value_type) {
592 case NUMBER:
593 return Value(number_value.lg());
594 case INTERVAL:
595 return Value(interval_value.lg());
596 default:
597 throw std::runtime_error("Cannot compute base-10 logarithm of this type");
598 }
599}
600
602 switch (value_type) {
603 case NUMBER:
604 return Value(number_value.lb());
605 case INTERVAL:
606 return Value(interval_value.lb());
607 default:
608 throw std::runtime_error("Cannot compute base-2 logarithm of this type");
609 }
610}
611
612Value Value::log(const Value &other) const {
613 if (value_type == NUMBER && other.value_type == NUMBER) {
614 return Value(number_value.log(other.number_value));
615 }
616 else {
617 throw std::runtime_error(
618 "Logarithm with custom base not implemented for intervals");
619 }
620}
621
623 switch (value_type) {
624 case NUMBER:
625 return Value(number_value.ceil());
626 case INTERVAL:
627 // Interval ceil would need specific implementation if needed
628 throw std::runtime_error("Ceiling function not implemented for intervals");
629 default:
630 throw std::runtime_error("Cannot compute ceiling of this type");
631 }
632}
633
635 switch (value_type) {
636 case NUMBER:
637 return Value(number_value.floor());
638 case INTERVAL:
639 // Interval floor would need specific implementation if needed
640 throw std::runtime_error("Floor function not implemented for intervals");
641 default:
642 throw std::runtime_error("Cannot compute floor of this type");
643 }
644}
645
647 switch (value_type) {
648 case NUMBER:
649 return Value(number_value.round());
650 case INTERVAL:
651 // Interval round would need specific implementation if needed
652 throw std::runtime_error("Round function not implemented for intervals");
653 default:
654 throw std::runtime_error("Cannot round this type");
655 }
656}
657
658// Trigonometric functions
660 switch (value_type) {
661 case NUMBER:
662 return Value(number_value.cot());
663 case INTERVAL:
664 return Value(interval_value.cot());
665 default:
666 throw std::runtime_error("Cannot compute cotangent of this type");
667 }
668}
669
671 switch (value_type) {
672 case NUMBER:
673 return Value(number_value.sec());
674 case INTERVAL:
675 return Value(interval_value.sec());
676 default:
677 throw std::runtime_error("Cannot compute secant of this type");
678 }
679}
680
682 switch (value_type) {
683 case NUMBER:
684 return Value(number_value.csc());
685 case INTERVAL:
686 return Value(interval_value.csc());
687 default:
688 throw std::runtime_error("Cannot compute cosecant of this type");
689 }
690}
691
692// Inverse trigonometric functions
694 switch (value_type) {
695 case NUMBER:
696 return Value(number_value.asin());
697 case INTERVAL:
698 return Value(interval_value.asin());
699 default:
700 throw std::runtime_error("Cannot compute arcsine of this type");
701 }
702}
703
705 switch (value_type) {
706 case NUMBER:
707 return Value(number_value.acos());
708 case INTERVAL:
709 return Value(interval_value.acos());
710 default:
711 throw std::runtime_error("Cannot compute arccosine of this type");
712 }
713}
714
716 switch (value_type) {
717 case NUMBER:
718 return Value(number_value.atan());
719 case INTERVAL:
720 return Value(interval_value.atan());
721 default:
722 throw std::runtime_error("Cannot compute arctangent of this type");
723 }
724}
725
727 switch (value_type) {
728 case NUMBER:
729 return Value(number_value.acot());
730 case INTERVAL:
731 return Value(interval_value.acot());
732 default:
733 throw std::runtime_error("Cannot compute arccotangent of this type");
734 }
735}
736
738 switch (value_type) {
739 case NUMBER:
740 return Value(number_value.asec());
741 case INTERVAL:
742 return Value(interval_value.asec());
743 default:
744 throw std::runtime_error("Cannot compute arcsecant of this type");
745 }
746}
747
749 switch (value_type) {
750 case NUMBER:
751 return Value(number_value.acsc());
752 case INTERVAL:
753 return Value(interval_value.acsc());
754 default:
755 throw std::runtime_error("Cannot compute arccosecant of this type");
756 }
757}
758
759Value Value::atan2(const Value &other) const {
760 if (value_type == NUMBER && other.value_type == NUMBER) {
762 }
763 else {
764 throw std::runtime_error("atan2 not implemented for intervals");
765 }
766}
767
768// Hyperbolic functions
770 switch (value_type) {
771 case NUMBER:
772 return Value(number_value.sinh());
773 case INTERVAL:
774 return Value(interval_value.sinh());
775 default:
776 throw std::runtime_error("Cannot compute hyperbolic sine of this type");
777 }
778}
779
781 switch (value_type) {
782 case NUMBER:
783 return Value(number_value.cosh());
784 case INTERVAL:
785 return Value(interval_value.cosh());
786 default:
787 throw std::runtime_error("Cannot compute hyperbolic cosine of this type");
788 }
789}
790
792 switch (value_type) {
793 case NUMBER:
794 return Value(number_value.tanh());
795 case INTERVAL:
796 return Value(interval_value.tanh());
797 default:
798 throw std::runtime_error("Cannot compute hyperbolic tangent of this type");
799 }
800}
801
803 switch (value_type) {
804 case NUMBER:
805 return Value(number_value.coth());
806 case INTERVAL:
807 return Value(interval_value.coth());
808 default:
809 throw std::runtime_error(
810 "Cannot compute hyperbolic cotangent of this type");
811 }
812}
813
815 switch (value_type) {
816 case NUMBER:
817 return Value(number_value.sech());
818 case INTERVAL:
819 return Value(interval_value.sech());
820 default:
821 throw std::runtime_error("Cannot compute hyperbolic secant of this type");
822 }
823}
824
826 switch (value_type) {
827 case NUMBER:
828 return Value(number_value.csch());
829 case INTERVAL:
830 return Value(interval_value.csch());
831 default:
832 throw std::runtime_error("Cannot compute hyperbolic cosecant of this type");
833 }
834}
835
836// Inverse hyperbolic functions
838 switch (value_type) {
839 case NUMBER:
840 return Value(number_value.asinh());
841 case INTERVAL:
842 return Value(interval_value.asinh());
843 default:
844 throw std::runtime_error(
845 "Cannot compute inverse hyperbolic sine of this type");
846 }
847}
848
850 switch (value_type) {
851 case NUMBER:
852 return Value(number_value.acosh());
853 case INTERVAL:
854 return Value(interval_value.acosh());
855 default:
856 throw std::runtime_error(
857 "Cannot compute inverse hyperbolic cosine of this type");
858 }
859}
860
862 switch (value_type) {
863 case NUMBER:
864 return Value(number_value.atanh());
865 case INTERVAL:
866 return Value(interval_value.atanh());
867 default:
868 throw std::runtime_error(
869 "Cannot compute inverse hyperbolic tangent of this type");
870 }
871}
872
874 switch (value_type) {
875 case NUMBER:
876 return Value(number_value.acoth());
877 case INTERVAL:
878 return Value(interval_value.acoth());
879 default:
880 throw std::runtime_error(
881 "Cannot compute inverse hyperbolic cotangent of this type");
882 }
883}
884
886 switch (value_type) {
887 case NUMBER:
888 return Value(number_value.asech());
889 case INTERVAL:
890 return Value(interval_value.asech());
891 default:
892 throw std::runtime_error(
893 "Cannot compute inverse hyperbolic secant of this type");
894 }
895}
896
898 switch (value_type) {
899 case NUMBER:
900 return Value(number_value.acsch());
901 case INTERVAL:
902 return Value(interval_value.acsch());
903 default:
904 throw std::runtime_error(
905 "Cannot compute inverse hyperbolic cosecant of this type");
906 }
907}
908
909// String operations
910Value Value::concatStr(const Value &other) const {
911 if (value_type == STRING && other.value_type == STRING) {
912 return Value(string_value + other.string_value);
913 }
914 else {
915 throw std::runtime_error("String concatenation requires string operands");
916 }
917}
918
919Value Value::repeatStr(const Value &other) const {
920 if (value_type == STRING && other.value_type == NUMBER) {
921 std::string result;
922 int count = other.number_value.toInteger().toInt();
923 for (int i = 0; i < count; i++) {
924 result += string_value;
925 }
926 return Value(result);
927 }
928 else {
929 throw std::runtime_error("String repeat requires a string and a number");
930 }
931}
932
934 if (value_type == STRING) {
935 std::string result = string_value;
936 std::sort(result.begin(), result.end());
937 return Value(result);
938 }
939 else {
940 throw std::runtime_error("Sort operation requires a string");
941 }
942}
943
944// String operations (continued)
945Value Value::substr(const Value &start, const Value &end) const {
946 if (value_type == STRING && start.value_type == NUMBER &&
947 end.value_type == NUMBER) {
948 int startPos = start.number_value.toInteger().toInt();
949 int length = end.number_value.toInteger().toInt();
950
951 if (startPos < 0 || startPos >= static_cast<int>(string_value.length())) {
952 throw std::out_of_range("String substring start position out of range");
953 }
954
955 return Value(string_value.substr(startPos, length));
956 }
957 else {
958 throw std::runtime_error(
959 "Substring operation requires a string and two numbers");
960 }
961}
962
963Value Value::replace(const Value &old, const Value &newVal) const {
964 if (value_type == STRING && old.value_type == STRING &&
965 newVal.value_type == STRING) {
966 std::string result = string_value;
967 size_t pos = 0;
968 while ((pos = result.find(old.string_value, pos)) != std::string::npos) {
969 result.replace(pos, old.string_value.length(), newVal.string_value);
970 pos += newVal.string_value.length();
971 }
972 return Value(result);
973 }
974 else {
975 throw std::runtime_error("Replace operation requires three strings");
976 }
977}
978
979Value Value::split(const Value &delimiter) const {
980 (void)delimiter;
981 throw std::runtime_error("String split not implemented");
982}
983
984Value Value::join(const Value &delimiter) const {
985 (void)delimiter;
986 throw std::runtime_error("String join not implemented");
987}
988
990 if (value_type == STRING) {
991 std::string result = string_value;
992 std::reverse(result.begin(), result.end());
993 return Value(result);
994 }
995 else {
996 throw std::runtime_error("Reverse operation requires a string");
997 }
998}
999
1001 if (value_type == STRING) {
1002 std::string result;
1003 for (char c : string_value) {
1004 if (result.find(c) == std::string::npos) {
1005 result += c;
1006 }
1007 }
1008 return Value(result);
1009 }
1010 else {
1011 throw std::runtime_error("Unique operation requires a string");
1012 }
1013}
1014
1016 if (value_type == STRING) {
1017 std::string result = string_value;
1018 // Trim left
1019 result.erase(result.begin(),
1020 std::find_if(result.begin(), result.end(), [](int ch) { return !std::isspace(ch); }));
1021 // Trim right
1022 result.erase(std::find_if(result.rbegin(), result.rend(), [](int ch) { return !std::isspace(ch); })
1023 .base(),
1024 result.end());
1025 return Value(result);
1026 }
1027 else {
1028 throw std::runtime_error("Trim operation requires a string");
1029 }
1030}
1031
1033 if (value_type == STRING) {
1034 std::string result = string_value;
1035 result.erase(result.begin(),
1036 std::find_if(result.begin(), result.end(), [](int ch) { return !std::isspace(ch); }));
1037 return Value(result);
1038 }
1039 else {
1040 throw std::runtime_error("Left trim operation requires a string");
1041 }
1042}
1043
1045 if (value_type == STRING) {
1046 std::string result = string_value;
1047 result.erase(std::find_if(result.rbegin(), result.rend(), [](int ch) { return !std::isspace(ch); })
1048 .base(),
1049 result.end());
1050 return Value(result);
1051 }
1052 else {
1053 throw std::runtime_error("Right trim operation requires a string");
1054 }
1055}
1056
1058 if (value_type == STRING) {
1059 std::string result = string_value;
1060 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
1061 return Value(result);
1062 }
1063 else {
1064 throw std::runtime_error("toLower operation requires a string");
1065 }
1066}
1067
1069 if (value_type == STRING) {
1070 std::string result = string_value;
1071 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
1072 return Value(result);
1073 }
1074 else {
1075 throw std::runtime_error("toUpper operation requires a string");
1076 }
1077}
1078
1079// Type conversion operations
1081 if (value_type == STRING) {
1082 try {
1083 return Value(Number(string_value));
1084 }
1085 catch (const std::exception &e) {
1086 throw std::runtime_error("Cannot convert string to number: " +
1087 std::string(e.what()));
1088 }
1089 }
1090 else if (value_type == BOOLEAN) {
1091 return Value(Number(boolean_value ? 1 : 0));
1092 }
1093 else if (value_type == NUMBER) {
1094 return *this;
1095 }
1096 else {
1097 throw std::runtime_error("Cannot convert this type to number");
1098 }
1099}
1100
1102 if (value_type == STRING) {
1103 return Value(string_value == "true" || string_value == "1");
1104 }
1105 else if (value_type == NUMBER) {
1106 return Value(number_value != Number(0));
1107 }
1108 else if (value_type == BOOLEAN) {
1109 return *this;
1110 }
1111 else {
1112 throw std::runtime_error("Cannot convert this type to boolean");
1113 }
1114}
1115
1117 throw std::runtime_error("Conversion to bit-vector not implemented");
1118}
1119
1121 throw std::runtime_error("Conversion to floating-point not implemented");
1122}
1123
1125 throw std::runtime_error("Conversion to array not implemented");
1126}
1127
1128// Other method implementations can be added as needed
1129// For array operations, etc.
1130
1131// Bitwise assignment operators
1133 if (value_type != NUMBER || other.value_type != NUMBER) {
1134 throw std::runtime_error("Bitwise XOR requires number operands");
1135 }
1136 throw std::runtime_error(
1137 "Bitwise XOR operation not implemented for Number class");
1138 return *this;
1139}
1140
1142 if (value_type != NUMBER || other.value_type != NUMBER) {
1143 throw std::runtime_error("Bitwise AND requires number operands");
1144 }
1145 throw std::runtime_error(
1146 "Bitwise AND operation not implemented for Number class");
1147 return *this;
1148}
1149
1151 if (value_type != NUMBER || other.value_type != NUMBER) {
1152 throw std::runtime_error("Bitwise OR requires number operands");
1153 }
1154 throw std::runtime_error(
1155 "Bitwise OR operation not implemented for Number class");
1156 return *this;
1157}
1158
1160 if (value_type != NUMBER || other.value_type != NUMBER) {
1161 throw std::runtime_error("Left shift requires number operands");
1162 }
1163 throw std::runtime_error(
1164 "Left shift operation not implemented for Number class");
1165 return *this;
1166}
1167
1169 if (value_type != NUMBER || other.value_type != NUMBER) {
1170 throw std::runtime_error("Right shift requires number operands");
1171 }
1172 throw std::runtime_error(
1173 "Right shift operation not implemented for Number class");
1174 return *this;
1175}
1176} // namespace stabilizer::parser
Interval mod(const Number &value) const
std::string toString() const
Definition interval.cpp:88
Interval pow(const Number &exp) const
Interval safeSqrt() const
Definition interval.cpp:654
Number pow(const Number &exp) const
Definition number.cpp:1468
std::string toString() const
Definition number.cpp:1431
HighPrecisionInteger toInteger() const
Definition number.cpp:1169
static Number atan2(const Number &y, const Number &x)
Definition number.cpp:1636
Number log(const Number &base) const
Definition number.cpp:1543
Number safeSqrt() const
Definition number.cpp:1460
Value round() const
Definition value.cpp:646
Value operator*(const Value &other) const
Definition value.cpp:291
Value & operator<<=(const Value &other)
Definition value.cpp:1159
Value toNumber() const
Definition value.cpp:1080
void setValue(const std::string &string_value)
Definition value.cpp:75
Value acosh() const
Definition value.cpp:849
Value asech() const
Definition value.cpp:885
Value pow(const Value &other) const
Definition value.cpp:408
bool operator==(const Value &other) const
Definition value.cpp:213
Interval getIntervalValue() const
Definition value.cpp:112
Value log(const Value &other) const
Definition value.cpp:612
Value toLower() const
Definition value.cpp:1057
Value split(const Value &delimiter) const
Definition value.cpp:979
std::string string_value
Definition value.h:192
Value operator-(const Value &other) const
Definition value.cpp:278
Value operator&(const Value &other) const
Definition value.cpp:481
Value substr(const Value &start, const Value &end) const
Definition value.cpp:945
Value join(const Value &delimiter) const
Definition value.cpp:984
Value acoth() const
Definition value.cpp:873
bool operator>(const Value &other) const
Definition value.cpp:257
bool operator<(const Value &other) const
Definition value.cpp:234
Value operator/(const Value &other) const
Definition value.cpp:304
Value & operator-=(const Value &other)
Definition value.cpp:452
ValueType getType() const
Definition value.cpp:96
std::string toString() const
Definition value.cpp:152
Value atan2(const Value &other) const
Definition value.cpp:759
bool getBooleanValue() const
Definition value.cpp:119
Value & operator+=(const Value &other)
Definition value.cpp:447
std::string getStringValue() const
Definition value.cpp:98
Value operator<<(const Value &other) const
Definition value.cpp:497
Value reverse() const
Definition value.cpp:989
Value asinh() const
Definition value.cpp:837
Value & operator|=(const Value &other)
Definition value.cpp:1150
Value & operator>>=(const Value &other)
Definition value.cpp:1168
Value & operator=(const Value &other)
Definition value.cpp:45
Number getNumberValue() const
Definition value.cpp:105
bool operator<=(const Value &other) const
Definition value.cpp:253
Value floor() const
Definition value.cpp:634
bool operator||(const Value &other) const
Definition value.cpp:432
Value concatStr(const Value &other) const
Definition value.cpp:910
Value & operator*=(const Value &other)
Definition value.cpp:457
Value atanh() const
Definition value.cpp:861
Value repeatStr(const Value &other) const
Definition value.cpp:919
bool operator!() const
Definition value.cpp:439
Value operator>>(const Value &other) const
Definition value.cpp:505
Value operator%(const Value &other) const
Definition value.cpp:317
Value operator+(const Value &other) const
Definition value.cpp:262
bool operator>=(const Value &other) const
Definition value.cpp:259
Value & operator&=(const Value &other)
Definition value.cpp:1141
Value operator^(const Value &other) const
Definition value.cpp:473
Value acsch() const
Definition value.cpp:897
Value sortStr() const
Definition value.cpp:933
Value safeSqrt() const
Definition value.cpp:579
Value & operator^=(const Value &other)
Definition value.cpp:1132
Value & operator%=(const Value &other)
Definition value.cpp:467
Value toBoolean() const
Definition value.cpp:1101
Value & operator/=(const Value &other)
Definition value.cpp:462
Value operator|(const Value &other) const
Definition value.cpp:489
bool operator&&(const Value &other) const
Definition value.cpp:425
bool operator!=(const Value &other) const
Definition value.cpp:232
Value replace(const Value &old, const Value &newVal) const
Definition value.cpp:963
Value operator~() const
Definition value.cpp:553
Value toArray() const
Definition value.cpp:1124
Value toUpper() const
Definition value.cpp:1068
std::shared_ptr< Value > newValue(const std::string &string_value)
Definition value.cpp:168