OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4AulParse.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // parses scripts
17 
18 #include "C4Include.h"
19 #include "script/C4AulParse.h"
20 
21 #include <utility>
22 #include <stack>
23 
24 #include "script/C4Aul.h"
25 #include "script/C4AulDebug.h"
26 #include "script/C4AulExec.h"
27 #include "object/C4Def.h"
28 #include "game/C4Game.h"
29 #include "lib/C4Log.h"
30 #include "config/C4Config.h"
31 #include "script/C4AulAST.h"
32 #include "script/C4AulCompiler.h"
33 
34 #ifndef DEBUG_BYTECODE_DUMP
35 #define DEBUG_BYTECODE_DUMP 0
36 #endif
37 #include <iomanip>
38 
39 #define C4AUL_Include "#include"
40 #define C4AUL_Append "#appendto"
41 #define C4AUL_Warning "#warning"
42 
43 #define C4Aul_Warning_enable "enable"
44 #define C4Aul_Warning_disable "disable"
45 
46 #define C4AUL_Func "func"
47 
48 #define C4AUL_Private "private"
49 #define C4AUL_Protected "protected"
50 #define C4AUL_Public "public"
51 #define C4AUL_Global "global"
52 #define C4AUL_Const "const"
53 
54 #define C4AUL_If "if"
55 #define C4AUL_Else "else"
56 #define C4AUL_Do "do"
57 #define C4AUL_While "while"
58 #define C4AUL_For "for"
59 #define C4AUL_In "in"
60 #define C4AUL_Return "return"
61 #define C4AUL_Var "Var"
62 #define C4AUL_Par "Par"
63 #define C4AUL_Break "break"
64 #define C4AUL_Continue "continue"
65 #define C4AUL_this "this"
66 
67 #define C4AUL_GlobalNamed "static"
68 #define C4AUL_LocalNamed "local"
69 #define C4AUL_VarNamed "var"
70 
71 #define C4AUL_TypeInt "int"
72 #define C4AUL_TypeBool "bool"
73 #define C4AUL_TypeC4ID "id"
74 #define C4AUL_TypeDef "def"
75 #define C4AUL_TypeEffect "effect"
76 #define C4AUL_TypeC4Object "object"
77 #define C4AUL_TypePropList "proplist"
78 #define C4AUL_TypeString "string"
79 #define C4AUL_TypeArray "array"
80 #define C4AUL_TypeFunction "func"
81 
82 #define C4AUL_True "true"
83 #define C4AUL_False "false"
84 #define C4AUL_Nil "nil"
85 #define C4AUL_New "new"
86 
87 // script token type
88 enum C4AulTokenType : int
89 {
90  ATT_INVALID,// invalid token
91  ATT_DIR, // directive
92  ATT_IDTF, // identifier
93  ATT_INT, // integer constant
94  ATT_STRING, // string constant
95  ATT_DOT, // "."
96  ATT_COMMA, // ","
97  ATT_COLON, // ":"
98  ATT_SCOLON, // ";"
99  ATT_BOPEN, // "("
100  ATT_BCLOSE, // ")"
101  ATT_BOPEN2, // "["
102  ATT_BCLOSE2,// "]"
103  ATT_BLOPEN, // "{"
104  ATT_BLCLOSE,// "}"
105  ATT_CALL, // "->"
106  ATT_CALLFS, // "->~"
107  ATT_LDOTS, // '...'
108  ATT_SET, // '='
109  ATT_OPERATOR,// operator
110  ATT_EOF // end of file
111 };
112 
114  Fn(0), Host(a), pOrgScript(a), Engine(a->Engine),
115  SPos(a->Script.getData()), TokenSPos(SPos),
116  TokenType(ATT_INVALID),
117  ContextToExecIn(nullptr)
118 { }
119 
121  Fn(Fn), Host(nullptr), pOrgScript(nullptr), Engine(Engine),
122  SPos(Fn->Script), TokenSPos(SPos),
123  TokenType(ATT_INVALID),
124  ContextToExecIn(context)
125 { }
126 
128 {
129  ClearToken();
130 }
131 
132 void C4ScriptHost::Warn(const char *pMsg, ...)
133 {
134  va_list args; va_start(args, pMsg);
135  StdStrBuf Buf = FormatStringV(pMsg, args);
136  Buf.AppendFormat(" (%s)", ScriptName.getData());
138  va_end(args);
139 }
140 
141 void C4AulParse::Warn(C4AulWarningId warning, ...)
142 {
143  if (!pOrgScript->IsWarningEnabled(TokenSPos, warning))
144  return;
145  va_list args; va_start(args, warning);
146  StdStrBuf Buf = FormatStringV(C4AulWarningMessages[static_cast<size_t>(warning)], args);
147  AppendPosition(Buf);
148  Buf.AppendFormat(" [%s]", C4AulWarningIDs[static_cast<size_t>(warning)]);
150  va_end(args);
151 }
152 
153 void C4AulParse::Error(const char *pMsg, ...)
154 {
155  va_list args; va_start(args, pMsg);
156  StdStrBuf Buf;
157  Buf.FormatV(pMsg, args);
158 
159  throw C4AulParseError(this, Buf.getData());
160 }
161 
162 void C4AulParse::AppendPosition(StdStrBuf & Buf)
163 {
164  if (Fn && Fn->GetName())
165  {
166  // Show function name
167  Buf.AppendFormat(" (in %s", Fn->GetName());
168 
169  // Exact position
170  if (Fn->pOrgScript && TokenSPos)
171  Buf.AppendFormat(", %s:%d:%d)",
173  SGetLine(Fn->pOrgScript->GetScript(), TokenSPos),
174  SLineGetCharacters(Fn->pOrgScript->GetScript(), TokenSPos));
175  else
176  Buf.AppendChar(')');
177  }
178  else if (pOrgScript)
179  {
180  // Script name
181  Buf.AppendFormat(" (%s:%d:%d)",
182  pOrgScript->ScriptName.getData(),
183  SGetLine(pOrgScript->GetScript(), TokenSPos),
184  SLineGetCharacters(pOrgScript->GetScript(), TokenSPos));
185  }
186  // show a warning if the error is in a remote script
187  if (pOrgScript != Host && Host)
188  Buf.AppendFormat(" (as #appendto/#include to %s)", Host->ScriptName.getData());
189 }
190 
191 C4AulParseError::C4AulParseError(C4AulParse * state, const char *pMsg)
192 {
193  // compose error string
194  sMessage.Copy(pMsg);
195  state->AppendPosition(sMessage);
196 }
197 
198 C4AulParseError::C4AulParseError(C4ScriptHost *pScript, const char *pMsg)
199 {
200  // compose error string
201  sMessage.Copy(pMsg);
202  if (pScript)
203  {
204  // Script name
205  sMessage.AppendFormat(" (%s)",
206  pScript->ScriptName.getData());
207  }
208 }
209 
210 C4AulParseError::C4AulParseError(C4AulScriptFunc * Fn, const char *SPos, const char *pMsg)
211 {
212  // compose error string
213  sMessage.Copy(pMsg);
214  if (!Fn) return;
215  sMessage.Append(" (");
216  // Show function name
217  if (Fn->GetName())
218  sMessage.AppendFormat("in %s", Fn->GetName());
219  if (Fn->GetName() && Fn->pOrgScript && SPos)
220  sMessage.Append(", ");
221  // Exact position
222  if (Fn->pOrgScript && SPos)
223  sMessage.AppendFormat("%s:%d:%d)",
225  SGetLine(Fn->pOrgScript->GetScript(), SPos),
226  SLineGetCharacters(Fn->pOrgScript->GetScript(), SPos));
227  else
228  sMessage.AppendChar(')');
229 }
230 
231 bool C4AulParse::AdvanceSpaces()
232 {
233  if (!SPos)
234  return false;
235  while(*SPos)
236  {
237  if (*SPos == '/')
238  {
239  // // comment
240  if (SPos[1] == '/')
241  {
242  SPos += 2;
243  while (*SPos && *SPos != 13 && *SPos != 10)
244  ++SPos;
245  }
246  // /* comment */
247  else if (SPos[1] == '*')
248  {
249  SPos += 2;
250  while (*SPos && (*SPos != '*' || SPos[1] != '/'))
251  ++SPos;
252  SPos += 2;
253  }
254  else
255  return true;
256  }
257  // Skip any "zero width no-break spaces" (also known as Byte Order Marks)
258  else if (*SPos == '\xEF' && SPos[1] == '\xBB' && SPos[2] == '\xBF')
259  SPos += 3;
260  else if ((unsigned)*SPos > 32)
261  return true;
262  else
263  ++SPos;
264  }
265  // end of script reached
266  return false;
267 }
268 
269 //=========================== C4Script Operator Map ===================================
271 {
272  // priority postfix
273  // | identifier | changer
274  // | | Bytecode | | no second id
275  // | | | | | | RetType ParType1 ParType2
276  // prefix
277  { 15, "++", AB_Inc, 0, 1, 0, C4V_Int, C4V_Int, C4V_Any},
278  { 15, "--", AB_Dec, 0, 1, 0, C4V_Int, C4V_Int, C4V_Any},
279  { 15, "~", AB_BitNot, 0, 0, 0, C4V_Int, C4V_Int, C4V_Any},
280  { 15, "!", AB_Not, 0, 0, 0, C4V_Bool, C4V_Bool, C4V_Any},
281  { 15, "+", AB_ERR, 0, 0, 0, C4V_Int, C4V_Int, C4V_Any},
282  { 15, "-", AB_Neg, 0, 0, 0, C4V_Int, C4V_Int, C4V_Any},
283 
284  // postfix (whithout second statement)
285  { 16, "++", AB_Inc, 1, 1, 1, C4V_Int, C4V_Int, C4V_Any},
286  { 16, "--", AB_Dec, 1, 1, 1, C4V_Int, C4V_Int, C4V_Any},
287 
288  // postfix
289  { 14, "**", AB_Pow, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
290  { 13, "/", AB_Div, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
291  { 13, "*", AB_Mul, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
292  { 13, "%", AB_Mod, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
293  { 12, "-", AB_Sub, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
294  { 12, "+", AB_Sum, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
295  { 11, "<<", AB_LeftShift, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
296  { 11, ">>", AB_RightShift, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
297  { 10, "<", AB_LessThan, 1, 0, 0, C4V_Bool, C4V_Int, C4V_Int},
298  { 10, "<=", AB_LessThanEqual, 1, 0, 0, C4V_Bool, C4V_Int, C4V_Int},
299  { 10, ">", AB_GreaterThan, 1, 0, 0, C4V_Bool, C4V_Int, C4V_Int},
300  { 10, ">=", AB_GreaterThanEqual, 1, 0, 0, C4V_Bool, C4V_Int, C4V_Int},
301  { 9, "==", AB_Equal, 1, 0, 0, C4V_Bool, C4V_Any, C4V_Any},
302  { 9, "!=", AB_NotEqual, 1, 0, 0, C4V_Bool, C4V_Any, C4V_Any},
303  { 8, "&", AB_BitAnd, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
304  { 6, "^", AB_BitXOr, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
305  { 6, "|", AB_BitOr, 1, 0, 0, C4V_Int, C4V_Int, C4V_Int},
306  { 5, "&&", AB_JUMPAND, 1, 0, 0, C4V_Bool, C4V_Bool, C4V_Bool},
307  { 4, "||", AB_JUMPOR, 1, 0, 0, C4V_Bool, C4V_Bool, C4V_Bool},
308  { 3, "??", AB_JUMPNNIL, 1, 0, 0, C4V_Any, C4V_Any, C4V_Any},
309 
310  // changers
311  { 2, "*=", AB_Mul, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
312  { 2, "/=", AB_Div, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
313  { 2, "%=", AB_Mod, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
314  { 2, "+=", AB_Sum, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
315  { 2, "-=", AB_Sub, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
316  { 2, "&=", AB_BitAnd, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
317  { 2, "|=", AB_BitOr, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
318  { 2, "^=", AB_BitXOr, 1, 1, 0, C4V_Int, C4V_Int, C4V_Int},
319 
320  { 0, nullptr, AB_ERR, 0, 0, 0, C4V_Nil, C4V_Nil, C4V_Nil}
321 };
322 
323 int C4AulParse::GetOperator(const char* pScript)
324 {
325  // return value:
326  // >= 0: operator found. could be found in C4ScriptOfDef
327  // -1: isn't an operator
328 
329  unsigned int i;
330 
331  if (!*pScript) return 0;
332  // operators are not alphabetical
333  if ((*pScript >= 'a' && *pScript <= 'z') ||
334  (*pScript >= 'A' && *pScript <= 'Z'))
335  {
336  return -1;
337  }
338 
339  // find the longest operator
340  int len = 0; int maxfound = -1;
341  for (i=0; C4ScriptOpMap[i].Identifier; i++)
342  {
343  if (SEqual2(pScript, C4ScriptOpMap[i].Identifier))
344  {
345  int oplen = SLen(C4ScriptOpMap[i].Identifier);
346  if (oplen > len)
347  {
348  len = oplen;
349  maxfound = i;
350  }
351  }
352  }
353  return maxfound;
354 }
355 
356 void C4AulParse::ClearToken()
357 {
358  // if last token was a string, make sure its ref is deleted
359  if (TokenType == ATT_STRING && cStr)
360  {
361  cStr->DecRef();
362  TokenType = ATT_INVALID;
363  }
364 }
365 
366 C4AulTokenType C4AulParse::GetNextToken()
367 {
368  // clear mem of prev token
369  ClearToken();
370  // move to start of token
371  if (!AdvanceSpaces()) return ATT_EOF;
372  // store offset
373  TokenSPos = SPos;
374 
375  // get char
376  char C = *(SPos++);
377  // Mostly sorted by frequency, except that tokens that have
378  // other tokens as prefixes need to be checked for first.
379  if (Inside(C, 'a', 'z') || Inside(C, 'A', 'Z') || C == '_' || C == '#')
380  {
381  // identifier or directive
382  bool dir = C == '#';
383  int Len = 1;
384  C = *SPos;
385  while (Inside(C, '0', '9') || Inside(C, 'a', 'z') || Inside(C, 'A', 'Z') || C == '_')
386  {
387  ++Len;
388  C = *(++SPos);
389  }
390 
391  // Special case for #warning because we don't want to give it to the parser
392  if (dir && SEqual2(TokenSPos, C4AUL_Warning))
393  {
394  // Look for end of line or end of file
395  while (*SPos != '\n' && *SPos != '\0') ++SPos;
396  Parse_WarningPragma();
397  // And actually return the next token.
398  return GetNextToken();
399  }
400 
401  Len = std::min(Len, C4AUL_MAX_Identifier);
402  SCopy(TokenSPos, Idtf, Len);
403  return dir ? ATT_DIR : ATT_IDTF;
404  }
405  else if (C == '(') return ATT_BOPEN; // "("
406  else if (C == ')') return ATT_BCLOSE; // ")"
407  else if (C == ',') return ATT_COMMA; // ","
408  else if (C == ';') return ATT_SCOLON; // ";"
409  else if (Inside(C, '0', '9'))
410  {
411  // integer
412  if (C == '0' && *SPos == 'x')
413  {
414  // hexadecimal
415  cInt = StrToI32(SPos + 1, 16, &SPos);
416  return ATT_INT;
417  }
418  else
419  {
420  // decimal
421  cInt = StrToI32(TokenSPos, 10, &SPos);
422  return ATT_INT;
423  }
424  }
425  else if (C == '-' && *SPos == '>' && *(SPos + 1) == '~')
426  { SPos+=2; return ATT_CALLFS;}// "->~"
427  else if (C == '-' && *SPos == '>')
428  { ++SPos; return ATT_CALL; } // "->"
429  else if ((cInt = GetOperator(SPos - 1)) != -1)
430  {
431  SPos += SLen(C4ScriptOpMap[cInt].Identifier) - 1;
432  return ATT_OPERATOR;
433  }
434  else if (C == '=') return ATT_SET; // "="
435  else if (C == '{') return ATT_BLOPEN; // "{"
436  else if (C == '}') return ATT_BLCLOSE;// "}"
437  else if (C == '"')
438  {
439  // string
440  std::string strbuf;
441  strbuf.reserve(512); // assume most strings to be smaller than this
442  // string end
443  while (*SPos != '"')
444  {
445  C = *SPos;
446  ++SPos;
447  if (C == '\\') // escape
448  switch (*SPos)
449  {
450  case '"': ++SPos; strbuf.push_back('"'); break;
451  case '\\': ++SPos; strbuf.push_back('\\'); break;
452  case 'n': ++SPos; strbuf.push_back('\n'); break;
453  case 't': ++SPos; strbuf.push_back('\t'); break;
454  case 'x':
455  {
456  ++SPos;
457  // hexadecimal escape: \xAD.
458  // First char must be a hexdigit
459  if (!std::isxdigit(*SPos))
460  {
461  Warn(C4AulWarningId::invalid_hex_escape);
462  strbuf.push_back('\\'); strbuf.push_back('x');
463  }
464  else
465  {
466  char ch = 0;
467  while (std::isxdigit(*SPos))
468  {
469  ch *= 16;
470  if (*SPos >= '0' && *SPos <= '9')
471  ch += *SPos - '0';
472  else if (*SPos >= 'a' && *SPos <= 'f')
473  ch += *SPos - 'a' + 10;
474  else if (*SPos >= 'A' && *SPos <= 'F')
475  ch += *SPos - 'A' + 10;
476  ++SPos;
477  };
478  strbuf.push_back(ch);
479  }
480  break;
481  }
482  case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
483  {
484  // Octal escape: \142
485  char ch = 0;
486  while (SPos[0] >= '0' && SPos[0] <= '7')
487  {
488  ch *= 8;
489  ch += *SPos++ -'0';
490  }
491  strbuf.push_back(ch);
492  break;
493  }
494  default:
495  {
496  // just insert "\"
497  strbuf.push_back('\\');
498  // show warning
499  Warn(C4AulWarningId::invalid_escape_sequence, *(SPos + 1));
500  }
501  }
502  else if (C == 0 || C == 10 || C == 13) // line break / feed
503  throw C4AulParseError(this, "string not closed");
504  else
505  // copy character
506  strbuf.push_back(C);
507  }
508  ++SPos;
509  cStr = Strings.RegString(StdStrBuf(strbuf.data(),strbuf.size()));
510  // hold onto string, ClearToken will deref it
511  cStr->IncRef();
512  return ATT_STRING;
513  }
514  else if (C == '[') return ATT_BOPEN2; // "["
515  else if (C == ']') return ATT_BCLOSE2;// "]"
516  else if (C == '.' && *SPos == '.' && *(SPos + 1) == '.')
517  { SPos+=2; return ATT_LDOTS; } // "..."
518  else if (C == '.') return ATT_DOT; // "."
519  else if (C == ':') return ATT_COLON; // ":"
520  else
521  {
522  // show appropriate error message
523  if (C >= '!' && C <= '~')
524  throw C4AulParseError(this, FormatString("unexpected character '%c' found", C).getData());
525  else
526  throw C4AulParseError(this, FormatString("unexpected character \\x%x found", (int)(unsigned char) C).getData());
527  }
528 }
529 
530 static const char * GetTTName(C4AulBCCType e)
531 {
532  switch (e)
533  {
534  case AB_ARRAYA: return "ARRAYA"; // array access
535  case AB_ARRAYA_SET: return "ARRAYA_SET"; // setter
536  case AB_PROP: return "PROP";
537  case AB_PROP_SET: return "PROP_SET";
538  case AB_ARRAY_SLICE: return "ARRAY_SLICE";
539  case AB_ARRAY_SLICE_SET: return "ARRAY_SLICE_SET";
540  case AB_STACK_SET: return "STACK_SET";
541  case AB_LOCALN: return "LOCALN"; // a named local
542  case AB_LOCALN_SET: return "LOCALN_SET";
543  case AB_GLOBALN: return "GLOBALN"; // a named global
544  case AB_GLOBALN_SET: return "GLOBALN_SET";
545  case AB_PAR: return "PAR"; // Par statement
546  case AB_THIS: return "THIS";
547  case AB_FUNC: return "FUNC"; // function
548 
549 // prefix
550  case AB_Inc: return "Inc"; // ++
551  case AB_Dec: return "Dec"; // --
552  case AB_BitNot: return "BitNot"; // ~
553  case AB_Not: return "Not"; // !
554  case AB_Neg: return "Neg"; // -
555 
556 // postfix
557  case AB_Pow: return "Pow"; // **
558  case AB_Div: return "Div"; // /
559  case AB_Mul: return "Mul"; // *
560  case AB_Mod: return "Mod"; // %
561  case AB_Sub: return "Sub"; // -
562  case AB_Sum: return "Sum"; // +
563  case AB_LeftShift: return "LeftShift"; // <<
564  case AB_RightShift: return "RightShift"; // >>
565  case AB_LessThan: return "LessThan"; // <
566  case AB_LessThanEqual: return "LessThanEqual"; // <=
567  case AB_GreaterThan: return "GreaterThan"; // >
568  case AB_GreaterThanEqual: return "GreaterThanEqual"; // >=
569  case AB_Equal: return "Equal"; // ==
570  case AB_NotEqual: return "NotEqual"; // !=
571  case AB_BitAnd: return "BitAnd"; // &
572  case AB_BitXOr: return "BitXOr"; // ^
573  case AB_BitOr: return "BitOr"; // |
574 
575  case AB_CALL: return "CALL"; // direct object call
576  case AB_CALLFS: return "CALLFS"; // failsafe direct call
577  case AB_STACK: return "STACK"; // push nulls / pop
578  case AB_INT: return "INT"; // constant: int
579  case AB_BOOL: return "BOOL"; // constant: bool
580  case AB_STRING: return "STRING"; // constant: string
581  case AB_CPROPLIST: return "CPROPLIST"; // constant: proplist
582  case AB_CARRAY: return "CARRAY"; // constant: array
583  case AB_CFUNCTION: return "CFUNCTION"; // constant: function
584  case AB_NIL: return "NIL"; // constant: nil
585  case AB_NEW_ARRAY: return "NEW_ARRAY"; // semi-constant: array
586  case AB_DUP: return "DUP"; // duplicate value from stack
587  case AB_DUP_CONTEXT: return "AB_DUP_CONTEXT"; // duplicate value from stack of parent function
588  case AB_NEW_PROPLIST: return "NEW_PROPLIST"; // create a new proplist
589  case AB_POP_TO: return "POP_TO"; // initialization of named var
590  case AB_JUMP: return "JUMP"; // jump
591  case AB_JUMPAND: return "JUMPAND";
592  case AB_JUMPOR: return "JUMPOR";
593  case AB_JUMPNNIL: return "JUMPNNIL"; // nil-coalescing operator ("??")
594  case AB_CONDN: return "CONDN"; // conditional jump (negated, pops stack)
595  case AB_COND: return "COND"; // conditional jump (pops stack)
596  case AB_FOREACH_NEXT: return "FOREACH_NEXT"; // foreach: next element
597  case AB_RETURN: return "RETURN"; // return statement
598  case AB_ERR: return "ERR"; // parse error at this position
599  case AB_DEBUG: return "DEBUG"; // debug break
600  case AB_EOFN: return "EOFN"; // end of function
601  }
602  assert(false); return "UNKNOWN";
603 }
604 
606 {
608  {
609  fprintf(stderr, "%s:\n", GetName());
610  std::map<C4AulBCC *, int> labels;
611  int labeln = 0;
612  for (auto & bcc: Code)
613  {
614  switch (bcc.bccType)
615  {
616  case AB_JUMP: case AB_JUMPAND: case AB_JUMPOR: case AB_JUMPNNIL: case AB_CONDN: case AB_COND:
617  labels[&bcc + bcc.Par.i] = ++labeln; break;
618  default: break;
619  }
620  }
621  for (auto & bcc: Code)
622  {
623  C4AulBCCType eType = bcc.bccType;
624  if (labels.find(&bcc) != labels.end())
625  fprintf(stderr, "%d:\n", labels[&bcc]);
626  fprintf(stderr, "\t%d\t%-20s", GetLineOfCode(&bcc), GetTTName(eType));
627  switch (eType)
628  {
629  case AB_FUNC:
630  fprintf(stderr, "\t%s\n", bcc.Par.f->GetFullName().getData()); break;
631  case AB_ERR:
632  if (bcc.Par.s)
633  case AB_CALL: case AB_CALLFS: case AB_LOCALN: case AB_LOCALN_SET: case AB_PROP: case AB_PROP_SET:
634  fprintf(stderr, "\t%s\n", bcc.Par.s->GetCStr()); break;
635  case AB_STRING:
636  {
637  const StdStrBuf &s = bcc.Par.s->GetData();
638  std::string es;
639  std::for_each(s.getData(), s.getData() + s.getLength(), [&es](char c) {
640  if (std::isgraph((unsigned char)c))
641  {
642  es += c;
643  }
644  else
645  {
646  switch (c)
647  {
648  case '\'': es.append("\\'"); break;
649  case '\"': es.append("\\\""); break;
650  case '\\': es.append("\\\\"); break;
651  case '\a': es.append("\\a"); break;
652  case '\b': es.append("\\b"); break;
653  case '\f': es.append("\\f"); break;
654  case '\n': es.append("\\n"); break;
655  case '\r': es.append("\\r"); break;
656  case '\t': es.append("\\t"); break;
657  case '\v': es.append("\\v"); break;
658  default:
659  {
660  std::stringstream hex;
661  hex << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>((unsigned char)c);
662  es.append(hex.str());
663  break;
664  }
665  }
666  }
667  });
668  fprintf(stderr, "\t\"%s\"\n", es.c_str()); break;
669  }
670  case AB_DEBUG: case AB_NIL: case AB_RETURN:
671  case AB_PAR: case AB_THIS:
673  case AB_EOFN:
674  assert(!bcc.Par.X); fprintf(stderr, "\n"); break;
675  case AB_CARRAY:
676  fprintf(stderr, "\t%s\n", C4VArray(bcc.Par.a).GetDataString().getData()); break;
677  case AB_CPROPLIST:
678  fprintf(stderr, "\t%s\n", C4VPropList(bcc.Par.p).GetDataString().getData()); break;
679  case AB_JUMP: case AB_JUMPAND: case AB_JUMPOR: case AB_JUMPNNIL: case AB_CONDN: case AB_COND:
680  fprintf(stderr, "\t% -d\n", labels[&bcc + bcc.Par.i]); break;
681  default:
682  fprintf(stderr, "\t% -d\n", bcc.Par.i); break;
683  }
684  }
685  }
686 }
687 
689 {
690  // handle easiest case first
691  if (State < ASS_NONE) return false;
692 
693  // clear stuff
694  Includes.clear(); Appends.clear();
695 
696  GetPropList()->C4PropList::Clear();
698  LocalValues.Clear();
699 
700  // Add any engine functions specific to this script
702 
703  // Insert default warnings
704  assert(enabledWarnings.empty());
705  auto &warnings = enabledWarnings[Script.getData()];
706 #define DIAG(id, text, enabled) warnings.set(static_cast<size_t>(C4AulWarningId::id), enabled);
707 #include "C4AulWarnings.h"
708 #undef DIAG
709 
710  C4AulParse parser(this);
711  ast = parser.Parse_Script(this);
712 
713  C4AulCompiler::Preparse(this, this, ast.get());
714 
715  // #include will have to be resolved now...
716  IncludesResolved = false;
717 
718  // Parse will write the properties back after the ones from included scripts
719  GetPropList()->Properties.Swap(&LocalValues);
720 
721  // return success
722  this->State = ASS_PREPARSED;
723  return true;
724 }
725 
726 static const char * GetTokenName(C4AulTokenType TokenType)
727 {
728  switch (TokenType)
729  {
730  case ATT_INVALID: return "invalid token";
731  case ATT_DIR: return "directive";
732  case ATT_IDTF: return "identifier";
733  case ATT_INT: return "integer constant";
734  case ATT_STRING: return "string constant";
735  case ATT_DOT: return "'.'";
736  case ATT_COMMA: return "','";
737  case ATT_COLON: return "':'";
738  case ATT_SCOLON: return "';'";
739  case ATT_BOPEN: return "'('";
740  case ATT_BCLOSE: return "')'";
741  case ATT_BOPEN2: return "'['";
742  case ATT_BCLOSE2: return "']'";
743  case ATT_BLOPEN: return "'{'";
744  case ATT_BLCLOSE: return "'}'";
745  case ATT_CALL: return "'->'";
746  case ATT_CALLFS: return "'->~'";
747  case ATT_LDOTS: return "'...'";
748  case ATT_SET: return "'='";
749  case ATT_OPERATOR: return "operator";
750  case ATT_EOF: return "end of file";
751  default: return "unrecognized token";
752  }
753 }
754 
756 {
757  TokenType = GetNextToken();
758 }
759 void C4AulParse::Check(C4AulTokenType RefTokenType, const char * Expected)
760 {
761  if (TokenType != RefTokenType)
762  UnexpectedToken(Expected ? Expected : GetTokenName(RefTokenType));
763 }
764 void C4AulParse::Match(C4AulTokenType RefTokenType, const char * Expected)
765 {
766  Check(RefTokenType, Expected);
767  Shift();
768 }
769 void C4AulParse::UnexpectedToken(const char * Expected)
770 {
771  throw C4AulParseError(this, FormatString("%s expected, but found %s", Expected, GetTokenName(TokenType)).getData());
772 }
773 
774 void C4AulParse::Parse_WarningPragma()
775 {
776  assert(SEqual2(TokenSPos, C4AUL_Warning));
777  assert(std::isspace(TokenSPos[sizeof(C4AUL_Warning) - 1]));
778 
779 
780  // Read parameters in to string buffer. The sizeof() includes the terminating \0, but
781  // that's okay because we need to skip (at least) one whitespace character anyway.
782  std::string line(TokenSPos + sizeof(C4AUL_Warning), SPos);
783  auto end = line.end();
784  auto cursor = std::find_if_not(begin(line), end, IsWhiteSpace);
785 
786  if (cursor == end)
787  throw C4AulParseError(this, "'" C4Aul_Warning_enable "' or '" C4Aul_Warning_disable "' expected, but found end of line");
788 
789  // Split directive on whitespace
790  auto start = cursor;
791  cursor = std::find_if(start, end, IsWhiteSpace);
792  bool enable_warning = false;
793  if (std::equal(start, cursor, C4Aul_Warning_enable))
794  {
795  enable_warning = true;
796  }
797  else if (std::equal(start, cursor, C4Aul_Warning_disable))
798  {
799  enable_warning = false;
800  }
801  else
802  {
803  throw C4AulParseError(this, FormatString("'" C4Aul_Warning_enable "' or '" C4Aul_Warning_disable "' expected, but found '%s'", std::string(start, cursor).c_str()).getData());
804  }
805 
806  cursor = std::find_if_not(cursor, end, IsWhiteSpace);
807  if (cursor == end)
808  {
809  // enable or disable all warnings
810 #define DIAG(id, text, enabled) pOrgScript->EnableWarning(TokenSPos, C4AulWarningId::id, enable_warning);
811 #include "C4AulWarnings.h"
812 #undef DIAG
813  return;
814  }
815 
816  // enable or disable specific warnings
817  static const std::map<std::string, C4AulWarningId> warnings{
818 #define DIAG(id, text, enabled) std::make_pair(#id, C4AulWarningId::id),
819 #include "C4AulWarnings.h"
820 #undef DIAG
821  };
822  while (cursor != end)
823  {
824  start = std::find_if_not(cursor, end, IsWhiteSpace);
825  cursor = std::find_if(start, end, IsWhiteSpace);
826  auto entry = warnings.find(std::string(start, cursor));
827  if (entry != warnings.end())
828  {
829  pOrgScript->EnableWarning(TokenSPos, entry->second, enable_warning);
830  }
831  }
832 }
833 
835 {
836  ClearCode();
837  // parse
838  C4AulParse state(this, context, Engine);
839  auto func = state.Parse_DirectExec(Script, true);
840  C4AulCompiler::Compile(this, func.get());
841 }
842 
844 {
845  ClearCode();
846  // parse
847  C4AulParse state(this, context, Engine);
848  auto func = state.Parse_DirectExec(Script, false);
849  C4AulCompiler::Compile(this, func.get());
850 }
851 
852 std::unique_ptr<::aul::ast::FunctionDecl> C4AulParse::Parse_DirectExec(const char *code, bool whole_function)
853 {
854  // get first token
855  Shift();
856  // Synthesize a wrapping function which we can call
857  std::unique_ptr<::aul::ast::FunctionDecl> func;
858  if (whole_function)
859  {
861  }
862  else
863  {
864  auto expr = Parse_Expression();
865  func = std::make_unique<::aul::ast::FunctionDecl>("$internal$eval");
866  func->body = std::make_unique<::aul::ast::Block>();
867  func->body->children.push_back(std::make_unique<::aul::ast::Return>(std::move(expr)));
868  }
869  Match(ATT_EOF);
870  return func;
871 }
872 
873 std::unique_ptr<::aul::ast::Script> C4AulParse::Parse_Script(C4ScriptHost * scripthost)
874 {
875  pOrgScript = scripthost;
876  SPos = pOrgScript->Script.getData();
877  const char * SPos0 = SPos;
878  bool first_error = true;
879  auto script = ::aul::ast::Script::New(SPos0);
880  while (true) try
881  {
882  // Go to the next token if the current token could not be processed or no token has yet been parsed
883  if (SPos == SPos0)
884  {
885  Shift();
886  }
887  SPos0 = SPos;
888  switch (TokenType)
889  {
890  case ATT_DIR:
891  // check for include statement
892  if (SEqual(Idtf, C4AUL_Include))
893  {
894  Shift();
895  // get id of script to include
896  Check(ATT_IDTF, "script name");
897  script->declarations.push_back(::aul::ast::IncludePragma::New(TokenSPos, Idtf));
898  Shift();
899  }
900  else if (SEqual(Idtf, C4AUL_Append))
901  {
902  if (pOrgScript->GetPropList()->GetDef())
903  throw C4AulParseError(this, "#appendto in a Definition");
904  Shift();
905  // get id of script to include/append
906  switch (TokenType)
907  {
908  case ATT_IDTF:
909  script->declarations.push_back(::aul::ast::AppendtoPragma::New(TokenSPos, Idtf));
910  break;
911  case ATT_OPERATOR:
912  if (SEqual(C4ScriptOpMap[cInt].Identifier, "*"))
913  {
914  script->declarations.push_back(::aul::ast::AppendtoPragma::New(TokenSPos));
915  break;
916  }
917  //fallthrough
918  default:
919  // -> ID expected
920  UnexpectedToken("identifier or '*'");
921  }
922  Shift();
923  }
924  else
925  // -> unknown directive
926  Error("unknown directive: %s", Idtf);
927  break;
928  case ATT_IDTF:
929  // need a keyword here to avoid parsing random function contents
930  // after a syntax error in a function
931  // check for object-local variable definition (local)
932  if (SEqual(Idtf, C4AUL_LocalNamed) || SEqual(Idtf, C4AUL_GlobalNamed))
933  {
934  script->declarations.push_back(Parse_Var());
935  Match(ATT_SCOLON);
936  }
937  // check for variable definition (static)
938  else
939  script->declarations.push_back(Parse_ToplevelFunctionDecl());
940  break;
941  case ATT_EOF:
942  return script;
943  default:
944  UnexpectedToken("declaration");
945  }
946  first_error = true;
947  }
948  catch (C4AulError &err)
949  {
950  if (first_error)
951  {
952  ++Engine->errCnt;
954  }
955  first_error = false;
956  }
957 }
958 
959 std::unique_ptr<::aul::ast::FunctionDecl> C4AulParse::Parse_ToplevelFunctionDecl()
960 {
961  const char *NodeStart = TokenSPos;
962  bool is_global = SEqual(Idtf, C4AUL_Global);
963  // skip access modifier
964  if (SEqual(Idtf, C4AUL_Private) ||
965  SEqual(Idtf, C4AUL_Protected) ||
966  SEqual(Idtf, C4AUL_Public) ||
967  SEqual(Idtf, C4AUL_Global))
968  {
969  Shift();
970  }
971 
972  // check for func declaration
973  if (!SEqual(Idtf, C4AUL_Func))
974  Error("Declaration expected, but found identifier: %s", Idtf);
975  Shift();
976  // get next token, must be func name
977  Check(ATT_IDTF, "function name");
978 
979  auto func = ::aul::ast::FunctionDecl::New(NodeStart, Idtf);
980  func->is_global = is_global;
981  Shift();
982  Parse_Function(func.get());
983  return func;
984 }
985 
986 void C4AulParse::Parse_Function(::aul::ast::Function *func)
987 {
988  Match(ATT_BOPEN);
989  // get pars
990  while (TokenType != ATT_BCLOSE)
991  {
992  // too many parameters?
993  if (func->params.size() >= C4AUL_MAX_Par)
994  throw C4AulParseError(this, "'func' parameter list: too many parameters (max 10)");
995  if (TokenType == ATT_LDOTS)
996  {
997  func->has_unnamed_params = true;
998  Shift();
999  // don't allow any more parameters after ellipsis
1000  break;
1001  }
1002  // must be a name or type now
1003  Check(ATT_IDTF, "parameter, '...', or ')'");
1004  // type identifier?
1005  C4V_Type t = C4V_Any;
1006  if (SEqual(Idtf, C4AUL_TypeInt)) { t = C4V_Int; Shift(); }
1007  else if (SEqual(Idtf, C4AUL_TypeBool)) { t = C4V_Bool; Shift(); }
1008  else if (SEqual(Idtf, C4AUL_TypeC4ID)) { t = C4V_Def; Shift(); }
1009  else if (SEqual(Idtf, C4AUL_TypeDef)) { t = C4V_Def; Shift(); }
1010  else if (SEqual(Idtf, C4AUL_TypeEffect)) { t = C4V_Effect; Shift(); }
1011  else if (SEqual(Idtf, C4AUL_TypeC4Object)) { t = C4V_Object; Shift(); }
1012  else if (SEqual(Idtf, C4AUL_TypePropList)) { t = C4V_PropList; Shift(); }
1013  else if (SEqual(Idtf, C4AUL_TypeString)) { t = C4V_String; Shift(); }
1014  else if (SEqual(Idtf, C4AUL_TypeArray)) { t = C4V_Array; Shift(); }
1015  else if (SEqual(Idtf, C4AUL_TypeFunction)) { t = C4V_Function; Shift(); }
1016  // a parameter name which matched a type name?
1017  std::string par_name;
1018  if (TokenType == ATT_BCLOSE || TokenType == ATT_COMMA)
1019  {
1020  par_name = Idtf;
1021  Warn(C4AulWarningId::type_name_used_as_par_name, Idtf);
1022  }
1023  else
1024  {
1025  Check(ATT_IDTF, "parameter name");
1026  par_name = Idtf;
1027  Shift();
1028  }
1029  func->params.emplace_back(par_name, t);
1030  // end of params?
1031  if (TokenType == ATT_BCLOSE)
1032  {
1033  break;
1034  }
1035  // must be a comma now
1036  Match(ATT_COMMA, "',' or ')'");
1037  }
1038  Match(ATT_BCLOSE);
1039  func->body = Parse_Block();
1040 }
1041 
1042 std::unique_ptr<::aul::ast::Block> C4AulParse::Parse_Block()
1043 {
1044  auto block = ::aul::ast::Block::New(TokenSPos);
1045  Match(ATT_BLOPEN);
1046  while (TokenType != ATT_BLCLOSE)
1047  {
1048  block->children.push_back(Parse_Statement());
1049  }
1050  Shift();
1051  return block;
1052 }
1053 
1054 std::unique_ptr<::aul::ast::Stmt> C4AulParse::Parse_Statement()
1055 {
1056  const char *NodeStart = TokenSPos;
1057  std::unique_ptr<::aul::ast::Stmt> stmt;
1058  switch (TokenType)
1059  {
1060  // do we have a block start?
1061  case ATT_BLOPEN:
1062  return Parse_Block();
1063  case ATT_BOPEN:
1064  case ATT_BOPEN2:
1065  case ATT_SET:
1066  case ATT_OPERATOR:
1067  case ATT_INT:
1068  case ATT_STRING:
1069  {
1070  stmt = Parse_Expression();
1071  Match(ATT_SCOLON);
1072  return stmt;
1073  }
1074  // additional function separator
1075  case ATT_SCOLON:
1076  Shift();
1077  return ::aul::ast::Noop::New(NodeStart);
1078  case ATT_IDTF:
1079  // check for variable definition
1080  if (SEqual(Idtf, C4AUL_VarNamed) || SEqual(Idtf, C4AUL_LocalNamed) || SEqual(Idtf, C4AUL_GlobalNamed))
1081  stmt = Parse_Var();
1082  // check new-form func begin
1083  else if (SEqual(Idtf, C4AUL_Func) ||
1084  SEqual(Idtf, C4AUL_Private) ||
1085  SEqual(Idtf, C4AUL_Protected) ||
1086  SEqual(Idtf, C4AUL_Public) ||
1087  SEqual(Idtf, C4AUL_Global))
1088  {
1089  throw C4AulParseError(this, "unexpected end of function");
1090  }
1091  // get function by identifier: first check special functions
1092  else if (SEqual(Idtf, C4AUL_If)) // if
1093  {
1094  return Parse_If();
1095  }
1096  else if (SEqual(Idtf, C4AUL_Else)) // else
1097  {
1098  throw C4AulParseError(this, "misplaced 'else'");
1099  }
1100  else if (SEqual(Idtf, C4AUL_Do)) // while
1101  {
1102  stmt = Parse_DoWhile();
1103  }
1104  else if (SEqual(Idtf, C4AUL_While)) // while
1105  {
1106  return Parse_While();
1107  }
1108  else if (SEqual(Idtf, C4AUL_For)) // for
1109  {
1110  PushParsePos();
1111  Shift();
1112  // Look if it's the for([var] foo in array)-form
1113  // must be followed by a bracket
1114  Match(ATT_BOPEN);
1115  // optional var
1116  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_VarNamed))
1117  Shift();
1118  // variable and "in"
1119  if (TokenType == ATT_IDTF
1120  && GetNextToken() == ATT_IDTF
1121  && SEqual(Idtf, C4AUL_In))
1122  {
1123  // reparse the stuff in the brackets like normal statements
1124  PopParsePos();
1125  return Parse_ForEach();
1126  }
1127  else
1128  {
1129  // reparse the stuff in the brackets like normal statements
1130  PopParsePos();
1131  return Parse_For();
1132  }
1133  }
1134  else if (SEqual(Idtf, C4AUL_Return)) // return
1135  {
1136  Shift();
1137  if (TokenType == ATT_SCOLON)
1138  {
1139  // allow return; without return value (implies nil)
1140  stmt = ::aul::ast::Return::New(NodeStart, ::aul::ast::NilLit::New(NodeStart));
1141  }
1142  else
1143  {
1144  // return retval;
1145  stmt = ::aul::ast::Return::New(NodeStart, Parse_Expression());
1146  }
1147  }
1148  else if (SEqual(Idtf, C4AUL_Break)) // break
1149  {
1150  Shift();
1151  stmt = ::aul::ast::Break::New(NodeStart);
1152  }
1153  else if (SEqual(Idtf, C4AUL_Continue)) // continue
1154  {
1155  Shift();
1156  stmt = ::aul::ast::Continue::New(NodeStart);
1157  }
1158  else
1159  {
1160  stmt = Parse_Expression();
1161  }
1162  Match(ATT_SCOLON);
1163  assert(stmt);
1164  return stmt;
1165  default:
1166  UnexpectedToken("statement");
1167  }
1168 }
1169 
1170 void C4AulParse::Parse_CallParams(::aul::ast::CallExpr *call)
1171 {
1172  assert(call != nullptr);
1173  assert(call->args.empty());
1174 
1175  // so it's a regular function; force "("
1176  Match(ATT_BOPEN);
1177  while(TokenType != ATT_BCLOSE) switch(TokenType)
1178  {
1179  case ATT_COMMA:
1180  // got no parameter before a ","
1181  Warn(C4AulWarningId::empty_parameter_in_call, (unsigned)call->args.size(), call->callee.c_str());
1182  call->args.push_back(::aul::ast::NilLit::New(TokenSPos));
1183  Shift();
1184  break;
1185  case ATT_LDOTS:
1186  // functions using ... always take as many parameters as possible
1187  Shift();
1188  call->append_unnamed_pars = true;
1189  // Do not allow more parameters even if there is space left
1190  Check(ATT_BCLOSE);
1191  break;
1192  default:
1193  // get a parameter
1194  call->args.push_back(Parse_Expression());
1195  // end of parameter list?
1196  if (TokenType != ATT_BCLOSE)
1197  Match(ATT_COMMA, "',' or ')'");
1198  break;
1199  }
1200  Match(ATT_BCLOSE);
1201 }
1202 
1203 std::unique_ptr<::aul::ast::ArrayLit> C4AulParse::Parse_Array()
1204 {
1205  auto arr = ::aul::ast::ArrayLit::New(TokenSPos);
1206  // force "["
1207  Match(ATT_BOPEN2);
1208  // Create an array
1209  while (TokenType != ATT_BCLOSE2)
1210  {
1211  // got no parameter before a ","? then push nil
1212  if (TokenType == ATT_COMMA)
1213  {
1214  Warn(C4AulWarningId::empty_parameter_in_array, (unsigned)arr->values.size());
1215  arr->values.emplace_back(::aul::ast::NilLit::New(TokenSPos));
1216  }
1217  else
1218  arr->values.emplace_back(Parse_Expression());
1219  if (TokenType == ATT_BCLOSE2)
1220  break;
1221  Match(ATT_COMMA, "',' or ']'");
1222  // [] -> size 0, [*,] -> size 2, [*,*,] -> size 3
1223  if (TokenType == ATT_BCLOSE2)
1224  {
1225  Warn(C4AulWarningId::empty_parameter_in_array, (unsigned)arr->values.size());
1226  arr->values.emplace_back(::aul::ast::NilLit::New(TokenSPos));
1227  }
1228  }
1229  Shift();
1230  return arr;
1231 }
1232 
1233 std::unique_ptr<::aul::ast::ProplistLit> C4AulParse::Parse_PropList()
1234 {
1235  auto proplist = ::aul::ast::ProplistLit::New(TokenSPos);
1236  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_New))
1237  {
1238  Shift();
1239  proplist->values.emplace_back(Strings.P[P_Prototype].GetCStr(), Parse_Expression());
1240  }
1241  Match(ATT_BLOPEN);
1242  while (TokenType != ATT_BLCLOSE)
1243  {
1244  std::string key;
1245  if (TokenType == ATT_IDTF)
1246  {
1247  key = Idtf;
1248  Shift();
1249  }
1250  else if (TokenType == ATT_STRING)
1251  {
1252  key = cStr->GetCStr();
1253  Shift();
1254  }
1255  else UnexpectedToken("string or identifier");
1256  if (TokenType != ATT_COLON && TokenType != ATT_SET)
1257  UnexpectedToken("':' or '='");
1258  Shift();
1259  proplist->values.emplace_back(key, Parse_Expression());
1260  if (TokenType == ATT_COMMA)
1261  Shift();
1262  else if (TokenType != ATT_BLCLOSE)
1263  UnexpectedToken("'}' or ','");
1264  }
1265  Shift();
1266  return proplist;
1267 }
1268 
1269 std::unique_ptr<::aul::ast::DoLoop> C4AulParse::Parse_DoWhile()
1270 {
1271  auto loop = ::aul::ast::DoLoop::New(TokenSPos);
1272  Shift();
1273  loop->body = Parse_Statement();
1274  // Execute condition
1275  if (TokenType != ATT_IDTF || !SEqual(Idtf, C4AUL_While))
1276  UnexpectedToken("'while'");
1277  Shift();
1278  Match(ATT_BOPEN);
1279  loop->cond = Parse_Expression();
1280  Match(ATT_BCLOSE);
1281  return loop;
1282 }
1283 
1284 std::unique_ptr<::aul::ast::WhileLoop> C4AulParse::Parse_While()
1285 {
1286  auto loop = ::aul::ast::WhileLoop::New(TokenSPos);
1287  Shift();
1288  // Execute condition
1289  Match(ATT_BOPEN);
1290  loop->cond = Parse_Expression();
1291  Match(ATT_BCLOSE);
1292  // Execute body
1293  loop->body = Parse_Statement();
1294  return loop;
1295 }
1296 
1297 std::unique_ptr<::aul::ast::If> C4AulParse::Parse_If()
1298 {
1299  auto stmt = ::aul::ast::If::New(TokenSPos);
1300  Shift();
1301  Match(ATT_BOPEN);
1302  stmt->cond = Parse_Expression();
1303  Match(ATT_BCLOSE);
1304  // parse controlled statement
1305  stmt->iftrue = Parse_Statement();
1306  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_Else))
1307  {
1308  Shift();
1309  // expect a command now
1310  stmt->iffalse = Parse_Statement();
1311  }
1312  return stmt;
1313 }
1314 
1315 std::unique_ptr<::aul::ast::ForLoop> C4AulParse::Parse_For()
1316 {
1317  auto loop = ::aul::ast::ForLoop::New(TokenSPos);
1318  Match(ATT_IDTF); Match(ATT_BOPEN);
1319  // Initialization
1320  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_VarNamed))
1321  {
1322  loop->init = Parse_Var();
1323  }
1324  else if (TokenType != ATT_SCOLON)
1325  {
1326  loop->init = Parse_Expression();
1327  }
1328  // Consume first semicolon
1329  Match(ATT_SCOLON);
1330  // Condition
1331  if (TokenType != ATT_SCOLON)
1332  {
1333  loop->cond = Parse_Expression();
1334  }
1335  // Consume second semicolon
1336  Match(ATT_SCOLON);
1337  // Incrementor
1338  if (TokenType != ATT_BCLOSE)
1339  {
1340  loop->incr = Parse_Expression();
1341  }
1342  // Consume closing bracket
1343  Match(ATT_BCLOSE);
1344  loop->body = Parse_Statement();
1345  return loop;
1346 }
1347 
1348 std::unique_ptr<::aul::ast::RangeLoop> C4AulParse::Parse_ForEach()
1349 {
1350  auto loop = ::aul::ast::RangeLoop::New(TokenSPos);
1351  Match(ATT_IDTF); Match(ATT_BOPEN);
1352  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_VarNamed))
1353  {
1354  loop->scoped_var = true;
1355  Shift();
1356  }
1357  // get variable name
1358  Check(ATT_IDTF, "variable name");
1359  loop->var = Idtf;
1360  Shift();
1361  if (TokenType != ATT_IDTF || !SEqual(Idtf, C4AUL_In))
1362  UnexpectedToken("'in'");
1363  Shift();
1364  // get expression for array
1365  loop->cond = Parse_Expression();
1366  Match(ATT_BCLOSE);
1367  // loop body
1368  loop->body = Parse_Statement();
1369  return loop;
1370 }
1371 
1372 static bool GetPropertyByS(const C4PropList * p, const char * s, C4Value & v)
1373 {
1374  C4String * k = Strings.FindString(s);
1375  if (!k) return false;
1376  return p->GetPropertyByS(k, &v);
1377 }
1378 
1379 std::unique_ptr<::aul::ast::Expr> C4AulParse::Parse_Expression(int iParentPrio)
1380 {
1381  const char *NodeStart = TokenSPos;
1382  std::unique_ptr<::aul::ast::Expr> expr;
1383  const C4ScriptOpDef * op;
1384  C4AulFunc *FoundFn = 0;
1385  C4Value val;
1386  switch (TokenType)
1387  {
1388  case ATT_IDTF:
1389  // XXX: Resolving literals here means that you can't create a variable
1390  // with the names "true", "false", "nil" or "this" anymore. I don't
1391  // consider this too much of a problem, because there is never a reason
1392  // to do this and it makes my job a lot easier
1393  if (SEqual(Idtf, C4AUL_True))
1394  {
1395  Shift();
1396  expr = ::aul::ast::BoolLit::New(NodeStart, true);
1397  }
1398  else if (SEqual(Idtf, C4AUL_False))
1399  {
1400  Shift();
1401  expr = ::aul::ast::BoolLit::New(NodeStart, false);
1402  }
1403  else if (SEqual(Idtf, C4AUL_Nil))
1404  {
1405  Shift();
1406  expr = ::aul::ast::NilLit::New(NodeStart);
1407  }
1408  else if (SEqual(Idtf, C4AUL_this))
1409  {
1410  Shift();
1411  if (TokenType == ATT_BOPEN)
1412  {
1413  Shift();
1414  Match(ATT_BCLOSE);
1415  // TODO: maybe warn about "this" with parentheses?
1416  }
1417  expr = ::aul::ast::ThisLit::New(NodeStart);
1418  }
1419  // XXX: Other things that people aren't allowed to do anymore: name their variables or functions any of:
1420  // "if", "else", "for", "while", "do", "return", or "Par".
1421  // We could allow variables with these names and disambiguate based on the syntax, but no.
1422  else if (SEqual(Idtf, C4AUL_If) || SEqual(Idtf, C4AUL_Else) || SEqual(Idtf, C4AUL_For) || SEqual(Idtf, C4AUL_While) || SEqual(Idtf, C4AUL_Do) || SEqual(Idtf, C4AUL_Return))
1423  {
1424  Error("reserved identifier not allowed in expressions: %s", Idtf);
1425  }
1426  else if (SEqual(Idtf, C4AUL_Par))
1427  {
1428  Shift();
1429  // "Par" is special in that it isn't a function and thus doesn't accept an arbitrary number of parameters
1430  Match(ATT_BOPEN);
1431  expr = ::aul::ast::ParExpr::New(NodeStart, Parse_Expression());
1432  Match(ATT_BCLOSE);
1433  }
1434  else if (SEqual(Idtf, C4AUL_New))
1435  {
1436  // Because people might call a variables or functions "new", we need to look ahead and guess whether it's a proplist constructor.
1437  PushParsePos();
1438  Shift();
1439  if (TokenType == ATT_IDTF)
1440  {
1441  // this must be a proplist because two identifiers can't immediately follow each other
1442  PopParsePos();
1443  expr = Parse_PropList();
1444  }
1445  else
1446  {
1447  // Some non-identifier means this is either a variable, a function, or a syntax error. Which one exactly is something we'll figure out later.
1448  PopParsePos();
1449  }
1450  }
1451  else if (SEqual(Idtf, C4AUL_Func))
1452  {
1453  PushParsePos();
1454  Shift();
1455  if (TokenType == ATT_BOPEN)
1456  {
1457  auto func = ::aul::ast::FunctionExpr::New(NodeStart);
1458  Parse_Function(func.get());
1459  expr = std::move(func);
1460  DiscardParsePos();
1461  }
1462  else
1463  {
1464  PopParsePos();
1465  }
1466  }
1467  if (!expr)
1468  {
1469  // If we end up here, it must be a proper identifier (or a reserved word that's used as an identifier).
1470  // Time to look ahead and see whether it's a function call.
1471  std::string identifier = Idtf;
1472  Shift();
1473  if (TokenType == ATT_BOPEN)
1474  {
1475  // Well, it looks like one, at least
1476  auto func = ::aul::ast::CallExpr::New(NodeStart);
1477  func->callee = identifier;
1478  Parse_CallParams(func.get());
1479  expr = std::move(func);
1480  }
1481  else
1482  {
1483  // It's most certainly not a function call.
1484  expr = ::aul::ast::VarExpr::New(NodeStart, identifier);
1485  }
1486  }
1487  break;
1488  case ATT_INT: // constant in cInt
1489  expr = ::aul::ast::IntLit::New(NodeStart, cInt);
1490  Shift();
1491  break;
1492  case ATT_STRING: // reference in cStr
1493  expr = ::aul::ast::StringLit::New(NodeStart, cStr->GetCStr());
1494  Shift();
1495  break;
1496  case ATT_OPERATOR:
1497  // -> must be a prefix operator
1498  op = &C4ScriptOpMap[cInt];
1499  // postfix?
1500  if (op->Postfix)
1501  // oops. that's wrong
1502  throw C4AulParseError(this, "postfix operator without first expression");
1503  Shift();
1504  // generate code for the following expression
1505  expr = Parse_Expression(op->Priority);
1506  if (SEqual(op->Identifier, "+"))
1507  {
1508  // This is a no-op.
1509  }
1510  else
1511  {
1512  expr = ::aul::ast::UnOpExpr::New(NodeStart, op - C4ScriptOpMap, std::move(expr));
1513  }
1514  break;
1515  case ATT_BOPEN:
1516  Shift();
1517  expr = Parse_Expression();
1518  Match(ATT_BCLOSE);
1519  break;
1520  case ATT_BOPEN2:
1521  expr = Parse_Array();
1522  break;
1523  case ATT_BLOPEN:
1524  expr = Parse_PropList();
1525  break;
1526  default:
1527  UnexpectedToken("expression");
1528  }
1529 
1530  assert(expr);
1531 
1532  while (1)
1533  {
1534  NodeStart = TokenSPos;
1535  switch (TokenType)
1536  {
1537  case ATT_SET:
1538  {
1539  // back out of any kind of parent operator
1540  // (except other setters, as those are right-associative)
1541  if (iParentPrio > 1)
1542  return expr;
1543  Shift();
1544  expr = ::aul::ast::AssignmentExpr::New(NodeStart, std::move(expr), Parse_Expression(1));
1545  break;
1546  }
1547  case ATT_OPERATOR:
1548  {
1549  // expect postfix operator
1550  const C4ScriptOpDef * op = &C4ScriptOpMap[cInt];
1551  if (!op->Postfix)
1552  {
1553  // does an operator with the same name exist?
1554  // when it's a postfix-operator, it can be used instead.
1555  const C4ScriptOpDef * postfixop;
1556  for (postfixop = op + 1; postfixop->Identifier; ++postfixop)
1557  if (SEqual(op->Identifier, postfixop->Identifier))
1558  if (postfixop->Postfix)
1559  break;
1560  // not found?
1561  if (!postfixop->Identifier)
1562  {
1563  Error("unexpected prefix operator: %s", op->Identifier);
1564  }
1565  // otherwise use the new-found correct postfix operator
1566  op = postfixop;
1567  }
1568 
1569  if (iParentPrio + !op->Changer > op->Priority)
1570  return expr;
1571 
1572  Shift();
1573  if (op->NoSecondStatement)
1574  {
1575  // Postfix unary op
1576  expr = ::aul::ast::UnOpExpr::New(NodeStart, op - C4ScriptOpMap, std::move(expr));
1577  }
1578  else
1579  {
1580  expr = ::aul::ast::BinOpExpr::New(NodeStart, op - C4ScriptOpMap, std::move(expr), Parse_Expression(op->Priority));
1581  }
1582  break;
1583  }
1584  case ATT_BOPEN2:
1585  {
1586  // parse either [index], or [start:end] in which case either index is optional
1587  Shift();
1588  ::aul::ast::ExprPtr start;
1589  if (TokenType == ATT_COLON)
1590  start = ::aul::ast::IntLit::New(TokenSPos, 0); // slice with first index missing -> implicit start index zero
1591  else
1592  start = Parse_Expression();
1593 
1594  if (TokenType == ATT_BCLOSE2)
1595  {
1596  expr = ::aul::ast::SubscriptExpr::New(NodeStart, std::move(expr), std::move(start));
1597  }
1598  else if (TokenType == ATT_COLON)
1599  {
1600  Shift();
1601  ::aul::ast::ExprPtr end;
1602  if (TokenType == ATT_BCLOSE2)
1603  {
1604  end = ::aul::ast::IntLit::New(TokenSPos, std::numeric_limits<int32_t>::max());
1605  }
1606  else
1607  {
1608  end = Parse_Expression();
1609  }
1610  expr = ::aul::ast::SliceExpr::New(NodeStart, std::move(expr), std::move(start), std::move(end));
1611  }
1612  else
1613  {
1614  UnexpectedToken("']' or ':'");
1615  }
1616  Match(ATT_BCLOSE2);
1617  break;
1618  }
1619  case ATT_DOT:
1620  Shift();
1621  Check(ATT_IDTF, "property name");
1622  expr = ::aul::ast::SubscriptExpr::New(NodeStart, std::move(expr), ::aul::ast::StringLit::New(TokenSPos, Idtf));
1623  Shift();
1624  break;
1625  case ATT_CALL: case ATT_CALLFS:
1626  {
1627  auto call = ::aul::ast::CallExpr::New(NodeStart);
1628  call->context = std::move(expr);
1629  call->safe_call = TokenType == ATT_CALLFS;
1630  Shift();
1631  Check(ATT_IDTF, "function name after '->'");
1632  call->callee = Idtf;
1633  Shift();
1634  Parse_CallParams(call.get());
1635  expr = std::move(call);
1636  break;
1637  }
1638  default:
1639  return expr;
1640  }
1641  }
1642 }
1643 
1644 std::unique_ptr<::aul::ast::VarDecl> C4AulParse::Parse_Var()
1645 {
1646  auto decl = ::aul::ast::VarDecl::New(TokenSPos);
1647  if (SEqual(Idtf, C4AUL_VarNamed))
1648  {
1649  decl->scope = ::aul::ast::VarDecl::Scope::Func;
1650  }
1651  else if (SEqual(Idtf, C4AUL_LocalNamed))
1652  {
1653  decl->scope = ::aul::ast::VarDecl::Scope::Object;
1654  }
1655  else if (SEqual(Idtf, C4AUL_GlobalNamed))
1656  {
1657  decl->scope = ::aul::ast::VarDecl::Scope::Global;
1658  }
1659  else
1660  {
1661  assert(0 && "C4AulParse::Parse_Var called with invalid parse state (current token should be scope of variable)");
1662  // Uh this shouldn't happen, ever
1663  Error("internal error: C4AulParse::Parse_Var called with invalid parse state (current token should be scope of variable, but is '%s')", Idtf);
1664  }
1665  Shift();
1666  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_Const))
1667  {
1668  decl->constant = true;
1669  Shift();
1670  }
1671  while (1)
1672  {
1673  // get desired variable name
1674  Check(ATT_IDTF, "variable name");
1675  std::string identifier = Idtf;
1676  Shift();
1677  ::aul::ast::ExprPtr init;
1678  if (TokenType == ATT_SET)
1679  {
1680  Shift();
1681  init = Parse_Expression();
1682  }
1683  decl->decls.push_back({ identifier, std::move(init) });
1684  if (TokenType == ATT_SCOLON)
1685  return decl;
1686  Match(ATT_COMMA, "',' or ';'");
1687  }
1688 }
1689 
1691 {
1692  // append all funcs and local variable initializations
1693  const C4Property * prop = from.First();
1694  while (prop)
1695  {
1696  switch(prop->Value.GetType())
1697  {
1698  case C4V_Function:
1699  {
1700  C4AulScriptFunc * sf = prop->Value.getFunction()->SFunc();
1701  if (sf)
1702  {
1703  C4AulScriptFunc *sfc;
1704  if (sf->pOrgScript != this)
1705  sfc = new C4AulScriptFunc(to, *sf);
1706  else
1707  sfc = sf;
1708  sfc->SetOverloaded(to->GetFunc(sf->Name));
1709  to->SetPropertyByS(prop->Key, C4VFunction(sfc));
1710  }
1711  else
1712  {
1713  // engine function
1714  to->SetPropertyByS(prop->Key, prop->Value);
1715  }
1716  }
1717  break;
1718  case C4V_PropList:
1719  {
1720  C4PropListStatic * p = prop->Value._getPropList()->IsStatic();
1721  assert(p);
1722  if (prop->Key != &::Strings.P[P_Prototype])
1723  if (!p || p->GetParent() != to)
1724  {
1725  p = C4PropList::NewStatic(nullptr, to, prop->Key);
1726  CopyPropList(prop->Value._getPropList()->Properties, p);
1727  }
1728  to->SetPropertyByS(prop->Key, C4VPropList(p));
1729  }
1730  break;
1731  case C4V_Array: // FIXME: copy the array if necessary
1732  default:
1733  to->SetPropertyByS(prop->Key, prop->Value);
1734  }
1735  prop = from.Next(prop);
1736  }
1737 }
1738 
1740 {
1741  // check state
1742  if (State != ASS_LINKED) return false;
1743 
1744  if (!Appends.empty())
1745  {
1746  // #appendto scripts are not allowed to contain global functions or belong to definitions
1747  // so their contents are not reachable
1748  return true;
1749  }
1750 
1751  C4PropListStatic * p = GetPropList();
1752 
1753  for (std::list<C4ScriptHost *>::iterator s = SourceScripts.begin(); s != SourceScripts.end(); ++s)
1754  {
1755  CopyPropList((*s)->LocalValues, p);
1756  if (*s == this)
1757  continue;
1758  // definition appends
1759  if (GetPropList() && GetPropList()->GetDef() && (*s)->GetPropList() && (*s)->GetPropList()->GetDef())
1760  GetPropList()->GetDef()->IncludeDefinition((*s)->GetPropList()->GetDef());
1761  }
1762 
1763  // generate bytecode
1764  for (auto &s : SourceScripts)
1765  C4AulCompiler::Compile(this, s, s->ast.get());
1766 
1767  // save line count
1769 
1770  // finished
1771  State = ASS_PARSED;
1772 
1773  return true;
1774 }
1775 
1776 void C4ScriptHost::EnableWarning(const char *pos, C4AulWarningId warning, bool enable)
1777 {
1778  auto entry = enabledWarnings.emplace(pos, decltype(enabledWarnings)::mapped_type{});
1779  if (entry.second)
1780  {
1781  // If there was no earlier entry for this position, copy the previous
1782  // warning state
1783  assert(entry.first != enabledWarnings.begin());
1784  auto previous = entry.first;
1785  --previous;
1786  entry.first->second = previous->second;
1787  }
1788  entry.first->second.set(static_cast<size_t>(warning), enable);
1789 }
1790 
1791 bool C4ScriptHost::IsWarningEnabled(const char *pos, C4AulWarningId warning) const
1792 {
1793  assert(!enabledWarnings.empty());
1794  if (enabledWarnings.empty())
1795  return false;
1796 
1797  // find nearest set of warnings at or before the current position
1798  auto entry = enabledWarnings.upper_bound(pos);
1799  assert(entry != enabledWarnings.begin());
1800  if (entry != enabledWarnings.begin())
1801  {
1802  --entry;
1803  }
1804 
1805  return entry->second.test(static_cast<size_t>(warning));
1806 }
1807 
1808 void C4AulParse::PushParsePos()
1809 {
1810  parse_pos_stack.push(TokenSPos);
1811 }
1812 
1813 void C4AulParse::PopParsePos()
1814 {
1815  assert(!parse_pos_stack.empty());
1816  SPos = parse_pos_stack.top();
1817  DiscardParsePos();
1818  Shift();
1819 }
1820 
1821 void C4AulParse::DiscardParsePos()
1822 {
1823  assert(!parse_pos_stack.empty());
1824  parse_pos_stack.pop();
1825 }
#define C4AUL_Const
Definition: C4AulParse.cpp:52
const char * getData() const
Definition: StdBuf.h:450
void ParseDirectExecStatement(C4AulScriptEngine *Engine, C4AulScriptContext *context=nullptr)
Definition: C4AulParse.cpp:843
C4String P[P_LAST]
C4Value Value
Definition: C4PropList.h:57
#define C4AUL_For
Definition: C4AulParse.cpp:58
#define C4AUL_TypeBool
Definition: C4AulParse.cpp:72
bool IncludesResolved
Definition: C4ScriptHost.h:88
#define C4AUL_Public
Definition: C4AulParse.cpp:50
C4V_Type
Definition: C4Value.h:23
C4AulErrorHandler * GetErrorHandler() const
Definition: C4Aul.h:175
void EnableWarning(const char *pos, C4AulWarningId warning, bool enable=true)
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:270
#define C4AUL_Continue
Definition: C4AulParse.cpp:64
C4PropListStatic * GetPropList()
Definition: C4Aul.h:153
C4AulWarningId
Definition: C4Aul.h:31
void IncRef()
Definition: C4StringTable.h:27
void ParseDirectExecFunc(C4AulScriptEngine *Engine, C4AulScriptContext *context=nullptr)
Definition: C4AulParse.cpp:834
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
C4String * FindString(const char *strString) const
int SGetLine(const char *szText, const char *cpPosition)
Definition: Standard.cpp:443
std::list< StdCopyStrBuf > Appends
Definition: C4ScriptHost.h:80
const char * GetName() const
Definition: C4AulFunc.h:57
C4AulFunc * GetFunc(C4PropertyName k) const
Definition: C4PropList.h:107
#define DEBUG_BYTECODE_DUMP
Definition: C4AulParse.cpp:35
bool NoSecondStatement
Definition: C4AulParse.h:37
#define C4AUL_Func
Definition: C4AulParse.cpp:46
static void Preparse(C4ScriptHost *out, C4ScriptHost *source, const ::aul::ast::Script *s)
const char * GetCStr() const
Definition: C4StringTable.h:49
C4String * RegString(StdStrBuf String)
#define C4AUL_Return
Definition: C4AulParse.cpp:60
#define C4AUL_Private
Definition: C4AulParse.cpp:48
bool append_unnamed_pars
Definition: C4AulAST.h:260
C4AulBCCType
void SetProperty(C4PropertyName k, const C4Value &to)
Definition: C4PropList.h:122
std::unique_ptr<::aul::ast::FunctionDecl > Parse_DirectExec(const char *code, bool whole_function)
Definition: C4AulParse.cpp:852
bool IsWhiteSpace(char cChar)
Definition: Standard.h:76
std::vector< C4AulBCC > Code
bool IsWarningEnabled(const char *pos, C4AulWarningId warning) const
std::string callee
Definition: C4AulAST.h:263
C4ScriptHost * pOrgScript
#define C4AUL_VarNamed
Definition: C4AulParse.cpp:69
virtual class C4PropListStatic * IsStatic()
Definition: C4PropList.h:87
#define C4AUL_Append
Definition: C4AulParse.cpp:40
StdStrBuf GetDataString(int depth=10, const class C4PropListStatic *ignore_reference_parent=nullptr) const
Definition: C4Value.cpp:133
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
void Clear()
virtual const char * what() const noexcept
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
#define a
size_t SLen(const char *sptr)
Definition: Standard.h:78
std::vector< ExprPtr > args
Definition: C4AulAST.h:262
#define C4AUL_If
Definition: C4AulParse.cpp:54
void IncludeDefinition(C4Def *pIncludeDef)
Definition: C4Def.cpp:635
std::vector< Parameter > params
Definition: C4AulAST.h:382
#define C4AUL_TypeInt
Definition: C4AulParse.cpp:71
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
#define C4AUL_Global
Definition: C4AulParse.cpp:51
int SLineGetCharacters(const char *szText, const char *cpPosition)
Definition: Standard.cpp:455
std::unique_ptr<::aul::ast::FunctionDecl > Parse_ToplevelFunctionDecl()
Definition: C4AulParse.cpp:959
void AppendFormat(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:197
virtual C4Def const * GetDef() const
Definition: C4PropList.cpp:662
#define C4AUL_Do
Definition: C4AulParse.cpp:56
#define C4AUL_In
Definition: C4AulParse.cpp:59
void AppendChar(char cChar)
Definition: StdBuf.h:596
std::unique_ptr<::aul::ast::ArrayLit > Parse_Array()
#define C4AUL_Warning
Definition: C4AulParse.cpp:41
#define C4AUL_TypeEffect
Definition: C4AulParse.cpp:75
void CopyPropList(C4Set< C4Property > &from, C4PropListStatic *to)
std::unique_ptr< Block > body
Definition: C4AulAST.h:384
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
std::unique_ptr< Expr > ExprPtr
Definition: C4AulAST.h:152
#define C4AUL_this
Definition: C4AulParse.cpp:65
virtual void AddEngineFunctions()
Definition: C4ScriptHost.h:82
T const * Next(T const *p) const
int GetLineOfCode(C4AulBCC *bcc)
#define C4AUL_Break
Definition: C4AulParse.cpp:63
std::unique_ptr<::aul::ast::ForLoop > Parse_For()
std::unique_ptr<::aul::ast::VarDecl > Parse_Var()
std::unique_ptr<::aul::ast::WhileLoop > Parse_While()
C4V_Type GetType() const
Definition: C4Value.h:161
C4StringTable Strings
Definition: C4Globals.cpp:42
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:734
bool has_unnamed_params
Definition: C4AulAST.h:383
std::unique_ptr<::aul::ast::Block > Parse_Block()
C4Value C4VPropList(C4PropList *p)
Definition: C4Value.h:245
friend class C4AulScriptFunc
Definition: C4ScriptHost.h:110
std::unique_ptr<::aul::ast::If > Parse_If()
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
C4AulParse(class C4ScriptHost *host)
Definition: C4AulParse.cpp:113
#define C4AUL_TypeC4Object
Definition: C4AulParse.cpp:76
#define C4AUL_New
Definition: C4AulParse.cpp:85
std::list< StdCopyStrBuf > Includes
Definition: C4ScriptHost.h:79
#define C4AUL_MAX_Identifier
Definition: C4Aul.h:28
#define C4AUL_Include
Definition: C4AulParse.cpp:39
#define C4AUL_GlobalNamed
Definition: C4AulParse.cpp:67
friend class C4AulParseError
Definition: C4AulParse.h:101
#define C4AUL_TypeC4ID
Definition: C4AulParse.cpp:73
#define C4AUL_False
Definition: C4AulParse.cpp:83
static void Compile(C4AulScriptFunc *out, const ::aul::ast::Function *f)
virtual bool Parse()
StdStrBuf FormatStringV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:283
C4Value C4VArray(C4ValueArray *pArray)
Definition: C4Value.h:249
const char * C4AulWarningMessages[]
Definition: C4Aul.cpp:30
static C4PropListStatic * NewStatic(C4PropList *prototype, const C4PropListStatic *parent, C4String *key)
Definition: C4PropList.cpp:70
unsigned short Priority
Definition: C4AulParse.h:32
virtual void OnError(const char *msg)=0
std::unique_ptr<::aul::ast::DoLoop > Parse_DoWhile()
std::unique_ptr<::aul::ast::RangeLoop > Parse_ForEach()
#define C4AUL_Else
Definition: C4AulParse.cpp:55
C4Set< C4Property > LocalValues
Definition: C4ScriptHost.h:92
int32_t StrToI32(const char *s, int base, const char **scan_end)
Definition: Standard.cpp:97
void Warn(const char *pMsg,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: C4AulParse.cpp:132
C4String * Key
Definition: C4PropList.h:56
std::unique_ptr<::aul::ast::Expr > Parse_Expression(int iParentPrio=-1)
virtual void OnWarning(const char *msg)=0
#define C4AUL_TypePropList
Definition: C4AulParse.cpp:77
bool Preparse()
Definition: C4AulParse.cpp:688
#define C4AUL_TypeString
Definition: C4AulParse.cpp:78
void SetOverloaded(C4AulFunc *)
#define C4Aul_Warning_enable
Definition: C4AulParse.cpp:43
#define C4AUL_TypeFunction
Definition: C4AulParse.cpp:80
#define C4AUL_While
Definition: C4AulParse.cpp:57
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:168
void FormatV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:189
const char * getPtr(size_t i) const
Definition: StdBuf.h:456
#define C4AUL_LocalNamed
Definition: C4AulParse.cpp:68
size_t getLength() const
Definition: StdBuf.h:453
C4Value C4VFunction(C4AulFunc *pFn)
Definition: C4Value.h:250
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:929
const char * Script
const char * GetScript() const
Definition: C4ScriptHost.h:52
std::list< C4ScriptHost * > SourceScripts
Definition: C4ScriptHost.h:56
T const * First() const
std::unique_ptr<::aul::ast::Stmt > Parse_Statement()
C4RefCntPointer< C4String > Name
Definition: C4AulFunc.h:61
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:130
void DecRef()
Definition: C4StringTable.h:28
C4PropList * _getPropList() const
Definition: C4Value.h:129
#define C4AUL_Par
Definition: C4AulParse.cpp:62
const char * C4AulWarningIDs[]
Definition: C4Aul.cpp:36
const char * Identifier
Definition: C4AulParse.h:33
#define C4AUL_Nil
Definition: C4AulParse.cpp:84
void Shift()
Definition: C4AulParse.cpp:755
#define C4AUL_Protected
Definition: C4AulParse.cpp:49
virtual C4AulScriptFunc * SFunc()
Definition: C4AulFunc.h:66
std::unique_ptr<::aul::ast::ProplistLit > Parse_PropList()
#define C4AUL_True
Definition: C4AulParse.cpp:82
C4AulTokenType
Definition: C4AulParse.cpp:88
#define C4AUL_TypeArray
Definition: C4AulParse.cpp:79
void Copy()
Definition: StdBuf.h:475
StdCopyStrBuf ScriptName
Definition: C4ScriptHost.h:57
#define s
C4AulScriptState State
Definition: C4ScriptHost.h:93
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45
C4AulFunc * getFunction() const
Definition: C4Value.h:119
StdCopyStrBuf sMessage
Definition: C4Aul.h:46
const C4PropListStatic * GetParent() const
Definition: C4PropList.h:272
StdStrBuf Script
Definition: C4ScriptHost.h:90
std::unique_ptr<::aul::ast::Script > Parse_Script(C4ScriptHost *)
Definition: C4AulParse.cpp:873
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:277
#define C4Aul_Warning_disable
Definition: C4AulParse.cpp:44
#define C4AUL_TypeDef
Definition: C4AulParse.cpp:74