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 : // not all platforms (windows) can parse hexadecimal integers via strtod
110 276 : inline float parsefloat(const char *s)
111 : {
112 : char *end;
113 276 : double val = std::strtod(s, &end);
114 : return val
115 44 : || end==s
116 320 : || (*end!='x' && *end!='X') ? static_cast<float>(val) : static_cast<float>(parseint(s));
117 : }
118 :
119 2 : inline double parsenumber(const char *s)
120 : {
121 : char *end;
122 2 : double val = std::strtod(s, &end);
123 : return val
124 1 : || end==s
125 3 : || (*end!='x' && *end!='X') ? static_cast<double>(val) : static_cast<double>(parseint(s));
126 : }
127 :
128 : /**
129 : * @brief Sets the buffer to the int value provided.
130 : *
131 : * @param buf the output buffer to set
132 : * @param v the int value to set
133 : * @param len the maximum size of the output string
134 : */
135 : extern void intformat(char *buf, int v, int len = 20);
136 :
137 : /**
138 : * @brief Sets the buffer to the float value provided.
139 : *
140 : * If the value passed is an integer, returns to buf a truncated version without
141 : * extra trailing zeros.
142 : *
143 : * @param buf the output buffer to set
144 : * @param v the float value to set
145 : * @param len the maximum size of the output string
146 : */
147 :
148 : extern void floatformat(char *buf, float v, int len = 20);
149 :
150 : /**
151 : * @brief Sets the next retbuf entry to the int value passed.
152 : *
153 : * The return value is not heap allocated, but instead a circular buffer that
154 : * remains allocated for the lifetime of the program. The buffer is formatted
155 : * by intformat().
156 : *
157 : * @return pointer to the retbuf with the formatted int string inside it
158 : */
159 : extern const char *intstr(int v);
160 :
161 : extern void getval(const identval &v, int type, tagval &r);
162 :
163 0 : inline void tagval::getval(tagval &r) const
164 : {
165 0 : ::getval(*this, type, r);
166 0 : }
167 :
168 : struct NullVal final : tagval
169 : {
170 29 : NullVal()
171 29 : {
172 29 : setnull();
173 29 : }
174 : };
175 :
176 : struct IdentLink final
177 : {
178 : ident *id;
179 : IdentLink *next;
180 : int usedargs;
181 : identstack *argstack;
182 : };
183 :
184 : extern const char *sourcefile,
185 : *sourcestr;
186 :
187 : extern std::array<std::vector<char>, 4> strbuf;
188 : extern int stridx;
189 :
190 : extern tagval *commandret;
191 : extern void executeret(const uint *code, tagval &result = *commandret);
192 :
193 : /**
194 : * @brief Executes a given string, returning a tagval by reference parameter
195 : *
196 : * @param p a string to execute
197 : * @param result tagval containing result metadata
198 : */
199 : extern void executeret(const char *p, tagval &result = *commandret);
200 :
201 : /**
202 : * @brief Executes a given ident, returning a tagval by reference parameter
203 : *
204 : * @param id the ident to execute
205 : * @param args an array of arguments
206 : * @param numargs size of args array
207 : * @param lookup whether to lookup (dereference) args (?)
208 : * @param result tagval containing result metadata
209 : */
210 : extern void executeret(ident *id, tagval *args, int numargs, bool lookup = false, tagval &result = *commandret);
211 :
212 : extern void poparg(ident &id);
213 : extern void pusharg(ident &id, const tagval &v, identstack &stack);
214 : extern bool getbool(const tagval &v);
215 : extern void cleancode(ident &id);
216 : extern char *conc(const tagval *v, int n, bool space);
217 : extern char *conc(const tagval *v, int n, bool space, const char *prefix);
218 : extern void freearg(tagval &v);
219 : extern int unescapestring(char *dst, const char *src, const char *end);
220 : extern const char *parsestring(const char *p);
221 : extern void setarg(ident &id, tagval &v);
222 : extern void setalias(ident &id, tagval &v);
223 : extern void undoarg(ident &id, identstack &stack);
224 : extern void redoarg(ident &id, const identstack &stack);
225 : extern const char *parseword(const char *p);
226 :
227 : extern bool validateblock(const char *s);
228 : extern std::unordered_map<std::string, ident> idents;
229 :
230 : extern void setvarchecked(ident *id, int val);
231 : extern void setfvarchecked(ident *id, float val);
232 : extern void setsvarchecked(ident *id, const char *val);
233 :
234 : extern void printvar(const ident *id);
235 : extern void printvar(const ident *id, int i);
236 :
237 : extern void clearoverrides();
238 :
239 : /**
240 : * @brief Drops sleep commands, optionally only dropping sleeps with override bit set.
241 : *
242 : * If clearoverrides is `false`, drops every element in the sleepcmds vector.
243 : *
244 : * If clearoverrides is `true`, keeps elements where Idf_Overridden bit is not set
245 : * and discards elements where Idf_Overridden bit is set.
246 : *
247 : * Idf_Overridden is an enum element in command.h.
248 : *
249 : * @param clearoverrides whether to drop elements with Idf_Overridden bit set.
250 : */
251 : extern void clearsleep(bool clearoverrides = true);
252 :
253 : extern char *executestr(ident *id, tagval *args, int numargs, bool lookup = false);
254 : extern uint *compilecode(const char *p);
255 : extern void freecode(uint *p);
256 : extern int execute(ident *id, tagval *args, int numargs, bool lookup = false);
257 : extern bool executebool(ident *id, tagval *args, int numargs, bool lookup = false);
258 : extern void alias(const char *name, const char *action);
259 :
260 : /**
261 : * @brief Converts a list of delimited strings into a vector of elements.
262 : *
263 : * List elements in the input string are added to the vector of strings passed
264 : * as elems.
265 : *
266 : * The list is parsed according to the CubeScript list formatting.
267 : * Tokens in the list are delimited by spaces, unless those tokens are themselves
268 : * delimited by [] "" or () characters.
269 : *
270 : * Existing elements in the elems vector will not be modified. The limit parameter
271 : * sets the maximum number of elements in the vector, regardless of whether they
272 : * were added by this function.
273 : *
274 : * Elements added to this vector are all heap-allocated raw character strings and
275 : * therefore must be delete[]'d after they are no longer being used
276 : *
277 : * @param s the list to explode
278 : * @param elems the vector to fill with elements
279 : * @param limit maximum size of the elems vector allowed
280 : */
281 : extern void explodelist(const char *s, std::vector<char *> &elems, int limit = -1);
282 : extern void explodelist(const char *s, std::vector<std::string> &elems, int limit = -1);
283 :
284 : extern void result(tagval &v);
285 : extern const char *numberstr(double v);
286 : extern float clampfvar(std::string name, float val, float minval, float maxval);
287 :
288 : #endif
|