Line data Source code
1 : #ifndef CS_H_
2 : #define CS_H_
3 :
4 : /**
5 : * @file cs.h
6 : * @brief Internal CubeScript functionality
7 : *
8 : * low level cubescript functionality beyond script binding in command.h
9 : */
10 :
11 : enum
12 : {
13 : Max_Args = 25,
14 : Max_Results = 7,
15 : Max_CommandArgs = 12
16 : };
17 :
18 : enum CubeScriptCodes
19 : {
20 : Code_Start = 0, //0
21 : Code_Offset,
22 : Code_Null,
23 : Code_True,
24 : Code_False,
25 : Code_Not, //5
26 : Code_Pop,
27 : Code_Enter,
28 : Code_EnterResult,
29 : Code_Exit,
30 : Code_ResultArg, //10
31 : Code_Val,
32 : Code_ValI,
33 : Code_Dup,
34 : Code_Macro,
35 : Code_Bool, //15 (unused)
36 : Code_Block,
37 : Code_Empty,
38 : Code_Compile,
39 : Code_Cond,
40 : Code_Force, //20
41 : Code_Result,
42 : Code_Ident,
43 : Code_IdentU,
44 : Code_IdentArg,
45 : Code_Com, //25
46 : Code_ComD,
47 : Code_ComC,
48 : Code_ComV,
49 : Code_ConC,
50 : Code_ConCW, //30
51 : Code_ConCM,
52 : Code_Down, // (unused)
53 : Code_StrVar,
54 : Code_StrVarM,
55 : Code_StrVar1, //35
56 : Code_IntVar,
57 : Code_IntVar1,
58 : Code_IntVar2,
59 : Code_IntVar3,
60 : Code_FloatVar, //40
61 : Code_FloatVar1,
62 : Code_Lookup,
63 : Code_LookupU,
64 : Code_LookupArg,
65 : Code_LookupM, //45
66 : Code_LookupMU,
67 : Code_LookupMArg,
68 : Code_Alias,
69 : Code_AliasU,
70 : Code_AliasArg, //50
71 : Code_Call,
72 : Code_CallU,
73 : Code_CallArg,
74 : Code_Print,
75 : Code_Local, //55
76 : Code_Do,
77 : Code_DoArgs,
78 : Code_Jump,
79 : Code_JumpTrue,
80 : Code_JumpFalse,
81 : Code_JumpResultTrue, //60
82 : Code_JumpResultFalse,
83 :
84 : Code_OpMask = 0x3F,
85 : Code_Ret = 6,
86 : Code_RetMask = 0xC0,
87 :
88 : /* return type flags */
89 : Ret_Null = Value_Null<<Code_Ret,
90 : Ret_String = Value_String<<Code_Ret,
91 : Ret_Integer = Value_Integer<<Code_Ret,
92 : Ret_Float = Value_Float<<Code_Ret,
93 : };
94 :
95 : struct stringslice final
96 : {
97 : const char *str;
98 : int len;
99 4477 : stringslice() {}
100 1 : stringslice(const char *newstr, int newlen) : str(newstr), len(newlen) {}
101 0 : stringslice(const char *newstr, const char *newend) : str(newstr), len(static_cast<int>(newend-newstr)) {}
102 :
103 50 : const char *end() const
104 : {
105 50 : return &str[len];
106 : }
107 : };
108 :
109 : // parsefloat/parsenumber: not all platforms (windows) can parse hexadecimal integers via strtod
110 :
111 : /**
112 : * @brief Returns a float parsed from the provided string.
113 : *
114 : * @param s the string to parse
115 : *
116 : * @return the number as a float if parsable, or parseint(s) if not
117 : */
118 : extern float parsefloat(const char *s);
119 :
120 : /**
121 : * @brief Returns a double parsed from the provided string.
122 : *
123 : * @param s the string to parse
124 : *
125 : * @return the number as a double if parsable, or parseint(s) if not
126 : */
127 : extern double parsenumber(const char *s);
128 :
129 : /**
130 : * @brief Sets the buffer to the int value provided.
131 : *
132 : * @param buf the output buffer to set
133 : * @param v the int value to set
134 : * @param len the maximum size of the output string
135 : */
136 : extern void intformat(char *buf, int v, int len = 20);
137 :
138 : /**
139 : * @brief Sets the buffer to the float value provided.
140 : *
141 : * If the value passed is an integer, returns to buf a truncated version without
142 : * extra trailing zeros.
143 : *
144 : * @param buf the output buffer to set
145 : * @param v the float value to set
146 : * @param len the maximum size of the output string
147 : */
148 :
149 : extern void floatformat(char *buf, float v, int len = 20);
150 :
151 : /**
152 : * @brief Sets the next retbuf entry to the int value passed.
153 : *
154 : * The return value is not heap allocated, but instead a circular buffer that
155 : * remains allocated for the lifetime of the program. The buffer is formatted
156 : * by intformat().
157 : *
158 : * @return pointer to the retbuf with the formatted int string inside it
159 : */
160 : extern const char *intstr(int v);
161 :
162 : extern void getval(const identval &v, int type, tagval &r);
163 :
164 0 : inline void tagval::getval(tagval &r) const
165 : {
166 0 : ::getval(*this, type, r);
167 0 : }
168 :
169 : struct NullVal final : tagval
170 : {
171 29 : NullVal()
172 29 : {
173 29 : setnull();
174 29 : }
175 : };
176 :
177 : struct IdentLink final
178 : {
179 : ident *id;
180 : IdentLink *next;
181 : int usedargs;
182 : identstack *argstack;
183 : };
184 :
185 : extern const char *sourcefile,
186 : *sourcestr;
187 :
188 : extern std::array<std::vector<char>, 4> strbuf;
189 : extern int stridx;
190 :
191 : extern tagval *commandret;
192 : extern void executeret(const uint *code, tagval &result = *commandret);
193 :
194 : /**
195 : * @brief Executes a given string, returning a tagval by reference parameter
196 : *
197 : * @param p a string to execute
198 : * @param result tagval containing result metadata
199 : */
200 : extern void executeret(const char *p, tagval &result = *commandret);
201 :
202 : /**
203 : * @brief Executes a given ident, returning a tagval by reference parameter
204 : *
205 : * @param id the ident to execute
206 : * @param args an array of arguments
207 : * @param numargs size of args array
208 : * @param lookup whether to lookup (dereference) args (?)
209 : * @param result tagval containing result metadata
210 : */
211 : extern void executeret(ident *id, tagval *args, int numargs, bool lookup = false, tagval &result = *commandret);
212 :
213 : extern void poparg(ident &id);
214 : extern void pusharg(ident &id, const tagval &v, identstack &stack);
215 : extern bool getbool(const tagval &v);
216 : extern void cleancode(ident &id);
217 : extern char *conc(const tagval *v, int n, bool space);
218 : extern char *conc(const tagval *v, int n, bool space, const char *prefix);
219 : extern void freearg(tagval &v);
220 : extern int unescapestring(char *dst, const char *src, const char *end);
221 : extern const char *parsestring(const char *p);
222 : extern void setarg(ident &id, tagval &v);
223 : extern void setalias(ident &id, tagval &v);
224 : extern void undoarg(ident &id, identstack &stack);
225 : extern void redoarg(ident &id, const identstack &stack);
226 : extern const char *parseword(const char *p);
227 :
228 : extern bool validateblock(const char *s);
229 : extern std::unordered_map<std::string, ident> idents;
230 :
231 : extern void setvarchecked(ident *id, int val);
232 : extern void setfvarchecked(ident *id, float val);
233 : extern void setsvarchecked(ident *id, const char *val);
234 :
235 : /**
236 : * @brief Prints out the value inside in ident.
237 : *
238 : * Formats output using `printvar(const ident*, int)` for an integral value, `printfvar()`
239 : * for a float value, or `printsvar()` for a stirng. Uses the stored value inside
240 : * of the given ident as the printed value.
241 : *
242 : * @param id the ident to parse
243 : */
244 : extern void printvar(const ident *id);
245 :
246 : /**
247 : * @brief Prints out a value using an ident's properties
248 : *
249 : * If i is negative, prints out the value of i directly. If id is a color, prints
250 : * out the three color bytes composing the color. If id is a hex value, prints out
251 : * i in hex form. Otherwise prints out the value of i directly.
252 : *
253 : * Used by `printvar(const ident*)`, which fetches the value of i from the ident.
254 : *
255 : * @param id the ident to parse
256 : * @param i value to print out
257 : */
258 : extern void printvar(const ident *id, int i);
259 :
260 : extern void clearoverrides();
261 :
262 : /**
263 : * @brief Drops sleep commands, optionally only dropping sleeps with override bit set.
264 : *
265 : * If clearoverrides is `false`, drops every element in the sleepcmds vector.
266 : *
267 : * If clearoverrides is `true`, keeps elements where Idf_Overridden bit is not set
268 : * and discards elements where Idf_Overridden bit is set.
269 : *
270 : * Idf_Overridden is an enum element in command.h.
271 : *
272 : * @param clearoverrides whether to drop elements with Idf_Overridden bit set.
273 : */
274 : extern void clearsleep(bool clearoverrides = true);
275 :
276 : extern char *executestr(ident *id, tagval *args, int numargs, bool lookup = false);
277 : extern uint *compilecode(const char *p);
278 : extern void freecode(uint *p);
279 : extern int execute(ident *id, tagval *args, int numargs, bool lookup = false);
280 : extern bool executebool(ident *id, tagval *args, int numargs, bool lookup = false);
281 : extern void alias(const char *name, const char *action);
282 :
283 : /**
284 : * @brief Converts a list of delimited strings into a vector of elements.
285 : *
286 : * List elements in the input string are added to the vector of strings passed
287 : * as elems.
288 : *
289 : * The list is parsed according to the CubeScript list formatting.
290 : * Tokens in the list are delimited by spaces, unless those tokens are themselves
291 : * delimited by [] "" or () characters.
292 : *
293 : * Existing elements in the elems vector will not be modified. The limit parameter
294 : * sets the maximum number of elements in the vector, regardless of whether they
295 : * were added by this function.
296 : *
297 : * Elements added to this vector are all heap-allocated raw character strings and
298 : * therefore must be delete[]'d after they are no longer being used
299 : *
300 : * @param s the list to explode
301 : * @param elems the vector to fill with elements
302 : * @param limit maximum size of the elems vector allowed
303 : */
304 : extern void explodelist(const char *s, std::vector<char *> &elems, int limit = -1);
305 :
306 : /**
307 : * @brief Converts a list of delimited strings into a vector of elements.
308 : *
309 : * List elements in the input string are added to the vector of strings passed
310 : * as elems.
311 : *
312 : * The list is parsed according to the CubeScript list formatting.
313 : * Tokens in the list are delimited by spaces, unless those tokens are themselves
314 : * delimited by [] "" or () characters.
315 : *
316 : * Existing elements in the elems vector will not be modified. The limit parameter
317 : * sets the maximum number of elements in the vector, regardless of whether they
318 : * were added by this function.
319 : *
320 : * Elements added to this vector are standard std::strings and do not need special
321 : * memory management.
322 : *
323 : * @param s the list to explode
324 : * @param elems the vector to fill with elements
325 : * @param limit maximum size of the elems vector allowed
326 : */
327 : extern void explodelist(const char *s, std::vector<std::string> &elems, int limit = -1);
328 :
329 : extern void result(tagval &v);
330 : extern const char *numberstr(double v);
331 : extern float clampfvar(std::string name, float val, float minval, float maxval);
332 :
333 : #endif
|