// Enter the given symbol information into the symbol table. If the given // symbol is already present at the current scope level, assign error. public void enter(string id, Attributes attr) { ScopeTable currentScopeTable = symbolTable.Pop(); TypeDescriptor err = currentScopeTable.Add(id, attr); symbolTable.Push(currentScopeTable); if (err != null) { attr.TypeDescriptor = err; } if (PRINT_STATUS) { if (err != null) { Console.WriteLine(((ErrorDescriptor)err).Message); } else { Console.WriteLine("[\"" + id + "\"" + ", " + attr + "] " + "added to Symbol Table, scope level: " + CurrentScopeLevel); } } }
public void openScope(ScopeTable scopeTable) { scopeLevel++; symbolTable.Push(scopeTable); setBaseline(); if (PRINT_STATUS) { Console.WriteLine(" Pushed Symbol Table: scope level " + scopeLevel); } }
// Close the innermost scope public Dictionary <string, Attributes> closeScope() { scopeLevel--; ScopeTable copy = symbolTable.Pop(); if (PRINT_STATUS) { Console.WriteLine(" Popped Symbol Table: scope level " + scopeLevel); } return(copy.GetCopy()); }
private void setBaseline() { ScopeTable currScopeTable = symbolTable.Pop(); // Until given additional info, Java objects are placeholders Attr javaAttr = new Attr(new JavaObjectDescriptor()); javaAttr.Kind = Kind.TypeAttributes; currScopeTable.Add("java", javaAttr); currScopeTable.Add("io", javaAttr); currScopeTable.Add("lang", javaAttr); currScopeTable.Add("System", javaAttr); currScopeTable.Add("out", javaAttr); currScopeTable.Add("print", javaAttr); currScopeTable.Add("outint", javaAttr); currScopeTable.Add("PrintStream", javaAttr); currScopeTable.Add("TestClasses", javaAttr); // primitive types PrimitiveAttributes primVoidAttrs = new PrimitiveAttributes(new PrimitiveTypeVoidDescriptor()); currScopeTable.Add("VOID", primVoidAttrs); PrimitiveAttributes primIntAttrs = new PrimitiveAttributes(new PrimitiveTypeIntDescriptor()); currScopeTable.Add("INT", primIntAttrs); PrimitiveAttributes primBooleanAttrs = new PrimitiveAttributes(new PrimitiveTypeBooleanDescriptor()); currScopeTable.Add("BOOLEAN", primBooleanAttrs); // special names SpecialNameAttributes spNameThis = new SpecialNameAttributes (SpecialNameEnums.THIS); currScopeTable.Add(spNameThis.Name, spNameThis); SpecialNameAttributes spNameNull = new SpecialNameAttributes( SpecialNameEnums.NULL); currScopeTable.Add(spNameNull.Name, spNameNull); // Write & WriteLine PrimitiveTypeVoidDescriptor returnType = new PrimitiveTypeVoidDescriptor(); // no parameters SignatureDescriptor sigDescNone = new SignatureDescriptor(); // integer parameter SignatureDescriptor sigDescInt = new SignatureDescriptor(); sigDescInt.AddParameter(new PrimitiveTypeIntDescriptor()); // boolean parameter SignatureDescriptor sigDescBoolean = new SignatureDescriptor(); sigDescBoolean.AddParameter(new PrimitiveTypeBooleanDescriptor()); // literal parameter (string) SignatureDescriptor sigDescLiteral = new SignatureDescriptor(); sigDescLiteral.AddParameter(new LiteralTypeDescriptor()); // chain signature type descriptors together (sigDescNode = first) sigDescNone.Next = sigDescInt; sigDescInt.Next = sigDescBoolean; sigDescBoolean.Next = sigDescLiteral; MethodTypeDescriptor methodTypeDescriptor = new MethodTypeDescriptor(); methodTypeDescriptor.ReturnType = returnType; methodTypeDescriptor.Signature = sigDescNone; Attr methodAttr = new Attr(methodTypeDescriptor); currScopeTable.Add("Write", methodAttr); currScopeTable.Add("WriteLine", methodAttr); // TODO: add additional baseline information symbolTable.Push(currScopeTable); }
public bool isDeclaredLocally(string id) { ScopeTable currentScopeTable = symbolTable.Peek(); return(currentScopeTable.Contains(id)); }