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