public override void VisitForEachStatement(ForEachStatement node) { Append("foreach (var "); node.Identifier.Accept(this); Append(" in "); node.Expression.Accept(this); Append(")"); NewLine(); Append("{"); Indent(); NewLine(); if (node.Statement is Block) { node.Statement.Accept(this); } else { var block = new Block(node.Statement); block.Accept(this); } Outdent(); Append("}"); }
public override int Compile(FunctionContext context) { context.Position(Token); var stack = 0; var start = context.MakeLabel("doWhileStart"); var cont = context.MakeLabel("doWhileContinue"); var brk = context.MakeLabel("doWhileBreak"); var end = context.MakeLabel("doWhileEnd"); var containsFunction = new LoopContainsFunctionVisitor(); Block.Accept(containsFunction); var loopContext = containsFunction.Value ? new LoopContext(context) : context; // body loopContext.PushScope(); loopContext.PushLoop(cont, containsFunction.Value ? brk : end); stack += loopContext.Bind(start); if (containsFunction.Value) { stack += loopContext.Enter(); } stack += Block.Compile(loopContext); loopContext.PopLoop(); // condition check stack += context.Bind(cont); // continue if (containsFunction.Value) { stack += context.Leave(); } context.Statement(Condition); stack += Condition.Compile(context); stack += context.JumpTrue(start); if (containsFunction.Value) { stack += context.Jump(end); stack += context.Bind(brk); // break (with function) stack += context.Leave(); } stack += context.Bind(end); // break (without function) loopContext.PopScope(); CheckStack(stack, 0); return(stack); }
public void AcceptTest() { Block target = new Block(); IVisitor visitor = null; // TODO: Initialize to an appropriate value target.Accept(visitor); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
public static void Apply(Block block, JSParser parser) { // create a new instance of the visitor and apply it to the block var visitor = new ReorderScopeVisitor(parser); block.Accept(visitor); // get the first insertion point. Make sure that we skip over any comments and directive prologues. // we do NOT want to insert anything between the start of the scope and any directive prologues. int insertAt = 0; while (insertAt < block.Count && (block[insertAt].IsDirectivePrologue || block[insertAt] is ImportantComment)) { ++insertAt; } // first, we want to move all function declarations to the top of this block if (visitor.m_functionDeclarations != null) { foreach (var funcDecl in visitor.m_functionDeclarations) { insertAt = RelocateFunction(block, insertAt, funcDecl); } } // special case: if there is only one var statement in the entire scope, // then just leave it alone because we will only add bytes by moving it around, // or be byte-neutral at best (no initializers and not in a for-statement). if (visitor.m_varStatements != null && visitor.m_varStatements.Count > 1) { // then we want to move all variable declarations after to the top (after the functions) foreach (var varStatement in visitor.m_varStatements) { insertAt = RelocateVar(block, insertAt, varStatement); } } // then we want to do the same thing for all child functions (declarations AND other) if (visitor.m_functionDeclarations != null) { foreach (var funcDecl in visitor.m_functionDeclarations) { Apply(funcDecl.Body, parser); } } if (visitor.m_functionExpressions != null) { foreach (var funcExpr in visitor.m_functionExpressions) { Apply(funcExpr.Body, parser); } } }
private static Type FindModelType(Block block, Type passedModelType) { var modelFinder = new ModelFinder(); block.Accept(modelFinder); if (string.IsNullOrWhiteSpace(modelFinder.ModelTypeName)) { return(passedModelType ?? typeof(object)); } Type modelType; if (passedModelType != null) { modelType = passedModelType; while (modelType != null) { if (modelType.FullName == modelFinder.ModelTypeName || modelType.Name == modelFinder.ModelTypeName) { return(modelType); } modelType = modelType.BaseType; } throw new NotSupportedException(string.Format("Unable to discover CLR Type for model by the name of {0}. Ensure that the model passed to the view is assignable to the model declared in the view.", modelFinder.ModelTypeName)); } modelType = Type.GetType(modelFinder.ModelTypeName); if (modelType != null) { return(modelType); } modelType = AppDomainAssemblyTypeScanner.Types.Where(t => t.FullName == modelFinder.ModelTypeName).FirstOrDefault(); if (modelType != null) { return(modelType); } modelType = AppDomainAssemblyTypeScanner.Types.Where(t => t.Name == modelFinder.ModelTypeName).FirstOrDefault(); if (modelType != null) { return(modelType); } throw new NotSupportedException(string.Format("Unable to discover CLR Type for model by the name of {0}. Try using a fully qualified type name and ensure that the assembly is added to the configuration file.", modelFinder.ModelTypeName)); }
public static void RenameLocals(Block block, StringComparer nameComparer) { FindVariableDeclarationsVisitor fvdv = new FindVariableDeclarationsVisitor(); block.Accept(fvdv); List <DeclarationStatement> list = new List <DeclarationStatement>(); foreach (DeclarationStatement decl in fvdv.Declarations) { DeclarationStatement conflict = null; int conflictIndex = -1; for (int i = 0; i < list.Count; i++) { if (nameComparer.Equals(list[i].Declaration.Name, decl.Declaration.Name)) { conflict = list[i]; conflictIndex = i; break; } } if (conflict == null) { list.Add(decl); } else { // Handle conflict: try if "moveup" would be sufficient if (IsSameType(decl.Declaration.Type, conflict.Declaration.Type, nameComparer)) { // create declaration at beginning of class and // replace decl & conflict by assignment DeclarationStatement newDecl = new DeclarationStatement(conflict.LexicalInfo); newDecl.Declaration = new Declaration(conflict.Declaration.LexicalInfo, conflict.Declaration.Name, conflict.Declaration.Type); block.Insert(0, newDecl); ReplaceWithInitializer(decl); ReplaceWithInitializer(conflict); list[conflictIndex] = newDecl; } else { string newName = FindFreeName(decl.Declaration.Name, list, fvdv.Declarations, nameComparer); decl.ParentNode.Accept(new RenameLocalsVisitor(decl.Declaration.Name, newName, nameComparer)); decl.Declaration.Name = newName; } } } }
public virtual Block Rewrite(Block input) { input.Accept(this); Debug.Assert(_blocks.Count == 1); return(_blocks.Pop().Build()); }
public void OnLoopBody(Block block) { this._level++; block.Accept(this); this._level--; }
public override int Compile(FunctionContext context) { context.Position(Token); var stack = 0; var start = context.MakeLabel("foreachStart"); var cont = context.MakeLabel("foreachContinue"); var brk = context.MakeLabel("foreachBreak"); var end = context.MakeLabel("foreachEnd"); var containsFunction = new LoopContainsFunctionVisitor(); Block.Accept(containsFunction); var enumerator = context.DefineInternal("enumerator", true); // set enumerator context.Statement(Expression); stack += Expression.Compile(context); stack += context.LoadField(context.String("getEnumerator")); stack += context.Call(0, new List <ImmediateOperand>()); stack += context.Store(enumerator); var loopContext = containsFunction.Value ? new LoopContext(context) : context; // loop body loopContext.PushScope(); loopContext.PushLoop(containsFunction.Value ? cont : start, containsFunction.Value ? brk : end); IdentifierOperand identifier; if (DestructureExpression != null) { identifier = context.DefineInternal(Identifier, true); } else { // create the loop variable outside of the loop context (but inside of its scope!) if (!context.DefineIdentifier(Identifier)) { throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, Identifier); } identifier = context.Identifier(Identifier); } stack += loopContext.Bind(start); // continue (without function) if (containsFunction.Value) { stack += loopContext.Enter(); } // loop while moveNext returns true context.Statement(InToken, InToken); stack += loopContext.Load(enumerator); stack += loopContext.LoadField(context.String("moveNext")); stack += loopContext.Call(0, new List <ImmediateOperand>()); stack += loopContext.JumpFalse(containsFunction.Value ? brk : end); stack += loopContext.Load(enumerator); stack += loopContext.LoadField(context.String("current")); stack += loopContext.Store(identifier); if (DestructureExpression != null) { stack += loopContext.Load(identifier); stack += DestructureExpression.Compile(loopContext); } stack += Block.Compile(loopContext); if (containsFunction.Value) { stack += loopContext.Bind(cont); // continue (with function) stack += loopContext.Leave(); } stack += loopContext.Jump(start); if (containsFunction.Value) { stack += loopContext.Bind(brk); // break (with function) stack += loopContext.Leave(); } loopContext.PopLoop(); loopContext.PopScope(); // after loop stack += context.Bind(end); // break (without function) stack += context.Load(enumerator); stack += context.LoadField(context.String("dispose")); stack += context.Call(0, new List <ImmediateOperand>()); stack += context.Drop(); CheckStack(stack, 0); return(stack); }