aboutsummaryrefslogtreecommitdiffstats
path: root/var.cpp
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-06-15 08:04:15 -0400
committerClyne Sullivan <clyne@bitgloo.com>2024-06-15 08:04:15 -0400
commit0ad40ced64ba5b2b83f7c9274efea1aa3677a534 (patch)
treed019d32a7919f52e4aa6897bfa279ed57ef7038f /var.cpp
parent47d7c964d075f92b9ce657f1b946f5bd6895439b (diff)
break code into separate files
Diffstat (limited to 'var.cpp')
-rw-r--r--var.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/var.cpp b/var.cpp
new file mode 100644
index 0000000..339054f
--- /dev/null
+++ b/var.cpp
@@ -0,0 +1,42 @@
+#include "var.hpp"
+
+#include <list>
+#include <map>
+#include <string>
+
+static std::list<std::map<std::string, Var>> llvmVars;
+
+Var Var::lookup(const std::string& name, int skip)
+{
+ for (auto sc = llvmVars.rbegin(); sc != llvmVars.rend(); ++sc) {
+ if (skip > 0) {
+ --skip;
+ continue;
+ }
+ if (sc->contains(name))
+ return (*sc)[name];
+ }
+
+ return {};
+}
+
+void Var::pushScope()
+{
+ llvmVars.emplace_back();
+}
+
+void Var::popScope()
+{
+ llvmVars.pop_back();
+}
+
+Var& Var::addGlobal(const std::string& name, Var var)
+{
+ return llvmVars.front().emplace(name, var).first->second;
+}
+
+Var& Var::addLocal(const std::string& name, Var var)
+{
+ return llvmVars.back().emplace(name, var).first->second;
+}
+