#include "lookup.h" #include "messages.h" #include "types.h" #include "Node.h" #include "SyntaxTree.h" #include "Symbol.h" #include "SymbolTable.h" #include /* declared in comp.l */ extern int lineno; /* declared in comp.y */ extern SyntaxTree *tree; extern SymbolTable *symtab; extern Messages messages; extern string mainScopeName; Symbol *lookup(const string& id, SymbolType default_type) { /* Lookup ID */ Symbol *symbol = symtab->GetSymbol(id); if (symbol == NULL) { /* Symbol not found */ messages.UndeclaredIdError(lineno, id); /* Add ID with return type error to avoid triggering error again */ symbol = new Symbol(); symbol->SetSymbolType(default_type); symbol->SetName(id); symbol->SetLine(lineno); symbol->SetReturnType(RT_ERROR); symtab->AddSymbol(symbol); } return symbol; } /* Checks if the id already exists and gives an error if it does */ void check_id(const string& id) { /* check for program name */ if (id == mainScopeName) { messages.DeclaredIdError(lineno, id, 0); return; } /* check for local identifiers */ Symbol *symbol = symtab->GetSymbol(symtab->GetCurrentScopeName(), id, false); if (symbol != NULL) { messages.DeclaredIdError(lineno, id, symbol->GetLine()); return; } /* check for global identifiers */ symbol = symtab->GetSymbol(id); if (symbol != NULL) { SymbolType symboltype = symbol->GetSymbolType(); if (symboltype == ST_FUNCTION || symboltype == ST_PROCEDURE) { messages.DeclaredIdError(lineno, id, symbol->GetLine()); return; } } }