methods.cpp
来自「GNU ccScript is a C++ class framework fo」· C++ 代码 · 共 2,306 行 · 第 1/3 页
CPP
2,306 行
if(cp) size = 12 + atoi(cp); cp = getKeyword("decimal"); if(cp) size = 12 + atoi(cp); while(NULL != (cp = getOption(NULL))) { sym = mapSymbol(cp, size); if(!sym) { errmsg = "var-symbol-invalid"; continue; } if(sym->type != symINITIAL || sym->size != size) { errmsg = "var-already-defined"; continue; } sym->type = symNUMBER; clear(sym); } if(errmsg) error(errmsg); else skip(); return true;}bool ScriptMethods::scrRef(void){ const char *target = getOption(NULL); Symbol *sym = getSymbol(); ScriptSymbols *syms; if(!target) { error("ref-target-unknown"); return true; } if(!sym) { error("ref-missing-source"); return true; } syms = getSymbols(sym->id); if(syms != dynamic_cast<ScriptSymbols*>(this) && syms != frame[stack].local) { error("ref-invalid-source"); return true; } if(strchr(target, '.')) { error("ref-invalid-target"); return true; } if(frame[stack].local) syms = frame[stack].local; else syms = dynamic_cast<ScriptSymbols*>(this); if(!syms->setReference(target, sym)) error("ref-failed"); else skip(); return true;}bool ScriptMethods::scrReturn(void){ Line *line = getLine(); const char *label = getOption(NULL), *var; char *ext; char namebuf[256]; unsigned len = symsize; unsigned argc = 0; Name *scr; char *n; if(label && *label != '@' && *label != '{') label = getContent(label); tempidx = 0; while(argc < line->argc) { if(*line->args[argc++] != '=') continue; snprintf(temps[tempidx], symsize + 1, "%s", getContent(line->args[argc]));// line->args[argc] = temps[tempidx]; if(tempidx++ >= SCRIPT_TEMP_SPACE) tempidx = 0; ++argc; } while(stack) { if(frame[stack - 1].local == frame[stack].local && frame[stack - 1].script == frame[stack].script) pull(); else break; } if(stack) pull(); else { error("return-failed"); return true; } argc = 0; tempidx = 0; while(argc < line->argc) { var = line->args[argc++]; if(*var != '=') continue; ++argc; if(*(++var) == '%') ++var; else if(*var == '.') { snprintf(namebuf, sizeof(namebuf), "%s", frame[stack].script->name); n = strchr(namebuf, ':'); if(n) *n = 0; snprintf(namebuf + strlen(namebuf), sizeof(namebuf) - strlen(namebuf), "%s", var); var = namebuf; } ext = temps[tempidx++]; if(tempidx >= SCRIPT_TEMP_SPACE) tempidx = 0; setSymbol(var, ext, len); }retry: if(!label) { skip(); return true; } if(!*label) { skip(); return true; } if(*label == '@' || *label == '{') { if(scriptEvent(label + 1)) return true; } if(*label == '^') { if(!signal(++label)) { error("trap-invalid"); return true; } return true; } len = (unsigned)strlen(label); if(!strncmp(label, "::", 2)) { setString(namebuf, sizeof(namebuf), frame[stack].script->name); ext = strstr(namebuf, "::"); if(ext) setString(ext, sizeof(namebuf) + namebuf - ext, label); else addString(namebuf, sizeof(namebuf), label); label = namebuf; } else if(label[len - 1] == ':') { setString(namebuf, sizeof(namebuf), frame[stack].script->name); ext = strstr(namebuf, "::"); if(ext) *ext = 0; addString(namebuf, sizeof(namebuf), "::"); addString(namebuf, sizeof(namebuf), label); label = ext = namebuf; len = (unsigned)strlen(label); ext[len - 1] = 0; } scr = getScript(label); if(!scr) { label = getValue(NULL); if(label) goto retry; error("script-not-found"); return true; } clearStack(); frame[stack].caseflag = frame[stack].tranflag = false; frame[stack].script = scr; frame[stack].line = frame[stack].first = scr->first; frame[stack].index = 0; frame[stack].mask = getMask(); return true;}bool ScriptMethods::scrCall(void){ Line *line = getLine(); const char *cmd = line->cmd; const char *cp, *vp; unsigned idx = 0; Symbol *sym; if(!push()) { error("stack-overflow"); return true; } frame[stack].tranflag = false; frame[stack].index = 0; frame[stack].caseflag = false; if(!strnicmp(cmd, "source", 6)) { frame[stack].tranflag = true; return redirect(false); } frame[stack].local = new ScriptSymbols(); if(!strnicmp(cmd, "call", 4)) frame[stack].tranflag = true; else if(!strnicmp(cmd, "gosub", 5)) frame[stack].base = stack; while(idx < line->argc) { cp = line->args[idx++]; if(*cp != '=') continue; ++cp; vp = line->args[idx++]; if(*vp == '&') { --stack; sym = mapSymbol(vp); ++stack; frame[stack].local->setReference(cp, sym); continue; } setConst(cp, getKeyword(cp)); } return redirect(false);} bool ScriptMethods::scrBegin(void){ if(frame[stack].tranflag) { error("begin-already-in-transaction"); return true; } frame[stack].tranflag = true; skip(); return true;}bool ScriptMethods::scrEnd(void){ if(!frame[stack].tranflag) { error("end-not-in-transaction"); return true; } frame[stack].tranflag = false; skip(); return true;}bool ScriptMethods::scrCase(void){ unsigned short loop = 0xfffe; Line *line; if(!frame[stack].caseflag) if(conditional() || !frame[stack].line->argc) { frame[stack].caseflag = true; skip(); while(frame[stack].line) { if(frame[stack].line->scr.method == (Method)&ScriptMethods::scrCase) skip(); else return true; } return true; } if(stack && frame[stack].line->loop) loop = frame[stack - 1].line->loop; skip(); while(NULL != (line = frame[stack].line)) { if(line->loop == loop) return true; if(line->scr.method == (Method)&ScriptMethods::scrCase && !frame[stack].caseflag) return true; if(line->scr.method == (Method)&ScriptMethods::scrEndcase) return true; skip(); } return true;}bool ScriptMethods::scrEndcase(void){ frame[stack].caseflag = frame[stack].tranflag = false; skip(); return true;}bool ScriptMethods::scrOffset(void){ long offset; Method method; Line *line; numericExpression(&offset, 1, 0); --offset; if(!stack) { error("stack-underflow"); return true; } line = frame[stack - 1].line; method = line->scr.method; if(method != (Method)&ScriptMethods::scrForeach && method != (Method)&ScriptMethods::scrFor) { error("offset-not-indexed-loop"); return true; } --stack; if(offset < 0) { if((unsigned)(-offset) >= frame[stack].index) frame[stack].index = 1; else frame[stack].index += (unsigned short)offset; } else frame[stack].index += (unsigned short)offset; updated = false; return execute(frame[stack].line->scr.method);}bool ScriptMethods::scrRepeat(void){ unsigned short loop = frame[stack].line->loop; Line *line; int index = frame[stack].index; long count; frame[stack].index = 0; numericExpression(&count, 1, 0); if(index >= count) { line = frame[stack].line->next; while(line) { if(line->loop == loop) { frame[stack].line = line; skip(); return true; } line = line->next; } error("loop-overflow"); return true; } frame[stack].index = ++index; if(!push()) return true; skip(); return true;}bool ScriptMethods::scrFor(void){ Symbol *sym; unsigned short loop = frame[stack].line->loop; Line *line; int index = frame[stack].index; unsigned size = symsize; const char *opt; const char *kw = getKeyword("size"); char buffer[12]; if(kw) size = atoi(kw); frame[stack].index = 0; opt = getOption(NULL); if(!index) { kw = getKeyword("index"); if(kw) index = atoi(kw); } if(!index) ++index; sym = getKeysymbol("index"); if(sym) { snprintf(buffer, sizeof(buffer), "%d", index); commit(sym, buffer); } sym = mapSymbol(opt, size); if(!sym) { error("symbol-not-found"); return true; } frame[stack].index = index; opt = getValue(NULL); if(!opt) {failed: line = frame[stack].line->next; while(line) { if(line->loop == loop) { frame[stack].line = line; skip(); return true; } line = line->next; } error("loop-overflow"); return true; } if(!push()) goto failed; if(commit(sym, opt)) skip(); else error("for-cannot-set"); return true;}bool ScriptMethods::scrForeach(void){ Symbol *sym, *src; unsigned short loop = frame[stack].line->loop; Line *line; int index = frame[stack].index; unsigned size = symsize; const char *opt, *val; char pack = getPackToken(); const char *kw = getKeyword("size"); char buffer[1024]; char *cp; Array *a; ScriptProperty *p; if(kw) size = atoi(kw); kw = getKeyword("token"); if(kw && *kw) pack = *kw; frame[stack].index = 0; opt = getOption(NULL); val = getOption(NULL); if(!index) { kw = getKeyoption("index"); if(kw) kw = getSymContent(kw); if(kw) index = atoi(kw); } if(!index) ++index; sym = getKeysymbol("index"); if(sym) { snprintf(buffer, 11, "%d", index); commit(sym, buffer); } sym = mapSymbol(opt, size); src = mapSymbol(val, 0); if(!sym || !src) { error("symbol-not-found"); return true; } frame[stack].index = index; ++frame[stack].index; opt = NULL; switch(src->type) { case symARRAY: if(!symindex(src, --index)) goto failed; case symFIFO: case symSTACK: a = (Array *)&src->data; if(a->head == a->tail) goto failed; opt = extract(src); default: break; case symPROPERTY: memcpy(&p, &sym->data, sizeof(p)); if(p->token()) pack = p->token(); case symNORMAL: case symNUMBER: opt = extract(src); while(opt && --index) { opt = strchr(opt, pack); if(opt && *opt == pack) ++opt; } if(opt) { snprintf(buffer, sizeof(buffer), "%s", opt); opt = buffer; cp = (char *)strchr(opt, pack); if(cp) *cp = 0; } } if(!opt) {failed: line = frame[stack].line->next; while(line) { if(line->loop == loop) { frame[stack].line = line; skip(); return true; } line = line->next; } error("loop-overflow"); return true; } if(!push()) goto failed; if(commit(sym, opt)) skip(); else error("for-cannot-set"); return true;}bool ScriptMethods::scrDo(void){ unsigned short loop = frame[stack].line->loop; Line *line; frame[stack].index = 0; // always reset if(frame[stack].line->argc) { if(!conditional()) { line = frame[stack].line->next; while(line) { if(line->loop == loop) { frame[stack].line = line; skip(); return true; } line = line->next; } error("loop-overflow"); return true; } } if(!push()) return true; skip(); return true;}bool ScriptMethods::scrLoop(void){ unsigned short loop; if(stack < 1) { error("stack-underflow"); return true; } loop = frame[stack - 1].line->loop; if(!loop) { error("stack-not-loop"); return true; } if(frame[stack].line->argc) { if(!conditional()) { frame[stack - 1] = frame[stack]; --stack; skip(); return true; } } --stack; updated = false; return execute(frame[stack].line->scr.method);}bool ScriptMethods::scrContinue(void){ Line *line; unsigned short loop; if(frame[stack].line->argc) { if(!conditional()) { skip(); return true; } } if(stack < 1) { error("stack-underflow"); return true; } loop = frame[stack - 1].line->loop; line = frame[stack].line->next; if(!loop) { error("stack-not-loop"); return true; } while(line) { if(line->loop == loop) { frame[stack].line = line; return true; } line = line->next; } error("loop-overflow"); return true;}bool ScriptMethods::scrBreak(void){ Line *line; unsigned short loop; if(frame[stack].line->argc) { if(!conditional()) { skip(); return true; } } if(stack < 1) { error("stack-underflow"); return true; } loop = frame[stack - 1].line->loop; line = frame[stack].line->next; if(!loop) { error("stack-not-loop"); return true; } while(line) { if(line->loop == loop) { --stack; frame[stack].line = line; skip(); return true; } line = line->next; } error("loop-overflow"); return true;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?