IronPyton分析表达式

全部代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using IronPython.Runtime;
using IronPython.Compiler;
using IronPython.Compiler.Ast;

namespace ConsoleApplication1 {
    
class Program {
        
static void Main(string[] args) {
            
//string expression = "Sum(ActiveObject.Parent.Discount*ActiveObject.PrimePrice*Amount)";
            string expression = "Sum(ActiveObject.Parent.Details,\"Money\")*ActiveObject.ExchangeRate";
            SystemState state 
= new SystemState();
            CompilerContext context 
= new CompilerContext();
            Parser p 
= Parser.FromString(state, context, expression);
            IronPython.Compiler.Ast.Expression ex 
= p.ParseTestListAsExpression();
            MyWalker w 
= new MyWalker();
            ex.Walk(w);
            
foreach (string item in w._expressions) {
                Console.WriteLine(item);                
            }

            Console.ReadLine();

        }

        
class MyWalker : AstWalker {
            
private Stack<string> _stack;
            
public List<string> _expressions;
            
public MyWalker() {
                _stack 
= new Stack<string>();
                _expressions 
= new List<string>();
            }

            
public override bool Walk(FieldExpression node) {
                
//        node.Target.ToString()    "IronPython.Compiler.Ast.FieldExpression:Parent"    string
                
//        node.Name.ToString()    "Discount"    string
                string name = node.Name.ToString();
                
if (_stack.Count > 0{
                    
if (_stack.Peek() != name) {
                        _stack.Push(name);
                    }

                }

                
else {
                    _stack.Push(name);
                }

                
string[] temps = node.Target.ToString().Split(':');
                
string target = temps[1];
                _stack.Push(target);
                
if (target == "ActiveObject"{
                    
//弹出
                    StringBuilder expressionBuilder = new StringBuilder();
                    
while (_stack.Count > 1{
                        expressionBuilder.Append(_stack.Pop())
                       .Append(
".");
                    }

                    expressionBuilder.Append(_stack.Pop());
                    _expressions.Add(expressionBuilder.ToString());
                }

                
                
//Console.WriteLine("NodeName = "+node.Name + ",      NodeTrage = " + node.Target);
                
                

                
return base.Walk(node);
            }


            
public override bool Walk(ConstantExpression node) {
                
int lastIndex = this._expressions.Count - 1;
                
if (lastIndex > -1{
                    
this._expressions[lastIndex] = this._expressions[lastIndex] + "." + node.Value.ToString();
                }

                
return base.Walk(node);
            }


        }

    }

}

posted @ 2007-11-07 15:23  大约在冬季  阅读(236)  评论(0编辑  收藏  举报