ArkScript
A small, lisp-inspired, functional scripting language
Value.cpp
Go to the documentation of this file.
1#include <Ark/VM/Value.hpp>
3
4#include <fmt/format.h>
5#include <fmt/ostream.h>
6
7namespace Ark
8{
9 Value::Value() noexcept :
10 m_type(ValueType::Undefined)
11 {}
12
13 Value::Value(ValueType type) noexcept :
14 m_type(type)
15 {
16 if (type == ValueType::List)
17 m_value = std::vector<Value>();
18 else if (type == ValueType::String)
19 m_value = std::string();
20 }
21
22 Value::Value(const int value) noexcept :
23 m_type(ValueType::Number), m_value(static_cast<double>(value))
24 {}
25
26 Value::Value(double value) noexcept :
27 m_type(ValueType::Number), m_value(value)
28 {}
29
30 Value::Value(const std::string& value) noexcept :
31 m_type(ValueType::String), m_value(value)
32 {}
33
34 Value::Value(const char* value) noexcept :
35 m_type(ValueType::String), m_value(std::string(value))
36 {}
37
39 m_type(ValueType::PageAddr), m_value(value)
40 {}
41
42 Value::Value(Procedure&& value) noexcept :
43 m_type(ValueType::CProc), m_value(std::move(value))
44 {}
45
46 Value::Value(std::vector<Value>&& value) noexcept :
47 m_type(ValueType::List), m_value(std::move(value))
48 {}
49
50 Value::Value(internal::Closure&& value) noexcept :
51 m_type(ValueType::Closure), m_value(std::move(value))
52 {}
53
54 Value::Value(UserType&& value) noexcept :
55 m_type(ValueType::User), m_value(value)
56 {}
57
58 Value::Value(Value* ref) noexcept :
59 m_type(ValueType::Reference), m_value(ref)
60 {}
61
62 void Value::push_back(const Value& value)
63 {
64 list().emplace_back(value);
65 }
66
67 void Value::push_back(Value&& value)
68 {
69 list().emplace_back(std::move(value));
70 }
71
72 std::string Value::toString(VM& vm) const noexcept
73 {
74 switch (valueType())
75 {
77 return fmt::format("{}", number());
78
80 return string();
81
83 return fmt::format("Function@{}", pageAddr());
84
86 return "CProcedure";
87
88 case ValueType::List:
89 {
90 std::string out = "[";
91 for (auto it = constList().begin(), it_end = constList().end(); it != it_end; ++it)
92 {
93 if (it->valueType() == ValueType::String)
94 out += "\"" + it->toString(vm) + "\"";
95 else
96 out += it->toString(vm);
97 if (it + 1 != it_end)
98 out += " ";
99 }
100 return out + "]";
101 }
102
104 return closure().toString(vm);
105
106 case ValueType::User:
107 return fmt::format("{}", fmt::streamed(usertype()));
108
109 case ValueType::Nil:
110 return "nil";
111
112 case ValueType::True:
113 return "true";
114
115 case ValueType::False:
116 return "false";
117
119 return "undefined";
120
122 if (reference() != this)
123 return reference()->toString(vm);
124 return "Ref(self)";
125
127 return fmt::format("Instruction@{}", pageAddr());
128
129 default:
130 return "~\\._./~";
131 }
132 }
133}
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:47
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
std::vector< Value > & list()
Definition Value.hpp:139
Value() noexcept
Construct a new Value object.
Definition Value.cpp:9
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:62
std::string toString(VM &vm) const noexcept
Definition Value.cpp:72
Closure management.
Definition Closure.hpp:36
uint16_t PageAddr_t
Definition Closure.hpp:27
ValueType
Definition Value.hpp:33