4
4
#include < vector>
5
5
#include < stdexcept>
6
6
#include < iostream>
7
+ #include " ../ast/ast.hpp"
8
+ #include " ./FuncObject.hpp"
7
9
8
10
using namespace std ;
9
11
@@ -16,7 +18,8 @@ class Value {
16
18
INT, FLOAT,
17
19
BOOLEAN,
18
20
STRING,
19
- LIST
21
+ LIST ,
22
+ FUNC
20
23
};
21
24
22
25
Value () : type(ValueType::NONE), data(nullptr ) {}
@@ -26,6 +29,7 @@ class Value {
26
29
explicit Value (bool v) : type(ValueType::BOOLEAN), data(new bool (v)) {}
27
30
explicit Value (const string& v) : type(ValueType::STRING), data(new string(v)) {}
28
31
explicit Value (const vector<Value>& v) : type(ValueType::LIST), data(new vector<Value>(v)) {}
32
+ explicit Value (FunctionNode * func) : type(ValueType::INT), data(new FuncObj(func)) {}
29
33
30
34
// copy constructor
31
35
Value (const Value& other) : type(other.type), data(nullptr ) {
@@ -42,6 +46,9 @@ class Value {
42
46
case ValueType::LIST:
43
47
data = new vector<Value>(*other.getListData ());
44
48
break ;
49
+ case ValueType::FUNC:
50
+ data = new FuncObj (*other.getFuncData ());
51
+ break ;
45
52
default :
46
53
break ;
47
54
}
@@ -64,6 +71,7 @@ class Value {
64
71
inline bool is_bool () const { return type == ValueType::BOOLEAN; }
65
72
inline bool is_string () const { return type == ValueType::STRING; }
66
73
inline bool is_list () const { return type == ValueType::LIST; }
74
+ inline bool is_function () const { return type == ValueType::FUNC; }
67
75
inline bool is_none () const { return type == ValueType::NONE; }
68
76
69
77
const long long getInt () const {
@@ -97,6 +105,16 @@ class Value {
97
105
return *getListData ();
98
106
}
99
107
108
+ const FuncObj& getFunc () const {
109
+
110
+ if (not is_function ()) {
111
+ throw runtime_error (" Value is not a Function" );
112
+ }
113
+ return *getFuncData ();
114
+
115
+
116
+ }
117
+
100
118
// Copy assignment operator
101
119
Value& operator =(const Value& other) {
102
120
if (this != &other) {
@@ -119,6 +137,10 @@ class Value {
119
137
case ValueType::LIST:
120
138
data = new vector<Value>(*other.getListData ());
121
139
break ;
140
+ case ValueType::FUNC:
141
+ data = new FuncObj (*other.getFuncData ());
142
+ break ;
143
+
122
144
default :
123
145
break ;
124
146
}
@@ -651,6 +673,10 @@ class Value {
651
673
const vector<Value>* getListData () const {
652
674
return static_cast <vector<Value>*>(data);
653
675
}
676
+ const FuncObj* getFuncData () const {
677
+ return static_cast <FuncObj*>(data);
678
+ }
679
+
654
680
655
681
void deleteData () {
656
682
if (data != nullptr ) {
@@ -670,6 +696,11 @@ class Value {
670
696
case ValueType::LIST:
671
697
delete getListData ();
672
698
break ;
699
+ case ValueType::FUNC:
700
+ delete getFuncData ();
701
+
702
+ break ;
703
+
673
704
default :
674
705
break ;
675
706
}
0 commit comments