Пример #1
0
        public static LambdaExpression Analzye(AnalysisContext analysisContext, Function function)
        {
            analysisContext.CurrentOutputExpression = Expression.Parameter(typeof(TextWriter), AnalysisContext.CurrentOutputIdentifier);

            List<Expression> outerExpressions = new List<Expression>
            {
                Expression.Assign(
                    analysisContext.CurrentOutputExpression,
                    Expression.Property(analysisContext.RuntimeContextExpression,
                                        analysisContext.OutputWriterProperty))
            };

            using (analysisContext.ScopeStack.TrackPush(function.ParameterList.SymbolTable))
            {
                //Create return target
                analysisContext.ReturnLabelTarget = Expression.Label(typeof(object), "lambdaReturn");
                LabelExpression returnLabel = Expression.Label(analysisContext.ReturnLabelTarget, Expression.Default(typeof(object)));

                outerExpressions.Add(StatementBlockAnalyzer.Analyze(analysisContext, function.Body));
                outerExpressions.Add(returnLabel);

                BlockExpression completeFunctionBody = Expression.Block(new[] { analysisContext.CurrentOutputExpression }, outerExpressions);
                LambdaExpression lambda = Expression.Lambda(completeFunctionBody, function.Name.Text, function.ParameterList.SymbolTable.GetParameterExpressions());
                return lambda;
            }
        }
Пример #2
0
 public static Expression Analyze(AnalysisContext analysisContext, StatementNodeBase node)
 {
     var analyzer = new StatementAnalyzer(analysisContext);
     node.Accept(analyzer);
     if(analyzer._result == null)
         throw new InternalException("StatementAnalyzer did not handle a " + node.GetType() + " which is a descendant of StatementNodeBase");
     return analyzer._result;
 }
Пример #3
0
        public static Expression Analyze(AnalysisContext analysisContext, StatementBlock statementBlock)
        {
            var analyzer = new StatementBlockAnalyzer(analysisContext);
            statementBlock.Accept(analyzer);

            if (analyzer._result == null)
                throw new InternalException("A StatementBlockAnalyzer did not generate a _result");

            return analyzer._result;
        }
Пример #4
0
        public static Expression Analyze(AnalysisContext analysisContext, ExpressionNodeBase node)
        {
            var analyzer = new ExpressionAnalyzer(analysisContext);
            node.Accept(analyzer);

            if (analyzer._result == null)
                throw new InternalException("Node type " + node.NodeKind + " not recognized by " + analyzer.GetType().FullName);

            return analyzer._result;
        }
Пример #5
0
        public HappyScriptCode Analyze(Module module, SourceUnit sourceUnit)
        {
            Init();

            //This List<Expression> becomes the global scope initializer

            var rootNamespaces = LoadAllAssemblies(module.LoadDirectives);

            ExpandoObject importScope = new ExpandoObject();
            foreach (HappyNamespaceTracker tracker in rootNamespaces.Values)
                DynamicObjectHelpers.SetMember(importScope, tracker.Name, tracker);

            #if WRITE_AST
            AstWriter writer = new AstWriter(Console.Out);
            module.WriteString(writer);
            #endif

            var getRuntimeContextExpr = Expression.Dynamic(_languageContext.CreateGetMemberBinder(RuntimeContextIdentifier, false), typeof(object), _globalScopeExp);
            UnaryExpression runtimeContextExpression = Expression.Convert(getRuntimeContextExpr, typeof(HappyRuntimeContext));
            var errorCollector = new ErrorCollector(_languageContext.ErrorSink);
            _analysisContext = new AnalysisContext(errorCollector, _languageContext, runtimeContextExpression, _globalScopeExp);

            RunAllVisitors(module, rootNamespaces);

            List<Expression> body = new List<Expression>();

            //Initialize globals
            using (_scopeStack.TrackPush(module.SymbolTable))
            {
                foreach (VariableDef def in module.GlobalDefStatements.SelectMany(defStmt => defStmt.VariableDefs))
                {
                    Expression initialValue = def.InitializerExpression != null ? ExpressionAnalyzer.Analyze(_analysisContext, def.InitializerExpression) : Expression.Constant(null, typeof(object));
                    HappySymbolBase symbol = module.SymbolTable.Items[def.Name.Text];
                    body.Add(symbol.GetSetExpression(initialValue));
                }
            }

            body.AddRange(module.Functions.Select(
                func => _analysisContext.PropertyOrFieldSet(func.Name.Text, _globalScopeExp, FunctionAnalyzer.Analzye(_analysisContext, func))));

            //At this point analysis has completed and all of our stacks should be empty
            DebugAssert.AreEqual(0, _scopeStack.Count, "scope stack not empty after analysis");

            //Add an empty expression--prevents an exception by Expression.Lambda when body is empty.
            //This allows compilation of empty template sets.
            if(body.Count == 0)
                body.Add(Expression.Empty());

            LambdaExpression globalScopeInitializer = Expression.Lambda(typeof(Action<IDynamicMetaObjectProvider>),
                                                                        Expression.Block(body), new[] { _globalScopeExp });

            HappyScriptCode output = new HappyScriptCode(sourceUnit, globalScopeInitializer.Compile());

            return output;
        }
Пример #6
0
        public static Expression Analyze(AnalysisContext analysisContext, OutputStatement node)
        {
            List<Expression> expressionsToWrite = new List<Expression>();

            foreach (var expr in node.ExpressionsToWrite)
            {
                Expression expressionToWrite = ExpressionAnalyzer.Analyze(analysisContext, expr);
                Expression writeToOutputExpression = analysisContext.CreateWriteToOutputExpression(expressionToWrite);
                expressionsToWrite.Add(writeToOutputExpression);
            }

            DebugAssert.IsGreater(0, expressionsToWrite.Count, "number of output expressions should have been > 0");
            return Expression.Block(expressionsToWrite);
        }
Пример #7
0
 StatementBlockAnalyzer(AnalysisContext analysisContext)
     : base(VisitorMode.VisitNodeAndChildren)
 {
     _analysisContext = analysisContext;
 }
Пример #8
0
 public static Expression Analyze(AnalysisContext analysisContext, IfStatement node)
 {
     var analyzer = new IfStatementAnalyzer(analysisContext);
     node.Accept(analyzer);
     return analyzer._result;
 }
Пример #9
0
 protected IfStatementAnalyzer(AnalysisContext analysisContext)
     : base(VisitorMode.VisitNodeAndChildren)
 {
     _analysisContext = analysisContext;
 }
Пример #10
0
 ExpressionAnalyzer(AnalysisContext analysisContext)
     : base(VisitorMode.VisitNodeOnly)
 {
     _analysisContext = analysisContext;
 }
Пример #11
0
 protected StatementAnalyzer(AnalysisContext analysisContext)
     : base(VisitorMode.VisitNodeOnly)
 {
     _analysisContext = analysisContext;
 }