Commits

Jesse McGrew committed 5327a8d

Added scope display to RellorPad

  • Participants
  • Parent commits 9855296

Comments (0)

Files changed (2)

Rellor.Core/Scope.cs

         string Name { get; }
         Symbol Resolve(string name);
         void Define(string name, Symbol symbol);
+        IEnumerable<KeyValuePair<string, Symbol>> AllSymbols();
     }
 
     [ContractClassFor(typeof(IScope))]
             Contract.Requires(name != null);
             Contract.Requires(symbol != null);
         }
+
+        IEnumerable<KeyValuePair<string, Symbol>> IScope.AllSymbols()
+        {
+            Contract.Ensures(Contract.Result<IEnumerable<KeyValuePair<string, Symbol>>>() != null);
+            return default(IEnumerable<KeyValuePair<string, Symbol>>);
+        }
     }
 
     abstract class ScopeBase : IScope
 
             dict.Add(name, symbol);
         }
+
+        public virtual IEnumerable<KeyValuePair<string, Symbol>> AllSymbols()
+        {
+            return dict;
+        }
     }
 
     [ContractClass(typeof(IGlobalScopeContract))]
         public abstract string Name { get; }
         public abstract Symbol Resolve(string name);
         public abstract void Define(string name, Symbol symbol);
+        public abstract IEnumerable<KeyValuePair<string, Symbol>> AllSymbols();
         #endregion
     }
 

RellorPad/MainForm.cs

             var analyzer = new Analyzer();
             analyzer.LogSink = this;
             analyzer.Load(MakeCharStream());
-            DumpTree(analyzer.SyntaxTree, analyzer.TokenNames);
+            DumpTree(analyzer);
+            DumpScope(analyzer);
         }
 
-        private void DumpTree(ITree tree, string[] tokenNames)
+        private void DumpTree(Analyzer analyzer)
         {
             tvAST.BeginUpdate();
             try
             {
                 tvAST.Nodes.Clear();
 
-                DumpTree(tree, tokenNames, tvAST.Nodes);
+                DumpTree(analyzer.SyntaxTree, analyzer.TokenNames, tvAST.Nodes);
 
                 tvAST.ExpandAll();
             }
             }
         }
 
-        private void DumpTree(ITree tree, string[] tokenNames, TreeNodeCollection parentCollection)
+        private static void DumpTree(ITree tree, string[] tokenNames, TreeNodeCollection parentCollection)
         {
             Contract.Requires(tokenNames != null);
             Contract.Requires(tokenNames.Length > 0);
             return new ANTLRStringStream(txtSourceCode.Text, "<input>");
         }
 
+        private void DumpScope(Analyzer analyzer)
+        {
+            var scope = analyzer.GlobalScope;
+
+            tvGlobalScope.BeginUpdate();
+            try
+            {
+                tvGlobalScope.Nodes.Clear();
+
+                var headerNode = tvGlobalScope.Nodes.Add("Header");
+                headerNode.Nodes.Add("Release " + scope.Release.ToString());
+                headerNode.Nodes.Add("Serial " + scope.Serial.ToString());
+
+                var symbolsNode = tvGlobalScope.Nodes.Add("Symbols");
+                foreach (var pair in scope.AllSymbols())
+                {
+                    string text = DescribeSymbol(pair.Value);
+                    symbolsNode.Nodes.Add(text);
+
+                    switch (pair.Value.Type)
+                    {
+                        case SymbolType.Constant:
+
+                    }
+                }
+            }
+            finally
+            {
+                tvGlobalScope.EndUpdate();
+            }
+        }
+
+        private static string DescribeSymbol(Symbol symbol)
+        {
+            var sb = new StringBuilder();
+
+            // name: type
+            sb.Append(symbol.Name);
+            sb.Append(": ");
+            sb.Append(symbol.Type);
+
+            // [flags]
+            sb.Append(" [");
+            sb.Append(symbol.Defined ? 'D' : 'd');
+            sb.Append(symbol.Referenced ? 'R' : 'r');
+            sb.Append(symbol.Future != null ? 'F' : 'f');
+            sb.Append("]");
+
+            return sb.ToString();
+        }
+
         #region ILogSink Members
 
         public void LogMessage(LogLevel level, string text, bool count = true)