From 0b533ce8809190498bf6d9094734f904a9c5dfd7 Mon Sep 17 00:00:00 2001 From: Vasilii Rogin Date: Wed, 19 Apr 2023 23:13:02 +0300 Subject: [PATCH] Some steps with arrays --- src/sfall_arrays.cc | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/sfall_arrays.h | 0 2 files changed, 82 insertions(+) create mode 100644 src/sfall_arrays.cc create mode 100644 src/sfall_arrays.h diff --git a/src/sfall_arrays.cc b/src/sfall_arrays.cc new file mode 100644 index 0000000..f7529be --- /dev/null +++ b/src/sfall_arrays.cc @@ -0,0 +1,82 @@ +#include "interpreter.h" +#include +#include +#include +#include +#include + +namespace fallout { + +using ArrayId = unsigned int; + +static ArrayId nextArrayID = 1; +static ArrayId stackArrayId = 1; + +using ArraysMap = std::unordered_map; + +ArraysMap arrays; +std::unordered_set temporaryArrays; + +class SFallArrayElement : public ProgramValue { +public: + SFallArrayElement() + { + opcode = VALUE_TYPE_INT; + integerValue = 0; + } +}; + +#define ARRAYFLAG_ASSOC (1) // is map +#define ARRAYFLAG_CONSTVAL (2) // don't update value of key if the key exists in map +#define ARRAYFLAG_RESERVED (4) + +#define ARRAY_MAX_STRING (255) // maximum length of string to be stored as array key or value +#define ARRAY_MAX_SIZE (100000) // maximum number of array elements, + +class SFallArray { +private: +public: + uint32_t flags; + + SFallArray() = delete; + + SFallArray(unsigned int len, uint32_t flags) + : flags(flags) + { + data.resize(len); + } + std::vector data; +}; + +ArrayId CreateArray(int len, uint32_t flags) +{ + flags = (flags & ~1); // reset 1 bit + + if (len < 0) { + flags |= ARRAYFLAG_ASSOC; + throw(std::invalid_argument("Not implemented yet")); + }; + + if (len > ARRAY_MAX_SIZE) { + len = ARRAY_MAX_SIZE; // safecheck + } + + SFallArray arr { len, flags }; + + ArrayId array_id = nextArrayID++; + + stackArrayId = array_id; + + arrays[array_id] = std::move(arr); + + return array_id; +} + +ArrayId CreateTempArray(int len, uint32_t flags) +{ + ArrayId array_id = CreateArray(len, flags); + temporaryArrays.insert(array_id); + return array_id; +} + +} \ No newline at end of file diff --git a/src/sfall_arrays.h b/src/sfall_arrays.h new file mode 100644 index 0000000..e69de29