Пример #1
0
        internal LightExpression<Func<TotemEngine, IDynamicMetaObjectProvider, object>> ParseExprToLambda(SourceUnit sourceUnit)
        {
            bool isExpression;
            var ast = new Parser().ParseExpression(sourceUnit, out isExpression);
            if (ast == null)
                return null;
            var scope = new AnalysisScope.TopLevel(
                this,
                Expression.Parameter(typeof(TotemEngine), "totemRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider), "global"));

            var ret = Expression.Variable(typeof(object), "#__returnVariable");
            List<Expression> body = new List<Expression>();
            var b = AstGenerator.Generate(ast, scope);
            body.Add(Expression.Assign(ret, Expression.Convert(b, typeof(object))));
            if (sourceUnit.Kind == SourceCodeKind.InteractiveCode)
            {
                if(isExpression) body.Add(Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(object) }), ret));
                body.Add(Expression.Call(typeof(Console).GetMethod("WriteLine", Type.EmptyTypes)));
                body.Add(ret);
            }

            var moduleFun = Utils.LightLambda<Func<TotemEngine, IDynamicMetaObjectProvider, object>>(
                typeof(object),
                Expression.Block(new ParameterExpression[] { ret }, body),
                "_snippet_",
                new List<ParameterExpression>() { scope.RuntimeExpr, scope.GlobalExpr }
            );
            //var moduleFun = Expression.Lambda<Func<TotemEngine, IDynamicMetaObjectProvider, object>>(
            //    Expression.Block(new ParameterExpression[] { ret }, body),
            //    scope.RuntimeExpr,
            //    scope.ModuleExpr
            //);
            return moduleFun;
        }
Пример #2
0
 private AstGenerator(AnalysisScope.TopLevel global)
 {
     this.global = global;
     this.engine = global.Engine;
     this.scope = this.function = new AnalysisScope.Function(global, "module", true);
 }