Ejemplo n.º 1
0
 public void compile(XElement parameter, Stack data)
 {
     data.push(parameter.Value);
 }
Ejemplo n.º 2
0
 public void compile(XElement parameter, Stack data)
 {
     XAttribute name = parameter.Attribute(XName.Get("Id"));
     data.push(name.Value);
 }
Ejemplo n.º 3
0
 public void execute(Stack data, Heap heap)
 {
     if (m_macroService != null && m_resourceService != null)
     {
         int addr = data.popInt();
         XnaScrapId macro_id = new XnaScrapId(heap.readString(addr));//(data.popString());
         addr = data.popInt();
         XnaScrapId scriptName = new XnaScrapId(heap.readString(addr));//(data.popString());
         if (!m_macroService.RegisteredMacros.Keys.Contains(macro_id))
         {
             if (m_resourceService.Scripts.Keys.Contains(scriptName.ToString()))
             {
                 IScript script = m_resourceService.Scripts[scriptName.ToString()];
                 ScriptedMacro newMacro = new ScriptedMacro(null);
                 newMacro.Script = script;
                 newMacro.Name = macro_id.ToString();
                 m_macroService.RegisteredMacros.Add(scriptName, newMacro);
             }
         }
         else
         {
             throw new InvalidOperationException("Error Macro with Id \"" + macro_id + "\" already registered.");
         }
     }
 }
Ejemplo n.º 4
0
 public void execute(Stack data, Heap heap)
 {
     String id = XnaScrapId.CreateId().ToString();
     int addr = heap.allocString(id);
     data.push(addr);
 }
