Add templated memory
This commit is contained in:
parent
3e93021793
commit
5be12fbf9e
@ -1,6 +1,10 @@
|
||||
#ifndef ANALYSIS_H
|
||||
#define ANALYSIS_H
|
||||
|
||||
#include "types.h"
|
||||
#include "memory.h"
|
||||
using namespace std;
|
||||
|
||||
void analyze(Node &ast, Memory<int> &memory);
|
||||
|
||||
#endif
|
@ -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<EvalResult>& memory);
|
||||
|
||||
#endif
|
@ -5,6 +5,7 @@
|
||||
#include "types.h"
|
||||
using namespace std;
|
||||
|
||||
template <typename MemoryEntry>
|
||||
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<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
|
@ -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)) {
|
||||
InnerNode node = get<InnerNode>(ast);
|
||||
switch (node.type) {
|
||||
|
@ -24,7 +24,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
vector<string> input;
|
||||
vector<Token> tokens;
|
||||
Memory memory = Memory();
|
||||
Memory<EvalResult> memory;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
|
@ -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;
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
#include "../src/include/interpreter.h"
|
||||
|
||||
int execute(string s) {
|
||||
Memory memory = Memory();
|
||||
Memory<EvalResult> memory;;
|
||||
vector<Token> tokens = tokenize({ s });
|
||||
Node ast = parse(tokens);
|
||||
EvalResult res = eval(ast, memory);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "../src/include/interpreter.h"
|
||||
|
||||
int execute(string s) {
|
||||
Memory memory = Memory();
|
||||
Memory<EvalResult> memory;;
|
||||
vector<Token> tokens = tokenize({ s });
|
||||
Node ast = parse(tokens);
|
||||
EvalResult res = eval(ast, memory);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "../src/include/interpreter.h"
|
||||
|
||||
EvalResult execute(string s) {
|
||||
Memory memory = Memory();
|
||||
Memory<EvalResult> memory;;
|
||||
vector<Token> tokens = tokenize({ s });
|
||||
Node ast = parse(tokens);
|
||||
EvalResult res = eval(ast, memory);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "../src/include/interpreter.h"
|
||||
|
||||
EvalResult execute(string s) {
|
||||
Memory memory = Memory();
|
||||
Memory<EvalResult> memory;;
|
||||
vector<Token> tokens = tokenize({ s });
|
||||
Node ast = parse(tokens);
|
||||
EvalResult res = eval(ast, memory);
|
||||
|
Loading…
Reference in New Issue
Block a user