public UnaryOperandNode VisitUnaryOperand(UnaryOperandNode uon) { if (uon.Type == SymbolType.Identifier) { SymbolTableEntry entry = CheckThatIdentifierIsDeclaredAndGetEntry(uon.Value, uon.Token); if (entry.Type == SymbolType.IntegerValue && uon.Negative) { int v = ConvertStringToInteger(entry.Value, uon.Token); return(new UnaryOperandNode(OppositeOfInteger(v), entry.Type, uon.Token)); } else if (entry.Type == SymbolType.Boolean && uon.Not) { string newValue; if (entry.Value == "true") { newValue = "false"; } else { newValue = "true"; } return(new UnaryOperandNode(newValue, SymbolType.Boolean, uon.Token)); } return(new UnaryOperandNode(entry.Value, entry.Type, uon.Token)); } else if (uon.Type == SymbolType.IntegerValue) { int value = ConvertStringToInteger(uon.Value, uon.Token); return(new UnaryOperandNode(value.ToString(), uon.Type, uon.Token, uon.Negative)); } // Do not return the AST node. Create a copy return(new UnaryOperandNode(uon.Value, uon.Type, uon.Token, uon.Negative)); }
private SymbolTableEntry CheckThatIdentifierIsDeclaredAndGetEntry(string id, Token token) { SymbolTableEntry entry = this.symbolTable.GetEntry(id); if (entry.Type == SymbolType.Invalid) { throw new DeclarationError($"Variable {id} has not been declared.", token); } return(entry); }
public SymbolTableEntry GetEntry(string id) { foreach (SymbolTableBlock stb in this.blocks) { SymbolTableEntry entry = stb.GetEntry(id); if (entry.Type != SymbolType.Invalid) { return(entry); } } return(new SymbolTableEntry("error", SymbolType.Invalid, "error")); }
public bool AddEntry(SymbolTableEntry e) { SymbolTableBlock current = this.blocks.Peek(); SymbolTableEntry entry = GetEntry(e.Identifier); if (entry.Type != SymbolType.Invalid) { // Entry already exists in some of the parent blocks return(false); } current.AddEntry(e); return(true); }
public bool ModifyEntry(string id, string newValue) { SymbolTableEntry e = GetEntry(id); if (e.Type != SymbolType.Invalid) { if (!e.Locked) { e.Value = newValue; } else { return(false); } } else { Console.WriteLine("No such entry, Illegal assignment"); return(false); } return(true); }
public void AddEntry(SymbolTableEntry entry) { this.entries.Add(entry); }
public void ReleaseVariable(string id) { SymbolTableEntry e = GetEntry(id); e.Locked = false; }
public void LockVariable(string id) { SymbolTableEntry e = GetEntry(id); e.Locked = true; }
/* * Used in for loops to assign a new value to the identifier */ public void ForceModifyEntry(string id, string newValue) { SymbolTableEntry e = GetEntry(id); e.Value = newValue; }