Beispiel #1
0
        public override void ExportCodeStatements(IMethodCompile method)
        {
            MathNode.Trace("ExportCodeStatements for {0}", this.GetType());
            //0: start
            //1: end
            //2: function
            //3: dx             IsParam
            //4: sum            IsLocal
            //5: summing index  IsLocal IsParam
            base.ExportCodeStatements(method);
            //assign code expression to all x in the function
            OnPrepareVariable(method);
            MathNodeVariable.DeclareVariable(method.MethodCode.Statements, (IVariable)this[4]);
            CodeVariableReferenceExpression sum = new CodeVariableReferenceExpression(((IVariable)this[4]).CodeVariableName);

            method.MethodCode.Statements.Add(new CodeAssignStatement(sum, new CodePrimitiveExpression(0)));
            CodeExpression         c5  = this.GetParameterCode(method, 5);
            CodeIterationStatement cis = new CodeIterationStatement(
                new CodeVariableDeclarationStatement(this[5].DataType.Type, ((IVariable)this[5]).CodeVariableName,
                                                     new CodePrimitiveExpression(1)),
                new CodeBinaryOperatorExpression(c5, CodeBinaryOperatorType.LessThanOrEqual, new CodePrimitiveExpression(_intervals - 1)),
                new CodeAssignStatement(c5, new CodeBinaryOperatorExpression(c5, CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1))),
                new CodeStatement[] {
                new CodeAssignStatement(sum, new CodeBinaryOperatorExpression(sum, CodeBinaryOperatorType.Add, this.GetParameterCode(method, 2)))
            });

            method.MethodCode.Statements.Add(cis);
            //clear code expression to all x in the function
            this[2].AssignCodeExp(null, ((MathNodeVariable)this[3]).CodeVariableName);
        }
Beispiel #2
0
        /// <summary>
        /// generate code and put final result to n-th node
        /// </summary>
        /// <returns></returns>
        public override void ExportCodeStatements(IMethodCompile method)
        {
            int n = Branches;

            MathNode.Trace("ExportCodeStatements for {0}, branches: {1}", this.GetType(), n);
            IVariable v          = (IVariable)this[n + 1];
            string    resultName = v.CodeVariableName;

            v.VariableType = this.ResultType;
            OnPrepareVariable(method);
            MathNode.Trace("result is varibale {0}", resultName);
            CodeVariableReferenceExpression result = new CodeVariableReferenceExpression(resultName);

            MathNodeVariable.DeclareVariable(method.MethodCode.Statements, v);
            MathNode.Trace("default value: {0}", this[n].ToString());
            CodeExpression eDefault = this[n].ExportCode(method);

            if (!this[n].DataType.IsSameType(this.DataType))
            {
                MathNode.Trace("last code conversion from default value of {0} to {1}", this[n].DataType, this.DataType);
                eDefault = convert(eDefault);
            }
            CodeAssignStatement stDefault = new CodeAssignStatement(result, eDefault);

            if (n == 0)
            {
                MathNode.Trace("no condition given");
                method.MethodCode.Statements.Add(stDefault);
            }
            else
            {
                CodeExpression e = this[0][1].ExportCode(method);
                if (!this[0][1].DataType.IsSameType(this.DataType))
                {
                    e = convert(e);
                }
                CodeConditionStatement ccs = new CodeConditionStatement();
                CodeExpression         ce  = this[0][0].ExportCode(method);
                if (!(this[0][0].DataType.Type.Equals(typeof(bool))))
                {
                    ce = new CodeMethodInvokeExpression(
                        new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean", new CodeExpression[] { ce });
                }
                ccs.Condition = ce;
                bool isSameVariable = false;
                if (e is CodeVariableReferenceExpression)
                {
                    if (string.CompareOrdinal(((CodeVariableReferenceExpression)e).VariableName, resultName) == 0)
                    {
                        isSameVariable = true;
                    }
                }
                if (!isSameVariable)
                {
                    ccs.TrueStatements.Add(new CodeAssignStatement(result, e));
                }
                method.MethodCode.Statements.Add(ccs);
                for (int i = 1; i < n; i++)
                {
                    CodeConditionStatement ccs1 = new CodeConditionStatement();
                    ccs.FalseStatements.Add(ccs1);
                    ccs = ccs1;
                    e   = this[i][1].ExportCode(method);
                    if (!this[i][1].DataType.IsSameType(this.DataType))
                    {
                        e = convert(e);
                    }
                    ccs.TrueStatements.Add(new CodeAssignStatement(result, e));
                    ce = this[i][0].ExportCode(method);
                    if (!(this[i][0].DataType.Type.Equals(typeof(bool))))
                    {
                        ce = new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean", new CodeExpression[] { ce });
                    }
                    ccs.Condition = ce;
                }
                ccs.FalseStatements.Add(stDefault);
            }
        }