]> git.mdlowis.com Git - archive/parse-utils.git/commitdiff
Added symbol and scopestack classes
authorMike D. Lowis <mike@mdlowis.com>
Tue, 13 Mar 2012 18:16:37 +0000 (14:16 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Tue, 13 Mar 2012 18:16:37 +0000 (14:16 -0400)
source/symbol/scopestack.cpp [new file with mode: 0644]
source/symbol/scopestack.h [new file with mode: 0644]
source/symbol/symbol.cpp [new file with mode: 0644]
source/symbol/symbol.h [new file with mode: 0644]

diff --git a/source/symbol/scopestack.cpp b/source/symbol/scopestack.cpp
new file mode 100644 (file)
index 0000000..1b7b469
--- /dev/null
@@ -0,0 +1,59 @@
+#include "scopestack.h"
+
+using namespace std;
+
+ScopeStack::ScopeStack()
+{
+}
+
+ScopeStack::~ScopeStack()
+{
+}
+
+void ScopeStack::startScope()
+{
+    scope_stack.push_front( sym_table_t() );
+}
+
+void ScopeStack::stopScope()
+{
+    scope_stack.pop_front();
+}
+
+void ScopeStack::define(const std::string& name)
+{
+    sym_pair_t pair(name, Symbol(name));
+    scope_stack.front().insert( pair );
+}
+
+void ScopeStack::define(const std::string& name, symtype_t type)
+{
+    sym_pair_t pair(name, Symbol(name,type));
+    scope_stack.front().insert( pair );
+}
+
+const Symbol* ScopeStack::lookup(const std::string& name)
+{
+    Symbol* p_sym = NULL;
+    list<sym_table_t>::iterator it;
+    for(it = scope_stack.begin(); it != scope_stack.end(); it++)
+    {
+        sym_table_t::iterator p_val = (*it).find(name);
+        if( p_val != (*it).end())
+        {
+            p_sym = &(p_val->second);
+        }
+    }
+    return p_sym;
+}
+
+bool ScopeStack::isLocal(const std::string& name) const
+{
+    return false;
+}
+
+bool ScopeStack::isGlobal(const std::string& name) const
+{
+    return false;
+}
+
diff --git a/source/symbol/scopestack.h b/source/symbol/scopestack.h
new file mode 100644 (file)
index 0000000..f2d631c
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef SCOPE_TREE_H
+#define SCOPE_TREE_H
+
+#include <string>
+#include <list>
+#include <map>
+#include "symbol.h"
+
+typedef std::pair<std::string,Symbol> sym_pair_t;
+typedef std::map<std::string,Symbol> sym_table_t;
+
+class ScopeStack {
+    protected:
+        std::list<sym_table_t> scope_stack;
+    public:
+        ScopeStack();
+        virtual ~ScopeStack();
+
+        void startScope();
+        void stopScope();
+        void define(const std::string& name);
+        void define(const std::string& name, symtype_t type);
+        const Symbol* lookup(const std::string& name);
+        bool isLocal(const std::string& name) const;
+        bool isGlobal(const std::string& name) const;
+};
+
+#endif
diff --git a/source/symbol/symbol.cpp b/source/symbol/symbol.cpp
new file mode 100644 (file)
index 0000000..128f4ad
--- /dev/null
@@ -0,0 +1,34 @@
+#include "symbol.h"
+
+Symbol::Symbol(const std::string& name) : sym_name(name), sym_type(0)
+{
+}
+
+Symbol::Symbol(const std::string& name, symtype_t type) : sym_name(name), sym_type(0)
+{
+}
+
+Symbol::~Symbol()
+{
+}
+
+symtype_t Symbol::type() const
+{
+    return sym_type;
+}
+
+void Symbol::type(symtype_t type)
+{
+    sym_type = type;
+}
+
+const std::string& Symbol::name() const
+{
+    return sym_name;
+}
+
+void Symbol::name(const std::string& name)
+{
+    sym_name = name;
+}
+
diff --git a/source/symbol/symbol.h b/source/symbol/symbol.h
new file mode 100644 (file)
index 0000000..8c646dc
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef SYMBOL_H
+#define SYMBOL_H
+
+#include <string>
+
+typedef unsigned int symtype_t;
+
+class Symbol {
+    protected:
+        std::string sym_name;
+        symtype_t sym_type;
+    public:
+        Symbol(const std::string& name);
+        Symbol(const std::string& name, symtype_t type);
+        virtual ~Symbol();
+        symtype_t type() const;
+        void type(symtype_t type);
+        const std::string& name() const;
+        void name(const std::string& name);
+};
+
+#endif