2003-12-24 Cesar Lopez Nataren <cesar@ciencias.unam.mx>
authorCésar Natarén <cesar@mono-cvs.ximian.com>
Wed, 24 Dec 2003 17:33:19 +0000 (17:33 -0000)
committerCésar Natarén <cesar@mono-cvs.ximian.com>
Wed, 24 Dec 2003 17:33:19 +0000 (17:33 -0000)
* jscript-lexer-parser.g: keep track of parent reference for
IDENTIFIER, STRING_LITERAL.

* expression.cs: Added 'binding' field. Added 'parent' parameter
to constructor. Now we emit the proper code for the identifier.

* VariableStatement.cs: updated to field names instead of the old
property's name.

* VariableDeclaration.cs: Added class Decl, it constains the
references to the static field or LocalBuilder that are created
when a declaration is emitted. Deleted unuseful properties (Id,
InitValue, Type).

* SymbolTable.cs: Deleted Retrieve method. Contains now returns an
object instead of a boolean.

* StringLiteral.cs: Deleted unuseful properties. Use ig instead of ec.ig.

* IdentificationTable.cs: Deleted Retrieve method. Contains method
now return an object instead of a boolean, that reference is the
binding associated with the identifier being searched.

svn path=/trunk/mcs/; revision=21478

mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/StringLiteral.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/SymbolTable.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/VariableDeclaration.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/VariableStatement.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/jscript-lexer-parser.g

index ffe99f6b4251af747e788981c25adb0c2c08fbc9..3c81748578416c95a0f6beb9edfb9622982f5e94 100644 (file)
@@ -41,20 +41,16 @@ namespace Microsoft.JScript {
                        ((SymbolTable) stack.Peek ()).Add (id , decl);
                        System.Console.WriteLine ("IdentificationTable::Enter::{0}", id);
                }