Ejemplo n.º 5
0
 public void execute(Stack data, Heap heap)
 {
     //throw new Exception("Cannot call Main::execute in a compiled script");
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Compile a routine
        /// </summary>
        /// <param name="routine"></param>
        /// <param name="bIsMain">The main function doesn't need a RETURN commands</param>
        private void compileRoutine(XElement routine, bool bIsMain, Stack stack)
        {
            DataTable localDataTable = new DataTable();

            // push all parameters(attributes)
            int index = 2 + routine.Attributes().Count(); // 0 points to next free,-1 is eip, -2 is ebp, -3 is first param
            foreach (XAttribute attr in routine.Attributes())
            {
                //if (bIsMain)
                //{
                    //m_currentProgramCode.Writer.Write((int)XmlScriptExecutor.OpCode.ALLOC_STRING_ON_HEAP);
                    //stack.push();
                //}
                localDataTable.add(attr.Name.LocalName, -1 * index * sizeof(int));
                --index;
            }
            foreach (XElement instruction in routine.Elements())
            {
                // push parameters
                foreach (XAttribute attr in instruction.Attributes())
                {
                    pushParameter(localDataTable, attr.Value);
                }
                compile(instruction, localDataTable, instruction.Attributes().Count());
            }
            if (!bIsMain)
            {
                m_currentProgramCode.Writer.Write((int)XmlScriptExecutor.OpCode.RETURN);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Execute a script with the given parameters.
        /// </summary>
        /// <param name="script"></param>
        /// <param name="parameters"></param>
        public void execute(IScript script)
        {
            if (!(script is XmlScriptCompiled))
            {
                throw new InvalidOperationException("Wrong script type!");
            }
            XmlScriptCompiled xmlScript = script as XmlScriptCompiled;
            Heap heap = new Heap();

            //// push eip
            //parameters.push(0);
            //// push base pointer
            //parameters.push(0);

            //execute(script, parameters, heap);

            Stack stack = new Stack();
            // go through intern parametersequencer and alloc strings
            if (script.ParameterSequence.Root != null)
            {
                //stack = new Stack(script.ParameterSequence.getMemStream(false));
                foreach (Parameter p in script.ParameterSequence.Root.Parameters.Values)
                {
                    // for now we only allow strings
                    if (p is StringParameter)
                    {
                        StringParameter stringParam = p as StringParameter;
                        stack.push(heap.allocString(stringParam.Value));
                    }
                    else
                    {
                        throw new WrongParameterTypeException("Only StringParameters are allowed when initialising a script.", typeof(StringParameter));
                    }

                }
            }

            // push base pointer
            stack.push(0);
            // push instruction pointer
            stack.push(0);

            execute(xmlScript, stack, heap);
        }
Ejemplo n.º 8
0
 public void execute(Stack data, Heap heap)
 {
     if (m_objectBuilder != null)
     {
         int addr = data.popInt();
         XnaScrapId id = new XnaScrapId(heap.readString(addr));//(data.popString());
         m_objectBuilder.beginGameObject(id);
     }
 }
Ejemplo n.º 9
0
 public void end(Stack data, Heap heap)
 {
     m_objectBuilder.endParameter();
 }
Ejemplo n.º 10
0
 public void end(Stack data, Heap heap)
 {
     m_objectBuilder.endElement();
 }
Ejemplo n.º 11
0
 public void end(Stack data, Heap heap)
 {
     m_objectBuilder.endGameObject();
 }
Ejemplo n.º 12
0
 public static String readStringFromHeap(Stack data, Heap heap)
 {
     // get address
     int addr = data.popInt();
     int ptr = data.readInt((int)data.Position + addr * sizeof(int));
     return heap.readString(ptr);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Executes a subroutine. All parameters must be written to the heap before execution.
        /// Afterwards the addresses of the parameters have to be stored in the stack (realtive to stackpointer).
        /// Mind this if you want to execute a script with external parameters.
        /// </summary>
        /// <param name="programCode">The script's code.</param>
        /// <param name="data">The data stack of the current script execution.</param>
        public void executeSub(MemoryStream programCode, int entryPoint, Stack data, Heap heap)
        {
            m_eip = entryPoint;
            m_ebp = (int)data.Position; // base pointer
            readNextOpCode(programCode);
            while (m_opcode != OpCode.HALT)
            {
                //eip = (int)programCode.Position;
                switch (m_opcode)
                {
                    case OpCode.POP_INT:
                        {
                            break;
                        }
                    case OpCode.POP_FLOAT:
                        {
                            break;
                        }
                    case OpCode.POP_STRING:
                        {
                            break;
                        }
                    case OpCode.PUSH_INT:
                        {
                            data.push(readNextProgramInt(programCode));
                            break;
                        }
                    case OpCode.PUSH_FLOAT:
                        {
                            data.push(readNextProgramFloat(programCode));
                            break;
                        }
                    case OpCode.PUSH_STRING:
                        {
                            data.push(readNextProgramString(programCode));
                            break;
                        }
                    case OpCode.PUSH_ADDR:
                        {
                            int addr = readNextProgramInt(programCode); // address relative to ebp
                            // read heap address from stack
                            int heapAddr = data.readInt(m_ebp + addr);
                            data.push(heapAddr);
                            break;
                        }
                    case OpCode.PUSH_EBP:
                        {
                            data.push(m_ebp);
                            break;
                        }
                    case OpCode.ADD_INT:
                        {
                            int val1 = data.popInt();
                            int val2 = data.popInt();
                            data.push(val1 + val2);
                            break;
                        }
                    case OpCode.SUB_INT:
                        {
                            int val1 = data.popInt();
                            int val2 = data.popInt();
                            data.push(val1 - val2);
                            break;
                        }
                    case OpCode.READ_INT:
                        {
                            int addr = data.popInt();
                            data.push(data.readInt(addr));
                            break;
                        }
                    case OpCode.WRITE_INT:
                        {
                            // read address
                            int addr = data.popInt();
                            // read value
                            int value = data.popInt();
                            // write
                            data.writeInt(addr,value);
                            break;
                        }
                    case OpCode.READ_FLOAT:
                        {
                            int addr = data.popInt();
                            data.push(data.readFlt(addr));
                            break;
                        }
                    case OpCode.WRITE_FLOAT:
                        {
                            // read address
                            int addr = data.popInt();
                            // read value
                            float value = data.popInt();
                            // write
                            data.writeFlt(addr, value);
                            break;
                        }
                    case OpCode.ALLOC_STRING_ON_HEAP:
                        {
                            String s = data.popString();
                            int address = heap.allocString(s);
                            data.push(address);
                            break;
                        }
                    case OpCode.READ_STRING_FROM_HEAP:
                        {
                            //int ptr = data.readInt((int)data.Position + addr * sizeof(int));
                            int ptr = data.popInt();
                            String s = heap.readString(ptr);
                            data.push(s);
                            break;
                        }
                    case OpCode.BEGIN_BLOCK:
                        {
                            // save base pointer
                            m_ebp = (int)data.Position;
                            data.push(m_ebp);
                            break;
                        }
                    case OpCode.CALL_FUNCTION:
                        {
                            // get function address
                            int func_addr = data.popInt();
                            // save instruction pointer
                            data.push(m_eip);

                            m_eip = func_addr;
                            programCode.Position = m_eip;
                            break;
                        }

                    case OpCode.RETURN:
                        {
                            m_eip = data.popInt(); // restore eip
                            programCode.Position = m_eip;
                            break;
                        }
                    case OpCode.END_BLOCK:
                        {
                            m_ebp = data.popInt(); // restore ebp
                            data.set(m_ebp);
                            break;
                        }
                        // Built in function
                    case OpCode.NATIVE_CALL:
                        {
                            int f = readNextProgramInt(programCode);
                            if (f > 0)
                            {
                                m_registeredFunctions.RegisteredFunctionAddresses[f].execute(data,heap);
                            }
                            else if (f < 0)
                            {
                                m_registeredFunctions.RegisteredFunctionAddresses[-1*f].end(data,heap);
                            }
                            break;
                        }
                }
                readNextOpCode(programCode);
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Executes a script from the beginging.
 /// </summary>
 /// <param name="script">The script tu execute.</param>
 /// <param name="data">Stack with the addresses of parameters on the heap. These addresses point to the beginning of the parameters in the heap Stack.</param>
 /// <param name="heap">Stack containing the parameter.</param>
 public void execute(XmlScriptCompiled script, Stack data, Heap heap)
 {
     script.reset();
     executeSub(script.ProgramCode, script.EntryPoint, data, heap);
 }
Ejemplo n.º 15
0
 public void end(Stack data, Heap heap)
 {
 }
Ejemplo n.º 16
0
 public void execute(Stack data, Heap heap)
 {
     if (m_objectBuilder != null)
     {
         int addr = data.popInt();
         String s = heap.readString(addr);//data.popString();
         m_objectBuilder.beginParameter(s);
     }
 }
Ejemplo n.º 17
0
 public void execute(Stack data, Heap heap)
 {
     if (m_objectBuilder != null)
     {
         int addr = data.popInt();
         m_objectBuilder.setValues(heap.readString(addr));
     }
 }
Ejemplo n.º 18
0
 public void execute(Stack data, Heap heap)
 {
     if (m_macroService != null)
     {
         int addr = data.popInt();
         XnaScrapId id = new XnaScrapId(heap.readString(addr));//(data.popString());
         if (m_macroService.RegisteredMacros.Keys.Contains(id))
         {
             IMacro macro = m_macroService.RegisteredMacros[id];
             // get number of arguments
             int numParameter = data.popInt();
             for (int i = 0; i < numParameter; ++i)
             {
                 addr = data.popInt();
                 String name = heap.readString(addr);//data.popString();
                 addr = data.popInt();
                 String value = heap.readString(addr);//data.popString();
                 macro.setParameter(name,value);
             }
             macro.execute();
         }
     }
 }
Ejemplo n.º 19
0
        public IScript Compile(String scriptCode, String name)
        {
            Stack stack = new Stack();
            //System.Diagnostics.Debugger.Launch();
            XDocument script = XDocument.Parse(scriptCode.Trim());

            m_entryPoint = -1;

            m_currentProgramCode = new MemoryStream();
            m_parameterSequence = new ParameterSequence();
            XElement root = script.Element("Root");
            if (root == null)
            {
                root = script.Element("root");
                if (root == null)
                {
                    throw new Exception("Error. Script must contain a root node.");
                }
            }
            foreach (XElement node in root.Elements())
            {
                bool isMain = false;
                if (node.Name.LocalName == "Main" || node.Name.LocalName == "main")
                {
                    // scripts starts here
                    m_entryPoint = (int)m_currentProgramCode.Position;
                    // push all attributes to the parameter sequencer (so the script can run with default parameters)
                    ParameterSequenceBuilder sequenceBuilder = new ParameterSequenceBuilder();
                    sequenceBuilder.createSequence();
                    foreach (XAttribute attr in node.Attributes())
                    {
                        sequenceBuilder.addParameter(new StringParameter(attr.Name.ToString(), attr.Value));
                    }
                    m_parameterSequence = sequenceBuilder.CurrentSequence;
                    stack = new Stack(m_parameterSequence.getMemStream());
                    isMain = true;
                }
                // add node as a function
                else
                {
                    //m_dataTable.Data.Add(node.Name.LocalName, (int)m_currentProgramCode.Position);
                    m_functionTable.add(node.Name.LocalName, (int)m_currentProgramCode.Position);
                    isMain = false;
                }
                compileRoutine(node, isMain,stack);
            }

            // end of program
            m_currentProgramCode.Writer.Write((int)XmlScriptExecutor.OpCode.HALT);

            XmlScriptCompiled compiledScript = new XmlScriptCompiled(scriptCode);
            compiledScript.Name = name;
            compiledScript.ProgramCode = m_currentProgramCode;
            compiledScript.EntryPoint = m_entryPoint;
            compiledScript.ParameterSequence = m_parameterSequence;

            if (m_entryPoint == -1)
            {
                throw new Exception("Error no entrypoint for script: " + name);
            }
            return compiledScript;
        }
Ejemplo n.º 20
0
        // URGENT: Use Parameter Sequencer
        /// <summary>
        /// Execute a script with the given parameters.
        /// </summary>
        /// <param name="script"></param>
        /// <param name="parameters"></param>
        public void execute(XmlScriptCompiled script, String[] parameters)
        {
            Stack data = new Stack();
            Heap heap = new Heap();
            // parameters in reverse order
            for(int i = parameters.Length-1; i >= 0; --i)
            {
                int a = heap.allocString(parameters[i]);
                data.push(a);
            }
            // push eip
            data.push(0);
            // push base pointer
            data.push(0);

            execute(script, data, heap);
        }