ccScript 5.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
script.cpp
Go to the documentation of this file.
1 // Copyright (C) 1995-1999 David Sugar, Tycho Softworks.
2 // Copyright (C) 1999-2005 Open Source Telecom Corp.
3 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
4 // Copyright (C) 2015 Cherokees of Idaho.
5 //
6 // This file is part of GNU ccScript C++.
7 //
8 // GNU ccScript C++ is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // GNU ccScript C++ is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with GNU ccScript C++. If not, see <http://www.gnu.org/licenses/>.
20 
21 #include <ccscript-config.h>
22 #include <ccscript.h>
23 
24 using namespace UCOMMON_NAMESPACE;
25 
26 static unsigned checks = 0;
27 static unsigned exchecks = 0;
28 static unsigned refchecks = 0;
29 static unsigned mainchecks = 0;
30 static unsigned debugcount = 0;
31 static unsigned printcount = 0;
32 static unsigned eventchecks = 0;
33 static unsigned loopingchecks = 0;
34 static unsigned manipchecks = 0;
35 
36 class debug
37 {
38 private:
39  __DELETE_COPY(debug);
40 
41 public:
42  debug();
43 
44  const char *flag;
45 
46  virtual void print(void);
47 };
48 
49 class testing : public debug, public Script::interp
50 {
51 private:
52  __DELETE_COPY(testing);
53 
54 public:
55  inline testing() : debug(), Script::interp() {}
56 
57  bool scrCheck(void);
58  bool scrCheckExit(void);
59  bool scrCheckMain(void);
60  bool scrCheckEvent(void);
61  bool scrCheckLoop(void);
62  bool scrCheckRefs(void);
63  bool scrArgs(void);
64  bool scrSleep(void);
65  bool scrCheckManip(void);
66 };
67 
69 {
70  ++debugcount;
71  flag = NULL;
72 }
73 
74 void debug::print(void)
75 {
76  if(flag)
77  printf("%s", flag);
78  else
79  ++printcount;
80 }
81 
83 {
84  ++manipchecks;
85  skip();
86  return true;
87 }
88 
90 {
91  ++eventchecks;
92  skip();
93  return true;
94 }
95 
97 {
98  ++refchecks;
99  skip();
100  return true;
101 }
102 
104 {
105  ++mainchecks;
106  debug::print();
107  if(scriptEvent("#"))
108  return false;
109  skip();
110  return true;
111 }
112 
114 {
115  ++loopingchecks;
116  skip();
117  return true;
118 }
119 
120 
122 {
123  ++exchecks;
124  skip();
125  return true;
126 }
127 
129 {
130  ++checks;
131  skip();
132  return true;
133 }
134 
136 {
137  unsigned index = 0;
138  Script::line_t *line = stack[frame].line;
139 
140  while(index < line->argc) {
141  const char *cp = getContent(line->argv[index]);
142  printf(" ARG %d %s <%s>\n", index, line->argv[index], cp);
143  ++index;
144  }
145  skip();
146  return false;
147 }
148 
150 {
151  Thread::sleep(500);
152  skip();
153  return true;
154 }
155 
156 int main(int argc, char **argv)
157 {
158  testing interp;
159  unsigned errors;
160 
161  static Script::keyword_t keywords[] = {
162  {"check", (Script::method_t)&testing::scrCheck, &Script::checks::chkNop},
163  {"check.exit", (Script::method_t)&testing::scrCheckExit, &Script::checks::chkNop},
164  {"check.main", (Script::method_t)&testing::scrCheckMain, &Script::checks::chkNop},
165  {"check.event", (Script::method_t)&testing::scrCheckEvent, &Script::checks::chkNop},
166  {"check.loop", (Script::method_t)&testing::scrCheckLoop, &Script::checks::chkNop},
167  {"check.refs", (Script::method_t)&testing::scrCheckRefs, &Script::checks::chkNop},
168  {"check.manip", (Script::method_t)&testing::scrCheckManip, &Script::checks::chkNop},
169  {"check.ignore", (Script::method_t)&Script::methods::scrNop, &Script::checks::chkIgnore},
170  {"args", (Script::method_t)&testing::scrArgs, &Script::checks::chkIgnore},
171  {"sleep", (Script::method_t)&testing::scrSleep, &Script::checks::chkNop},
172  {NULL}};
173 
174  const char *filename = "testscript.scr";
175  const char *mergename = "mergescript.scr";
176 
177  if(argc > 3) {
178  fprintf(stderr, "use: testscript [scrname]\n");
179  exit(-1);
180  }
181 
182  if(argc == 2) {
183  mergename = NULL;
184  filename = argv[1];
185  }
186 
187  Script::init();
188  Script::assign(keywords);
189 
190  Script *image;
191 
192  if(mergename) {
193  image = Script::compile(NULL, mergename);
194  image = Script::compile(image, filename);
195  }
196  else
197  image = Script::compile(NULL, filename);
198 
199  if(!image) {
200  fprintf(stderr, "*** failed to load %s\n", filename);
201  exit(-1);
202  }
203 
204  errors = image->getErrors();
205  if(errors) {
206  fprintf(stderr, "*** %d total errors in %s\n", errors, filename);
207  linked_pointer<Script::error> ep = image->getListing();
208  while(is(ep)) {
209  fprintf(stderr, "*** %s(%d): %s\n", image->getFilename(), ep->errline, ep->errmsg);
210  ep.next();
211  }
212  delete image;
213  exit(-1);
214  }
215 
216  interp.initialize();
217 
218  if(!interp.attach(image)) {
219  fprintf(stderr, "*** no main section in %s\n", filename);
220  exit(-1);
221  }
222  while(interp.step()) {
223  Thread::yield();
224  }
225  interp.detach();
226 
227  if(argc < 2) {
228  // enter correct generic number of "checks"
229  assert(checks == 4);
230 
231  // enter validation of defined call by ref
232  assert(refchecks == 1);
233 
234  // @exit handler called, and only once...
235  assert(exchecks == 1);
236 
237  // @main handler called
238  assert(mainchecks == 1);
239 
240  // @main ^event handler called
241  assert(eventchecks == 1);
242 
243  // check looping operation
244  assert(loopingchecks == 3);
245 
246  // in multiple inheritence base class initialized successfully
247  assert(debugcount == 1);
248 
249  // in multiple inheritence, method call this* has combined class
250  assert(printcount == 1);
251 
252  // check manipulations
253  assert(manipchecks == 8);
254  }
255 
256  exit(0);
257 }
258 
bool scrCheckEvent(void)
Definition: script.cpp:89
testing()
Definition: script.cpp:55
bool scrCheckRefs(void)
Definition: script.cpp:96
bool scrCheck(void)
Definition: script.cpp:128
virtual void print(void)
Definition: script.cpp:74
Definition: script.cpp:36
bool scrCheckExit(void)
Definition: script.cpp:121
bool scrCheckLoop(void)
Definition: script.cpp:113
debug()
Definition: script.cpp:68
bool scrCheckManip(void)
Definition: script.cpp:82
const char * flag
Definition: script.cpp:44
bool scrArgs(void)
Definition: script.cpp:135
bool scrCheckMain(void)
Definition: script.cpp:103
int main(int argc, char **argv)
Definition: script.cpp:156
bool scrSleep(void)
Definition: script.cpp:149