ccScript 5.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
ccscript.h
Go to the documentation of this file.
1 // Copyright (C) 2008-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef _CCSCRIPT_H_
18 #define _CCSCRIPT_H_
19 
20 #ifndef UCOMMON_UCOMMON_H_
21 #include <ucommon/ucommon.h>
22 #endif
23 
24 namespace ucommon {
25 
31 class __EXPORT Script : public CountedObject, public memalloc
32 {
33 public:
34  class interp;
35  class header;
36  class checks;
37 
41  typedef bool (Script::interp::*method_t)(void);
42 
49  typedef struct line {
50  struct line *next;
51  union {
52  const char *cmd;
54  };
55  char **argv;
56  unsigned short loop, argc;
57  unsigned lnum, mask;
58  method_t method;
59  } line_t;
60 
64  typedef const char *(*check_t)(Script *img, Script::header *scr, Script::line_t *line);
65 
71  typedef struct keyword
72  {
73  const char *name;
74  method_t method;
75  check_t check;
76  struct keyword *next;
77  } keyword_t;
78 
86  class __EXPORT strict : public LinkedObject
87  {
88  private:
89  __DELETE_DEFAULTS(strict);
90 
91  public:
92  const char *id;
93 
94  static bool find(Script *img, header *scr, const char *id);
95  static void createVar(Script *img, header *scr, const char *id);
96  static void createSym(Script *img, header *scr, const char *id);
97  static void createAny(Script *img, header *scr, const char *id);
98  static void createGlobal(Script *img, const char *id);
99 
100  void put(FILE *fp, const char *header);
101  };
102 
110  class __EXPORT symbol : public LinkedObject
111  {
112  private:
113  __DELETE_DEFAULTS(symbol);
114 
115  public:
116  const char *name;
117  char *data;
118  unsigned size;
120  };
121 
127  class __EXPORT event : public LinkedObject
128  {
129  private:
130  __DELETE_DEFAULTS(event);
131 
132  public:
134  const char *name;
135  };
136 
140  typedef event event_t;
141 
150  class __EXPORT header : public LinkedObject
151  {
152  private:
153  __DELETE_DEFAULTS(header);
154 
155  public:
160  const char *file;
161  const char *name;
162  unsigned resmask;
168  inline void link(header *scr)
169  {Next = scr;}
170  };
171 
179  class __EXPORT checks
180  {
181  private:
182  __DELETE_DEFAULTS(checks);
183 
184  public:
185  static bool isValue(const char *text);
186  static bool isText(const char *text);
187 
188  static const char *chkPush(Script *img, header *scr, line_t *line);
189  static const char *chkApply(Script *img, header *scr, line_t *line);
190  static const char *chkIgnore(Script *img, header *scr, line_t *line);
191  static const char *chkNop(Script *img, header *scr, line_t *line);
192  static const char *chkExit(Script *img, header *scr, line_t *line);
193  static const char *chkVar(Script *img, header *scr, line_t *line);
194  static const char *chkConst(Script *img, header *scr, line_t *line);
195  static const char *chkSet(Script *img, header *scr, line_t *line);
196  static const char *chkClear(Script *img, header *scr, line_t *line);
197  static const char *chkError(Script *img, header *scr, line_t *line);
198  static const char *chkPack(Script *img, header *scr, line_t *line);
199  static const char *chkExpand(Script *img, header *scr, line_t *line);
200  static const char *chkGosub(Script *img, header *src, line_t *line);
201  static const char *chkGoto(Script *img, header *scr, line_t *line);
202  static const char *chkDo(Script *img, header *scr, line_t *line);
203  static const char *chkUntil(Script *img, header *scr, line_t *line);
204  static const char *chkWhile(Script *ing, header *scr, line_t *line);
205  static const char *chkConditional(Script *img, header *scr, line_t *line);
206  static const char *chkContinue(Script *img, header *scr, line_t *line);
207  static const char *chkBreak(Script *img, header *scr, line_t *line);
208  static const char *chkLoop(Script *img, header *scr, line_t *line);
209  static const char *chkPrevious(Script *img, header *scr, line_t *line);
210  static const char *chkIndex(Script *img, header *scr, line_t *line);
211  static const char *chkForeach(Script *img, header *scr, line_t *line);
212  static const char *chkCase(Script *img, header *scr, line_t *line);
213  static const char *chkEndcase(Script *img, header *scr, line_t *line);
214  static const char *chkOtherwise(Script *img, header *scr, line_t *line);
215  static const char *chkIf(Script *img, header *scr, line_t *line);
216  static const char *chkElif(Script *img, header *scr, line_t *line);
217  static const char *chkElse(Script *img, header *scr, line_t *line);
218  static const char *chkEndif(Script *img, header *scr, line_t *line);
219  static const char *chkDefine(Script *img, header *scr, line_t *line);
220  static const char *chkInvoke(Script *img, header *scr, line_t *line);
221  static const char *chkWhen(Script *img, header *scr, line_t *line);
222  static const char *chkStrict(Script *img, header *scr, line_t *line);
223  static const char *chkExpr(Script *img, header *scr, line_t *line);
224  static const char *chkRef(Script *img, header *scr, line_t *line);
225  static const char *chkIgnmask(Script *img, header *scr, line_t *line);
226  };
227 
232  typedef struct {
238  unsigned short index;
239  unsigned short base;
240  unsigned short resmask;
241  } stack_t;
242 
253  class __EXPORT interp : protected memalloc
254  {
255  public:
256  typedef char num_t[16];
257 
258  interp();
259  virtual ~interp();
260 
266  bool step(void);
267 
276  bool attach(Script *image, const char *entry = NULL);
277 
281  void detach(void);
282 
288  void initialize(void);
289 
294  bool error(const char *text);
295 
300  unsigned getResource(void);
301 
306  const char *getFilename(void);
307 
308  protected:
309  symbol *find(const char *id);
310  void skip(void);
311  void push(void);
312  bool trylabel(const char *id);
313  bool tryexit(void);
314  void pullScope(void);
315  void pullBase(void);
316  void pullLoop(void);
317  bool pop(void);
318  void setStack(header *scr, event *ev = NULL);
319 
327  bool scriptEvent(const char *name);
328 
334  event *scriptMethod(const char *name);
335 
337  object_pointer<Script> image;
339  unsigned frame;
340 
341  char *getTemp(void);
342  bool setConst(const char *id, const char *value);
343  symbol *createSymbol(const char *id);
344  symbol *getVar(const char *id, const char *value = NULL);
345  const char *getValue(void);
346  const char *getContent(void);
347  const char *getContent(const char *text);
348  const char *getKeyword(const char *id);
349  method_t getLooping(void);
350  bool isConditional(unsigned index);
351  void setRef(header *scope, const char *id, char *data, unsigned size);
352  void getParams(header *scope, line_t *line);
353  void startScript(header *scr);
354 
355  virtual unsigned getTypesize(const char *type_id);
356  virtual const char *getTypeinit(const char *type_id);
357  virtual const char *getFormat(symbol *sym, const char *id, char *temp);
358  virtual bool getCondition(const char *test, const char *value);
359  const char *getIndex(void);
360 
361  private:
362  __DELETE_COPY(interp);
363 
364  bool getExpression(unsigned index);
365 
366  char *errmsg;
367  char *temps[3];
368  unsigned tempindex;
369  };
370 
377  class __EXPORT error : public OrderedObject
378  {
379  private:
380  friend class Script;
381 
382  __DELETE_DEFAULTS(error);
383 
384  error(Script *img, unsigned line, const char *str);
385 
386  public:
387  const char *filename;
388  char *errmsg;
389  unsigned errline;
390  };
391 
399  class __EXPORT methods : public interp
400  {
401  private:
402  __DELETE_DEFAULTS(methods);
403 
404  public:
405  bool scrPush(void);
406  bool scrApply(void);
407  bool scrExpr(void);
408  bool scrVar(void);
409  bool scrSet(void);
410  bool scrAdd(void);
411  bool scrClear(void);
412  bool scrConst(void);
413  bool scrPause(void);
414  bool scrNop(void);
415  bool scrPack(void);
416  bool scrExpand(void);
417  bool scrExit(void);
418  bool scrReturn(void);
419  bool scrError(void);
420  bool scrRestart(void);
421  bool scrGosub(void);
422  bool scrGoto(void);
423  bool scrDo(void);
424  bool scrLoop(void);
425  bool scrUntil(void);
426  bool scrWhile(void);
427  bool scrBreak(void);
428  bool scrContinue(void);
429  bool scrForeach(void);
430  bool scrPrevious(void);
431  bool scrRepeat(void);
432  bool scrIndex(void);
433  bool scrCase(void);
434  bool scrEndcase(void);
435  bool scrOtherwise(void);
436  bool scrIf(void);
437  bool scrElif(void);
438  bool scrElse(void);
439  bool scrEndif(void);
440  bool scrDefine(void);
441  bool scrInvoke(void);
442  bool scrWhen(void);
443  bool scrRef(void);
444  bool scrIgnore(void);
445  };
446 
447  ~Script();
448 
449  static unsigned stepping;
450  static unsigned indexing;
451  static size_t paging;
452  static unsigned sizing;
453  static unsigned stacking;
454  static unsigned decimals;
467  static Script *compile(Script *merge, const char *filename, Script *config = NULL);
468 
474  static void assign(keyword_t *list);
475 
483  static keyword_t *find(const char *id);
484 
488  static void init(void);
489 
490  static unsigned offset(const char *list, unsigned index);
491  static void copy(const char *list, char *item, unsigned size);
492  static unsigned count(const char *list);
493  static const char *get(const char *list, unsigned offset);
494  static char *get(char *list, unsigned offset);
495  static header *find(Script *img, const char *id);
496  static bool isEvent(header *scr, const char *id);
497 
500 
501  bool push(line_t *line);
502  method_t pull(void);
503  method_t looping(void);
504 
505  inline unsigned getErrors(void)
506  {return errors;}
507 
508  inline LinkedObject *getListing(void)
509  {return errlist.begin();}
510 
511  inline const char *getFilename(void)
512  {return filename;}
513 
514  inline bool isStrict(void)
515  {return global != NULL;}
516 
517  inline unsigned getLines(void)
518  {return lines;}
519 
520 private:
521  friend class strict;
522  friend class checks;
523  friend class error;
524  friend class interp;
525  friend class methods;
526 
527  __DELETE_COPY(Script);
528 
529  Script();
530 
531  void errlog(unsigned line, const char *fmt, ...);
532 
533  unsigned long instance;
534  unsigned errors;
535  unsigned loop;
536  unsigned lines;
537  bool thencheck;
538  line_t **stack;
539  LinkedObject *global;
540  OrderedIndex errlist;
541  object_pointer<Script> shared;
542  const char *filename;
543  unsigned serial;
544  LinkedObject *headers;
545 };
546 
547 } // namespace ucommon
548 
549 #endif
550 
unsigned getErrors(void)
Definition: ccscript.h:505
unsigned resmask
post-compile processing resource mask
Definition: ccscript.h:162
header * scr
executing script for stack
Definition: ccscript.h:233
static size_t paging
default heap paging
Definition: ccscript.h:451
LinkedObject ** scripts
Definition: ccscript.h:499
void link(header *scr)
Used to set linked list linkage.
Definition: ccscript.h:168
LinkedObject * getListing(void)
Definition: ccscript.h:508
line_t * ignore
ignored events
Definition: ccscript.h:237
LinkedObject * scheduler
scheduler list
Definition: ccscript.h:456
unsigned getLines(void)
Definition: ccscript.h:517
A class to collect compile-time errors.
Definition: ccscript.h:377
struct line * next
Definition: ccscript.h:50
static unsigned stepping
default stepping increment
Definition: ccscript.h:449
static unsigned indexing
default symbol indexing
Definition: ccscript.h:450
line_t * first
first line of section or define
Definition: ccscript.h:159
event event_t
Convenience typedef to allow use of event name.
Definition: ccscript.h:140
const char * name
Definition: ccscript.h:134
const char * name
name of command
Definition: ccscript.h:73
NAMESPACE_UCOMMON A structure to introduce new core commands to the runtime engine.
Definition: ccscript.h:71
char * data
content of symbol
Definition: ccscript.h:117
line_t * line
executing line at stack level
Definition: ccscript.h:236
const char * name
name of symbol
Definition: ccscript.h:116
const char * getFilename(void)
Definition: ccscript.h:511
unsigned short index
index marker for loops
Definition: ccscript.h:238
Basic compiled statement.
Definition: ccscript.h:49
static unsigned stacking
stack frames in script runtime
Definition: ccscript.h:453
Contains instance of a runtime symbol.
Definition: ccscript.h:110
bool isStrict(void)
Definition: ccscript.h:514
object_pointer< Script > image
Definition: ccscript.h:337
Contains defined variables found by scope when strict is used.
Definition: ccscript.h:86
header * scope
scope of symbol definition
Definition: ccscript.h:119
static unsigned decimals
default decimal places
Definition: ccscript.h:454
const char * id
Definition: ccscript.h:92
LinkedObject ** syms
Definition: ccscript.h:338
header * scope
effective symbol scope
Definition: ccscript.h:234
const char * cmd
Definition: ccscript.h:52
struct keyword * next
linked list set by assign()
Definition: ccscript.h:76
header * first
Definition: ccscript.h:498
const char * name
name of script section or define
Definition: ccscript.h:161
const char * filename
Definition: ccscript.h:387
const char * file
filename of script
Definition: ccscript.h:160
A container class for compile-time check methods.
Definition: ccscript.h:179
Compiled script container.
Definition: ccscript.h:31
check_t check
compile-time check routine
Definition: ccscript.h:75
An instance of the runtime interpreter.
Definition: ccscript.h:253
method_t method
runtime method or NULL if c-t only
Definition: ccscript.h:74
unsigned short base
base stack of "@section"
Definition: ccscript.h:239
Header describes a script section.
Definition: ccscript.h:150
An event block for a script.
Definition: ccscript.h:127
Runtime methods collection class.
Definition: ccscript.h:399
event_t * event
so we don't redo our event
Definition: ccscript.h:235
unsigned size
size of data buffer or 0 if const
Definition: ccscript.h:118
unsigned short loop
Definition: ccscript.h:56
Runtime stack for each interpreter instance.
Definition: ccscript.h:232
static unsigned sizing
default symbol size
Definition: ccscript.h:452
unsigned short resmask
effective dsp resource mask
Definition: ccscript.h:240
LinkedObject * methods
named members
Definition: ccscript.h:158
LinkedObject * scoped
scoped symbol defs
Definition: ccscript.h:156
LinkedObject * events
named events
Definition: ccscript.h:157