asar coverage - build #267


src/asar/
File: src/asar/asar_math.h
Date: 2025-03-01 20:31:10
Lines:
27/27
100.0%
Functions:
8/8
100.0%
Branches:
18/26
69.2%

Line Branch Exec Source
1 #pragma once
2 #include <cstdint>
3 #include <memory>
4 #include <vector>
5 #include "errors.h"
6 #include "libstr.h"
7
8 // Public interface of the Asar mathematical subsystem //
9
10 // Asar's math is dynamically typed, this enum defines the possible types.
11 enum class math_val_type {
12 // 64-bit (double) floating point
13 floating,
14 // 64-bit signed int
15 integer,
16 // string
17 string,
18 // a resolved label with a name and value. used for datasize() and whatnot.
19 // auto-converts to integer when necessary.
20 identifier,
21 };
22
23 class math_val {
24 public:
25 math_val_type m_type;
26 union {
27 double double_;
28 int64_t int_;
29 } m_numeric_val;
30 // this is used for both labelname in case of
31 // m_type=identifier, and string value in case of m_type=string
32 string m_string_val;
33
34 math_val() {
35 m_type = math_val_type::integer;
36 m_numeric_val.int_ = 0;
37 }
38 332 math_val(double v) {
39 332 m_type = math_val_type::floating;
40 332 m_numeric_val.double_ = v;
41 332 }
42 23699 math_val(int64_t v) {
43 23699 m_type = math_val_type::integer;
44 23699 m_numeric_val.int_ = v;
45 23699 }
46 1048 math_val(string v) {
47 1048 m_type = math_val_type::string;
48
2/4
✓ Branch 0 taken 519 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 529 times.
✗ Branch 3 not taken.
1048 m_string_val = v;
49 1048 }
50 906 static math_val make_identifier(string v) {
51
4/6
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 455 times.
✓ Branch 2 taken 451 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 455 times.
✗ Branch 5 not taken.
906 math_val res(v);
52 906 res.m_type = math_val_type::identifier;
53 906 return res;
54 }
55
56 double get_double() const;
57 int64_t get_integer() const;
58 const string &get_str() const;
59 const string &get_identifier() const;
60 bool get_bool() const;
61 };
62
63 class math_ast_node {
64 public:
65 // any info that's necessary during evaluation.
66 // external callers shouldn't need to pass in anything other than the default-constructed one.
67 class eval_context {
68 public:
69 // TODO would it be faster to make this a reference? does that avoid any significant copies?
70 std::vector<math_val> userfunc_params;
71 };
72
73 virtual math_val evaluate(const eval_context& = {}) const = 0;
74 // 0 - no label, 1 - static label, 3 - nonstatic label, 7 - forward label
75 virtual int has_label() const = 0;
76 // how many bytes long should the result of this expression be?
77 virtual int get_len(bool could_be_bank_ex) const = 0;
78 39186 virtual ~math_ast_node() = default;
79
80 // helper functions:
81
82 // evaluate an expression that doesn't allow non-static label references
83 6681 math_val evaluate_static() {
84
4/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 3297 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 3300 times.
6681 if(has_label() > 1)
85 84 asar_throw_error(0, error_type_block, error_id_no_labels_here);
86
2/4
✓ Branch 0 taken 3297 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3300 times.
✗ Branch 3 not taken.
9897 return evaluate();
87 6597 }
88 // evaluate an expression that doesn't allow forward label references
89 1116 math_val evaluate_non_forward() {
90
4/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 515 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 599 times.
1116 if(has_label() > 3)
91 2 asar_throw_error(0, error_type_block, error_id_label_forward);
92
2/4
✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 599 times.
✗ Branch 3 not taken.
1713 return evaluate();
93 1114 }
94 };
95
96 void initmathcore();
97 void deinitmathcore();
98 void closecachedfiles();
99
100 void createuserfunc(const char * name, const char * arguments, const char * content);
101
102 std::unique_ptr<math_ast_node> parse_math_expr(const char * str);
103
104 // old interface: to be nuked in due time
105
106 int64_t getnum(const char * str);
107
108 // still used in arch-spc700... god that one's a proper mess
109 int getlen(const char * str, bool optimizebankextraction=false);
110 extern bool foundlabel;
111