序列化算术表达式树

时间:2017-01-26 10:43:53

标签: c# serialization lambda abstract-syntax-tree expression-trees

我正在为调查目的做一个简单的任务。问题如下:

  1. 使用变量创建算术表达式。
  2. 为表达式构建AST。
  3. 将其发送到服务器(使用套接字)
  4. 计算服务器端的结果并返回结果。
  5. 现在我可以建造一棵树了。这个方法做到了:

        private readonly Stack<Expression> expressionStack = new Stack<Expression>();
        private readonly Stack<Symbol> operatorStack = new Stack<Symbol>();
        private readonly List<string> parameters = new List<string>();
        public Expression<Func<decimal[], decimal>> Parse(string expression)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return s => 0;
            }
    
            var arrayParameter = Expression.Parameter(typeof(decimal[]), "args");
    
            parameters.Clear();
            operatorStack.Clear();
            expressionStack.Clear();
    
            using (var reader = new StringReader(expression))
            {
                int peek;
                while ((peek = reader.Peek()) > -1)
                {
                    var next = (char)peek;
    
                    if (char.IsDigit(next))
                    {
                        expressionStack.Push(ReadOperand(reader));
                        continue;
                    }
    
                    if (char.IsLetter(next))
                    {
                        expressionStack.Push(ReadParameter(reader, arrayParameter));
                        continue;
                    }
    
                    if (Operation.IsDefined(next))
                    {
                        if (next == '-' && expressionStack.Count == 0)
                        {
                            reader.Read();
                            operatorStack.Push(Operation.UnaryMinus);
                            continue;
                        }
    
                        var currentOperation = ReadOperation(reader);
    
                        EvaluateWhile(() => operatorStack.Count > 0 && operatorStack.Peek() != Parentheses.Left &&
                            currentOperation.Precedence <= ((Operation)operatorStack.Peek()).Precedence);
    
                        operatorStack.Push(currentOperation);
                        continue;
                    }
    
                    if (next == '(')
                    {
                        reader.Read();
                        operatorStack.Push(Parentheses.Left);
    
                        if (reader.Peek() == '-')
                        {
                            reader.Read();
                            operatorStack.Push(Operation.UnaryMinus);
                        }
    
                        continue;
                    }
    
                    if (next == ')')
                    {
                        reader.Read();
                        EvaluateWhile(() => operatorStack.Count > 0 && operatorStack.Peek() != Parentheses.Left);
                        operatorStack.Pop();
                        continue;
                    }
    
                    if (next == ' ')
                    {
                        reader.Read();
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Encountered invalid character {0}", next),
                            "expression");
                    }
                }
            }
    
            EvaluateWhile(() => operatorStack.Count > 0);
    
            return Expression.Lambda<Func<decimal[], decimal>>(expressionStack.Pop(), arrayParameter);
        }
    

    此方法有效,并返回预期结果 在发送到服务器之前,我想将树序列化为二进制类型。我的问题如下。这是我申请的最简单的方法吗? 我找到了许多LINQ序列化的解决方案,但它们太大了。我不需要这些解决方案的全部功能。另外,通常,它们提供JSON或XML序列化,但我需要二进制序列化。 有人可以为这个问题建议一个简单易行的解决方案吗?

0 个答案:

没有答案