Exemplo n.º 1
0
BinaryOperator Engine::binaryOperator ( QString name,int prio,PriorityMatch match )
{
	//correct prio
	if(prio<1)prio=1;
	if(prio>99)prio=99;
	//if new: declare success
	if(!d->binary.contains(name)){
		d->binary.insert(name,Private::BinaryOperator_s(prio));
		return d->binary[name].op;
	}
	//check whether prio matches
	if(d->binary[name].prio==prio)
		return d->binary[name].op;
	//override if the op is empty
	if(d->binary[name].op.isNull())
		match=OverridePrio;
	//check matching mode
	switch(match){
		case OverridePrio:
			//change prio
			d->binary[name].prio=prio;
			//fall through
		case IgnoreMismatch:
			//return original op
			return d->binary[name].op;
			break;
		default://FailIfMismatch
			//return fake
			return BinaryOperator();
			break;
	}
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
    if (argc < 3 || argc > 4)
    {
        cout << "usage: " << argv[0] << " <input.nsb> <output.nss> [charset]" << endl;
        return 1;
    }

    if (argc == 4)
        NpaFile::SetLocale(argv[3]);
    else
        NpaFile::SetLocale("ja_JP.CP932");

    ScriptFile Script(argv[1], ScriptFile::NSB);
    Output.Output.open(argv[2]);

    for (auto i : Script.GetIncludes())
        Output << "#include \"" << i << "\"\n";
    if (!Script.GetIncludes().empty())
        Output << '\n';

    uint32_t SourceIter = 0;
    while (pLine = Script.GetLine(SourceIter++))
    {
        switch (pLine->Magic)
        {
            case MAGIC_SCOPE_BEGIN:
                Indent();
                Output << "{\n";
                ++IndentLevel;
                Output.Written = false;
                break;
            case MAGIC_SCOPE_END:
                --IndentLevel;
                Indent();
                Output << "}\n";
                break;
            case MAGIC_CLEAR_PARAMS:
                if (!Output.Written && !Params.empty())
                {
                    Indent();
                    Output << Params[0];
                }
                Output << ";\n";
                Params.clear();
                Output.Written = false;
                break;
            case MAGIC_VARIABLE:
                Params.push_back(pLine->Params[0]);
                break;
            case MAGIC_LITERAL:
                if (pLine->Params[0] == "STRING" && pLine->Params[1][0] != '#' && !Nsb::IsValidConstant(pLine->Params[1].c_str()))
                    Params.push_back(string("\"" + pLine->Params[1] + "\""));
                else
                    Params.push_back(pLine->Params[1]);
                break;
            case MAGIC_CALL_FUNCTION:
                Indent();
                Output << pLine->Params[0] << GenParams(Params);
                break;
            case MAGIC_FUNCTION_DECLARATION:
            {
                pLine->Params[0][8] = ' ';
                Output << pLine->Params[0];
                pLine->Params.erase(pLine->Params.begin());
                Output << GenParams(pLine->Params) << '\n';
                break;
            }
            case MAGIC_SCENE_DECLARATION:
                pLine->Params[0][5] = ' ';
                Output << pLine->Params[0] << '\n';
                break;
            case MAGIC_CHAPTER_DECLARATION:
                pLine->Params[0][7] = ' ';
                Output << pLine->Params[0] << '\n';
                break;
            case MAGIC_END_FUNCTION:
            case MAGIC_END_SCENE:
            case MAGIC_END_CHAPTER:
                Output << '\n';
                break;
            case MAGIC_ADD_ASSIGN:
                AssignOperator("+=");
                break;
            case MAGIC_SUB_ASSIGN:
                AssignOperator("-=");
                break;
            case MAGIC_ADD_EXPRESSION:
                BinaryOperator("+");
                break;
            case MAGIC_SUB_EXPRESSION:
                BinaryOperator("-");
                break;
            case MAGIC_DIV_EXPRESSION:
                BinaryOperator("/");
                break;
            case MAGIC_MUL_EXPRESSION:
                BinaryOperator("*");
                break;
            case MAGIC_MOD_EXPRESSION:
                BinaryOperator("%");
                break;
            case MAGIC_CMP_EQUAL:
                BinaryOperator("==");
                break;
            case MAGIC_CMP_GREATER:
                BinaryOperator(">");
                break;
            case MAGIC_CMP_LESS:
                BinaryOperator("<");
                break;
            case MAGIC_LOGICAL_NOT_EQUAL:
                BinaryOperator("!=");
                break;
            case MAGIC_LOGICAL_GREATER_EQUAL:
                BinaryOperator(">=");
                break;
            case MAGIC_LOGICAL_LESS_EQUAL:
                BinaryOperator("<=");
                break;
            case MAGIC_CMP_LOGICAL_AND:
                BinaryOperator("&&");
                break;
            case MAGIC_CMP_LOGICAL_OR:
                BinaryOperator("||");
                break;
            case MAGIC_LOGICAL_NOT:
                Params[Params.size() - 1] = string("!") + Params[Params.size() - 1];
                break;
            case MAGIC_ASSIGN:
                Indent();
                if (!Params.empty())
                {
                    if (pLine->Params[0] == "__array_variable__")
                        Output << Params[0];
                    else
                        Output << pLine->Params[0];
                    Output << " = " << Params.back();
                }
                else
                    cout << "WARNING: MAGIC_SET without parameters! (Perhaps builtin return?)" << endl;
                break;
            case MAGIC_IF:
                Indent();
                Output << "if " << GenParams(Params) << '\n';
                Params.clear();
                break;
            case MAGIC_WHILE:
                Indent();
                Output << "while " << GenParams(Params) << '\n';
                Params.clear();
                break;
            case MAGIC_BREAK:
                Indent();
                Output << "break;\n";
                Params.clear();
                break;
            case MAGIC_PARSE_TEXT:
                Output << pLine->Params[2] << '\n';
                break;
            case MAGIC_NEGA_EXPRESSION:
                Params[Params.size() - 1] = string("-") + Params[Params.size() - 1];
                break;
            case MAGIC_AT_EXPRESSION:
                Params.back() = '@' + Params.back();
                break;
            // Currently ignored builtins
            case MAGIC_JUMP: // TODO: Check for else/else if
            case MAGIC_WHILE_END:
            case MAGIC_CASE_END:
            case MAGIC_SELECT_END:
            case MAGIC_SELECT_BREAK_END:
                break;
            case MAGIC_INCREMENT:
                Indent();
                Output << Params.back() << "++";
                break;
            case MAGIC_DECREMENT:
                Indent();
                Output << Params.back() << "--";
                break;
            case MAGIC_CALL_CHAPTER:
                Indent();
                Output << "call_chapter " << pLine->Params[0] << ";\n";
                break;
            case MAGIC_CALL_SCENE:
                Indent();
                Output << "call_scene " << pLine->Params[0] << ";\n";
                break;
            // Builtins which return value
            case MAGIC_UNK116:
            case MAGIC_UNK129:
            case MAGIC_UNK146:
            case MAGIC_UNK150:
            case MAGIC_RANDOM:
            case MAGIC_MESSAGE:
            case MAGIC_STR_STR:
            case MAGIC_IMAGE_HORIZON:
            case MAGIC_IMAGE_VERTICAL:
            case MAGIC_SOUND_AMPLITUDE:
            case MAGIC_COUNT:
            case MAGIC_SCROLLBAR_VALUE:
            case MAGIC_EXIST_SAVE:
            case MAGIC_STRING:
            case MAGIC_UNK140:
            case MAGIC_READ_FILE:
            case MAGIC_UNK184:
            case MAGIC_TIME:
            case MAGIC_REMAIN_TIME:
            case MAGIC_DURATION_TIME:
            case MAGIC_PASSAGE_TIME:
            case MAGIC_MODULE_FILE_NAME:
                WriteToParams();
                break;
            case MAGIC_SELECT:
                Indent();
                Output << "select\n";
                break;
            case MAGIC_CASE:
                Indent();
                Output << "case " << pLine->Params[0] << ":\n";
                break;
            case MAGIC_RETURN:
                Indent();
                Output << "return;\n";
                break;
            case MAGIC_SUB_SCRIPT:
            {
                string Param = pLine->Params[0];
                int Size = stoi(pLine->Params[1]);
                for (int i = Params.size() - Size; i < Params.size(); ++i)
                    Param += "[" + Params[i] + "]";
                Params.resize(Params.size() - Size);
                Params.push_back(Param);
                break;
            }
            case MAGIC_VARIABLE_VALUE:
                if (pLine->Params.size() == 2)
                    WriteToParams();
                else
                    WriteDefault();
                break;
            default:
                WriteDefault();
                break;
        }
    }
}