From 6c56e7209fc82888cb7a8c85c38ff5c3479cefbb Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 5 Feb 2017 22:10:09 +0100 Subject: Add refcounting code --- evaluate.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ evaluate.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 evaluate.cpp create mode 100644 evaluate.h diff --git a/evaluate.cpp b/evaluate.cpp new file mode 100644 index 0000000..c73d59a --- /dev/null +++ b/evaluate.cpp @@ -0,0 +1,50 @@ +#include +#include +#include "evaluate.h" + +using namespace std; + + +Value::Value(double numval) + :type(Type::number),numval(numval){} +Value::Value(const string &strval) + :type(Type::string),strval(strval){} +Value::Value(const Scope &scope) + :type(Type::scope),scope(scope){} + + +namespace A { + map collection; + + template + Value* ref_create(Args... args){ + Value *value=new Value(args...); + return ref(value); + } + + Value* ref(Value *value){ + auto it=collection.find(value); + if(it==collection.end()){ + collection.emplace(value,1); + } else { + it->second++; + } + return value; + } + + void unref(Value *value){ + auto it=collection.find(value); + assert(it!=collection.end()); + if(it->second==1){ + delete value; + collection.erase(it); + } else { + it->second--; + } + } +} + + +void evaluateSTL(const StatementList &stl){ + ; +} diff --git a/evaluate.h b/evaluate.h new file mode 100644 index 0000000..09d0c81 --- /dev/null +++ b/evaluate.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include "ast.h" + +using namespace std; + + +class Value{ +public: + enum class Type{ + number, + string, + scope, + }; + + Type type; + double numval; + string strval; + Scope scope; + + Value(double numval); + Value(const string &strval); + Value(const Scope &scope); +}; + + +namespace A { + + template + Value* ref_create(Args... args); + + Value* ref(Value *value); + void unref(Value *value); + +} + + +void evaluateSTL(const StatementList &stl); -- cgit v1.2.3-54-g00ecf