ArkScript
A small, lisp-inspired, functional scripting language
Dict.cpp
Go to the documentation of this file.
2
4
5#include <ranges>
6
7namespace Ark::internal
8{
9 void Dict::set(const Value& key, const Value& value)
10 {
11 m_dict.insert_or_assign(key, value);
12 }
13
14 const Value& Dict::get(const Value& key)
15 {
16 if (const auto it = m_dict.find(key); it != m_dict.end())
17 return it->second;
18 return Nil;
19 }
20
21 bool Dict::contains(const Value& key) const
22 {
23 return m_dict.contains(key);
24 }
25
26 void Dict::remove(const Value& key)
27 {
28 m_dict.erase(key);
29 }
30
31 std::vector<Value> Dict::keys()
32 {
33 std::vector<Value> keys;
34 keys.reserve(m_dict.size());
35 std::ranges::copy(std::ranges::views::keys(m_dict), std::back_inserter(keys));
36
37 return keys;
38 }
39
40 std::size_t Dict::size() const
41 {
42 return m_dict.size();
43 }
44
45 std::string Dict::toString(VM& vm) const
46 {
47 std::string out = "{";
48
49 std::size_t i = 0;
50 for (const auto& [key, value] : m_dict)
51 {
52 out += key.toString(vm) + ": " + value.toString(vm);
53
54 if (i + 1 != m_dict.size())
55 out += ", ";
56 ++i;
57 }
58
59 return out + "}";
60 }
61}
Define how dictionaries are handled.
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
ankerl::unordered_dense::map< Value, Value > m_dict
Definition Dict.hpp:90
std::vector< Value > keys()
Get a list of the dict keys.
Definition Dict.cpp:31
std::string toString(VM &vm) const
Convert the dictionary to a string for pretty printing.
Definition Dict.cpp:45
bool contains(const Value &key) const
Check that a key exists.
Definition Dict.cpp:21
void set(const Value &key, const Value &value)
Assign a key to a value inside the dict.
Definition Dict.cpp:9
void remove(const Value &key)
Remove an entry from the dict via its key.
Definition Dict.cpp:26
std::size_t size() const
Compute the number of (key, value) pairs in the dict.
Definition Dict.cpp:40
const Value & get(const Value &key)
Try to get a value from a given key. If no value is found, return Nil.
Definition Dict.cpp:14
const auto Nil
ArkScript Nil value.