19 #include <ucommon/ucommon.h>
20 #include <ucommon/export.h>
24 using namespace UCOMMON_NAMESPACE;
26 bool Script::methods::scrDo(
void)
33 bool Script::methods::scrPrevious(
void)
36 if(stack[frame].index < 2) {
37 stack[frame].index = 0;
41 stack[frame].index -= 2;
45 bool Script::methods::scrExpand(
void)
48 Script::line_t *line = stack[frame].line;
49 const char *tuples = getContent(line->argv[0]);
50 unsigned tcount = Script::count(tuples);
54 while(line->argv[index]) {
55 sym = find(line->argv[index]);
58 sym = getVar(line->argv[index]);
61 return error(
"symbol not found");
64 return error(
"symbol not writable");
67 String::set(sym->data, sym->size + 1,
"");
69 cp = Script::get(tuples, index - 1);
70 Script::copy(cp, sym->data, sym->size + 1);
78 bool Script::methods::scrForeach(
void)
80 Script::line_t *line = stack[frame].line;
81 Script::symbol *sym = find(line->argv[0]);
83 unsigned index = stack[frame].index;
86 sym = getVar(line->argv[0]);
89 return error(
"symbol not found");
92 return error(
"symbol not writable");
96 if(cp && (*cp ==
'%' || *cp ==
'&')) {
97 Script::symbol *skip = find(cp);
101 index = atoi(skip->data);
103 String::set(skip->data, 2,
"0");
106 else if(NULL != (cp = getContent(cp))) {
113 cp = Script::get(getContent(line->argv[1]), index);
115 stack[frame].index = 0;
120 Script::copy(cp, sym->data, sym->size + 1);
121 stack[frame].index = ++index;
127 bool Script::methods::scrWhile(
void)
129 if(isConditional(0)) {
138 bool Script::methods::scrEndcase(
void)
144 bool Script::methods::scrCase(
void)
146 Script::method_t method = getLooping();
147 Script::line_t *line = stack[frame].line;
149 if(method == (method_t)&Script::methods::scrCase) {
151 while(stack[frame].line && stack[frame].line->loop > line->loop)
156 if(isConditional(0)) {
162 return scrOtherwise();
164 if(stack[frame].line->method == (method_t)&Script::methods::scrOtherwise) {
172 bool Script::methods::scrOtherwise(
void)
174 Script::line_t *line = stack[frame].line;
177 while(stack[frame].line && stack[frame].line->loop > line->loop)
183 bool Script::methods::scrUntil(
void)
192 bool Script::methods::scrLoop(
void)
198 bool Script::methods::scrElif(
void)
203 bool Script::methods::scrEndif(
void)
208 bool Script::methods::scrDefine(
void)
213 bool Script::methods::scrInvoke(
void)
215 Script::line_t *line = stack[frame].line;
218 getParams(line->sub, line->sub->first);
219 getParams(line->sub, line);
223 stack[frame].scope = line->sub;
224 stack[frame].line = line->sub->first->next;
225 startScript(line->sub);
227 mask = stack[frame].resmask |= line->sub->resmask;
228 if(mask != stack[frame].resmask) {
229 stack[frame].resmask = mask;
235 bool Script::methods::scrElse(
void)
237 Script::line_t *line = stack[frame].line;
241 while(NULL != (line = stack[frame].line)) {
242 if(line->method == (method_t)&Script::methods::scrIf)
244 else if(line->method == (method_t)&Script::methods::scrEndif) {
256 bool Script::methods::scrIf(
void)
258 Script::line_t *line = stack[frame].line;
261 if(isConditional(0)) {
267 while(NULL != (line = stack[frame].line)) {
268 if(line->method == (method_t)&Script::methods::scrIf)
270 else if(line->method == (method_t)&Script::methods::scrEndif) {
277 else if(!loop && line->method == (method_t)&Script::methods::scrElse) {
281 else if(!loop && line->method == (method_t)&Script::methods::scrElif) {
282 if(isConditional(0)) {
292 bool Script::methods::scrPause(
void)
297 bool Script::methods::scrBreak(
void)
299 Script::line_t *line = stack[frame].line;
300 Script::method_t method = getLooping();
302 if(method == (method_t)&Script::methods::scrCase || method == (method_t)&Script::methods::scrOtherwise) {
304 while(stack[frame].line && stack[frame].line->loop >= line->loop)
310 while(stack[frame].line && stack[frame].line->loop >= line->loop)
315 bool Script::methods::scrRepeat(
void)
318 --stack[frame].index;
322 bool Script::methods::scrContinue(
void)
328 bool Script::methods::scrNop(
void)
334 bool Script::methods::scrWhen(
void)
336 if(!isConditional(0))
343 bool Script::methods::scrExit(
void)
346 stack[frame].line = NULL;
350 bool Script::methods::scrRestart(
void)
352 unsigned mask = stack[frame].resmask;
354 setStack(stack[frame].scr);
355 if(mask == stack[frame].resmask)
360 bool Script::methods::scrGoto(
void)
362 Script::line_t *line = stack[frame].line;
364 Script::header *scr = NULL;
366 unsigned mask = stack[frame].resmask;
368 while(!scr && index < line->argc) {
369 cp = line->argv[index++];
371 if(scriptEvent(++cp))
375 scr = Script::find(*image, line->argv[0]);
378 return error(
"label not found");
383 if(mask == stack[frame].resmask)
388 bool Script::methods::scrGosub(
void)
390 unsigned index = stack[frame].index;
391 Script::line_t *line = stack[frame].line;
392 Script::header *scr = NULL;
394 unsigned mask = stack[frame].resmask;
395 Script::event *ev = NULL;
397 while(ev == NULL && scr == NULL && index < line->argc) {
398 cp = line->argv[index++];
400 scr = Script::find(*image, cp);
401 if(!scr && image->shared.get())
402 scr = Script::find(image->shared.get(), cp);
405 ev = scriptMethod(cp);
409 if(!stack[frame].index)
410 return error(
"label not found");
412 stack[frame].index = 0;
417 stack[frame].index = index;
422 stack[frame].base = frame;
423 if(mask == stack[frame].resmask)
427 stack[frame].line = ev->first;
428 stack[frame].index = 0;
434 bool Script::methods::scrReturn(
void)
436 unsigned mask = stack[frame].resmask;
440 if(mask == stack[frame].resmask)
445 bool Script::methods::scrIndex(
void)
448 int result = 0, value;
450 stack[frame].index = 0;
451 result = atoi(getValue());
453 while(NULL != (op = getValue())) {
456 result += atoi(getValue());
459 result -= atoi(getValue());
462 result *= atoi(getValue());
465 value = atoi(getValue());
467 return error(
"div by zero");
471 value = atoi(getValue());
473 return error(
"div by zero");
485 stack[frame].index = --result;
490 bool Script::methods::scrRef(
void)
492 const char *cp = stack[frame].line->argv[0];
498 bool Script::methods::scrExpr(
void)
500 unsigned exdecimals = Script::decimals;
501 const char *id, *cp, *op, *aop;
502 double result, value;
507 stack[frame].index = 0;
509 cp = getKeyword(
"decimals");
511 exdecimals = atoi(cp);
515 result = atof(getValue());
517 while(NULL != (op = getValue())) {
520 result += atof(getValue());
523 result -= atof(getValue());
526 result *= atof(getValue());
529 value = atof(getValue());
531 return error(
"div by zero");
536 lvalue = atol(getValue());
538 return error(
"div by zero");
539 result = (long)result % lvalue;
549 else if(eq(aop,
"#="))
550 value = (long)value % (
long)result;
551 else if(eq(aop,
"-="))
553 else if(eq(aop,
"*="))
555 else if(eq(aop,
"/=") && result)
557 else if(eq(aop,
"?=") || eq(aop,
":="))
560 stack[frame].index = 0;
563 cvt[2] =
'0' + exdecimals;
566 snprintf(buf,
sizeof(buf), cvt, value);
569 return error(
"invalid symbol");
575 bool Script::methods::scrVar(
void)
578 Script::line_t *line = stack[frame].line;
581 while(index < line->argc) {
582 id = line->argv[index++];
584 if(!getVar(++
id, getContent(line->argv[index++])))
585 return error(
"invalid symbol");
589 return error(
"invalid symbol");
596 bool Script::methods::scrConst(
void)
599 Script::line_t *line = stack[frame].line;
602 while(index < line->argc) {
603 id = line->argv[index++];
605 if(!setConst(++
id, getContent(line->argv[index++])))
606 return error(
"invalid constant");
613 bool Script::methods::scrError(
void)
617 Script::line_t *line = stack[frame].line;
622 while(index < line->argc) {
624 String::add(msg, 65,
" ");
627 cp = line->argv[index++];
630 String::add(msg, 65, getContent(cp));
635 bool Script::methods::scrClear(
void)
638 Script::line_t *line = stack[frame].line;
642 while(index < line->argc) {
643 id = line->argv[index++];
646 if(!sym || !sym->size)
647 return error(
"symbol not found");
655 bool Script::methods::scrAdd(
void)
658 Script::line_t *line = stack[frame].line;
659 Script::symbol *sym = find(line->argv[0]);
663 sym = getVar(line->argv[0]);
666 return error(
"symbol not found");
669 return error(
"symbol not writable");
671 while(index < line->argc) {
672 cp = getContent(line->argv[index++]);
673 String::add(sym->data, sym->size + 1, cp);
679 bool Script::methods::scrPack(
void)
682 Script::line_t *line = stack[frame].line;
683 Script::symbol *sym = find(line->argv[0]);
685 char quote[2] = {0,0};
688 sym = getVar(line->argv[0]);
691 return error(
"symbol not found");
694 return error(
"symbol not writable");
696 while(index < line->argc) {
697 cp = line->argv[index++];
700 String::add(sym->data, sym->size + 1,
",");
702 String::add(sym->data, sym->size + 1, ++cp);
703 String::add(sym->data, sym->size + 1,
"=");
704 cp = getContent(line->argv[index++]);
713 String::add(sym->data, sym->size + 1,
",");
715 if(!quote[0] && strchr(cp,
',')) {
716 if(*cp !=
'\'' && *cp !=
'\"')
720 if(!quote[0] && *cp ==
'\'')
723 String::add(sym->data, sym->size + 1, quote);
724 String::add(sym->data, sym->size + 1, cp);
728 String::add(sym->data, sym->size + 1, quote);
735 bool Script::methods::scrPush(
void)
737 Script::line_t *line = stack[frame].line;
738 Script::symbol *sym = createSymbol(line->argv[0]);
741 const char *key = NULL, *value;
744 return error(
"invalid symbol id");
747 return error(
"symbol not writable");
751 value = line->argv[2];
754 value = line->argv[1];
757 size = strlen(key) + 1;
759 if(*value !=
'\"' && *value !=
'\'') {
763 size += strlen(value);
765 size = strlen(line->argv[1]) + 3;
767 size += strlen(line->argv[2]);
769 if(strlen(sym->data) + size > sym->size)
770 return error(
"symbol too small");
773 String::add(sym->data, sym->size + 1,
",");
775 size = strlen(sym->data);
777 snprintf(sym->data + size, sym->size + 1 - size,
"%s='%s'", key, value);
779 snprintf(sym->data + size, sym->size + 1 - size,
"%s=%s", key, value);
781 snprintf(sym->data + size, sym->size + 1 - size,
"'%s'", value);
783 String::set(sym->data + size, sym->size + 1, value);
788 bool Script::methods::scrSet(
void)
791 Script::line_t *line = stack[frame].line;
792 Script::symbol *sym = createSymbol(line->argv[0]);
796 return error(
"invalid symbol id");
799 return error(
"symbol not writable");
802 while(index < line->argc) {
803 cp = getContent(line->argv[index++]);
804 String::add(sym->data, sym->size + 1, cp);
#define BAYONNE_NAMESPACE
GNU Bayonne library namespace.