示例#1
0
文件: Program.cs 项目: PtrMan/ai2
        public void dispatchInvokeStart(List<string> path, List<Datastructures.Variadic> parameters, List<ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel> calleeScopeLevels, out Datastructures.Variadic result, out ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult callResult)
        {
            if( path.Count == 2 )
            {
                if( path[0] == "array" && path[1] == "at" )
                {
                    Datastructures.Variadic indexVariadic;
                    Datastructures.Variadic arrayVariadic;
                    int index;
                    List<Datastructures.Variadic> array;

                    if( parameters.Count != 2 )
                    {
                        throw new Exception("array.at required two parameters!");
                    }

                    indexVariadic = parameters[1];
                    arrayVariadic = parameters[0];

                    if( indexVariadic.type != Datastructures.Variadic.EnumType.INT )
                    {
                        throw new Exception("array.at index is no int");
                    }

                    if( arrayVariadic.type != Datastructures.Variadic.EnumType.ARRAY )
                    {
                        throw new Exception("array.at array is not an array!");
                    }

                    index = indexVariadic.valueInt;
                    array = arrayVariadic.valueArray;

                    result = builtinArrayAt(array, index);

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if( path[0] == "math" && path[1] == "sin" )
                {
                    Datastructures.Variadic parameterVariadic;
                    float resultFloat;

                    if (parameters.Count != 1)
                    {
                        throw new Exception("math sin required one parameters!");
                    }

                    parameterVariadic = parameters[0];

                    if( parameterVariadic.type != Datastructures.Variadic.EnumType.FLOAT )
                    {
                        throw new Exception("math sin parameter must be a float");
                    }

                    resultFloat = (float)System.Math.Sin(parameterVariadic.valueFloat);

                    result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT);
                    result.valueFloat = resultFloat;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if (path[0] == "math" && path[1] == "cos")
                {
                    Datastructures.Variadic parameterVariadic;
                    float resultFloat;

                    if (parameters.Count != 1)
                    {
                        throw new Exception("math sin required one parameters!");
                    }

                    parameterVariadic = parameters[0];

                    if (parameterVariadic.type != Datastructures.Variadic.EnumType.FLOAT)
                    {
                        throw new Exception("math cos parameter must be a float");
                    }

                    resultFloat = (float)System.Math.Cos(parameterVariadic.valueFloat);

                    result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT);
                    result.valueFloat = resultFloat;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if (path[0] == "math" && path[1] == "equals")
                {
                    Datastructures.Variadic parameterA;
                    Datastructures.Variadic parameterB;
                    bool isEqual;

                    if( parameters.Count != 2 )
                    {
                        throw new Exception("math equals required two parameters!");
                    }

                    parameterA = parameters[0];
                    parameterB = parameters[1];

                    if( parameterA.type != Datastructures.Variadic.EnumType.INT && parameterA.type != Datastructures.Variadic.EnumType.FLOAT )
                    {
                        throw new Exception("math equals parameterA must be a number!");
                    }

                    if (parameterB.type != Datastructures.Variadic.EnumType.INT && parameterB.type != Datastructures.Variadic.EnumType.FLOAT)
                    {
                        throw new Exception("math equals parameterB must be a number!");
                    }

                    isEqual = Datastructures.Variadic.isEqual(parameterA, parameterB, 0.0001f);

                    result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.BOOL);
                    result.valueBool = isEqual;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if (path[0] == "math" && path[1] == "mod")
                {
                    Datastructures.Variadic parameterA;
                    Datastructures.Variadic parameterB;

                    if (parameters.Count != 2)
                    {
                        throw new Exception("math mod required two parameters!");
                    }

                    parameterA = parameters[0];
                    parameterB = parameters[1];

                    if (parameterA.type != Datastructures.Variadic.EnumType.INT && parameterA.type != Datastructures.Variadic.EnumType.FLOAT)
                    {
                        throw new Exception("math mod parameterA must be a number!");
                    }

                    if (parameterB.type != Datastructures.Variadic.EnumType.INT && parameterB.type != Datastructures.Variadic.EnumType.FLOAT)
                    {
                        throw new Exception("math mod parameterB must be a number!");
                    }

                    if( parameterA.type == Datastructures.Variadic.EnumType.INT && parameterB.type == Datastructures.Variadic.EnumType.INT )
                    {
                        result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT);
                        result.valueInt = parameterA.valueInt % parameterB.valueInt;

                        callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                        return;
                    }
                    else
                    {
                        // TODO
                        throw new Exception("TODO");
                    }
                }
                else if( path[0] == "array" && path[1] == "append" )
                {
                    Datastructures.Variadic arrayVariadic;
                    Datastructures.Variadic elementVariadic;

                    if (parameters.Count != 2)
                    {
                        throw new Exception("array append required two parameters!");
                    }

                    arrayVariadic = parameters[0];
                    elementVariadic = parameters[1];

                    if( arrayVariadic.type != Datastructures.Variadic.EnumType.ARRAY )
                    {
                        throw new Exception("array append  first parameter must be an array");
                    }

                    debug("--- array append");
                    debugVariadic("array ", arrayVariadic);
                    debugVariadic("element ", elementVariadic);

                    // NOTE< we mutate it inplace and return it >
                    arrayVariadic.valueArray.Add(elementVariadic);

                    debugVariadic("= array append result ", arrayVariadic);

                    result = arrayVariadic;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if( path[0] == "array" && path[1] == "extend" )
                {
                    Datastructures.Variadic arrayVariadic;
                    Datastructures.Variadic extendVariadic;

                    if (parameters.Count != 2)
                    {
                        throw new Exception("array extend  required two parameters!");
                    }

                    arrayVariadic = parameters[0];
                    extendVariadic = parameters[1];

                    if( arrayVariadic.type != Datastructures.Variadic.EnumType.ARRAY )
                    {
                        throw new Exception("array extend  first parameter must be an array");
                    }

                    if( extendVariadic.type != Datastructures.Variadic.EnumType.ARRAY )
                    {
                        throw new Exception("array extend  second parameter must be an array");
                    }

                    // NOTE< we mutate it inplace and return it >
                    arrayVariadic.valueArray.AddRange(extendVariadic.valueArray);

                    result = arrayVariadic;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if( path[0] == "array" && path[1] == "generate" )
                {
                    Datastructures.Variadic beginVariadic;
                    Datastructures.Variadic endVariadic;
                    int begin, end;
                    Datastructures.Variadic resultVariadic;
                    int i;

                    if (parameters.Count != 2)
                    {
                        throw new Exception("array generate  required two parameters!");
                    }

                    beginVariadic = parameters[0];
                    endVariadic = parameters[1];

                    if( beginVariadic.type != Datastructures.Variadic.EnumType.INT )
                    {
                        throw new Exception("array generate  first parameter must be an integer");
                    }

                    if( endVariadic.type != Datastructures.Variadic.EnumType.INT )
                    {
                        throw new Exception("array generate  second parameter must be an integer");
                    }

                    begin = beginVariadic.valueInt;
                    end = endVariadic.valueInt;

                    resultVariadic = new Datastructures.Variadic(Datastructures.Variadic.EnumType.ARRAY);
                    resultVariadic.valueArray = new List<Datastructures.Variadic>();

                    for( i = begin; i < end; i++ )
                    {
                        Datastructures.Variadic elementVariadic;

                        elementVariadic = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT);
                        elementVariadic.valueInt = i;

                        resultVariadic.valueArray.Add(elementVariadic);
                    }

                    result = resultVariadic;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else if( path[0] == "array" && path[1] == "length" )
                {
                    Datastructures.Variadic arrayVariadic;

                    if (parameters.Count != 1)
                    {
                        throw new Exception("array length  required one parameters!");
                    }

                    arrayVariadic = parameters[0];

                    if( arrayVariadic.type != Datastructures.Variadic.EnumType.ARRAY )
                    {
                        throw new Exception("array length  first parameter must be an array!");
                    }

                    result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT);
                    result.valueInt = arrayVariadic.valueArray.Count;

                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.DONE;
                    return;
                }
                else
                {
                    result = null;
                    callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.PATHINVALID;
                    return;
                }
            }
            else
            {
                result = null;
                callResult = ProgramRepresentation.Execution.FunctionalInterpreter.EnumInvokeDispatcherCallResult.PATHINVALID;
                return;
            }
        }
示例#2
0
        /**
         *
         * returns the value of the dag element as the result
         *
         */
        private static void returnValue(ProgramRepresentation.DagElementData dagElementData, InterpretationState state)
        {
            Datastructures.Variadic resultValue;

            if( dagElementData.type == DagElementData.EnumType.CONSTINT )
            {
                resultValue = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT);
                resultValue.valueInt = dagElementData.valueInt;
            }
            else if( dagElementData.type == DagElementData.EnumType.CONSTFLOAT )
            {
                resultValue = new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT);
                resultValue.valueFloat = dagElementData.valueFloat;
            }
            else if( dagElementData.type == DagElementData.EnumType.CONSTBOOL )
            {
                resultValue = new Datastructures.Variadic(Datastructures.Variadic.EnumType.BOOL);
                resultValue.valueBool = dagElementData.valueBool;
            }
            else
            {
                // also TODO< handling for other types >
                throw new Exception("Internal Error");
            }

            returnResult(state, resultValue);
            removeTopScope(state);
        }
示例#3
0
        private static void doLinkage(ProgramRepresentation.Program program, List<InstructionLinkageHolder> instructionLinkageHolders)
        {
            int instructionI;

            System.Diagnostics.Debug.Assert(program.dag.elements.Count == instructionLinkageHolders.Count);

            for( instructionI = 0; instructionI < instructionLinkageHolders.Count; instructionI++ )
            {
                foreach( InstructionLinkageHolder.LinkageHolder iterationLinkage in instructionLinkageHolders[instructionI].childLinks )
                {
                    if( iterationLinkage.type == InstructionLinkageHolder.LinkageHolder.Type.LOCAL )
                    {
                        // local

                        int linkedIndex;

                        linkedIndex = getIndexOfLocalLabel(instructionLinkageHolders, iterationLinkage.name);

                        program.dag.elements[instructionI].childIndices.Add(linkedIndex);
                    }
                    else
                    {
                        // global

                        // TODO
                        throw new LinkageError("TODO");

                        //program.dag.elements[instructionI].childIndices.Add(linkedIndex);
                    }
                }
            }
        }