Skip to content

Commit f06b299

Browse files
Add function object
1 parent 5d49277 commit f06b299

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/libs/value/FuncObject.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#pragma once
3+
#include "../ast/ast.hpp"
4+
5+
class FuncObj
6+
{
7+
public:
8+
9+
FunctionNode *function_node;
10+
11+
FuncObj(FunctionNode *function) : function_node(function_node) {}
12+
};

src/libs/value/value.hpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <vector>
55
#include <stdexcept>
66
#include <iostream>
7+
#include "../ast/ast.hpp"
8+
#include "./FuncObject.hpp"
79

810
using namespace std;
911

@@ -16,7 +18,8 @@ class Value {
1618
INT, FLOAT,
1719
BOOLEAN,
1820
STRING,
19-
LIST
21+
LIST ,
22+
FUNC
2023
};
2124

2225
Value() : type(ValueType::NONE), data(nullptr) {}
@@ -26,6 +29,7 @@ class Value {
2629
explicit Value(bool v) : type(ValueType::BOOLEAN), data(new bool(v)) {}
2730
explicit Value(const string& v) : type(ValueType::STRING), data(new string(v)) {}
2831
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)) {}
2933

3034
// copy constructor
3135
Value(const Value& other) : type(other.type), data(nullptr) {
@@ -42,6 +46,9 @@ class Value {
4246
case ValueType::LIST:
4347
data = new vector<Value>(*other.getListData());
4448
break;
49+
case ValueType::FUNC:
50+
data = new FuncObj(*other.getFuncData());
51+
break;
4552
default:
4653
break;
4754
}
@@ -64,6 +71,7 @@ class Value {
6471
inline bool is_bool() const { return type == ValueType::BOOLEAN; }
6572
inline bool is_string() const { return type == ValueType::STRING; }
6673
inline bool is_list() const { return type == ValueType::LIST; }
74+
inline bool is_function() const { return type == ValueType::FUNC; }
6775
inline bool is_none() const { return type == ValueType::NONE; }
6876

6977
const long long getInt() const {
@@ -97,6 +105,16 @@ class Value {
97105
return *getListData();
98106
}
99107

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+
100118
// Copy assignment operator
101119
Value& operator=(const Value& other) {
102120
if(this != &other) {
@@ -119,6 +137,10 @@ class Value {
119137
case ValueType::LIST:
120138
data = new vector<Value>(*other.getListData());
121139
break;
140+
case ValueType::FUNC:
141+
data = new FuncObj(*other.getFuncData());
142+
break;
143+
122144
default:
123145
break;
124146
}
@@ -651,6 +673,10 @@ class Value {
651673
const vector<Value>* getListData() const {
652674
return static_cast<vector<Value>*>(data);
653675
}
676+
const FuncObj* getFuncData() const {
677+
return static_cast<FuncObj*>(data);
678+
}
679+
654680

655681
void deleteData() {
656682
if(data != nullptr) {
@@ -670,6 +696,11 @@ class Value {
670696
case ValueType::LIST:
671697
delete getListData();
672698
break;
699+
case ValueType::FUNC:
700+
delete getFuncData();
701+
702+
break;
703+
673704
default:
674705
break;
675706
}

0 commit comments

Comments
 (0)