This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_defs.hpp
191 lines (141 loc) · 4.35 KB
/
user_defs.hpp
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
#ifndef YODA_USER_DEFS_HPP
#define YODA_USER_DEFS_HPP
#include <stack>
#include <vector>
#include <cstring>
#include <cstdlib>
#include "calc_value.hpp"
#include "core.hpp"
#include "process_line.hpp"
class UserDef {
public:
UserDef(){}
UserDef(const UserDef& in) {
proc_type = in.proc_type;
cond_type = in.cond_type;
if (proc_type == FXNPTR)
method = in.method;
else
macro = in.macro;
if (cond_type == BASIC)
bas_cond = in.bas_cond;
else if (cond_type == LABEL) {
label = (char*) malloc(strlen(in.label) + 1);
strcpy(label, in.label);
} else
adv_cond = in.adv_cond;
}
~UserDef(){
if (cond_type == LABEL)
free(label);
}
// what actually happens, the code run when operator is triggered
union {
char* (*method)(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable);
StrStack macro;
};
// using C++ or YodaScript to perform the operation
enum { FXNPTR, MACRO } proc_type;
// 3 condition types
// what triggers the operator
union {
bool (*adv_cond)(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable);
bool (*bas_cond)(char* p);
char* label;
};
// are they usng a basic or advanced condition?
enum { ADVANCED, BASIC, LABEL } cond_type;
UserDef& operator=(const UserDef& in){
// no mem leaks here :)
if (cond_type == LABEL)
free(label);
proc_type = in.proc_type;
cond_type = in.cond_type;
// copy
if (proc_type == FXNPTR)
method = in.method;
else
macro = in.macro;
// copy condition in
if (cond_type == BASIC)
bas_cond = in.bas_cond;
else if (cond_type == LABEL) {
label = (char*) malloc(strlen(in.label) + 1);
strcpy(label, in.label);
} else
adv_cond = in.adv_cond;
return *this;
}
// assign process
UserDef& setProc(char* (*_method)(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable))
{
method = _method;
proc_type = FXNPTR;
return *this;
};
UserDef& setProc(const StrStack& _macro) {
macro = _macro;
proc_type = MACRO;
return *this;
}
// assign condition
UserDef& setCond(bool (*condition)(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable)
) {
// prevent mem leak
if (cond_type == LABEL)
free(label);
adv_cond = condition;
cond_type = ADVANCED;
return *this;
}
UserDef& setCond(bool (*condition)(char* p)) {
// prevent mem leak
if (cond_type == LABEL)
free(label);
bas_cond = condition;
cond_type = BASIC;
return *this;
}
UserDef& setCond(const char* _label) {
// prevent mem leak
if (cond_type == LABEL)
free(label);
label = (char*) malloc(strlen(_label) + 1);
strcpy(label, _label);
cond_type = LABEL;
return *this;
}
// check the condition
bool cond(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable)
{
if (cond_type == BASIC)
return bas_cond(p);
else if (cond_type == LABEL) {
return strcmp(label, p) == 0;
} else
return adv_cond(p, mainStack, var_nodes, showErrors, rpnln, elseStatement, codeFeed, freeable);
}
};
namespace udefs {
// list of defined operators
extern std::vector<UserDef> userDefs;
// attempt call an operator
char* callOperator(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable, bool& ret);
// find index of appropriate operator
int findOperator(char* p, std::stack<CalcValue>& mainStack, std::vector<UserVar>& var_nodes,
bool& showErrors, char*& rpnln, bool& elseStatement, FILE* codeFeed,
std::vector<void*>& freeable);
};
#endif //YODA_USER_DEFS_HPP