📄 compiler.cpp
字号:
if(!base) base = script; if(sub) { sub = false; memcpy(script->trap, base->trap, sizeof(base->trap)); } for(;;) { if(ftoken) goto first; quote = false; paren = 0; if(!then) { cmask = 0; if(offset > bufsize - 160) { bufsize += 512; buffer = (char *)realloc(buffer, bufsize); } scrStream->getline(buffer + offset, bufsize - 1 - offset); if(scrStream->eof()) { if(wrapper && !execflag) { wrapper = false; execflag = true; embed = NULL; apps = false; setString(buffer, 16, "exec"); } else if(wrapper || (!total && !count && !offset)) { wrapper = false; embed = NULL; if(ripple && !strchr(name, ':')) break; setString(buffer, 16, exit_token); } else break; } ++lnum; bp = strrchr(buffer, '\\'); if(bp) { ++bp; if(isspace(*bp) || !*bp) { if(!embed) offset = bp - buffer - 1; continue; } } bp = buffer; if(embed) { if(!mscmd) { if(*bp != '#') continue; ++bp; if(strnicmp(embed, bp, strlen(embed))) continue; } if(mscmd) { while(isspace(*bp)) ++bp; if(*bp == '@') ++bp; if(stricmp(bp, "rem")) continue; bp += 3; while(isspace(*bp)) ++bp; if(strnicmp(embed, bp, strlen(embed))) continue; } bp += strlen(embed); if(isalnum(*bp)) { --bp; *bp = ' '; } } } else then = false; offset = 0; ++number;first: while(NULL != (token = getToken(&ftoken))) { if(*token == '~') { esave = (NamedEvent *)alloc(sizeof(NamedEvent)); esave->name = alloc(token + 1); esave->line = NULL; esave->next = events; esave->type = '~'; events = esave; continue; } if(*token == '@' || *token == '{') { esave = (NamedEvent *)alloc(sizeof(NamedEvent)); esave->name = alloc(token + 1); esave->line = NULL; esave->next = events; esave->type = '@'; events = esave; continue; } if(!ripple && !embed && !apps) if(!stricmp(token, "private") || !stricmp(token,"protected") || !stricmp(token, "public") || !stricmp(token, "program") ||!stricmp(token, "module")) { if(!stricmp(token, "module")) { ftoken = "fconst"; token = "program"; } pmode = alloc(token);repname1: name = getToken(); if(!name) break; if(*name == '+') { if(!stricmp(name, "+dtmf")) addPmask |= 0x08; else addPmask |= cmds->getTrapModifier(name + 1); goto repname1; } if(*name == '-') { subPmask &= ~cmds->getTrapModifier(name + 1); goto repname1; } token = "::"; break; } if(!strnicmp(token, "func", 4) && !ripple && !embed && !apps && !strchr(token, ':')) { ftoken = "fconst"; pmode = "function"; goto repname1; } if(!stricmp(token, "macro") && !ripple && !embed && !apps && !strchr(token, ':')) { ftoken = "fconst"; pmode = "function"; goto repname1; } if(!stricmp(token, "local") && !ripple && !embed && !apps && !strchr(token, ':')) { ftoken = "fconst"; pmode = "local"; goto repname1; } if(!strnicmp(token, "proc", 4) && !ripple && !embed && !apps && !strchr(token, ':')) { pmode = "function"; goto repname1; } if(!stricmp(token, "catch") && !ripple && !embed && !apps) { ftoken = "fconst"; name = getToken(); if(*name == '^') snprintf(catchname, sizeof(catchname), "-catch-signal:%s", ++name); else if(*name == '@') snprintf(catchname, sizeof(catchname), "-catch-%s", ++name); else snprintf(catchname, sizeof(catchname), "-catch-%s", name); name = catchname; pmode = "function"; token = "::"; break; } if((ripple || apps) && *token == '[') { if(use_funcs) pmode = "function"; else pmode = "public"; name = token + 1; cp = strchr(name, ']'); if(cp) *cp = 0; token = "::"; break; } tlen = (int)strlen(token); if(token[tlen - 1] == ':' && !ripple && !apps && !embed) { token[tlen - 1] = 0; name = token; token = "::"; break; } if(*token == '^') { if(!trapflag) { trapmask = 0; trapflag = true; } } if(!stricmp(token, "->") && !last) { token = "goto"; break; } if(*token != '^' && *token != '+' && *token != '-' && *token != '?') break; if(*token == '^') mask = cmds->getTrapMask(token + 1); else mask = cmds->getTrapModifier(token + 1); if(!mask) { slog.error("%s(%d): %s: unknown trap id", filename, lnum, token + 1); continue; } switch(*token) { case '^': last = NULL; script->mask |= mask | cmds->getTrapDefault(); trapmask |= mask; break; case '+': addmask |= mask; break; case '-': submask |= mask; break; case '?': cmask |= mask; } } if(!token) continue; if(!stricmp(token, "::") && !embed) break; if(!strnicmp(token, "exec.", 5) && (embed || apps)) execflag = true; if(!stricmp(token, "exec") && (embed || apps)) execflag = true; if(!strnicmp(token, "exec.", 5) && !execflag && exec_token) goto noexec; if(!stricmp(token, "exec") && !execflag && exec_token) {noexec: slog.error("%s(%d): exec only used in embedded", filename, lnum); continue; } if(!stricmp(token, "disuse") && defs) { if(NULL != (token = getToken())) { snprintf(temp, sizeof(temp), "use.%s", token); cmds->setValue(temp, "none"); continue; } token = "disuse"; } if(!stricmp(token, "use")) { if(NULL != (token = getToken())) { snprintf(temp, sizeof(temp), "use.%s", token); cp = (char *)cmds->getLast(temp); if(cp) token = cp; if(!stricmp(token, "none")) continue; if(!Script::use(token)) slog.warn("%s(%d): %s: package missing", filename, lnum, token); } else { slog.error("%s(%d): use: name missing", filename, lnum); continue; } snprintf(catchname, sizeof(catchname), "use.%s", token); token = catchname; } else if(!stricmp(token, "virtual") && !embed && (!ripple || use_macros) && scrStream == (istream *)&scrSource) { token = getToken(); if(!token) continue; if(strchr(token, '/')) continue; if(inccount > 255) continue; snprintf(temp, sizeof(temp), "virtual.%s", token); if(!cmds->getLast(temp)) { cp = getToken(); if(!cp || !*cp) cp = "none"; cmds->setValue(temp, "cp"); } incfiles[inccount++] = alloc(token); continue; } else if(!stricmp(token, "include") && use_merge && !apps && !embed) { token = getToken(); if(!token) continue; if(!strchr(token, ':')) { snprintf(temp, sizeof(temp), name); cp = strchr(temp, ':'); if(cp) *cp = 0; addString(temp, sizeof(temp), "::"); addString(temp, sizeof(temp), token); token = temp; } token = alloc(token); filter = getToken(); for(;;) { if(filter) { snprintf(temp, sizeof(temp), "%s", filter); cp = strchr(temp, ':'); if(cp) *cp = 0; addString(temp, sizeof(temp), ":"); filter = alloc(temp); } else filter = ""; merge = (merge_t *)alloc(sizeof(merge_t)); merge->next = mlist; merge->target = script; merge->source = token; merge->prefix = filter; mlist = merge; filter = getToken(); if(!filter) break; } continue; } else if((!stricmp(token, "requires") || !stricmp(token, "import")) && scrStream == (istream *)&scrSource && (!ripple || use_macros) && !apps && !embed) { token = getToken(); if(!token) continue; if(strchr(token, '/')) continue; if(inccount > 255) continue; incfiles[inccount++] = alloc(token); continue; } ignore = false; if(!token) continue; if(*token == '%' && !ripple) { pretoken = token; token = "expr"; } else if(*token == '&' && !ripple && !apps) { pretoken = token; token = "call"; } else if(!strnicmp(token, "*::", 3) && !ripple && !apps) { pretoken = token + 3; token = "call"; } else if(!strnicmp(token, "::", 2) && !ripple && !apps) { pretoken = token + 2; token = "call"; } else if(strstr(token, "::") && (!ripple || use_macros) && !apps) { pretoken = token; token = "call"; } else pretoken = NULL; if(*token == '@') { ignore = true; ++token; } trapflag = false; handler = cmds->getHandler(token); if(handler == (Method)NULL && use_definitions) { snprintf(temp, sizeof(temp), "definitions::%s", token); ds = getScript(temp); if(ds && ds->access == scrFUNCTION) { pretoken = alloc(temp); token = "call"; handler = cmds->getHandler(token); } } if(handler == (Method)NULL) { errmsg = preproc(token); if(!errmsg) continue; addmask = submask = 0; if(!ignore) slog.error("%s(%d): %s: %s", filename, lnum, token, errmsg); continue; } command = alloc(token); argc = 0; while(argc < maxargs && NULL != (token = getToken(&pretoken))) { if(token[0] == '$' && isalpha(token[1])) { if(!stricmp(token, "$script.name")) token = alloc((char *)basename); else if(!stricmp(token, "$script.file")) token = alloc((char *)name); else if(!stricmp(token, "$script.line")) { sprintf(temp, "%d", number); token = alloc(temp); } else { if(!strchr(++token, '.')) { snprintf(path, sizeof(path), "%s.%s", basename, token); token = path; } token = (char *)getDefined(token); } if(!token) token = ""; } else if(token[0] == 0x01) { token[0]='{'; token = alloc(token); } else if(token[0] == '.' && isalpha(token[1])) { cp = token + strlen(token) - 1; if(*cp != '.') { gvarname[0] = '%'; snprintf(gvarname + gvarlen, sizeof(gvarname) - gvarlen, "%s", token + 1); token = alloc(gvarname); } else token = alloc(token); } else if(!strnicmp(token, "=.", 2)) { gvarname[0] = '='; snprintf(gvarname + gvarlen, sizeof(gvarname) - gvarlen, "%s", token + 2); token = alloc(gvarname); } else if(!strnicmp(token, "#.", 2)) { gvarname[0] = '#'; snprintf(gvarname + gvarlen, sizeof(gvarname) - gvarlen, "%s", token + 2); token = alloc(gvarname); } else if(!strnicmp(token, "&.", 2) || !strnicmp(token, ">.", 2)) { gvarname[0] = '&'; snprintf(gvarname + gvarlen,sizeof(gvarname) - gvarlen, "%s", token + 2); token = alloc(gvarname); } else if(!strnicmp(token, "@.", 2)) { gvarname[0] = '@'; snprintf(gvarname + gvarlen,sizeof(gvarname) - gvarlen, "%s", token + 2); token = alloc(gvarname); } else if(!strnicmp(token, "%.", 2)) { gvarname[0] = '%'; snprintf(gvarname + gvarlen, sizeof(gvarname) - gvarlen, "%s", token + 2); token = alloc(gvarname); } else if(*token == '>' && isalnum(token[1])) { token = alloc(token); *token = '&'; } else token = alloc(token); args[argc++] = token; if(!stricmp(token, "then") && handler == (Method)&ScriptMethods::scrIf) { --bp; *bp = ' '; then = true; handler = (Method)&ScriptMethods::scrIfThen; break; } } args[argc++] = NULL; line = (Line *)alloc(sizeof(Line)); line->line = number; line->lnum = lnum; line->cmask = cmask; line->mask = ((~0 & ~trapmask) | addmask) & ~submask; if(script->mask) line->mask &= cmds->getTrapHandler(script); if(!trapmask) { line->mask |= addPmask1; line->mask &= subPmask1; } line->next = NULL; line->args = (const char **)alloc(sizeof(char *) * argc); line->argc = --argc; line->scr.method = handler; line->cmd = command; line->loop = 0; if(cmds->isInitial(line->cmd)) line->loop = 0xffff; addmask = submask = 0; if(!stricmp(command, "repeat") || !stricmp(command, "for") || !stricmp(command, "do") || !stricmp(command, "foreach")) { if(!looplevel) ++loopid; ++looplevel; line->loop = loopid * 128 + looplevel; } if(!stricmp(command, "loop") || !strnicmp(command, "loop.", 5)) { line->loop = loopid * 128 + looplevel; if(!looplevel) { slog.error("%s(%d): loop nesting error", filename, line->lnum); continue; } else --looplevel; } memcpy(line->args, &args, sizeof(char *) * argc); err = cmds->check(command, line, this); if(err) { if(*err) slog.error("%s(%d): %s: %s", filename, lnum, command, err); continue; } ++count; script->mask |= trapmask; if(!script->first) script->first = line; while(events) { esave = events->next; events->line = line; events->next = script->events; script->events = events; events = esave; } if(trapmask && !last) { for(i = 0; i < TRAP_BITS; ++i) { if((1l << i) & trapmask) { if(!script->trap[i]) script->trap[i] = line; } } } if(last) last->next = line; last = line; } line = script->first; if(!script->mask) script->mask = cmds->getTrapDefault(); script->mask |= addPmask1; script->mask &= subPmask1; addPmask1 = 0; subPmask1 = (unsigned long)~0; while(line) { line->mask &= script->mask; line->mask |= ((~script->mask) & cmds->imask); line = line->next; } total += count; checkSegment(script); if(first && count) initcount = count; else if(count) slog.info("compiled %s; %d steps", script->name, count); first = false; if(!scrStream->eof()) { execflag = false; if(apps && apps_prefix) snprintf(namebuf, sizeof(namebuf), "%s::%s", apps_prefix, name); else if(strstr(name, "::") || (ripple && !use_prefix) || apps) snprintf(namebuf, sizeof(namebuf), "%s", name); else snprintf(namebuf, sizeof(namebuf), "%s::%s", basename, name); name = namebuf; if(!strnicmp(name, "exec:", 5) && exec_funcs) pmode = "function"; goto compile; } scrSource.close(); scrSource.clear(); if(init) { line = last = NULL; if(initkey == SCRIPT_INDEX_SIZE && initcount) line = init->first; while(line) { if(line->loop != 0xffff) { slog.error("%s(%d): %s: not allowed as initializer",filename, line->lnum, line->cmd); if(last) last->next = line->next; else first = true; } line = line->next; } if(initkey == SCRIPT_INDEX_SIZE && initcount) slog.info("compiled %s initializer; %d steps", init->name, initcount); else if(initcount) slog.info("compiled %s; %d steps", init->name, initcount); init->next = index[initkey]; index[initkey] = init; } free(buffer); return total;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -