public unsafe static void ForChunk <T0>(this EntityChunkList chunkList, ForEachChunk <T0> view) where T0 : unmanaged { Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <T0> .Type }; chunkList.ForChunk(componentTypes, view); }
protected override void Visit(ForEachChunk chunk) { ForEachInspector inspector = new ForEachInspector(chunk.Code); if (inspector.Recognized) { DetectCodeExpressionVisitor visitor = new DetectCodeExpressionVisitor(base.OuterPartial); DetectCodeExpressionVisitor.Entry entry = visitor.Add(inspector.VariableName + "Index"); DetectCodeExpressionVisitor.Entry entry2 = visitor.Add(inspector.VariableName + "Count"); DetectCodeExpressionVisitor.Entry entry3 = visitor.Add(inspector.VariableName + "IsFirst"); DetectCodeExpressionVisitor.Entry entry4 = visitor.Add(inspector.VariableName + "IsLast"); visitor.Accept(chunk.Body); if (entry4.Detected) { entry.Detected = true; entry2.Detected = true; } string str = "__iter__" + inspector.VariableName; if (entry2.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("Count=0;for(var ").Append(str).Append(" in ").Append(inspector.CollectionCode).Append("){ if(typeof(").Append(inspector.CollectionCode).Append("[").Append(str).Append("])!='function') {").Append("++").Append(inspector.VariableName).Append("Count;}}"); } if (entry.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("Index=0;"); } if (entry3.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("IsFirst=true;"); } this._source.Append("for (var ").Append(str).Append(" in ").Append(inspector.CollectionCode).Append(") {"); this._source.Append("var ").Append(inspector.VariableName).Append("=").Append(inspector.CollectionCode).Append("[__iter__").Append(inspector.VariableName).Append("];"); this._source.Append("if(typeof(").Append(inspector.VariableName).Append(")!='function') {"); if (entry4.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("IsLast=(").Append(inspector.VariableName).Append("Index==").Append(inspector.VariableName).Append("Count-1);"); } this._source.AppendLine(); base.Accept(chunk.Body); if (entry3.Detected) { this._source.Append(inspector.VariableName).Append("IsFirst=false;"); } if (entry.Detected) { this._source.Append("++").Append(inspector.VariableName).Append("Index;"); } this._source.AppendLine("}}"); } else { this._source.Append("for (").Append((string)chunk.Code).AppendLine(") {"); base.Accept(chunk.Body); this._source.Append("}"); } }
public void ChainingElseIfNestsProperly() { var condition1 = new ConditionalChunk { Type = ConditionalType.If, Condition = "x == 1" }; condition1.Body.Add(new SendLiteralChunk { Text = "a" }); var condition2 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 2" }; condition2.Body.Add(new SendLiteralChunk { Text = "b" }); var condition3 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 3" }; condition3.Body.Add(new SendLiteralChunk { Text = "c" }); var condition4 = new ConditionalChunk { Type = ConditionalType.Else }; condition4.Body.Add(new SendLiteralChunk { Text = "d" }); var loop = new ForEachChunk { Code = "x in @numbers" }; loop.Body.Add(condition1); loop.Body.Add(condition2); loop.Body.Add(condition3); loop.Body.Add(condition4); var chunks = Chunks( loop); _compiler.CompileView(chunks, chunks); var contents = ExecuteView(new StubViewData { { "numbers", new[] { 0, 1, 2, 3, 4, 7, 2, -4 } } }); Assert.AreEqual("dabcddbd", contents); }
public static void ForChunk <T0, T1>(this EntityManager em, ForEachChunk <T0, T1> view) where T0 : unmanaged where T1 : unmanaged { Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <T0> .Type, ComponentType <T1> .Type }; var arrays = em.EntityArrays.FindSmallest(componentTypes); if (arrays != null) { foreach (var array in arrays) { if (array.Specification.HasAll(componentTypes)) { array.ForChunk(componentTypes, view); } } } }
public void ForEachLoopOverArray() { var loop = new ForEachChunk { Code = "number in @numbers" }; loop.Body.Add(new SendExpressionChunk { Code = "number" }); var chunks = Chunks( loop); _compiler.CompileView(chunks, chunks); var contents = ExecuteView(new StubViewData { { "numbers", new[] { 1, 2, 3, 4, 5 } } }); Assert.AreEqual("12345", contents); }
private void VisitFor(SpecialNode specialNode, SpecialNodeInspector inspector) { var eachAttr = inspector.TakeAttribute("each"); var forEachChunk = new ForEachChunk { Code = AsCode(eachAttr), Position = Locate(specialNode.Element) }; Chunks.Add(forEachChunk); using (new Frame(this, forEachChunk.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new AssignVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } Accept(specialNode.Body); } }
private void VisitFor(SpecialNode specialNode, SpecialNodeInspector inspector) { AttributeNode attr = inspector.TakeAttribute("each"); ForEachChunk item = new ForEachChunk { Code = this.AsCode(attr), Position = this.Locate(specialNode.Element) }; this.Chunks.Add(item); using (new Frame(this, item.Body)) { foreach (AttributeNode node2 in inspector.Attributes) { AssignVariableChunk chunk2 = new AssignVariableChunk { Name = node2.Name, Value = this.AsCode(node2), Position = this.Locate(node2) }; this.Chunks.Add(chunk2); } base.Accept(specialNode.Body); } }
internal static void ForChunk <T0, T1>(this EntityChunkList chunkList, Span <ComponentType> componentTypes, ForEachChunk <T0, T1> view) where T0 : unmanaged where T1 : unmanaged { var c0 = chunkList.Specification.GetComponentIndex(componentTypes[0]); var c1 = chunkList.Specification.GetComponentIndex(componentTypes[1]); for (var k = 0; k < chunkList.ChunkCount; k++) { var chunk = chunkList[k]; var length = chunk.Count; var entities = chunk.Entities; var t0 = chunk.GetComponentData <T0>(c0, componentTypes[0]); var t1 = chunk.GetComponentData <T1>(c1, componentTypes[1]); view(length, entities, t0, t1); } }
public static void ForChunk <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(this EntityManager em, ForEachChunk <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> view) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged where T5 : unmanaged where T6 : unmanaged where T7 : unmanaged where T8 : unmanaged where T9 : unmanaged { Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <T0> .Type, ComponentType <T1> .Type, ComponentType <T2> .Type, ComponentType <T3> .Type, ComponentType <T4> .Type, ComponentType <T5> .Type, ComponentType <T6> .Type, ComponentType <T7> .Type, ComponentType <T8> .Type, ComponentType <T9> .Type }; var arrays = em.EntityArrays.FindSmallest(componentTypes); if (arrays != null) { foreach (var array in arrays) { if (array.Specification.HasAll(componentTypes)) { array.ForChunk(componentTypes, view); } } } }
internal static void ForChunk <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(this EntityChunkList chunkList, Span <ComponentType> componentTypes, ForEachChunk <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> view) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged where T5 : unmanaged where T6 : unmanaged where T7 : unmanaged where T8 : unmanaged where T9 : unmanaged { var c0 = chunkList.Specification.GetComponentIndex(componentTypes[0]); var c1 = chunkList.Specification.GetComponentIndex(componentTypes[1]); var c2 = chunkList.Specification.GetComponentIndex(componentTypes[2]); var c3 = chunkList.Specification.GetComponentIndex(componentTypes[3]); var c4 = chunkList.Specification.GetComponentIndex(componentTypes[4]); var c5 = chunkList.Specification.GetComponentIndex(componentTypes[5]); var c6 = chunkList.Specification.GetComponentIndex(componentTypes[6]); var c7 = chunkList.Specification.GetComponentIndex(componentTypes[7]); var c8 = chunkList.Specification.GetComponentIndex(componentTypes[8]); var c9 = chunkList.Specification.GetComponentIndex(componentTypes[9]); for (var k = 0; k < chunkList.ChunkCount; k++) { var chunk = chunkList[k]; var length = chunk.Count; var entities = chunk.Entities; var t0 = chunk.GetComponentData <T0>(c0, componentTypes[0]); var t1 = chunk.GetComponentData <T1>(c1, componentTypes[1]); var t2 = chunk.GetComponentData <T2>(c2, componentTypes[2]); var t3 = chunk.GetComponentData <T3>(c3, componentTypes[3]); var t4 = chunk.GetComponentData <T4>(c4, componentTypes[4]); var t5 = chunk.GetComponentData <T5>(c5, componentTypes[5]); var t6 = chunk.GetComponentData <T6>(c6, componentTypes[6]); var t7 = chunk.GetComponentData <T7>(c7, componentTypes[7]); var t8 = chunk.GetComponentData <T8>(c8, componentTypes[8]); var t9 = chunk.GetComponentData <T9>(c9, componentTypes[9]); view(length, entities, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9); } }
public unsafe static void ForChunk <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(this EntityChunkList chunkList, ForEachChunk <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> view) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged where T5 : unmanaged where T6 : unmanaged where T7 : unmanaged where T8 : unmanaged where T9 : unmanaged { Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <T0> .Type, ComponentType <T1> .Type, ComponentType <T2> .Type, ComponentType <T3> .Type, ComponentType <T4> .Type, ComponentType <T5> .Type, ComponentType <T6> .Type, ComponentType <T7> .Type, ComponentType <T8> .Type, ComponentType <T9> .Type }; chunkList.ForChunk(componentTypes, view); }
protected override void Visit(ForEachChunk chunk) { List <string> list = chunk.Code.ToString().Split(new char[] { ' ', '\r', '\n', '\t' }).ToList <string>(); int index = list.IndexOf("in"); string name = (index < 2) ? null : list[index - 1]; if (name == null) { this.CodeIndent(chunk).Write("foreach(").WriteCode(chunk.Code).WriteLine(")"); this.CodeDefault(); this.AppendOpenBrace(); base.Accept(chunk.Body); this.AppendCloseBrace(); } else { DetectCodeExpressionVisitor visitor = new DetectCodeExpressionVisitor(base.OuterPartial); DetectCodeExpressionVisitor.Entry entry = visitor.Add(name + "Index"); DetectCodeExpressionVisitor.Entry entry2 = visitor.Add(name + "Count"); DetectCodeExpressionVisitor.Entry entry3 = visitor.Add(name + "IsFirst"); DetectCodeExpressionVisitor.Entry entry4 = visitor.Add(name + "IsLast"); visitor.Accept(chunk.Body); if (entry4.Detected) { entry.Detected = true; entry2.Detected = true; } this.AppendOpenBrace(); if (entry.Detected) { this.DeclareVariable(name + "Index"); this._source.WriteLine("int {0}Index = 0;", new object[] { name }); } if (entry3.Detected) { this.DeclareVariable(name + "IsFirst"); this._source.WriteLine("bool {0}IsFirst = true;", new object[] { name }); } if (entry2.Detected) { this.DeclareVariable(name + "Count"); string str2 = string.Join(" ", list.ToArray(), index + 1, (list.Count - index) - 1); this._source.WriteLine("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});", new object[] { name, str2 }); } this.CodeIndent(chunk).Write("foreach(").WriteCode(chunk.Code).WriteLine(")"); this.CodeDefault(); this.AppendOpenBrace(); this.DeclareVariable(name); this.CodeHidden(); if (entry4.Detected) { this.DeclareVariable(name + "IsLast"); this._source.WriteLine("bool {0}IsLast = ({0}Index == {0}Count - 1);", new object[] { name }); } this.CodeDefault(); base.Accept(chunk.Body); this.CodeHidden(); if (entry.Detected) { this._source.WriteLine("++{0}Index;", new object[] { name }); } if (entry3.Detected) { this._source.WriteLine("{0}IsFirst = false;", new object[] { name }); } this.CodeDefault(); this.AppendCloseBrace(); this.AppendCloseBrace(); } }
protected override void Visit(ForEachChunk chunk) { var inspector = new ForEachInspector(chunk.Code); if (inspector.Recognized) { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(inspector.VariableName + "Index"); var autoCount = detect.Add(inspector.VariableName + "Count"); var autoIsFirst = detect.Add(inspector.VariableName + "IsFirst"); var autoIsLast = detect.Add(inspector.VariableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } if (autoCount.Detected) { // var itemCount=0;for(var __iter__item in coll){++itemCount;} _source .Append("var ") .Append(inspector.VariableName) .Append("Count=0;for(var __iter__") .Append(inspector.VariableName) .Append(" in ") .Append(inspector.CollectionCode) .Append("){++") .Append(inspector.VariableName) .Append("Count;}"); } if (autoIndex.Detected) { // var itemIndex=0; _source.Append("var ").Append(inspector.VariableName).Append("Index=0;"); } if (autoIsFirst.Detected) { // var itemIsFirst=true; _source.Append("var ").Append(inspector.VariableName).Append("IsFirst=true;"); } // for(var __iter__item in coll) { _source .Append("for (var __iter__") .Append(inspector.VariableName) .Append(" in ") .Append(inspector.CollectionCode) .Append(") {"); // var item=coll[__iter__item]; _source .Append("var ") .Append(inspector.VariableName) .Append("=") .Append(inspector.CollectionCode) .Append("[__iter__") .Append(inspector.VariableName) .Append("];"); if (autoIsLast.Detected) { // var itemIsLast=(itemIndex==itemCount-1); _source .Append("var ") .Append(inspector.VariableName) .Append("IsLast=(") .Append(inspector.VariableName) .Append("Index==") .Append(inspector.VariableName) .Append("Count-1);"); } _source.AppendLine(); Accept(chunk.Body); if (autoIsFirst.Detected) { // itemIsFirst=false; _source.Append(inspector.VariableName).Append("IsFirst=false;"); } if (autoIndex.Detected) { // ++itemIndex; _source.Append("++").Append(inspector.VariableName).Append("Index;"); } _source.AppendLine("}"); } else { _source.Append("for (").Append(chunk.Code).AppendLine(") {"); Accept(chunk.Body); _source.Append("}"); } }
protected override void Visit(ForEachChunk chunk) { List <string> list = chunk.Code.ToString().Split(new char[] { ' ', '\r', '\n', '\t' }).ToList <string>(); int num = list.FindIndex(new Predicate <string>(GeneratedCodeVisitor.IsIn)); int num2 = list.FindIndex(new Predicate <string>(GeneratedCodeVisitor.IsInOrAs)); string name = (num2 < 1) ? null : list[num2 - 1]; if (name == null) { this.CodeIndent(chunk).Write("For Each ").WriteLine((string)chunk.Code).AddIndent(); this.CodeDefault(); this.PushScope(); base.Accept(chunk.Body); this._source.RemoveIndent().WriteLine("Next"); this.PopScope(); } else { DetectCodeExpressionVisitor visitor = new DetectCodeExpressionVisitor(base.OuterPartial); DetectCodeExpressionVisitor.Entry entry = visitor.Add(name + "Index"); DetectCodeExpressionVisitor.Entry entry2 = visitor.Add(name + "Count"); DetectCodeExpressionVisitor.Entry entry3 = visitor.Add(name + "IsFirst"); DetectCodeExpressionVisitor.Entry entry4 = visitor.Add(name + "IsLast"); visitor.Accept(chunk.Body); if (entry4.Detected) { entry.Detected = true; entry2.Detected = true; } this.AppendOpenScope(); if (entry.Detected) { this.DeclareVariable(name + "Index"); this._source.WriteLine("Dim {0}Index As Integer = 0", new object[] { name }); } if (entry3.Detected) { this.DeclareVariable(name + "IsFirst"); this._source.WriteLine("Dim {0}IsFirst As Boolean = True", new object[] { name }); } if (entry2.Detected) { this.DeclareVariable(name + "Count"); string str2 = string.Join(" ", list.ToArray(), num + 1, (list.Count - num) - 1); this._source.WriteLine("Dim {0}Count As Integer = Global.Spark.Compiler.CollectionUtility.Count({1})", new object[] { name, str2 }); } this.CodeIndent(chunk).Write("For Each ").WriteLine((string)chunk.Code).AddIndent(); this.CodeDefault(); this.PushScope(); this.DeclareVariable(name); this.CodeHidden(); if (entry4.Detected) { this.DeclareVariable(name + "IsLast"); this._source.WriteLine("Dim {0}IsLast As Boolean = ({0}Index = {0}Count - 1)", new object[] { name }); } this.CodeDefault(); base.Accept(chunk.Body); this.CodeHidden(); if (entry.Detected) { this._source.WriteLine("{0}Index = {0}Index + 1", new object[] { name }); } if (entry3.Detected) { this._source.WriteLine("{0}IsFirst = False", new object[] { name }); } this.CodeDefault(); this.PopScope(); this._source.RemoveIndent().WriteLine("Next"); this.AppendCloseScope(); } }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.FindIndex(IsIn); var inOrAsIndex = terms.FindIndex(IsInOrAs); var variableName = (inOrAsIndex < 1 ? null : terms[inOrAsIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Write("For Each ") .WriteLine(chunk.Code) .AddIndent(); CodeDefault(); PushScope(); Accept(chunk.Body); _source .RemoveIndent() .WriteLine("Next"); PopScope(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } AppendOpenScope(); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.WriteLine("Dim {0}Index As Integer = 0", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.WriteLine("Dim {0}IsFirst As Boolean = True", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); var collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.WriteLine("Dim {0}Count As Integer = Global.Spark.Compiler.CollectionUtility.Count({1})", variableName, collectionCode); } CodeIndent(chunk) .Write("For Each ") .WriteLine(chunk.Code) .AddIndent(); CodeDefault(); PushScope(); DeclareVariable(variableName); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.WriteLine("Dim {0}IsLast As Boolean = ({0}Index = {0}Count - 1)", variableName); } CodeDefault(); Accept(chunk.Body); CodeHidden(); if (autoIndex.Detected) _source.WriteLine("{0}Index = {0}Index + 1", variableName); if (autoIsFirst.Detected) _source.WriteLine("{0}IsFirst = False", variableName); CodeDefault(); PopScope(); _source.RemoveIndent().WriteLine("Next"); AppendCloseScope(); } }
protected override void Visit(ForEachChunk chunk) { var inspector = new ForEachInspector(chunk.Code); if (inspector.Recognized) { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(inspector.VariableName + "Index"); var autoCount = detect.Add(inspector.VariableName + "Count"); var autoIsFirst = detect.Add(inspector.VariableName + "IsFirst"); var autoIsLast = detect.Add(inspector.VariableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } string iteratorName = "__iter__" + inspector.VariableName; if (autoCount.Detected) { // var itemCount=0;for(var __iter__item in coll){if(typeof(__iter__item)!='function'){++itemCount;}} _source .Append("var ") .Append(inspector.VariableName) .Append("Count=0;for(var ") .Append(iteratorName) .Append(" in ") .Append(inspector.CollectionCode) .Append("){ if(typeof(") .Append(inspector.CollectionCode) .Append("[") .Append(iteratorName) .Append("])!='function') {") .Append("++") .Append(inspector.VariableName) .Append("Count;}}"); } if (autoIndex.Detected) { // var itemIndex=0; _source.Append("var ").Append(inspector.VariableName).Append("Index=0;"); } if (autoIsFirst.Detected) { // var itemIsFirst=true; _source.Append("var ").Append(inspector.VariableName).Append("IsFirst=true;"); } // for(var __iter__item in coll) { _source .Append("for (var ") .Append(iteratorName) .Append(" in ") .Append(inspector.CollectionCode) .Append(") {"); // var item=coll[__iter__item]; _source .Append("var ") .Append(inspector.VariableName) .Append("=") .Append(inspector.CollectionCode) .Append("[__iter__") .Append(inspector.VariableName) .Append("];"); // if(typeof(item)!='function') { _source.Append("if(typeof(") .Append(inspector.VariableName) .Append(")!='function') {"); if (autoIsLast.Detected) { // var itemIsLast=(itemIndex==itemCount-1); _source .Append("var ") .Append(inspector.VariableName) .Append("IsLast=(") .Append(inspector.VariableName) .Append("Index==") .Append(inspector.VariableName) .Append("Count-1);"); } _source.AppendLine(); Accept(chunk.Body); if (autoIsFirst.Detected) { // itemIsFirst=false; _source.Append(inspector.VariableName).Append("IsFirst=false;"); } if (autoIndex.Detected) { // ++itemIndex; _source.Append("++").Append(inspector.VariableName).Append("Index;"); } _source.AppendLine("}}"); } else { _source.Append("for (").Append(chunk.Code).AppendLine(") {"); Accept(chunk.Body); _source.Append("}"); } }
protected override void Visit(ForEachChunk chunk) { chunk.Code = Process(chunk, chunk.Code); base.Visit(chunk); }
protected override void Visit(ForEachChunk chunk) { var forEach = new ForEachInspector(chunk.Code); if (!forEach.Recognized) { _source.Write("for ").Write(chunk.Code).WriteLine(":"); _source.Indent++; _variables.PushScope(); Accept(chunk.Body); _source.WriteLine("pass"); _variables.PopScope(); _source.Indent--; return; } _variables.PushScope(); var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(forEach.VariableName + "Index"); var autoCount = detect.Add(forEach.VariableName + "Count"); var autoIsFirst = detect.Add(forEach.VariableName + "IsFirst"); var autoIsLast = detect.Add(forEach.VariableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } if (autoIndex.Detected) { _variables.Declare(forEach.VariableName + "Index"); _source.Write(forEach.VariableName).WriteLine("Index=0"); } if (autoIsFirst.Detected) { _variables.Declare(forEach.VariableName + "IsFirst"); _source.Write(forEach.VariableName).WriteLine("IsFirst=True"); } if (autoCount.Detected) { _variables.Declare(forEach.VariableName + "Count"); _source .Write(forEach.VariableName).Write("Count=len([0 for ") .Write(chunk.Code).WriteLine("])"); } _variables.Declare(forEach.VariableName); _source.Write("for ").Write(chunk.Code).WriteLine(":"); _source.Indent++; if (autoIsLast.Detected) { _variables.Declare(forEach.VariableName + "IsLast"); _source .Write(forEach.VariableName).Write("IsLast=(") .Write(forEach.VariableName).Write("Index==") .Write(forEach.VariableName).WriteLine("Count - 1)"); } Accept(chunk.Body); if (autoIndex.Detected) { _source .Write(forEach.VariableName).Write("Index=") .Write(forEach.VariableName).WriteLine("Index+1"); } if (autoIsFirst.Detected) { _source.Write(forEach.VariableName).WriteLine("IsFirst=False"); } _source.WriteLine("pass"); _source.Indent--; _variables.PopScope(); }
protected override void Visit(ForEachChunk chunk) { Accept(chunk.Body); }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.FindIndex(IsIn); var inOrAsIndex = terms.FindIndex(IsInOrAs); var variableName = (inOrAsIndex < 1 ? null : terms[inOrAsIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Write("For Each ") .WriteLine(chunk.Code) .AddIndent(); CodeDefault(); PushScope(); Accept(chunk.Body); _source .RemoveIndent() .WriteLine("Next"); PopScope(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } AppendOpenScope(); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.WriteLine("Dim {0}Index As Integer = 0", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.WriteLine("Dim {0}IsFirst As Boolean = True", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); var collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.WriteLine("Dim {0}Count As Integer = Global.Spark.Compiler.CollectionUtility.Count({1})", variableName, collectionCode); } CodeIndent(chunk) .Write("For Each ") .WriteLine(chunk.Code) .AddIndent(); CodeDefault(); PushScope(); DeclareVariable(variableName); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.WriteLine("Dim {0}IsLast As Boolean = ({0}Index = {0}Count - 1)", variableName); } CodeDefault(); Accept(chunk.Body); CodeHidden(); if (autoIndex.Detected) { _source.WriteLine("{0}Index = {0}Index + 1", variableName); } if (autoIsFirst.Detected) { _source.WriteLine("{0}IsFirst = False", variableName); } CodeDefault(); PopScope(); _source.RemoveIndent().WriteLine("Next"); AppendCloseScope(); } }
protected override void Visit(ForEachChunk chunk) { this.Examine(chunk.Code); base.Accept(chunk.Body); }
protected override void Visit(ForEachChunk chunk) { Examine(chunk.Code); Accept(chunk.Body); }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.IndexOf("in"); var variableName = (inIndex < 2 ? null : terms[inIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Write("foreach(") .WriteCode(chunk.Code) .WriteLine(")"); CodeDefault(); AppendOpenBrace(); Accept(chunk.Body); AppendCloseBrace(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } AppendOpenBrace(); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.WriteLine("int {0}Index = 0;", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.WriteLine("bool {0}IsFirst = true;", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); var collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.WriteLine("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});", variableName, collectionCode); } CodeIndent(chunk) .Write("foreach(") .WriteCode(chunk.Code) .WriteLine(")"); CodeDefault(); AppendOpenBrace(); DeclareVariable(variableName); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.WriteLine("bool {0}IsLast = ({0}Index == {0}Count - 1);", variableName); } CodeDefault(); Accept(chunk.Body); CodeHidden(); if (autoIndex.Detected) { _source.WriteLine("++{0}Index;", variableName); } if (autoIsFirst.Detected) { _source.WriteLine("{0}IsFirst = false;", variableName); } CodeDefault(); AppendCloseBrace(); AppendCloseBrace(); } }
protected override void Visit(ForEachChunk chunk) { chunk.Code = this.Process(chunk, chunk.Code); base.Visit(chunk); }
protected abstract void Visit(ForEachChunk chunk);
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.IndexOf("in"); var variableName = (inIndex < 2 ? null : terms[inIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Append("foreach(") .Append(chunk.Code) .AppendLine(")"); CodeDefault(); PushScope(); AppendIndent().AppendLine("{"); Indent += 4; Accept(chunk.Body); Indent -= 4; AppendIndent().AppendLine(string.Format("}} //foreach {0}", chunk.Code.ToString().Replace("\r", "").Replace("\n", " "))); PopScope(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } PushScope(); AppendIndent().AppendLine("{"); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.Append(' ', Indent + 4).AppendFormat("int {0}Index = 0;\r\n", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.Append(' ', Indent + 4).AppendFormat("bool {0}IsFirst = true;\r\n", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); string collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.Append(' ', Indent + 4).AppendFormat("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});\r\n", variableName, collectionCode); } Indent += 4; CodeIndent(chunk) .Append("foreach(") .Append(chunk.Code) .AppendLine(")"); CodeDefault(); PushScope(); DeclareVariable(variableName); _source.Append(' ', Indent).AppendLine("{"); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.Append(' ', Indent + 4).AppendFormat("bool {0}IsLast = ({0}Index == {0}Count - 1);\r\n", variableName); } CodeDefault(); Indent += 8; Accept(chunk.Body); Indent -= 8; CodeHidden(); if (autoIndex.Detected) _source.Append(' ', Indent + 4).AppendFormat("++{0}Index;\r\n", variableName); if (autoIsFirst.Detected) _source.Append(' ', Indent + 4).AppendFormat("{0}IsFirst = false;\r\n", variableName); CodeDefault(); _source.Append(' ').AppendLine("}"); PopScope(); AppendIndent().AppendFormat("}} //foreach {0}\r\n", chunk.Code.ToString().Replace("\r", "").Replace("\n", " ")); PopScope(); } }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.IndexOf("in"); var variableName = (inIndex < 2 ? null : terms[inIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Write("foreach(") .WriteCode(chunk.Code) .WriteLine(")"); CodeDefault(); AppendOpenBrace(); Accept(chunk.Body); AppendCloseBrace(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } AppendOpenBrace(); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.WriteLine("int {0}Index = 0;", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.WriteLine("bool {0}IsFirst = true;", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); var collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.WriteLine("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});", variableName, collectionCode); } CodeIndent(chunk) .Write("foreach(") .WriteCode(chunk.Code) .WriteLine(")"); CodeDefault(); AppendOpenBrace(); DeclareVariable(variableName); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.WriteLine("bool {0}IsLast = ({0}Index == {0}Count - 1);", variableName); } CodeDefault(); Accept(chunk.Body); CodeHidden(); if (autoIndex.Detected) _source.WriteLine("++{0}Index;", variableName); if (autoIsFirst.Detected) _source.WriteLine("{0}IsFirst = false;", variableName); CodeDefault(); AppendCloseBrace(); AppendCloseBrace(); } }
public unsafe static void ForChunk <T0, T1, T2, T3, T4>(this EntityChunkList chunkList, ForEachChunk <T0, T1, T2, T3, T4> view) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged { Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <T0> .Type, ComponentType <T1> .Type, ComponentType <T2> .Type, ComponentType <T3> .Type, ComponentType <T4> .Type }; chunkList.ForChunk(componentTypes, view); }
internal static void ForChunk <T0, T1, T2, T3, T4, T5>(this EntityChunkList chunkList, Span <ComponentType> componentTypes, ForEachChunk <T0, T1, T2, T3, T4, T5> view) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged where T5 : unmanaged { var c0 = chunkList.Specification.GetComponentIndex(componentTypes[0]); var c1 = chunkList.Specification.GetComponentIndex(componentTypes[1]); var c2 = chunkList.Specification.GetComponentIndex(componentTypes[2]); var c3 = chunkList.Specification.GetComponentIndex(componentTypes[3]); var c4 = chunkList.Specification.GetComponentIndex(componentTypes[4]); var c5 = chunkList.Specification.GetComponentIndex(componentTypes[5]); for (var k = 0; k < chunkList.ChunkCount; k++) { var chunk = chunkList[k]; var length = chunk.Count; var entities = chunk.Entities; var t0 = chunk.GetComponentData <T0>(c0, componentTypes[0]); var t1 = chunk.GetComponentData <T1>(c1, componentTypes[1]); var t2 = chunk.GetComponentData <T2>(c2, componentTypes[2]); var t3 = chunk.GetComponentData <T3>(c3, componentTypes[3]); var t4 = chunk.GetComponentData <T4>(c4, componentTypes[4]); var t5 = chunk.GetComponentData <T5>(c5, componentTypes[5]); view(length, entities, t0, t1, t2, t3, t4, t5); } }