OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Aul.h
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 
17 #ifndef INC_C4Aul
18 #define INC_C4Aul
19 
20 #include "object/C4Id.h"
21 #include "script/C4StringTable.h"
22 #include "script/C4Value.h"
23 #include "script/C4ValueMap.h"
24 #include <string>
25 #include <vector>
26 
27 // consts
28 #define C4AUL_MAX_Identifier 100 // max length of function identifiers
29 
30 // warning flags
31 enum class C4AulWarningId
32 {
33 #define DIAG(id, text, enabled) id,
34 #include "C4AulWarnings.h"
35 #undef DIAG
37 };
38 
39 extern const char *C4AulWarningIDs[];
40 extern const char *C4AulWarningMessages[];
41 
42 // generic C4Aul error class
43 class C4AulError : public std::exception
44 {
45 protected:
47 
48 public:
49  virtual ~C4AulError() { } // destructor
50  virtual const char *what() const noexcept;
51 };
52 
53 // parse error
55 {
56  C4AulParseError() = default;
57 public:
58  C4AulParseError(C4ScriptHost *pScript, const char *pMsg); // constructor
59  C4AulParseError(class C4AulParse * state, const char *pMsg); // constructor
60  C4AulParseError(C4AulScriptFunc * Fn, const char *SPos, const char *pMsg);
61 };
62 
63 // execution error
64 class C4AulExecError : public C4AulError
65 {
66 public:
67  C4AulExecError(const char *szError);
68 };
69 
71 {
72 public:
73  C4AulFuncMap();
74  ~C4AulFuncMap();
75  C4AulFunc * GetFirstFunc(const char * Name);
76  C4AulFunc * GetNextSNFunc(const C4AulFunc * After);
77 private:
78  enum { HashSize = 1025 };
79  C4AulFunc * Funcs[HashSize];
80  int FuncCnt;
81  static unsigned int Hash(const char * Name);
82 protected:
83  void Add(C4AulFunc * func);
84  void Remove(C4AulFunc * func);
85  friend class C4AulFunc;
86  friend class C4ScriptHost;
87 };
88 
89 // user text file to which scripts can write using FileWrite().
90 // actually just writes to an internal buffer
92 {
93  StdCopyStrBuf sContents;
94  int32_t handle;
95 
96 public:
97  C4AulUserFile(int32_t handle) : handle(handle) {}
98  void Write(const char *data, size_t data_length) { sContents.Append(data, data_length); }
99 
100  const char *GetFileContents() { return sContents.getData(); }
101  StdStrBuf GrabFileContents() { StdStrBuf r; r.Take(sContents); return r; }
102  size_t GetFileLength() { return sContents.getLength(); }
103  int32_t GetHandle() const { return handle; }
104 };
105 
107 {
108 public:
109  virtual ~C4AulErrorHandler();
110  virtual void OnError(const char *msg) = 0;
111  virtual void OnWarning(const char *msg) = 0;
112 };
113 
114 // holds all C4AulScripts
116 {
117 protected:
119  C4AulFunc * GetFirstFunc(const char * Name)
120  { return FuncLookUp.GetFirstFunc(Name); }
122  { return FuncLookUp.GetNextSNFunc(After); }
123  C4ScriptHost *Child0, *ChildL; // tree structure
124 
125  // all open user files
126  // user files aren't saved - they are just open temporary e.g. during game saving
127  std::list<C4AulUserFile> UserFiles;
128  std::vector<C4Value> OwnedPropLists;
129 
131 
132 public:
133  int warnCnt, errCnt; // number of warnings/errors
134  int lineCnt; // line count parsed
135 
138 
139  // global constants (such as "static const C4D_Structure = 2;")
140  // cannot share var lists, because it's so closely tied to the data lists
141  // constants are used by the Parser only, anyway, so it's not
142  // necessary to pollute the global var list here
145 
146  C4Effect * pGlobalEffects = nullptr;
147 
148  C4AulScriptEngine(); // constructor
149  ~C4AulScriptEngine(); // destructor
150  void Clear(); // clear data
151  void Link(C4DefList *rDefs); // link and parse all scripts
152  void ReLink(C4DefList *rDefs); // unlink, link and parse all scripts
153  C4PropListStatic * GetPropList() { return this; }
154  bool ReloadScript(const char *szScript, const char *szLanguage); // search script and reload, if found
155 
156  // For the list of functions in the PropertyDlg
157  std::list<const char*> GetFunctionNames(C4PropList *);
158 
159  void RegisterGlobalConstant(const char *szName, const C4Value &rValue); // creates a new constants or overwrites an old one
160  bool GetGlobalConstant(const char *szName, C4Value *pTargetValue); // check if a constant exists; assign value to pTargetValue if not nullptr
161 
162  void Denumerate(C4ValueNumbers *);
163  void UnLink(); // called when a script is being reloaded (clears string table)
164 
165  // Compile scenario script data (without strings and constants)
166  void CompileFunc(StdCompiler *pComp, bool fScenarioSection, C4ValueNumbers * numbers);
167 
168  // Handle user files
169  int32_t CreateUserFile(); // create new file and return handle
170  void CloseUserFile(int32_t handle); // close user file given by handle
171  C4AulUserFile *GetUserFile(int32_t handle); // get user file given by handle
172 
173  void RegisterErrorHandler(C4AulErrorHandler *handler);
174  void UnregisterErrorHandler(C4AulErrorHandler *handler);
176  {
177  return ErrorHandler;
178  }
179 
180  friend class C4AulFunc;
181  friend class C4AulProfiler;
182  friend class C4ScriptHost;
183  friend class C4AulParse;
184  friend class C4AulCompiler;
185  friend class C4AulDebug;
186 };
187 
190 
191 #endif
const char * getData() const
Definition: StdBuf.h:450
const char * C4AulWarningMessages[]
Definition: C4Aul.cpp:30
C4ValueMapNames GlobalNamedNames
Definition: C4Aul.h:136
void Write(const char *data, size_t data_length)
Definition: C4Aul.h:98
C4AulErrorHandler * GetErrorHandler() const
Definition: C4Aul.h:175
StdStrBuf GrabFileContents()
Definition: C4Aul.h:101
C4PropListStatic * GetPropList()
Definition: C4Aul.h:153
std::list< C4AulUserFile > UserFiles
Definition: C4Aul.h:127
C4AulWarningId
Definition: C4Aul.h:31
C4ValueMapData GlobalNamed
Definition: C4Aul.h:137
C4AulFunc * GetFirstFunc(const char *Name)
virtual const char * what() const noexcept
C4ValueMapNames GlobalConstNames
Definition: C4Aul.h:143
C4AulFunc * GetFirstFunc(const char *Name)
Definition: C4Aul.h:119
C4AulFunc * GetNextSNFunc(const C4AulFunc *After)
Definition: C4Aul.h:121
C4AulFuncMap FuncLookUp
Definition: C4Aul.h:118
int32_t GetHandle() const
Definition: C4Aul.h:103
virtual ~C4AulError()
Definition: C4Aul.h:49
C4AulFunc * GetNextSNFunc(const C4AulFunc *After)
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
const char * C4AulWarningIDs[]
Definition: C4Aul.cpp:36
void Take(char *pnData)
Definition: StdBuf.h:465
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
C4ValueMapData GlobalConsts
Definition: C4Aul.h:144
C4AulUserFile(int32_t handle)
Definition: C4Aul.h:97
size_t GetFileLength()
Definition: C4Aul.h:102
void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
Definition: C4Script.cpp:1141
C4ScriptHost * ChildL
Definition: C4Aul.h:123
size_t getLength() const
Definition: StdBuf.h:453
std::vector< C4Value > OwnedPropLists
Definition: C4Aul.h:128
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:130
const char * GetFileContents()
Definition: C4Aul.h:100
StdCopyStrBuf sMessage
Definition: C4Aul.h:46
void CompileFunc(C4Real &rValue, StdCompiler *pComp)
Definition: C4Real.cpp:9033