Changeset 221

Show
Ignore:
Timestamp:
10/23/08 06:58:08 (3 months ago)
Author:
louis.dejardin
Message:

Several more chunk types re-implemented under CodeDomViewCompiler?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/codedom/src/Spark.Tests/CodeDomViewCompilerTester.cs

    r220 r221  
    88using Spark.Compiler.CodeDom; 
    99using Spark.Parser; 
     10using Spark.Tests.Stubs; 
    1011 
    1112namespace Spark.Tests 
     
    1920        public void Init() 
    2021        { 
    21             _compiler = new CodeDomViewCompiler("vb") { BaseClass = typeof(Stubs.StubSparkView).FullName, Debug = true }; 
     22            _compiler = new VbCodeDomViewCompiler { BaseClass = typeof(Stubs.StubSparkView).FullName, Debug = false }; 
    2223        } 
    2324 
     
    2728        } 
    2829 
    29         private StringWriter ExecuteView() 
    30         { 
    31             var view = (ISparkView)Activator.CreateInstance(_compiler.CompiledType); 
     30        private string ExecuteView() 
     31        { 
     32            return ExecuteView(new StubViewData()); 
     33        } 
     34 
     35        private string ExecuteView(StubViewData viewData) 
     36        { 
     37            var view = (Stubs.StubSparkView)Activator.CreateInstance(_compiler.CompiledType); 
     38            view.ViewData = viewData; 
    3239            var contents = new StringWriter(); 
    3340            view.RenderView(contents); 
    34             return contents
     41            return contents.ToString()
    3542        } 
    3643 
     
    123130            _compiler.CompileView(chunks, chunks); 
    124131            var contents = ExecuteView(); 
    125             Assert.AreEqual("Hello World", contents.ToString()); 
     132            Assert.AreEqual("Hello World", contents); 
    126133        } 
    127134 
     
    135142        } 
    136143 
     144 
    137145        [Test] 
    138146        public void SettingLocalVariable() 
    139147        { 
    140148            var chunks = Chunks( 
    141                 new LocalVariableChunk { Type = "object", Name = "x", Value = "4" }, 
     149                new LocalVariableChunk { Name = "x", Value = "4" }, 
    142150                new SendExpressionChunk { Code = "x" }, 
    143151                new AssignVariableChunk { Name = "x", Value = "2" }, 
     
    145153            _compiler.CompileView(chunks, chunks); 
    146154            var contents = ExecuteView(); 
    147             Assert.AreEqual("42", contents.ToString()); 
    148         } 
    149  
     155            Assert.AreEqual("42", contents); 
     156        } 
     157 
     158 
     159        [Test] 
     160        public void SettingGlobalVariable() 
     161        { 
     162            var chunks = Chunks( 
     163                new GlobalVariableChunk { Type = "int", Name = "x", Value = "4" }, 
     164                new SendExpressionChunk { Code = "x" }, 
     165                new AssignVariableChunk { Name = "x", Value = "2" }, 
     166                new SendExpressionChunk { Code = "x" }); 
     167            _compiler.CompileView(chunks, chunks); 
     168            var contents = ExecuteView(); 
     169            Assert.AreEqual("42", contents); 
     170        } 
     171 
     172        [Test] 
     173        public void UsingViewData() 
     174        { 
     175            var chunks = Chunks( 
     176                new ViewDataChunk { Type = "int", Name = "HelloNumber", Key = "hello" }, 
     177                new SendExpressionChunk { Code = "HelloNumber" }); 
     178            _compiler.CompileView(chunks, chunks); 
     179            var contents = ExecuteView(new StubViewData { { "hello", 42 } }); 
     180            Assert.AreEqual("42", contents); 
     181        } 
     182 
     183        [Test] 
     184        public void UsingViewDataDefault() 
     185        { 
     186            var chunks = Chunks( 
     187                new ViewDataChunk { Type = "int", Name = "HelloNumber", Key = "hello", Default = "55" }, 
     188                new SendExpressionChunk { Code = "HelloNumber" }); 
     189 
     190            _compiler.CompileView(chunks, chunks); 
     191 
     192            var contents = ExecuteView(new StubViewData { { "hello", 42 } }); 
     193            Assert.AreEqual("42", contents); 
     194 
     195            var contents2 = ExecuteView(); 
     196            Assert.AreEqual("55", contents2); 
     197        } 
     198 
     199        [Test] 
     200        public void InlineCodeStatements() 
     201        { 
     202            //TODO: null protection. silent nulls. 
     203 
     204            var chunks = Chunks( 
     205                new CodeStatementChunk { Code = "Dim x As Object" }, 
     206                new CodeStatementChunk { Code = "x = 20" }, 
     207                new SendExpressionChunk { Code = "x + 22" }); 
     208            _compiler.CompileView(chunks, chunks); 
     209            var contents = ExecuteView(); 
     210            Assert.AreEqual(contents, "42"); 
     211        } 
     212 
     213        [Test] 
     214        public void ScopeTest() 
     215        { 
     216            var scope1 = new ScopeChunk(); 
     217            scope1.Body.Add(new LocalVariableChunk { Name = "x", Value = "4" }); 
     218            scope1.Body.Add(new SendExpressionChunk { Code = "x" }); 
     219            var scope2 = new ScopeChunk(); 
     220            scope2.Body.Add(new LocalVariableChunk { Name = "x", Value = "2" }); 
     221            scope2.Body.Add(new SendExpressionChunk { Code = "x" }); 
     222 
     223            var chunks = Chunks(scope1, scope2); 
     224            _compiler.CompileView(chunks, chunks); 
     225            var contents = ExecuteView(); 
     226            Assert.AreEqual(contents, "42"); 
     227        } 
     228 
     229        [Test] 
     230        public void ForEachLoopOverArray() 
     231        { 
     232            var loop = new ForEachChunk { Code = "system.int32 number in numbers" }; 
     233            loop.Body.Add(new SendExpressionChunk { Code = "number" }); 
     234 
     235            var chunks = Chunks( 
     236                new ViewDataChunk { Name = "numbers", Type = "Integer()" }, 
     237                loop); 
     238            _compiler.CompileView(chunks, chunks); 
     239            var contents = ExecuteView(new StubViewData { { "numbers", new[] { 1, 2, 3, 4, 5 } } }); 
     240            Assert.AreEqual("12345", contents); 
     241        } 
    150242    } 
    151243} 
  • branches/codedom/src/Spark.Tests/Spark.Tests.csproj

    r218 r221  
    5858    <Compile Include="Compiler\ExpressionBuilderTester.cs" /> 
    5959    <Compile Include="Compiler\ForEachInspectorTester.cs" /> 
     60    <Compile Include="Compiler\VariableTrackerTester.cs" /> 
    6061    <Compile Include="DefaultResourcePathManagerTester.cs" /> 
    6162    <Compile Include="FileSystem\CombinedViewFolderTester.cs" /> 
  • branches/codedom/src/Spark/Compiler/BatchCompiler.cs

    r218 r221  
    5252                    continue; 
    5353                } 
    54                 compilerParameters.ReferencedAssemblies.Add(location); 
     54                if (!string.IsNullOrEmpty(location)) 
     55                    compilerParameters.ReferencedAssemblies.Add(location); 
    5556            } 
    5657 
     
    121122                    using (var reader = new StringReader(sourceCodeItem)) 
    122123                    { 
    123                         for (int lineNumber = 1;; ++lineNumber) 
     124                        for (int lineNumber = 1; ; ++lineNumber) 
    124125                        { 
    125126                            var line = reader.ReadLine(); 
  • branches/codedom/src/Spark/Compiler/CSharp/ChunkVisitors/GeneratedCodeVisitor.cs

    r208 r221  
    1313// limitations under the License. 
    1414//  
    15 using System.Collections; 
    1615using System.Collections.Generic; 
    17 using System.IO; 
    1816using System.Linq; 
    1917using System.Text; 
     
    2523    { 
    2624        private readonly StringBuilder _source; 
     25        private readonly VariableTracker _variables; 
    2726 
    2827        public GeneratedCodeVisitor(StringBuilder output, Dictionary<string, object> globalSymbols) 
    2928        { 
    3029            _source = output; 
    31  
    32             _scope = new Scope(new Scope(null) {Variables = globalSymbols}); 
     30            _variables = new VariableTracker(globalSymbols);             
    3331        } 
    3432 
     
    5957 
    6058 
    61         class Scope 
    62         { 
    63             public Scope(Scope prior) 
    64             { 
    65                 Variables = new Dictionary<string, object>(); 
    66                 Prior = prior; 
    67             } 
    68             public Dictionary<string,object> Variables { get; set; } 
    69             public Scope Prior { get; set; } 
    70         } 
    71  
    72         private Scope _scope; 
    73  
    74         void PushScope() 
    75         { 
    76             _scope = new Scope(_scope); 
    77         } 
    78         void PopScope() 
    79         { 
    80             _scope = _scope.Prior; 
    81         } 
    82         void DeclareVariable(string name) 
    83         { 
    84             _scope.Variables.Add(name, null); 
    85         } 
    86         bool IsVariableDeclared(string name) 
    87         { 
    88             var scan = _scope; 
    89             while(scan != null) 
    90             { 
    91                 if (scan.Variables.ContainsKey(name)) 
    92                     return true; 
    93                 scan = scan.Prior; 
    94             } 
    95             return false; 
    96         } 
    9759 
    9860 
     
    143105        protected override void Visit(LocalVariableChunk chunk) 
    144106        { 
    145             DeclareVariable(chunk.Name); 
     107            _variables.Declare(chunk.Name); 
    146108 
    147109            CodeIndent(chunk).Append(chunk.Type).Append(' ').Append(chunk.Name); 
     
    156118        protected override void Visit(DefaultVariableChunk chunk) 
    157119        { 
    158             if (IsVariableDeclared(chunk.Name)) 
     120            if (_variables.IsDeclared(chunk.Name)) 
    159121                return; 
    160122 
    161             DeclareVariable(chunk.Name); 
     123            _variables.Declare(chunk.Name); 
    162124            CodeIndent(chunk).Append(chunk.Type).Append(' ').Append(chunk.Name); 
    163125            if (!string.IsNullOrEmpty(chunk.Value)) 
     
    179141                CodeIndent(chunk).AppendLine(string.Format("foreach({0})", chunk.Code)); 
    180142                CodeDefault(); 
    181                 PushScope(); 
     143                _variables.PushScope(); 
    182144                AppendIndent().AppendLine("{"); 
    183145                Indent += 4; 
     
    185147                Indent -= 4; 
    186148                AppendIndent().AppendLine(string.Format("}} //foreach {0}", chunk.Code.Replace("\r", "").Replace("\n", " "))); 
    187                 PopScope(); 
     149                _variables.PopScope(); 
    188150            } 
    189151            else 
     
    202164                } 
    203165 
    204                 PushScope(); 
     166                _variables.PushScope(); 
    205167                AppendIndent().AppendLine("{"); 
    206168                if (autoIndex.Detected) 
    207169                { 
    208                     DeclareVariable(variableName + "Index"); 
     170                    _variables.Declare(variableName + "Index"); 
    209171                    _source.Append(' ', Indent + 4).AppendFormat("int {0}Index = 0;\r\n", variableName); 
    210172                } 
    211173                if (autoIsFirst.Detected) 
    212174                { 
    213                     DeclareVariable(variableName + "IsFirst"); 
     175                    _variables.Declare(variableName + "IsFirst"); 
    214176                    _source.Append(' ', Indent + 4).AppendFormat("bool {0}IsFirst = true;\r\n", variableName); 
    215177                } 
    216178                if (autoCount.Detected) 
    217179                { 
    218                     DeclareVariable(variableName + "Count"); 
     180                    _variables.Declare(variableName + "Count"); 
    219181                    string collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); 
    220182                    _source.Append(' ', Indent + 4).AppendFormat("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});\r\n", variableName, collectionCode); 
     
    225187                CodeDefault(); 
    226188 
    227                 PushScope(); 
    228                 DeclareVariable(variableName); 
     189                _variables.PushScope(); 
     190                _variables.Declare(variableName); 
    229191                _source.Append(' ', Indent).AppendLine("{"); 
    230192 
     
    232194                if (autoIsLast.Detected) 
    233195                { 
    234                     DeclareVariable(variableName + "IsLast"); 
     196                    _variables.Declare(variableName + "IsLast"); 
    235197                    _source.Append(' ', Indent + 4).AppendFormat("bool {0}IsLast = ({0}Index == {0}Count - 1);\r\n", 
    236198                                                                 variableName); 
     
    250212 
    251213                _source.Append(' ').AppendLine("}"); 
    252                 PopScope(); 
     214                _variables.PopScope(); 
    253215 
    254216                AppendIndent().AppendFormat("}} //foreach {0}\r\n", chunk.Code.Replace("\r", "").Replace("\n", " ")); 
    255                 PopScope(); 
     217                _variables.PopScope(); 
    256218            } 
    257219        } 
     
    259221        protected override void Visit(ScopeChunk chunk) 
    260222        { 
    261             PushScope(); 
     223            _variables.PushScope(); 
    262224            CodeIndent(chunk).AppendLine("{"); 
    263225            CodeDefault(); 
     
    266228            Indent -= 4; 
    267229            AppendIndent().AppendLine("}"); 
    268             PopScope(); 
     230            _variables.PopScope(); 
    269231        } 
    270232 
     
    283245        { 
    284246            CodeIndent(chunk).AppendLine(string.Format("using(OutputScope(\"{0}\"))", chunk.Name)); 
    285             PushScope(); 
     247            _variables.PushScope(); 
    286248            AppendIndent().AppendLine("{"); 
    287249            Indent += 4; 
     
    289251            Indent -= 4; 
    290252            AppendIndent().AppendLine("}"); 
    291             PopScope(); 
     253            _variables.PopScope(); 
    292254        } 
    293255 
    294256        protected override void Visit(UseImportChunk chunk) 
    295257        { 
    296              
     258 
    297259        } 
    298260 
     
    302264            CodeDefault(); 
    303265 
    304             PushScope(); 
     266            _variables.PushScope(); 
    305267            AppendIndent().AppendLine("{"); 
    306268            Indent += 4; 
     
    325287            Indent -= 4; 
    326288            AppendIndent().AppendLine("}"); 
    327             PopScope(); 
     289            _variables.PopScope(); 
    328290 
    329291            CodeDefault(); 
     
    334296            CodeIndent(chunk).AppendLine(string.Format("if (Content.ContainsKey(\"{0}\"))", chunk.Name)); 
    335297            CodeHidden(); 
    336             PushScope(); 
     298            _variables.PushScope(); 
    337299            AppendIndent().AppendLine("{"); 
    338300            _source.Append(' ', Indent + 4).AppendLine(string.Format("global::Spark.Spool.TextWriterExtensions.WriteTo(Content[\"{0}\"], Output);", chunk.Name)); 
    339301            AppendIndent().AppendLine("}"); 
    340             PopScope(); 
     302            _variables.PopScope(); 
    341303 
    342304            if (chunk.Default.Count != 0) 
    343305            { 
    344306                AppendIndent().AppendLine("else"); 
    345                 PushScope(); 
     307                _variables.PushScope(); 
    346308                AppendIndent().AppendLine("{"); 
    347309                Indent += 4; 
     
    349311                Indent -= 4; 
    350312                AppendIndent().AppendLine("}"); 
    351                 PopScope(); 
     313                _variables.PopScope(); 
    352314            } 
    353315            CodeDefault(); 
     
    408370                        CodeIndent(chunk).AppendLine(string.Format("if ({0})", chunk.Condition)); 
    409371                        CodeDefault(); 
    410                         PushScope(); 
     372                        _variables.PushScope(); 
    411373                        AppendIndent().AppendLine("{"); 
    412374                        Indent += 4; 
     
    415377                        AppendIndent().AppendLine(string.Format("}} // if ({0})", 
    416378                                                                chunk.Condition.Replace("\r", "").Replace("\n", " "))); 
    417                         PopScope(); 
     379                        _variables.PopScope(); 
    418380                    } 
    419381                    break; 
     
    422384                        CodeIndent(chunk).AppendLine(string.Format("else if ({0})", chunk.Condition)); 
    423385                        CodeDefault(); 
    424                         PushScope(); 
     386                        _variables.PushScope(); 
    425387                        AppendIndent().AppendLine("{"); 
    426388                        Indent += 4; 
     
    429391                        AppendIndent().AppendLine(string.Format("}} // else if ({0})", 
    430392                                                                chunk.Condition.Replace("\r", "").Replace("\n", " "))); 
    431                         PopScope(); 
     393                        _variables.PopScope(); 
    432394                    } 
    433395                    break; 
     
    435397                    { 
    436398                        AppendIndent().AppendLine("else"); 
    437                         PushScope(); 
     399                        _variables.PushScope(); 
    438400                        CodeIndent(chunk).AppendLine("{"); 
    439401                        CodeDefault(); 
     
    442404                        Indent -= 4; 
    443405                        AppendIndent().AppendLine("}"); 
    444                         PopScope(); 
     406                        _variables.PopScope(); 
    445407                    } 
    446408                    break; 
     
    448410                    { 
    449411                        CodeIndent(chunk).Append("if (Once(").Append(chunk.Condition).AppendLine("))"); 
    450                         PushScope(); 
     412                        _variables.PushScope(); 
    451413                        AppendIndent().AppendLine("{"); 
    452414                        CodeDefault(); 
     
    455417                        Indent -= 4; 
    456418                        AppendIndent().AppendLine("}"); 
    457                         PopScope(); 
     419                        _variables.PopScope(); 
    458420                    } 
    459421                    break; 
  • branches/codedom/src/Spark/Compiler/CodeDom/ChunkVisitors/CodeDomUtility.cs

    r220 r221  
    3232        public static CodeTypeReference TypeReference(string type) 
    3333        { 
     34            if (type == null) 
     35                return null; 
     36 
    3437            if (_primativeTypes.ContainsKey(type)) 
    3538                return _primativeTypes[type]; 
     39 
    3640            return new CodeTypeReference(type); 
    3741        } 
  • branches/codedom/src/Spark/Compiler/CodeDom/ChunkVisitors/GeneratedCodeVisitor.cs

    r220 r221  
    11using System; 
    22using System.CodeDom; 
     3using System.CodeDom.Compiler; 
    34using System.Collections.Generic; 
     5using System.IO; 
    46using System.Linq; 
    57using System.Text; 
    68using Spark.Compiler.ChunkVisitors; 
     9using Spark.Compiler.CSharp.ChunkVisitors; 
    710using Spark.Spool; 
    811 
     
    1114    public class GeneratedCodeVisitor : ChunkVisitor 
    1215    { 
     16        private readonly CodeDomViewCompiler _viewCompiler; 
    1317        private CodeStatementCollection _statements; 
    14  
    15         public GeneratedCodeVisitor() 
    16         { 
     18        private VariableTracker _variables; 
     19 
     20        public GeneratedCodeVisitor(CodeDomViewCompiler viewCompiler, Dictionary<string, object> globalSymbols) 
     21        { 
     22            _viewCompiler = viewCompiler; 
    1723            _statements = new CodeStatementCollection(); 
     24            _variables = new VariableTracker(globalSymbols); 
    1825        } 
    1926 
     
    4653        } 
    4754 
     55        protected override void Visit(CodeStatementChunk chunk) 
     56        { 
     57            Statements.Add(new CodeSnippetStatement(chunk.Code)); 
     58        } 
     59 
    4860        protected override void Visit(AssignVariableChunk chunk) 
    4961        { 
     
    5668        protected override void Visit(LocalVariableChunk chunk) 
    5769        { 
    58             //DeclareVariable(chunk.Name); 
    59  
    60             //CodeIndent(chunk).Append(chunk.Type).Append(' ').Append(chunk.Name); 
    61             //if (!string.IsNullOrEmpty(chunk.Value)) 
    62             //{ 
    63             //    _source.Append(" = ").Append(chunk.Value); 
    64             //} 
    65             //_source.AppendLine(";"); 
    66             //CodeDefault(); 
    67  
    68             var declarationStatement = new CodeVariableDeclarationStatement(CodeDomUtility.TypeReference(chunk.Type), chunk.Name); 
    69             if (!string.IsNullOrEmpty(chunk.Value)) 
    70             { 
    71                 declarationStatement.InitExpression = new CodeSnippetExpression(chunk.Value); 
    72             } 
    73             Statements.Add(declarationStatement); 
     70            _variables.Declare(chunk.Name); 
     71 
     72            if (chunk.Type == "var") 
     73            { 
     74                // not normally supported 
     75                Statements.Add(_viewCompiler.LanguageSpecific_ImplicitLocalVariable(chunk.Name, chunk.Value)); 
     76            } 
     77            else 
     78            { 
     79                // type name = value; 
     80 
     81                var declarationStatement = new CodeVariableDeclarationStatement(CodeDomUtility.TypeReference(chunk.Type), chunk.Name); 
     82                if (!string.IsNullOrEmpty(chunk.Value)) 
     83                { 
     84                    declarationStatement.InitExpression = new CodeSnippetExpression(chunk.Value); 
     85                } 
     86                Statements.Add(declarationStatement); 
     87            } 
    7488        } 
    7589 
     
    8397            try 
    8498            { 
     99                // if (Content.ContainsKey(name)) 
     100                //   TextWriterExtensions.WriteTo(Content[name], Output); 
     101                // else 
     102                //   default 
     103 
    85104                var containsKey = new CodeMethodInvokeExpression( 
    86105                    new CodePropertyReferenceExpression( 
     
    92111                var writeTo = new CodeExpressionStatement( 
    93112                    new CodeMethodInvokeExpression( 
    94                         new CodeTypeReferenceExpression(typeof (TextWriterExtensions)), 
     113                        new CodeTypeReferenceExpression(typeof(TextWriterExtensions)), 
    95114                        "WriteTo", 
    96115                        new CodeArrayIndexerExpression( 
     
    117136                _statements = back; 
    118137            } 
    119  
    120             //if (chunk.Default.Count != 0) 
    121             //{ 
    122             //    AppendIndent().AppendLine("else"); 
    123             //    PushScope(); 
    124             //    AppendIndent().AppendLine("{"); 
    125             //    Indent += 4; 
    126             //    Accept(chunk.Default); 
    127             //    Indent -= 4; 
    128             //    AppendIndent().AppendLine("}"); 
    129             //    PopScope(); 
    130             //} 
    131             //CodeDefault(); 
    132             //base.Visit(chunk); 
     138        } 
     139 
     140        protected override void Visit(ScopeChunk chunk) 
     141        { 
     142            var prior = _statements; 
     143            try 
     144            { 
     145                // using an if(true) because codedom doesn't have 
     146                // a universal block-with-nothing-in-it concept. 
     147 
     148                var block = new CodeConditionStatement(new CodePrimitiveExpression(true)); 
     149                _statements.Add(block); 
     150                _statements = block.TrueStatements; 
     151 
     152                _variables.PushScope(); 
     153                Accept(chunk.Body); 
     154                _variables.PopScope(); 
     155            } 
     156            finally 
     157            { 
     158                _statements = prior; 
     159            } 
     160        } 
     161 
     162        protected override void Visit(ForEachChunk chunk) 
     163        { 
     164            var terms = chunk.Code.Split(' ', '\r', '\n', '\t').ToList(); 
     165            var inIndex = terms.IndexOf("in"); 
     166            string variableName = (inIndex < 2 ? null : terms[inIndex - 1]); 
     167 
     168            if (variableName == null) 
     169            { 
     170                throw new CompilerException(string.Format("Unrecognized loop syntax: {0}", chunk.Code)); 
     171            } 
     172 
     173            var prior = _statements; 
     174            try 
     175            { 
     176                var detect = new DetectCodeExpressionVisitor(OuterPartial); 
     177                var autoIndex = detect.Add(variableName + "Index"); 
     178                var autoCount = detect.Add(variableName + "Count"); 
     179                var autoIsFirst = detect.Add(variableName + "IsFirst"); 
     180                var autoIsLast = detect.Add(variableName + "IsLast"); 
     181                detect.Accept(chunk.Body); 
     182 
     183                if (autoIsLast.Detected) 
     184                { 
     185                    autoIndex.Detected = true; 
     186                    autoCount.Detected = true; 
     187                } 
     188 
     189                string typeCode = string.Join(" ", terms.ToArray(), 0, inIndex - 1); 
     190                string collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); 
     191 
     192                _variables.PushScope(); 
     193                var scopeAutoVariables = new CodeConditionStatement(new CodePrimitiveExpression(true)); 
     194                _statements.Add(scopeAutoVariables); 
     195                _statements = scopeAutoVariables.TrueStatements; 
     196 
     197                if (autoIndex.Detected) 
     198                { 
     199                    _variables.Declare(variableName + "Index"); 
     200                    _statements.Add( 
     201                        new CodeVariableDeclarationStatement( 
     202                            typeof (int),  
     203                            variableName + "Index", 
     204                            new CodePrimitiveExpression(0))); 
     205                } 
     206                if (autoIsFirst.Detected) 
     207                { 
     208                    _variables.Declare(variableName + "IsFirst"); 
     209                    _statements.Add( 
     210                        new CodeVariableDeclarationStatement( 
     211                            typeof(bool), 
     212                            variableName + "IsFirst", 
     213                            new CodePrimitiveExpression(true))); 
     214                } 
     215                if (autoCount.Detected) 
     216                { 
     217                    //int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1}); 
     218                    _variables.Declare(variableName + "Count"); 
     219                    _statements.Add( 
     220                        new CodeVariableDeclarationStatement( 
     221                            typeof(int), 
     222                            variableName + "Count", 
     223                            new CodeMethodInvokeExpression( 
     224                                new CodeTypeReferenceExpression(typeof(CollectionUtility)), 
     225                                "Count", 
     226                                new CodeSnippetExpression(collectionCode)))); 
     227                } 
     228 
     229                _variables.PushScope(); 
     230                _variables.Declare(variableName); 
     231                _statements.Add(_viewCompiler.LanguageSpecific_ForEachBegin(typeCode    , variableName, collectionCode)); 
     232 
     233                if (autoIsLast.Detected) 
     234                { 
     235                    //bool {0}IsLast = ({0}Index == {0}Count - 1); 
     236                    _variables.Declare(variableName + "IsLast"); 
     237                    _statements.Add( 
     238                        new CodeVariableDeclarationStatement( 
     239                            typeof (bool), 
     240                            variableName + "IsLast", 
     241                            new CodeBinaryOperatorExpression( 
     242                                new CodeVariableReferenceExpression(variableName + "Index"), 
     243                                CodeBinaryOperatorType.ValueEquality, 
     244                                new CodeBinaryOperatorExpression( 
     245                                    new CodeVariableReferenceExpression(variableName + "Count"), 
     246                                    CodeBinaryOperatorType.Subtract, 
     247                                    new CodePrimitiveExpression(1))))); 
     248                } 
     249 
     250                Accept(chunk.Body); 
     251 
     252 
     253 
     254                if (autoIndex.Detected) 
     255                { 
     256                    //{0}Index={0}Index+1 
     257                    _statements.Add( 
     258                        new CodeAssignStatement( 
     259                            new CodeVariableReferenceExpression(variableName + "Index"), 
     260                            new CodeBinaryOperatorExpression( 
     261                                new CodeVariableReferenceExpression(variableName + "Index"), 
     262                                CodeBinaryOperatorType.Add, 
     263                                new CodePrimitiveExpression(1)))); 
     264                } 
     265                 
     266                if (autoIsFirst.Detected) 
     267                { 
     268                    //{0}IsFirst = false 
     269                    _statements.Add( 
     270                        new CodeAssignStatement( 
     271                            new CodeVariableReferenceExpression(variableName + "IsFirst"), 
     272                            new CodePrimitiveExpression(false))); 
     273                } 
     274 
     275 
     276                _statements.Add(_viewCompiler.LanguageSpecific_ForEachEnd()); 
     277                _variables.PopScope(); 
     278                _variables.PopScope(); 
     279            } 
     280            finally 
     281            { 
     282                _statements = prior; 
     283            } 
     284        } 
     285 
     286        public RenderPartialChunk OuterPartial { get; set; } 
     287        protected override void Visit(RenderPartialChunk chunk) 
     288        { 
     289            var priorOuterPartial = OuterPartial; 
     290            OuterPartial = chunk; 
     291            Accept(chunk.FileContext.Contents); 
     292            OuterPartial = priorOuterPartial; 
    133293        } 
    134294    } 
  • branches/codedom/src/Spark/Compiler/CodeDom/ChunkVisitors/GlobalMembersVisitor.cs

    r220 r221  
    2424    public class GlobalMembersVisitor : ChunkVisitor 
    2525    { 
     26        private readonly CodeDomViewCompiler _viewCompiler; 
    2627        private readonly CodeTypeDeclaration _viewType; 
    2728        private readonly Dictionary<string, object> _globalSymbols; 
     
    2930        readonly Dictionary<string, GlobalVariableChunk> _globalAdded = new Dictionary<string, GlobalVariableChunk>(); 
    3031 
    31         public GlobalMembersVisitor(CodeTypeDeclaration viewType, Dictionary<string, object> globalSymbols) 
    32         { 
     32        public GlobalMembersVisitor(CodeDomViewCompiler viewCompiler, CodeTypeDeclaration viewType, Dictionary<string, object> globalSymbols) 
     33        { 
     34            _viewCompiler = viewCompiler; 
    3335            _viewType = viewType; 
    3436            _globalSymbols = globalSymbols; 
     
    5658            if (typeParts.Contains("const") || typeParts.Contains("readonly")) 
    5759            { 
    58                 //_source.AppendFormat("\r\n    {0} {1} = {2};", 
    59                 //                     type, chunk.Name, chunk.Value); 
     60                //type name = value; 
    6061                _viewType.Members.Add(new CodeMemberField(CodeDomUtility.TypeReference(type), chunk.Name) { InitExpression = new CodeSnippetExpression(chunk.Value) }); 
    6162            } 
    6263            else 
    6364            { 
    64                 //_source.AppendFormat( 
    65                 //    "\r\n    {0} _{1} = {2};\r\n    public {0} {1} {{ get {{return _{1};}} set {{_{1} = value;}} }}", 
    66                 //    type, chunk.Name, chunk.Value); 
     65                //type _name = value; 
     66                //public type name {get {return _name;} set {_name = value;} } 
    6767                _viewType.Members.Add(new CodeMemberField(CodeDomUtility.TypeReference(type), "_" + chunk.Name) { InitExpression = new CodeSnippetExpression(chunk.Value) }); 
    6868                var memberProperty = new CodeMemberProperty 
     
    107107        protected override void Visit(ViewDataChunk chunk) 
    108108        { 
    109             //var key = chunk.Key; 
    110             //var name = chunk.Name; 
    111             //var type = chunk.Type ?? "object"; 
    112  
    113             //if (!_globalSymbols.ContainsKey(chunk.Name)) 
    114             //    _globalSymbols.Add(chunk.Name, null); 
    115  
    116             //if (_viewDataAdded.ContainsKey(name)) 
    117             //{ 
    118             //    if (_viewDataAdded[name] != key + ":" + type) 
    119             //    { 
    120             //        throw new CompilerException( 
    121             //            string.Format("The view data named {0} cannot be declared with different types '{1}' and '{2}'", 
    122             //                          name, type, _viewDataAdded[name])); 
    123             //    } 
    124             //    return; 
    125             //} 
    126  
    127             //_viewDataAdded.Add(name, key + ":" + type); 
    128             //AppendIndent().Append(type).Append(" ").AppendLine(name); 
    129             //if (string.IsNullOrEmpty(chunk.Default)) 
    130             //{ 
    131             //    CodeIndent(chunk) 
    132             //        .Append("{get {return (") 
    133             //        .Append(type) 
    134             //        .Append(")ViewData.Eval(\"") 
    135             //        .Append(key) 
    136             //        .AppendLine("\");}}"); 
    137             //} 
    138             //else 
    139             //{ 
    140             //    CodeIndent(chunk) 
    141             //        .Append("{get {return (") 
    142             //        .Append(type) 
    143             //        .Append(")(ViewData.Eval(\"") 
    144             //        .Append(key) 
    145             //        .Append("\")??") 
    146             //        .Append(chunk.Default) 
    147             //        .AppendLine(");}}"); 
    148             //} 
    149             //CodeDefault(); 
     109            var key = chunk.Key ?? chunk.Name; 
     110            var name = chunk.Name; 
     111            var type = chunk.Type ?? "object"; 
     112 
     113            if (!_globalSymbols.ContainsKey(chunk.Name)) 
     114                _globalSymbols.Add(chunk.Name, null); 
     115 
     116            if (_viewDataAdded.ContainsKey(name)) 
     117            { 
     118                if (_viewDataAdded[name] != key + ":" + type) 
     119                { 
     120                    throw new CompilerException( 
     121                        string.Format("The view data named {0} cannot be declared with different types '{1}' and '{2}'", 
     122                                      name, type, _viewDataAdded[name])); 
     123                } 
     124                return;