-
-               internal AST Retrieve (string id)
-               {
-                       return ((SymbolTable) stack.Peek ()).Retrieve (id);
-               }
-
-               internal bool Contains (string id)
+               
+               //
+               // It'll return the object asociated with the 'id', if found.
+               //
+               internal object Contains (string id)
                {
                        SymbolTable parent, current_scope = (SymbolTable) stack.Peek ();
-                       bool found = current_scope.Contains (id);
+                       object found = current_scope.Contains (id);
 
-                       if (found)
-                               return true;    
-                       else {
+                       if (found == null) {
                                parent = current_scope.parent;
 
                                if (parent != null)
index d5301c9c146ed7b7d8f4ffa55f4efcb5ac6846f5..e95071046798d65d48d24d299e4f7c3937ed4ace 100644 (file)
@@ -16,13 +16,9 @@ namespace Microsoft.JScript {
 
                internal string str;
 
-               public string Str {
-                       get { return str; }
-                       set { str = value; }
-               }
-
-               internal StringLiteral (string s)
+               internal StringLiteral (AST parent, string s)
                {
+                       this.parent = parent;
                        str = s;
                }
 
@@ -45,8 +41,7 @@ namespace Microsoft.JScript {
                        else 
                                ig = ec.ig;
 
-                       ec.ig.Emit (OpCodes.Ldstr, str);
+                       ig.Emit (OpCodes.Ldstr, str);
                }
        }
 }
-       
index 24e1da9f5280c82c566cca04c0be95198f1aa9df..37d09c102eff195989f8bb4fde9fdeb799d11d42 100644 (file)
@@ -17,7 +17,7 @@ namespace Microsoft.JScript {
 
                internal SymbolTable parent;
                internal Hashtable symbols;
-
+               
                internal SymbolTable (SymbolTable parent)
                {
                        symbols = new Hashtable ();
@@ -29,14 +29,9 @@ namespace Microsoft.JScript {
                        symbols.Add (id, d);
                }
 
-               internal AST Retrieve (string id)
-               {
-                       return ((AST) symbols [id]);
-               }
-
-               internal bool Contains (string id)
+               internal object Contains (string id)
                {
-                       return symbols.ContainsKey (id);
+                       return symbols [id];
                }
 
                public override string ToString ()
index 0d1ffd6e197558baa3f151a79340513595bf0c6b..457f00dee0e78d6c1e0e4ef56e631a35c03378a7 100644 (file)
@@ -14,12 +14,17 @@ using System.Reflection.Emit;
 
 namespace Microsoft.JScript {
 
-       public class VariableDeclaration : AST {
+       public abstract class Decl : AST {
+               internal FieldInfo field_info;
+               internal LocalBuilder local_builder;
+       }
+
+       public class VariableDeclaration : Decl {
 
-               private string id;
-               private Type type;
-               private string type_annot;
-               private AST val;
+               internal string id;
+               internal Type type;
+               internal string type_annot;
+               internal AST val;
 
                internal VariableDeclaration (AST parent, string id, string t, AST init)
                {
@@ -33,31 +38,14 @@ namespace Microsoft.JScript {
                                // FIXME: resolve the type annotations
                                this.type = typeof (System.Object);
                        }
-
                        this.val = init;
                }
 
-
-               public string Id {
-                       get { return id; }
-                       set { id = value; }
-               }
-
-               public AST InitValue {
-                       get { return val; }
-               }
-
-               public Type Type {
-                       get { return type; }
-                       set { type = value; }
-               }
-
-
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
 
-                       sb.Append (Id);
+                       sb.Append (id);
                        sb.Append (":" + type_annot);
                        sb.Append (" = ");
 
@@ -73,20 +61,23 @@ namespace Microsoft.JScript {
                                FieldBuilder field;
                                TypeBuilder type  = ec.type_builder;
                                
-                               field = type.DefineField (id, Type,
+                               field = type.DefineField (id, type,
                                                          FieldAttributes.Public |
                                                          FieldAttributes.Static);
+                               
+                               field_info = CodeGenerator.assembly_builder.GetType ("JScript 0").GetField (id);
+
                                if (val != null) {
                                        val.Emit (ec);
                                        ec.gc_ig.Emit (OpCodes.Stsfld, field);
                                }
                        } else {
                                ILGenerator ig = ec.ig;
-                               LocalBuilder lb = ig.DeclareLocal (Type);
+                               local_builder = ig.DeclareLocal (type);
 
                                if (val != null) {
                                        val.Emit (ec);
-                                       ig.Emit (OpCodes.Stloc, lb);
+                                       ig.Emit (OpCodes.Stloc, local_builder);
                                }
                        }
                }
index 18a84f65c93e562b7e5f3e55f55a73bb54e75b60..342fe53e3c745e881a6a6663f41176cfae5229d2 100644 (file)
@@ -56,15 +56,14 @@ namespace Microsoft.JScript {
 
                        for (i = 0; i < size; i++) {
                                tmp_decl = (VariableDeclaration) var_decls [i];
-                               init_val = tmp_decl.InitValue;
+                               init_val = tmp_decl.val;
 
                                if (init_val != null)
                                        res = init_val.Resolve (context);
 
                                if (res)
-                                       context.Enter (tmp_decl.Id, tmp_decl);
+                                       context.Enter (tmp_decl.id, tmp_decl);
                        }
-
                        return true;
                }
        }
index 93034c56f125b9321aad61fb426e64dc7ff9e03f..9be5fdc3c2d6a3302047ae7c95d147dad7e258e8 100755 (executable)
@@ -202,9 +202,11 @@ namespace Microsoft.JScript {
        internal class Identifier : AST {
 
                internal string name;
+               internal Decl binding;
 
-               internal Identifier (string id)
+               internal Identifier (AST parent, string id)
                {
+                       this.parent = parent;
                        this.name = id;
                }
 
@@ -217,16 +219,30 @@ namespace Microsoft.JScript {
                {
                        if (name == "print")
                                return SemanticAnalyser.print;
-                       else if (context.Contains (name))
-                               return true;
-                       else throw new Exception ("variable not found: " +  name);
+
+                       Decl bind = (Decl) context.Contains (name);
+                       
+                       if (bind == null)
+                               throw new Exception ("variable not found: " +  name);
+                       else
+                               binding = bind;
+                       return true;
                }
 
                internal override void Emit (EmitContext ec)
                {
-                       throw new NotImplementedException ();
+                       ILGenerator ig;
+
+                       if (parent == null) {
+                               ig = ec.gc_ig;
+                               ig.Emit (OpCodes.Ldsfld, binding.field_info);
+                       } else {
+                               ig = ec.ig;                             
+                               ig.Emit (OpCodes.Ldloc, binding.local_builder);
+                       }
+                       ig.Emit (OpCodes.Pop);
                }
-       }
+       }       
 
        public class Args : AST {
 
index 22067c6e1b7e52d50e7a29ee688afa89ae595d6d..8c93bd01a2e3533502c974deed7aa30b91b95b1f 100644 (file)
@@ -844,7 +844,7 @@ primary_expr [AST parent] returns [AST prim_exp]
        | object_literal
        | id:IDENTIFIER 
          { 
-               Identifier ident = new Identifier (id.getText ());
+               Identifier ident = new Identifier (parent, id.getText ());
                prim_exp = (AST) ident;
          }
        | l = literal [parent] { prim_exp = l; }
@@ -878,7 +878,7 @@ literal [AST parent] returns [AST l]
          }
        | s:STRING_LITERAL
          {
-                 StringLiteral str = new StringLiteral (s.getText ());
+                 StringLiteral str = new StringLiteral (parent, s.getText ());
                  l = str;
          }      
        | l = numeric_literal [parent]