Beispiel #1
0
        private void Eval()
        {
            if (!mainWindow.MakeUpToDate())
            {
                return;
            }

            int ErrorLineNumberCompilation;
            DataTypes ReturnType;
            PcodeRec FuncCode;
            Compiler.ASTExpression AST;
            CompileErrors CompileError = Compiler.CompileSpecialFunction(
                mainWindow.Document.CodeCenter,
                new FunctionParamRec[]
                {
                    new FunctionParamRec("frames", DataTypes.eInteger),
                    new FunctionParamRec("tables", DataTypes.eInteger),
                    new FunctionParamRec("data", DataTypes.eArrayOfFloat),
                },
                out ErrorLineNumberCompilation,
                out ReturnType,
                textBoxFormula.Text,
                false/*suppressCILEmission*/,
                out FuncCode,
                out AST);
            if (CompileError != CompileErrors.eCompileNoError)
            {
                textBoxFormula.Focus();
                textBoxFormula.SetSelectionLine(ErrorLineNumberCompilation - 1);
                textBoxFormula.ScrollToSelection();
                LiteralBuildErrorInfo errorInfo = new LiteralBuildErrorInfo(Compiler.GetCompileErrorString(CompileError), ErrorLineNumberCompilation);
                MessageBox.Show(errorInfo.CompositeErrorMessage, "Error", MessageBoxButtons.OK);
                return;
            }

            using (ParamStackRec ParamList = new ParamStackRec())
            {
                int numFrames = waveTableObject.WaveTableData.NumFrames;
                int numTables = waveTableObject.WaveTableData.NumTables;
                float[] vector = new float[numFrames * numTables];

                ArrayHandleFloat dataHandle = new ArrayHandleFloat(vector);

                int initialCapacity = 1/*frames*/ + 1/*tables*/ + 1/*data*/ + 1/*retaddr*/;
                ParamList.EmptyParamStackEnsureCapacity(initialCapacity);

                ParamList.AddIntegerToStack(numFrames);
                ParamList.AddIntegerToStack(numTables);
                ParamList.AddArrayToStack(dataHandle);
                ParamList.AddIntegerToStack(0); /* return address placeholder */

                for (int i = 0; i < numTables; i++)
                {
                    WaveTableStorageRec.Table table = waveTableObject.WaveTableData.ListOfTables[i];
                    for (int j = 0; j < numFrames; j++)
                    {
                        vector[i * numFrames + j] = table[j];
                    }
                }

                CodeCenterRec CodeCenter = mainWindow.Document.CodeCenter;
                EvalErrInfoRec ErrorInfo;
                EvalErrors EvaluationError = PcodeSystem.EvaluatePcodeThread.EvaluatePcode(
                    ParamList,
                    FuncCode,
                    CodeCenter,
                    out ErrorInfo,
                    new PcodeExterns(mainWindow));
                if (EvaluationError != EvalErrors.eEvalNoError)
                {
                    PcodeEvaluationErrorInfo errorInfo = new PcodeEvaluationErrorInfo(
                        EvaluationError,
                        ErrorInfo,
                        FuncCode,
                        CodeCenter);
                    MessageBox.Show(errorInfo.CompositeErrorMessage, "Error", MessageBoxButtons.OK);
                    return;
                }
                Debug.Assert(ParamList.GetStackNumElements() == initialCapacity); // args - retaddr + return value
#if DEBUG
                ParamList.Elements[2].AssertFloatArray();
#endif
                dataHandle = ParamList.Elements[2].reference.arrayHandleFloat;

                WaveTableStorageRec NewTable = new WaveTableStorageRec(numTables, numFrames, waveTableObject.WaveTableData.NumBits);
                float[] NewData = dataHandle.floats;
                if (NewData.Length != numTables * numFrames)
                {
                    PcodeEvaluationErrorInfo errorInfo = new PcodeEvaluationErrorInfo(
                        "<anonymous>",
                        PcodeSystem.GetPcodeErrorMessage(EvalErrors.eEvalArrayWrongDimensions),
                        1);
                    MessageBox.Show(errorInfo.CompositeErrorMessage, "Error", MessageBoxButtons.OK);
                    return;
                }
                SampConv.QuantizeAndClampVector(NewData, NewTable.NumBits);
                for (int i = 0; i < numTables; i++)
                {
                    WaveTableStorageRec.Table table = NewTable.ListOfTables[i];
                    for (int j = 0; j < numFrames; j++)
                    {
                        table[j] = NewData[i * numFrames + j];
                    }
                }

                undo.Push(waveTableObject.WaveTableData);
                redo.Clear();
                waveTableObject.WaveTableData = NewTable;
            }
        }
        private void DoCalculation()
        {
            if (!mainWindow.MakeUpToDate())
            {
                return;
            }

            SetUpSelection();

            PcodeRec  FuncCode;
            DataTypes ReturnType;

            if (!Compile(out FuncCode, out ReturnType))
            {
                return;
            }

            /* try to evaluate the code */

            StringBuilder Output = new StringBuilder();

            using (ParamStackRec ParamList = new ParamStackRec())
            {
                /* return address placeholder */
                ParamList.AddIntegerToStack(0);

                /* executing the actual code */
                EvalErrInfoRec ErrorInfo;
                EvalErrors     EvaluationError = PcodeSystem.EvaluatePcodeThread.EvaluatePcode(
                    ParamList,
                    FuncCode,
                    document.CodeCenter,
                    out ErrorInfo,
                    new PcodeExterns(mainWindow));
                if (EvaluationError != EvalErrors.eEvalNoError)
                {
                    PcodeEvaluationErrorInfo error = new PcodeEvaluationErrorInfo(
                        EvaluationError,
                        ErrorInfo,
                        FuncCode,
                        document.CodeCenter);
                    MessageBox.Show(error.CompositeErrorMessage, "Evaluation Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                Debug.Assert(ParamList.GetStackNumElements() == 1); // return value

                /* add new data to window */
                Output.AppendLine();
                switch (ReturnType)
                {
                default:
                    Debug.Assert(false);
                    throw new InvalidOperationException();

                case DataTypes.eBoolean:
                    Output.AppendLine(
                        ParamList.GetStackInteger(0) != 0
                                ? "returns boolean:  true"
                                : "returns boolean:  false");
                    break;

                case DataTypes.eInteger:
                    Output.AppendFormat("returns integer:  {0}", ParamList.GetStackInteger(0));
                    Output.AppendLine();
                    break;

                case DataTypes.eFloat:
                    Output.AppendFormat("returns float:  {0}", ParamList.GetStackFloat(0));
                    Output.AppendLine();
                    break;

                case DataTypes.eDouble:
                    Output.AppendFormat("returns double:  {0}", ParamList.GetStackDouble(0));
                    Output.AppendLine();
                    break;

                case DataTypes.eArrayOfBoolean:
                    Output.AppendFormat("returns array of booleans:");
                    Output.AppendLine();
                    goto ArrayMakePoint;

                case DataTypes.eArrayOfByte:
                    Output.AppendFormat("returns array of bytes:");
                    Output.AppendLine();
                    goto ArrayMakePoint;

                case DataTypes.eArrayOfInteger:
                    Output.AppendFormat("returns array of integers:");
                    Output.AppendLine();
                    goto ArrayMakePoint;

                case DataTypes.eArrayOfFloat:
                    Output.AppendFormat("returns array of floats:");
                    Output.AppendLine();
                    goto ArrayMakePoint;

                case DataTypes.eArrayOfDouble:
                    Output.AppendFormat("returns array of doubles:");
                    Output.AppendLine();
ArrayMakePoint:
                    if (ParamList.GetStackArray(0) == null)
                    {
                        Output.AppendFormat("NIL");
                        Output.AppendLine();
                    }
                    else
                    {
                        switch (ReturnType)
                        {
                        default:
                            Debug.Assert(false);
                            throw new InvalidOperationException();

                        case DataTypes.eArrayOfBoolean:
                        {
                            byte[] a = (byte[])ParamList.GetStackArray(0);
                            for (int i = 0; i < a.Length; i++)
                            {
                                int value = a[i];
                                Output.AppendFormat("{0,8}:  {1}", i, value != 0 ? "true" : "false");
                                Output.AppendLine();
                            }
                        }
                        break;

                        case DataTypes.eArrayOfByte:
                        {
                            byte[] a = (byte[])ParamList.GetStackArray(0);
                            for (int i = 0; i < a.Length; i++)
                            {
                                int value = a[i];
                                Output.AppendFormat("{0,8}:{1,3}", i, value);
                                Output.AppendLine();
                            }
                        }
                        break;

                        case DataTypes.eArrayOfInteger:
                        {
                            int[] a = (int[])ParamList.GetStackArray(0);
                            for (int i = 0; i < a.Length; i++)
                            {
                                int value = a[i];
                                Output.AppendFormat("{0,8}:{1,10}", i, value);
                                Output.AppendLine();
                            }
                        }
                        break;

                        case DataTypes.eArrayOfFloat:
                        {
                            float[] a = (float[])ParamList.GetStackArray(0);
                            for (int i = 0; i < a.Length; i++)
                            {
                                float value = a[i];
                                Output.AppendFormat("{0,8}:{1,12}", i, value);
                                Output.AppendLine();
                            }
                        }
                        break;

                        case DataTypes.eArrayOfDouble:
                        {
                            double[] a = (double[])ParamList.GetStackArray(0);
                            for (int i = 0; i < a.Length; i++)
                            {
                                double value = a[i];
                                Output.AppendFormat("{0,8}:{1,18}", i, value);
                                Output.AppendLine();
                            }
                        }
                        break;
                        }
                    }
                    break;
                }
            }

            Output.AppendLine();

            textBox.ReplaceRangeAndSelect(
                textBox.End,
                textBox.End,
                Output.ToString(),
                1);
            LastLine = textBox.Count - 1;
        }