ArkScript
A small, lisp-inspired, functional scripting language
Value.cpp
Go to the documentation of this file.
1#include <Ark/VM/Value.hpp>
4
5#include <fmt/format.h>
6#include <fmt/ostream.h>
7
8namespace Ark
9{
10 Value::Value() noexcept :
11 m_type(ValueType::Undefined)
12 {}
13
14 Value::Value(ValueType type) noexcept :
15 m_type(type)
16 {
17 if (type == ValueType::List)
18 m_value = List_t();
19 else if (type == ValueType::String)
20 m_value = std::string();
21 }
22
23 Value::Value(const int value) noexcept :
24 m_type(ValueType::Number), m_value(static_cast<double>(value))
25 {}
26
27 Value::Value(const double value) noexcept :
28 m_type(ValueType::Number), m_value(value)
29 {}
30
31 Value::Value(const String_t& value) noexcept :
32 m_type(ValueType::String), m_value(value)
33 {}
34
35 Value::Value(const char* value) noexcept :
36 m_type(ValueType::String), m_value(std::string(value))
37 {}
38
40 m_type(ValueType::PageAddr), m_value(value)
41 {}
42
43 Value::Value(Procedure&& value) noexcept :
44 m_type(ValueType::CProc), m_value(std::move(value))
45 {}
46
47 Value::Value(List_t&& value) noexcept :
48 m_type(ValueType::List), m_value(std::move(value))
49 {}
50
51 Value::Value(internal::Closure&& value) noexcept :
52 m_type(ValueType::Closure), m_value(std::move(value))
53 {}
54
55 Value::Value(UserType&& value) noexcept :
56 m_type(ValueType::User), m_value(value)
57 {}
58
59 Value::Value(Dict_t&& value) noexcept :
60 m_type(ValueType::Dict), m_value(std::make_shared<Dict_t>(std::move(value)))
61 {}
62
63 Value::Value(Ref_t ref) noexcept :
64 m_type(ValueType::Reference), m_value(ref)
65 {}
66
67 void Value::push_back(const Value& value)
68 {
69 list().emplace_back(value);
70 }
71
72 void Value::push_back(Value&& value)
73 {
74 list().emplace_back(std::move(value));
75 }
76
77 std::string Value::toString(VM& vm) const noexcept
78 {
79 switch (valueType())
80 {
82 return fmt::format("{}", number());
83
85 return string();
86
88 return fmt::format("Function@{}", pageAddr());
89
91 return "CProcedure";
92
93 case ValueType::List:
94 {
95 std::string out = "[";
96 for (auto it = constList().begin(), it_end = constList().end(); it != it_end; ++it)
97 {
98 if (it->valueType() == ValueType::String)
99 out += "\"" + it->toString(vm) + "\"";
100 else
101 out += it->toString(vm);
102 if (it + 1 != it_end)
103 out += " ";
104 }
105 return out + "]";
106 }
107
109 return closure().toString(vm);
110
111 case ValueType::User:
112 return fmt::format("{}", fmt::streamed(usertype()));
113
114 case ValueType::Dict:
115 return dict().toString(vm);
116
117 case ValueType::Nil:
118 return "nil";
119
120 case ValueType::True:
121 return "true";
122
123 case ValueType::False:
124 return "false";
125
127 return "undefined";
128
130 if (reference() != this)
131 return reference()->toString(vm);
132 return "Ref(self)";
133
135 return fmt::format("Instruction@{}", pageAddr());
136
137 default:
138 return "~\\._./~";
139 }
140 }
141
142 bool operator==(const Value& A, const Value& B) noexcept
143 {
144 // values should have the same type
145 if (A.m_type != B.m_type)
146 return false;
147 // all the types >= Nil are Nil itself, True, False, Undefined
148 if (A.typeNum() >= static_cast<uint8_t>(ValueType::Nil))
149 return true;
150
151 if (A.valueType() == ValueType::Dict)
152 return A.dict() == B.dict();
153
154 return A.m_value == B.m_value;
155 }
156}
Define how dictionaries are handled.
Wrapper object for user-defined functions.
Default value type handled by the virtual machine.
Storage class to hold custom functions.
Definition Procedure.hpp:26
A class to be use C++ objects in ArkScript.
Definition UserType.hpp:48
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
std::string String_t
Definition Value.hpp:94
List_t & list()
Definition Value.hpp:169
Value() noexcept
Construct a new Value object.
Definition Value.cpp:10
void push_back(const Value &value)
Add an element to the list held by the value (if the value type is set to list)
Definition Value.cpp:67
std::string toString(VM &vm) const noexcept
Definition Value.cpp:77
std::vector< Value > List_t
Definition Value.hpp:95
Closure management.
Definition Closure.hpp:35
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21
uint16_t PageAddr_t
Definition Closure.hpp:26
ValueType
Definition Value.hpp:32