惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
IT之家
IT之家
博客园_首页
人人都是产品经理
人人都是产品经理
博客园 - Franky
I
InfoQ
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
雷峰网
雷峰网
博客园 - 聂微东
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
D
Docker

博客园 - 大约在冬季

如何在IronPython中使用C#扩展方法 LINQ入门教程示例使用F#的实现 宝宝照片更新喽 再谈两种不同字符串比较方法的性能对比 - 大约在冬季 - 博客园 如何让DevExpress.TreeList单元格中的自定义控件包含标签 见证中国A股市场:上午大盘加速寻底 沪指跌129点,探低至4241.02 漂亮宝宝100天啦!庆祝一下! F# 学习笔记(1/n) 权重股杀跌沪指半日破两关跌135点 WCF服务契约 听课笔记 最近性能优化一些感触,分享中…… 两种不同字符串比较方法的性能对比 .Net 事件类型的实现和推荐做法 设计时支持:如何获取环境数据 如何在C#中调用 IronPython 代码 (基于IronPython 2.0A3) 运行Oracle数据库配置向导创建数据库失败ORA-24324的解决方案 C#3.0 自动属性——只能在简单属性上偷懒 性能——换个角度看问题 设计模式的滋味
IronPyton分析表达式
大约在冬季 · 2007-11-07 · via 博客园 - 大约在冬季

全部代码如下:

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  大约在冬季  阅读(260)  评论(0)    收藏  举报

刷新页面返回顶部