From 5b307fe7379cb1e47d4df1f735b8c8a5fc5263f3 Mon Sep 17 00:00:00 2001 From: Austin Sun Date: Mon, 16 May 2016 17:34:15 -0700 Subject: [PATCH] ummm not sure --- ast.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ ast.h | 19 ++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/ast.cc b/ast.cc index 146c42f..d93cd01 100644 --- a/ast.cc +++ b/ast.cc @@ -9,6 +9,7 @@ #include // strdup #include // printf + Node::Node(yyltype loc) { location = new yyltype(loc); parent = NULL; @@ -46,3 +47,51 @@ Identifier::Identifier(yyltype loc, const char *n) : Node(loc) { void Identifier::PrintChildren(int indentLevel) { printf("%s", name); } + +/*------------------------------------------------------------------------------------ + * Symbol Table Information + *----------------------------------------------------------------------------------*/ +SymbolTable symbols; + +pair findSymbol(string s) { + for(int i = symbols.size() - 1; i >= 0; i--) { + if(symbols[i].count(s) > 0) return symbols[i][s]; + } + return make_pair(NULL,NULL); +} + +pair findSymbol(Decl* d) { + string s(d->GetIdentifier()->GetName()); + return findSymbol(s); +} + +bool isInCurrentScope(string s) { + if(symbols.size() == 0) return false; + return symbols[symbols.size()-1].count(s); +} + +bool isInCurrentScope(Decl* d) { + string s(d->GetIdentifier()->GetName()); + return isInCurrentScope(s); +} + +bool isInScope(string s) { + pair tmp = findSymbol(s); + return tmp.first != NULL; +} + +bool isInScope(Decl* d) { + string s(d->GetIdentifier()->GetName()); + return isInScope(s); +} + +void pushScope() { + symbols.push_back(map >()); +} +void popScope() { + symbols.pop_back(); +} + +void addToScope(string s, Decl* d, Type* t) { + symbols[symbols.size()-1][s] = make_pair(d, t); +} diff --git a/ast.h b/ast.h index 88562cd..b92d2f7 100644 --- a/ast.h +++ b/ast.h @@ -41,12 +41,16 @@ #include "irgen.h" #include "location.h" #include +#include +#include +#include using namespace std; class SymbolTable; class MyStack; class FnDecl; +class Decl; class Node { protected: @@ -98,7 +102,20 @@ class Error : public Node Error() : Node() {} const char *GetPrintNameForNode() { return "Error"; } }; - +/*------------------------------------------------------------------------------------ + * Symbol Table Information + *----------------------------------------------------------------------------------*/ +typedef vector > > SymbolTable; +pair findSymbol(string s); +pair findSymbol(Decl* d); +bool isInCurrentScope(string s); +bool isInCurrentScope(Decl* d); +bool isInScope(string s); +bool isInScope(Decl* d); +void pushScope(); +void popScope(); +void addToScope(string s, Decl* d, llvm::Value * v t); +extern SymbolTable symbols; #endif -- 1.9.1