示例#1
0
        protected virtual void AnalysExpression(LexemList lexems)
        {
            for (int i = 0; i < lexems.Count; i++)
            {
                var lexem = lexems[i];

                if (lexem is LexemBracket)
                {
                    i = this.SkipBrackets(lexems, i);
                }
                else if (lexem is LexemSwitch && lexem.ToString() == "?")
                {
                    var sw = lexem as LexemSwitch;

                    var simetric = sw.Simetric;
                    if (simetric == null)
                        throw new FormatException("Switch syntax should have simetric Else path");

                    int index = lexems.FindPosition(simetric);
                    if (index <= i)
                        throw new FormatException("Switch syntax balance is incorrect");

                    var condition = lexems.Range(0, i);
                    var then = lexems.Range(i + 1, index - i - 1);
                    var other = lexems.Range(index + 1, lexems.Count - index - 1);

                    this.OnSwitch(sw, condition, then, other);

                    return;
                }
            }

            this.AnalysLogical(lexems);
        }
示例#2
0
        protected override void Analys(LexemList lexems)
        {
            if (lexems.Count > 2 && lexems[0] is LexemVariable && lexems[1] is LexemOperator && (lexems[1] as LexemOperator).Text == "=")
            {
                _variable = lexems[0] as LexemVariable;

                if (String.IsNullOrEmpty(_variable.Name))
                    throw new FormatException("Assignable Variable Name is empty");

                this.AnalysExpression(lexems.Range(2, lexems.Count - 2));
            }
            else
                this.AnalysExpression(lexems);
        }
示例#3
0
        protected void Scan(LexemList lexems, LexemOperator.OperationType type, string[] operators, Action<LexemList> action, bool parseFromLeftToRight = false)
        {
            //обычный разбор выражения должен идти справа налево (<-)
            //например, попробуйте построить полиз для выражения: a-b+c или a/b*c
            //только разбирая одноуровневые операции справа налево получится корректный полиз!!!

            for (int i = parseFromLeftToRight ? 0 : lexems.Count - 1; 0 <= i && i < lexems.Count; i += (parseFromLeftToRight ? 1 : -1))
            {
                var lexem = lexems[i];

                if (lexem is LexemBracket)
                {
                    i = this.SkipBrackets(lexems, i, parseFromLeftToRight);
                }
                else if (lexem is LexemOperator)
                {
                    var op = lexem as LexemOperator;

                    if (op.OperatorType == type && (operators == null || operators.Contains(op.Text)))
                    {
                        this.OnOperator(op, lexems.Range(0, i), lexems.Range(i + 1, lexems.Count - i - 1));
                        return;
                    }
                }
            }

            if (action != null)
                action(lexems);
        }
示例#4
0
 protected override void OnElement(LexemList lexems)
 {
     this.AnalysExpression(lexems.Range(1, lexems.Count - 2));
 }
示例#5
0
 protected override void OnElement(LexemList lexems)
 {
     _lexems.Add(lexems[0]);
     this.AnalysExpression(lexems.Range(1, lexems.Count - 2));
     _lexems.Add(lexems[lexems.Count - 1]);
 }