示例#1
0
        public void ExecuteAsParameter(RuntimeData runtime)
        {
            List <IExpression> line = new List <Interface.IExpression>();

            if (this.Count == 0)
            {
                return;
            }

            if (!(this.Items.Last() is LineBreak))
            {
                this.AddItem(runtime, new LineBreak());
            }

            for (int i = 0; i < this.Count; i++)
            {
                if (this.Items[i] is LineBreak)
                {
                    if (line.Count == 0)
                    {
                        throw new SyntaxException(string.Format("パラメータが空です。"), this);
                    }

                    if (line.Count == 1)
                    {
                        runtime.NowBlock.Push(line[0]);
                        line.Clear();
                    }
                    else
                    {
                        Formula f = new Expression.Formula();
                        foreach (var item in line)
                        {
                            f.AddItem(runtime, item);
                        }
                        runtime.NowBlock.Push(f);

                        line.Clear();
                    }
                    continue;
                }
                else
                {
                    line.Add(this.Items[i]);
                }
            }
        }
示例#2
0
        private void Step()
        {
            Token t = this.items[this.index];

            if (t.Type == TokenType.Syntax)
            {
                if (this.setting.Spec.StartBrackets.Contains(t.Text[0]))
                {
                    Formula f = new Expression.Formula()
                    {
                        Token = t
                    };
                    this.formulas.Peek().AddItem(null, f);
                    this.formulas.Push(f);
                }
                else if (this.setting.Spec.EndBrackets.Contains(t.Text[0]))
                {
                    IExpression f = this.formulas.Pop();
                    if (this.doNormalize)
                    {
                        f = this.Normalization(f as Formula);
                        this.formulas.Peek().RemoveAt(this.formulas.Peek().Count - 1);
                        this.formulas.Peek().AddItem(null, f);
                    }
                }
                else if (t.Text == ";" || t.Text == ",")
                {
                    this.formulas.Peek().AddItem(null, new LineBreak(t));
                }
                else
                {
                    throw new NotImplementedException(string.Format("制御文字 '{0}'を解析することができませんでした。", t.Text));
                }

                return;
            }


            this.formulas.Peek().AddItem(null, TokenToExpression(t));
        }
示例#3
0
        public override INumber Eval(RuntimeData runtime)
        {
            if (this.Count == 0)
            {
                return(null);
            }
            else if (this.Count == 1)
            {
                return((this.Items[0] as IEval).Eval(runtime));
            }
            else if (this.Items.Where(i => i is LineBreak).Count() >= 1)
            {
                // 一番最後がLineBreakじゃなければ追加する
                if (!(this._items.Last() is LineBreak))
                {
                    this._items.Add(new LineBreak(new Token(";", TokenType.Syntax)));
                }


                // LineBreakがあるごとに逆ポーランド記法に変換して実行
                Formula f     = new Expression.Formula();
                IEval   res   = null;
                Array   res_a = null;
                // 配列形式のフォーマットなら配列形式で返す
                if (this.token != null && this.token.Text == "[")
                {
                    res_a = new Array();
                }

                for (int i = 0; i < this._items.Count; i++)
                {
                    if (this._items[i] is LineBreak)
                    {
                        if ((this._items[i] as LineBreak).Token.Text == "," && res_a == null)
                        {
                            throw new SyntaxException(string.Format("不正な文字です。 '{0}' 関数ではないものを関数のように扱っている可能性があります。",
                                                                    this._items[i].Token.Text), this._items[i]);
                        }
                        if (runtime.Setting.IsDebug)
                        {
                            Console.WriteLine("Eval Formula : " + f.ToString());
                        }
                        SyntaxAnalyzer.ConvertToRPEFormula ea = new SyntaxAnalyzer.ConvertToRPEFormula(
                            f, runtime.Setting);
                        res = ea.ConvertToRPE().Eval(runtime);

                        if (res_a != null)
                        {
                            res_a.Items[0].Add(res as INumber);                // '配列' 部の処理
                        }
                        f._items.Clear();
                    }
                    else
                    {
                        f._items.Add(this._items[i]);
                    }
                }

                if (res_a == null)
                {
                    return(res as INumber);
                }
                else // 配列作成でこの式を読んでいる場合は配列として返す
                {
                    return(res_a as INumber);
                }
            }
            else   // This.Items.Countが2以上のときじゃないと逆ポーランド式に変換sれない
            {
                SyntaxAnalyzer.ConvertToRPEFormula ea = new Analyzer.SyntaxAnalyzer.ConvertToRPEFormula(this, runtime.Setting);
                return(ea.ConvertToRPE().Eval(runtime));
            }
        }