Commit 5b307fe7379cb1e47d4df1f735b8c8a5fc5263f3
1 parent
2531734375
Exists in
master
ummm not sure
Showing 2 changed files with 67 additions and 1 deletions Side-by-side Diff
ast.cc
View file @
5b307fe
... | ... | @@ -9,6 +9,7 @@ |
9 | 9 | #include <string.h> // strdup |
10 | 10 | #include <stdio.h> // printf |
11 | 11 | |
12 | + | |
12 | 13 | Node::Node(yyltype loc) { |
13 | 14 | location = new yyltype(loc); |
14 | 15 | parent = NULL; |
... | ... | @@ -45,5 +46,53 @@ |
45 | 46 | |
46 | 47 | void Identifier::PrintChildren(int indentLevel) { |
47 | 48 | printf("%s", name); |
49 | +} | |
50 | + | |
51 | +/*------------------------------------------------------------------------------------ | |
52 | + * Symbol Table Information | |
53 | + *----------------------------------------------------------------------------------*/ | |
54 | +SymbolTable symbols; | |
55 | + | |
56 | +pair<Decl*,llvm::Value *> findSymbol(string s) { | |
57 | + for(int i = symbols.size() - 1; i >= 0; i--) { | |
58 | + if(symbols[i].count(s) > 0) return symbols[i][s]; | |
59 | + } | |
60 | + return make_pair<Decl*,llvm::Value *>(NULL,NULL); | |
61 | +} | |
62 | + | |
63 | +pair<Decl*,llvm::Value *> findSymbol(Decl* d) { | |
64 | + string s(d->GetIdentifier()->GetName()); | |
65 | + return findSymbol(s); | |
66 | +} | |
67 | + | |
68 | +bool isInCurrentScope(string s) { | |
69 | + if(symbols.size() == 0) return false; | |
70 | + return symbols[symbols.size()-1].count(s); | |
71 | +} | |
72 | + | |
73 | +bool isInCurrentScope(Decl* d) { | |
74 | + string s(d->GetIdentifier()->GetName()); | |
75 | + return isInCurrentScope(s); | |
76 | +} | |
77 | + | |
78 | +bool isInScope(string s) { | |
79 | + pair<Decl*,llvm::Value *> tmp = findSymbol(s); | |
80 | + return tmp.first != NULL; | |
81 | +} | |
82 | + | |
83 | +bool isInScope(Decl* d) { | |
84 | + string s(d->GetIdentifier()->GetName()); | |
85 | + return isInScope(s); | |
86 | +} | |
87 | + | |
88 | +void pushScope() { | |
89 | + symbols.push_back(map<string,pair<Decl*,llvm::Value *> >()); | |
90 | +} | |
91 | +void popScope() { | |
92 | + symbols.pop_back(); | |
93 | +} | |
94 | + | |
95 | +void addToScope(string s, Decl* d, Type* t) { | |
96 | + symbols[symbols.size()-1][s] = make_pair(d, t); | |
48 | 97 | } |
ast.h
View file @
5b307fe
... | ... | @@ -41,12 +41,16 @@ |
41 | 41 | #include "irgen.h" |
42 | 42 | #include "location.h" |
43 | 43 | #include <iostream> |
44 | +#include <vector> | |
45 | +#include <map> | |
46 | +#include <algorithm> | |
44 | 47 | |
45 | 48 | using namespace std; |
46 | 49 | |
47 | 50 | class SymbolTable; |
48 | 51 | class MyStack; |
49 | 52 | class FnDecl; |
53 | +class Decl; | |
50 | 54 | |
51 | 55 | class Node { |
52 | 56 | protected: |
... | ... | @@ -98,7 +102,20 @@ |
98 | 102 | Error() : Node() {} |
99 | 103 | const char *GetPrintNameForNode() { return "Error"; } |
100 | 104 | }; |
101 | - | |
105 | +/*------------------------------------------------------------------------------------ | |
106 | + * Symbol Table Information | |
107 | + *----------------------------------------------------------------------------------*/ | |
108 | +typedef vector<map<string,pair<Decl*,llvm::Value *> > > SymbolTable; | |
109 | +pair<Decl*,llvm::Value * v> findSymbol(string s); | |
110 | +pair<Decl*,llvm::Value * v> findSymbol(Decl* d); | |
111 | +bool isInCurrentScope(string s); | |
112 | +bool isInCurrentScope(Decl* d); | |
113 | +bool isInScope(string s); | |
114 | +bool isInScope(Decl* d); | |
115 | +void pushScope(); | |
116 | +void popScope(); | |
117 | +void addToScope(string s, Decl* d, llvm::Value * v t); | |
118 | +extern SymbolTable symbols; | |
102 | 119 | |
103 | 120 | |
104 | 121 | #endif |