summaryrefslogtreecommitdiff
path: root/evaluate.cpp
blob: ebfb7735c083aadcce5ad430c8a4f705182d8e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdexcept>
#include <cassert>
#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(ScopeVal *scope)
	:type(Type::scope),scope(scope){}


namespace A {
	struct CollItem{
		int count;
		void (*deleter)(void*);
	};

	template <typename T>
	static void generic_deleter(void *p){
		delete (T*)p;
	}

	unordered_map<void*,CollItem> collection;

	template <typename T>
	T* ref(T *p){
		auto it=collection.find((void*)p);
		if(it==collection.end()){
			collection.emplace((void*)p,(CollItem){1,generic_deleter<T>});
		} else {
			it->second.count++;
		}
		return p;
	}

	template <typename T>
	void unref(T *p){
		auto it=collection.find(p);
		assert(it!=collection.end());
		if(it->second.count==1){
			if(it->second.deleter!=generic_deleter<T>){
				throw runtime_error("Unref'd pointer type not the same as the ref'd version!");
			}
			it->second.deleter(p);
			collection.erase(it);
		} else {
			it->second.count--;
		}
	}
}


void evaluateSTL(const StatementList &stl){
	;
}