Beispiel #1
0
    void Console::execute (const std::string& command)
    {
        // Log the command
        print("> " + command + "\n");

        Compiler::Locals locals;
        Compiler::Output output (locals);

        if (compile (command + "\n", output))
        {
            try
            {
                ConsoleInterpreterContext interpreterContext (*this, mPtr);
                Interpreter::Interpreter interpreter;
                MWScript::installOpcodes (interpreter, mConsoleOnlyScripts);
                std::vector<Interpreter::Type_Code> code;
                output.getCode (code);
                interpreter.run (&code[0], code.size(), interpreterContext);
            }
            catch (const std::exception& error)
            {
                printError (std::string ("Error: ") + error.what());
            }
        }
    }
Beispiel #2
0
    void ScriptManager::run (const std::string& name, Interpreter::Context& interpreterContext)
    {
        // compile script
        std::map<std::string, std::vector<Interpreter::Type_Code> >::iterator iter =
            mScripts.find (name);

        if (iter==mScripts.end())
        {
            if (!compile (name))
            {
                // failed -> ignore script from now on.
                std::vector<Interpreter::Type_Code> empty;
                mScripts.insert (std::make_pair (name, empty));
                return;
            }

            iter = mScripts.find (name);
            assert (iter!=mScripts.end());
        }

        // execute script
        if (!iter->second.empty())
            try
            {
                Interpreter::Interpreter interpreter (interpreterContext);
                installOpcodes (interpreter);
                interpreter.run (&iter->second[0], iter->second.size());
            }
            catch (const std::exception& e)
            {
                std::cerr << "exeution of script " << name << " failed." << std::endl;

                if (mVerbose)
                    std::cerr << "(" << e.what() << ")" << std::endl;

                iter->second.clear(); // don't execute again.
            }
    }