diff --git a/src/include/analysis.h b/src/include/analysis.h index bbaea2d..ded5f95 100644 --- a/src/include/analysis.h +++ b/src/include/analysis.h @@ -1,6 +1,10 @@ #ifndef ANALYSIS_H #define ANALYSIS_H +#include "types.h" +#include "memory.h" +using namespace std; +void analyze(Node &ast, Memory &memory); #endif \ No newline at end of file diff --git a/src/include/interpreter.h b/src/include/interpreter.h index 0a119cd..3582ef5 100644 --- a/src/include/interpreter.h +++ b/src/include/interpreter.h @@ -10,6 +10,6 @@ using namespace std; /* Evaluates the AST, returning the latest calulated value */ -EvalResult eval(Node &ast, Memory& memory); +EvalResult eval(Node &ast, Memory& memory); #endif \ No newline at end of file diff --git a/src/include/memory.h b/src/include/memory.h index 76c4c6d..01075f2 100644 --- a/src/include/memory.h +++ b/src/include/memory.h @@ -5,6 +5,7 @@ #include "types.h" using namespace std; +template class Memory { public: bool contains(string identifier); @@ -12,9 +13,9 @@ class Memory { void add_scope(ScopeType type); void remove_scope(void); - EvalResult get(string identifier); - void declare(string identifier, EvalResult value); - void update(string identifier, EvalResult value); + MemoryEntry get(string identifier); + void declare(string identifier, MemoryEntry value); + void update(string identifier, MemoryEntry value); Scope& _debug_top(void); @@ -24,4 +25,74 @@ class Memory { list scopes; }; +template +Memory::Memory(void) { + scopes.emplace_back(); + scopes.back().depth = 0; +} + +template +bool Memory::contains(string identifier) { + for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { + Scope& scope = *rit; + if (scope.vars.contains(identifier)) return true; + } + + return false; +} + +template +bool Memory::contains_top(string identifier) { + Scope& top = scopes.back(); + return top.vars.contains(identifier); +} + +template +void Memory::add_scope(ScopeType type) { + Scope& top = scopes.back(); + scopes.emplace_back(); + scopes.back().depth = top.depth + 1; + scopes.back().type = type; +} + +template +void Memory::remove_scope(void) { + scopes.pop_back(); +} + +template +MemoryEntry Memory::get(string identifier) { + for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { + Scope& scope = *rit; + if (scope.vars.contains(identifier)) return scope.vars[identifier]; + } + + throw; +} + +template +void Memory::declare(string identifier, MemoryEntry value) { + Scope& top = scopes.back(); + top.vars[identifier] = value; +} + +template +void Memory::update(string identifier, MemoryEntry value) { + for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { + Scope& scope = *rit; + if (scope.vars.contains(identifier)) { + scope.vars[identifier] = value; + return; + } + } + + throw; +} + +template +Scope& Memory::_debug_top(void) { + Scope& top = scopes.back(); + return top; +} + #endif \ No newline at end of file diff --git a/src/interpreter.cpp b/src/interpreter.cpp index b3353d9..76085a1 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -41,7 +41,7 @@ double double_cast(EvalResult value) { } } -EvalResult eval(Node &ast, Memory &memory) { +EvalResult eval(Node &ast, Memory &memory) { if (holds_alternative(ast)) { InnerNode node = get(ast); switch (node.type) { diff --git a/src/main.cpp b/src/main.cpp index 8b6b540..cedbc26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,7 @@ int main(int argc, char* argv[]) { vector input; vector tokens; - Memory memory = Memory(); + Memory memory; while (true) { try { diff --git a/src/memory.cpp b/src/memory.cpp deleted file mode 100644 index c89c485..0000000 --- a/src/memory.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include "include/memory.h" -using namespace std; - -Memory::Memory(void) { - scopes.emplace_back(); - scopes.back().depth = 0; -} - -bool Memory::contains(string identifier) { - for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { - Scope& scope = *rit; - if (scope.vars.contains(identifier)) return true; - } - - return false; -} - -bool Memory::contains_top(string identifier) { - Scope& top = scopes.back(); - return top.vars.contains(identifier); -} - -void Memory::add_scope(ScopeType type) { - Scope& top = scopes.back(); - scopes.emplace_back(); - scopes.back().depth = top.depth + 1; - scopes.back().type = type; -} - -void Memory::remove_scope(void) { - scopes.pop_back(); -} - - -EvalResult Memory::get(string identifier) { - for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { - Scope& scope = *rit; - if (scope.vars.contains(identifier)) return scope.vars[identifier]; - } - - throw; -} - -void Memory::declare(string identifier, EvalResult value) { - Scope& top = scopes.back(); - top.vars[identifier] = value; -} - -void Memory::update(string identifier, EvalResult value) { - for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { - Scope& scope = *rit; - if (scope.vars.contains(identifier)) { - scope.vars[identifier] = value; - return; - } - } - - throw; -} - -Scope& Memory::_debug_top(void) { - Scope& top = scopes.back(); - return top; -} \ No newline at end of file diff --git a/test/bool_logic.cpp b/test/bool_logic.cpp index 9dc4b4b..be9559e 100644 --- a/test/bool_logic.cpp +++ b/test/bool_logic.cpp @@ -6,7 +6,7 @@ #include "../src/include/interpreter.h" int execute(string s) { - Memory memory = Memory(); + Memory memory;; vector tokens = tokenize({ s }); Node ast = parse(tokens); EvalResult res = eval(ast, memory); diff --git a/test/expr_arithmetiques.cpp b/test/expr_arithmetiques.cpp index ddc1f5c..d37d7ff 100644 --- a/test/expr_arithmetiques.cpp +++ b/test/expr_arithmetiques.cpp @@ -6,7 +6,7 @@ #include "../src/include/interpreter.h" int execute(string s) { - Memory memory = Memory(); + Memory memory;; vector tokens = tokenize({ s }); Node ast = parse(tokens); EvalResult res = eval(ast, memory); diff --git a/test/statements.cpp b/test/statements.cpp index 1c9ac36..9e61618 100644 --- a/test/statements.cpp +++ b/test/statements.cpp @@ -6,7 +6,7 @@ #include "../src/include/interpreter.h" EvalResult execute(string s) { - Memory memory = Memory(); + Memory memory;; vector tokens = tokenize({ s }); Node ast = parse(tokens); EvalResult res = eval(ast, memory); diff --git a/test/variables.cpp b/test/variables.cpp index 2d4c74c..e379fb9 100644 --- a/test/variables.cpp +++ b/test/variables.cpp @@ -6,7 +6,7 @@ #include "../src/include/interpreter.h" EvalResult execute(string s) { - Memory memory = Memory(); + Memory memory;; vector tokens = tokenize({ s }); Node ast = parse(tokens); EvalResult res = eval(ast, memory);