ffe99f6b4251af747e788981c25adb0c2c08fbc9
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / IdentificationTable.cs
1 //
2 // ExecutionContext.cs: The stack of possible executions environments.
3 //
4 // Author:
5 //      Cesar Lopez Nataren
6 //
7 // (C) 2003, Cesar Lopez Nataren
8 //
9
10 using System.Collections;
11 using System.Text;
12
13 namespace Microsoft.JScript {
14
15         internal class IdentificationTable {
16
17                 internal Stack stack;
18
19                 internal IdentificationTable ()
20                 {
21                         stack = new Stack ();
22                         stack.Push (new SymbolTable (null));
23                 }
24                 
25                 internal void OpenBlock ()
26                 {
27                         System.Console.WriteLine ("IdTable::OpenBlock");
28
29                         SymbolTable parent = (SymbolTable) stack.Peek ();
30                         stack.Push (new SymbolTable (parent));
31                 }
32
33                 internal void CloseBlock ()
34                 {
35                         System.Console.WriteLine ("IdTable::CloseBlock");
36                         stack.Pop ();
37                 }
38
39                 internal void Enter (string id, object decl)
40                 {                       
41                         ((SymbolTable) stack.Peek ()).Add (id , decl);
42                         System.Console.WriteLine ("IdentificationTable::Enter::{0}", id);
43                 }
44
45                 internal AST Retrieve (string id)
46                 {
47                         return ((SymbolTable) stack.Peek ()).Retrieve (id);
48                 }
49
50                 internal bool Contains (string id)
51                 {
52                         SymbolTable parent, current_scope = (SymbolTable) stack.Peek ();
53                         bool found = current_scope.Contains (id);
54
55                         if (found)
56                                 return true;    
57                         else {
58                                 parent = current_scope.parent;
59
60                                 if (parent != null)
61                                         found = parent.Contains (id);
62                         }
63                         return found;
64                 }
65
66                 public override string ToString ()
67                 {
68                         StringBuilder sb = new StringBuilder ();
69
70                         int i, size = stack.Count;
71
72                         for (i = 0; i < size; i++)
73                                 sb.Append (stack.Pop ().ToString ());
74
75                         return sb.ToString ();
76                 }
77         }
78 }