コード例 #1
0
ファイル: ForStatement.cs プロジェクト: joshholmes/ironruby
        internal static MSAst.Expression TransformForStatement(AstGenerator ag, MSAst.ParameterExpression enumerator,
                                                    Expression list, Expression left, MSAst.Expression body,
                                                    Statement else_, SourceSpan span, SourceLocation header,
                                                    MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel) {
            // enumerator = PythonOps.GetEnumeratorForIteration(list)
            MSAst.Expression init = Ast.Assign(
                    enumerator, 
                    ag.Operation(
                        typeof(IEnumerator),
                        PythonOperationKind.GetEnumeratorForIteration,
                        ag.TransformAsObject(list)
                    )
                );

            // while enumerator.MoveNext():
            //    left = enumerator.Current
            //    body
            // else:
            //    else
            MSAst.Expression ls = AstUtils.Loop(
                    ag.AddDebugInfo(Ast.Call(
                        enumerator,
                        typeof(IEnumerator).GetMethod("MoveNext")
                    ), left.Span),
                    null,
                    Ast.Block(
                        left.TransformSet(
                            ag,
                            SourceSpan.None,
                            Ast.Call(
                                enumerator,
                                typeof(IEnumerator).GetProperty("Current").GetGetMethod()
                            ),
                            PythonOperationKind.None
                        ),
                        body,
                        ag.UpdateLineNumber(list.Start.Line),
                        AstUtils.Empty()
                    ), 
                    ag.Transform(else_),
                    breakLabel, 
                    continueLabel
            );

            return Ast.Block(
                init,
                ls,
                AstUtils.Empty()
            );
        }
コード例 #2
0
ファイル: PythonAst.cs プロジェクト: jcteague/ironruby
        internal ScriptCode/*!*/ TransformToAst(CompilationMode mode, CompilerContext/*!*/ context) {
            // Create the ast generator
            // Use the PrintExpression value for the body (global level code)
            PythonCompilerOptions pco = context.Options as PythonCompilerOptions;
            Debug.Assert(pco != null);
            
            string name;
            if (!context.SourceUnit.HasPath || (pco.Module & ModuleOptions.ExecOrEvalCode) != 0) {
                name = "<module>";
            } else {
                name = context.SourceUnit.Path;
            }

            AstGenerator ag = new AstGenerator(mode, context, _body.Span, name, false, _printExpressions);

            
            MSAst.Expression body = Ast.Block(
                Ast.Call(
                    AstGenerator.GetHelperMethod("ModuleStarted"),
                    ag.LocalContext,
                    AstUtils.Constant(ag.BinderState, typeof(object)),
                    AstUtils.Constant(_languageFeatures)
                ),
                ag.UpdateLineNumber(0),
                ag.UpdateLineUpdated(false),
                ag.WrapScopeStatements(Transform(ag)),   // new ComboActionRewriter().VisitNode(Transform(ag))
                AstUtils.Empty()
            );
            if (_isModule) {
                string moduleName = pco.ModuleName;
                if (moduleName == null) {
#if !SILVERLIGHT
                    if (context.SourceUnit.HasPath && context.SourceUnit.Path.IndexOfAny(Path.GetInvalidFileNameChars()) == -1) {
                        moduleName = Path.GetFileNameWithoutExtension(context.SourceUnit.Path);
#else
                    if (context.SourceUnit.HasPath) {                    
                        moduleName = context.SourceUnit.Path;
#endif
                    } else {
                        moduleName = "<module>";
                    }
                }

                Debug.Assert(moduleName != null);

                body = Ast.Block(
                    ag.Globals.Assign(ag.Globals.GetVariable(ag, _fileVariable), Ast.Constant(name)),
                    ag.Globals.Assign(ag.Globals.GetVariable(ag, _nameVariable), Ast.Constant(moduleName)),
                    body // already typed to void
                );

                if ((pco.Module & ModuleOptions.Initialize) != 0) {
                    MSAst.Expression tmp = ag.HiddenVariable(typeof(object), "$originalModule");
                    // TODO: Should be try/fault
                    body = AstUtils.Try(
                        Ast.Assign(tmp, Ast.Call(AstGenerator.GetHelperMethod("PublishModule"), ag.LocalContext, Ast.Constant(moduleName))),
                        body
                    ).Catch(
                        typeof(Exception),
                        Ast.Call(AstGenerator.GetHelperMethod("RemoveModule"), ag.LocalContext, Ast.Constant(moduleName), tmp),
                        Ast.Rethrow(body.Type)
                    );
                }
            }

            body = ag.AddProfiling(body);
            body = ag.AddReturnTarget(body);

            if (body.Type == typeof(void)) {
                body = Ast.Block(body, Ast.Constant(null));
            }

            return ag.MakeScriptCode(body, context, this);
        }
コード例 #3
0
ファイル: PythonAst.cs プロジェクト: octavioh/ironruby
        internal MSAst.LambdaExpression/*!*/ TransformToAst(CompilerContext context) {
            // Create the ast generator
            // Use the PrintExpression value for the body (global level code)
            PythonCompilerOptions pco = context.Options as PythonCompilerOptions;
            Debug.Assert(pco != null);

            string name;
            if (!context.SourceUnit.HasPath || (pco.Module & ModuleOptions.ExecOrEvalCode) != 0) {
                name = "<module>";
            } else {
                name = context.SourceUnit.Path;
            }

            AstGenerator ag = new AstGenerator(context, _body.Span, name, false, _printExpressions);            
            ag.Block.Global = true;

            ag.Block.Body = Ast.Block(
                Ast.Call(
                    AstGenerator.GetHelperMethod("ModuleStarted"),
                    AstUtils.CodeContext(),
                    Ast.Constant(ag.BinderState, typeof(object)),
                    Ast.Constant(_languageFeatures)
                ),
                ag.UpdateLineNumber(0),
                ag.UpdateLineUpdated(false),
                ag.WrapScopeStatements(Transform(ag)),   // new ComboActionRewriter().VisitNode(Transform(ag))
                Ast.Empty()
            );
            if (_isModule) {
                Debug.Assert(pco.ModuleName != null);

                ag.Block.Body = Ast.Block(
                    AstUtils.Assign(_fileVariable.Variable, Ast.Constant(name)),
                    AstUtils.Assign(_nameVariable.Variable, Ast.Constant(pco.ModuleName)),
                    ag.Block.Body // already typed to void
                );

                if ((pco.Module & ModuleOptions.Initialize) != 0) {
                    MSAst.Expression tmp = ag.Block.HiddenVariable(typeof(object), "$originalModule");
                    // TODO: Should be try/fault
                    ag.Block.Body = AstUtils.Try(
                        Ast.Assign(tmp, Ast.Call(AstGenerator.GetHelperMethod("PublishModule"), AstUtils.CodeContext(), Ast.Constant(pco.ModuleName))),
                        ag.Block.Body
                    ).Catch(
                        typeof(Exception),
                        Ast.Call(AstGenerator.GetHelperMethod("RemoveModule"), AstUtils.CodeContext(), Ast.Constant(pco.ModuleName), tmp),
                        Ast.Rethrow(ag.Block.Body.Type)
                    );
                }
            }

            ag.Block.Body = ag.AddReturnTarget(ag.Block.Body);
            DisableInterpreter = ag.DisableInterpreter;
            return ag.Block.MakeLambda();
        }