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