Add templated memory

This commit is contained in:
ala89 2023-12-08 10:12:23 +01:00
parent 3e93021793
commit 5be12fbf9e
10 changed files with 85 additions and 75 deletions

View File

@ -1,6 +1,10 @@
#ifndef ANALYSIS_H #ifndef ANALYSIS_H
#define ANALYSIS_H #define ANALYSIS_H
#include "types.h"
#include "memory.h"
using namespace std;
void analyze(Node &ast, Memory<int> &memory);
#endif #endif

View File

@ -10,6 +10,6 @@ using namespace std;
/* /*
Evaluates the AST, returning the latest calulated value Evaluates the AST, returning the latest calulated value
*/ */
EvalResult eval(Node &ast, Memory& memory); EvalResult eval(Node &ast, Memory<EvalResult>& memory);
#endif #endif

View File

@ -5,6 +5,7 @@
#include "types.h" #include "types.h"
using namespace std; using namespace std;
template <typename MemoryEntry>
class Memory { class Memory {
public: public:
bool contains(string identifier); bool contains(string identifier);
@ -12,9 +13,9 @@ class Memory {
void add_scope(ScopeType type); void add_scope(ScopeType type);
void remove_scope(void); void remove_scope(void);
EvalResult get(string identifier); MemoryEntry get(string identifier);
void declare(string identifier, EvalResult value); void declare(string identifier, MemoryEntry value);
void update(string identifier, EvalResult value); void update(string identifier, MemoryEntry value);
Scope& _debug_top(void); Scope& _debug_top(void);
@ -24,4 +25,74 @@ class Memory {
list<Scope> scopes; list<Scope> scopes;
}; };
template <typename MemoryEntry>
Memory<MemoryEntry>::Memory(void) {
scopes.emplace_back();
scopes.back().depth = 0;
}
template <typename MemoryEntry>
bool Memory<MemoryEntry>::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 <typename MemoryEntry>
bool Memory<MemoryEntry>::contains_top(string identifier) {
Scope& top = scopes.back();
return top.vars.contains(identifier);
}
template <typename MemoryEntry>
void Memory<MemoryEntry>::add_scope(ScopeType type) {
Scope& top = scopes.back();
scopes.emplace_back();
scopes.back().depth = top.depth + 1;
scopes.back().type = type;
}
template <typename MemoryEntry>
void Memory<MemoryEntry>::remove_scope(void) {
scopes.pop_back();
}
template <typename MemoryEntry>
MemoryEntry Memory<MemoryEntry>::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 <typename MemoryEntry>
void Memory<MemoryEntry>::declare(string identifier, MemoryEntry value) {
Scope& top = scopes.back();
top.vars[identifier] = value;
}
template <typename MemoryEntry>
void Memory<MemoryEntry>::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 <typename MemoryEntry>
Scope& Memory<MemoryEntry>::_debug_top(void) {
Scope& top = scopes.back();
return top;
}
#endif #endif

View File

@ -41,7 +41,7 @@ double double_cast(EvalResult value) {
} }
} }
EvalResult eval(Node &ast, Memory &memory) { EvalResult eval(Node &ast, Memory<EvalResult> &memory) {
if (holds_alternative<InnerNode>(ast)) { if (holds_alternative<InnerNode>(ast)) {
InnerNode node = get<InnerNode>(ast); InnerNode node = get<InnerNode>(ast);
switch (node.type) { switch (node.type) {

View File

@ -24,7 +24,7 @@ int main(int argc, char* argv[]) {
vector<string> input; vector<string> input;
vector<Token> tokens; vector<Token> tokens;
Memory memory = Memory(); Memory<EvalResult> memory;
while (true) { while (true) {
try { try {

View File

@ -1,65 +0,0 @@
#include <iostream>
#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;
}

View File

@ -6,7 +6,7 @@
#include "../src/include/interpreter.h" #include "../src/include/interpreter.h"
int execute(string s) { int execute(string s) {
Memory memory = Memory(); Memory<EvalResult> memory;;
vector<Token> tokens = tokenize({ s }); vector<Token> tokens = tokenize({ s });
Node ast = parse(tokens); Node ast = parse(tokens);
EvalResult res = eval(ast, memory); EvalResult res = eval(ast, memory);

View File

@ -6,7 +6,7 @@
#include "../src/include/interpreter.h" #include "../src/include/interpreter.h"
int execute(string s) { int execute(string s) {
Memory memory = Memory(); Memory<EvalResult> memory;;
vector<Token> tokens = tokenize({ s }); vector<Token> tokens = tokenize({ s });
Node ast = parse(tokens); Node ast = parse(tokens);
EvalResult res = eval(ast, memory); EvalResult res = eval(ast, memory);

View File

@ -6,7 +6,7 @@
#include "../src/include/interpreter.h" #include "../src/include/interpreter.h"
EvalResult execute(string s) { EvalResult execute(string s) {
Memory memory = Memory(); Memory<EvalResult> memory;;
vector<Token> tokens = tokenize({ s }); vector<Token> tokens = tokenize({ s });
Node ast = parse(tokens); Node ast = parse(tokens);
EvalResult res = eval(ast, memory); EvalResult res = eval(ast, memory);

View File

@ -6,7 +6,7 @@
#include "../src/include/interpreter.h" #include "../src/include/interpreter.h"
EvalResult execute(string s) { EvalResult execute(string s) {
Memory memory = Memory(); Memory<EvalResult> memory;;
vector<Token> tokens = tokenize({ s }); vector<Token> tokens = tokenize({ s });
Node ast = parse(tokens); Node ast = parse(tokens);
EvalResult res = eval(ast, memory); EvalResult res = eval(ast, memory);