From d1dd5f9dab25ba2e71c1de976042aa991652ca8a Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sat, 19 Jul 2025 13:04:30 +0200 Subject: [PATCH] made our wrapper objects for all of our primitve datatypes: ints, bools and null --- src/object/boolean.cpp | 9 +++++++++ src/object/boolean.hpp | 11 +++++++++++ src/object/integers.cpp | 9 +++++++++ src/object/integers.hpp | 11 +++++++++++ src/object/null.cpp | 7 +++++++ src/object/null.hpp | 10 ++++++++++ src/object/object.hpp | 13 +++++++++++++ src/object/type.cpp | 23 +++++++++++++++++++++++ src/object/type.hpp | 21 +++++++++++++++++++++ 9 files changed, 114 insertions(+) create mode 100644 src/object/boolean.cpp create mode 100644 src/object/boolean.hpp create mode 100644 src/object/integers.cpp create mode 100644 src/object/integers.hpp create mode 100644 src/object/null.cpp create mode 100644 src/object/null.hpp create mode 100644 src/object/object.hpp create mode 100644 src/object/type.cpp create mode 100644 src/object/type.hpp diff --git a/src/object/boolean.cpp b/src/object/boolean.cpp new file mode 100644 index 0000000..c798089 --- /dev/null +++ b/src/object/boolean.cpp @@ -0,0 +1,9 @@ +#include "boolean.hpp" + +#include + +namespace object { + std::string boolean::inspect() { + return std::to_string(value); + } +} // namespace object diff --git a/src/object/boolean.hpp b/src/object/boolean.hpp new file mode 100644 index 0000000..0ba3e06 --- /dev/null +++ b/src/object/boolean.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "object.hpp" + +namespace object { + struct boolean : object { + bool value; + ::object::type type = type::BOOLEAN_OBJ; + + std::string inspect(); + }; +} // namespace object diff --git a/src/object/integers.cpp b/src/object/integers.cpp new file mode 100644 index 0000000..af493d0 --- /dev/null +++ b/src/object/integers.cpp @@ -0,0 +1,9 @@ +#include "integers.hpp" + +#include + +namespace object { + std::string integer::inspect() { + return std::to_string(value); + } +} // namespace object diff --git a/src/object/integers.hpp b/src/object/integers.hpp new file mode 100644 index 0000000..e2de585 --- /dev/null +++ b/src/object/integers.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "object.hpp" + +namespace object { + struct integer : object { + int value; + ::object::type type = type::INTEGER_OBJ; + + std::string inspect(); + }; +} // namespace object diff --git a/src/object/null.cpp b/src/object/null.cpp new file mode 100644 index 0000000..030a3b1 --- /dev/null +++ b/src/object/null.cpp @@ -0,0 +1,7 @@ +#include "null.hpp" + +namespace object { + std::string null::inspect() { + return "null"; + } +} // namespace object diff --git a/src/object/null.hpp b/src/object/null.hpp new file mode 100644 index 0000000..76ff72b --- /dev/null +++ b/src/object/null.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "object.hpp" + +namespace object { + struct null : object { + ::object::type type = type::NULL_OBJ; + + std::string inspect(); + }; +} // namespace object diff --git a/src/object/object.hpp b/src/object/object.hpp new file mode 100644 index 0000000..953fbeb --- /dev/null +++ b/src/object/object.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "type.hpp" + +#include + +namespace object { + struct object { + ::object::type type; + virtual std::string inspect() = 0; + }; + +} // namespace object diff --git a/src/object/type.cpp b/src/object/type.cpp new file mode 100644 index 0000000..47dd0a9 --- /dev/null +++ b/src/object/type.cpp @@ -0,0 +1,23 @@ +#include "type.hpp" + +#include + +namespace object { + + // Array mapping enum values to their string representations + constexpr std:: + array(type::BOOLEAN_OBJ) + 1> + tokenTypeStrings = { +#define X(name, str) str, + OBJECT_LIST +#undef X + }; + + // Stream insertion operator using the lookup array + std::ostream& operator<<(std::ostream& os, type type) { + auto idx = static_cast(type); + if (idx < tokenTypeStrings.size()) + return os << tokenTypeStrings[idx]; + return os << "Unknown"; + } +} // namespace object diff --git a/src/object/type.hpp b/src/object/type.hpp new file mode 100644 index 0000000..63038fb --- /dev/null +++ b/src/object/type.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +namespace object { + +// X-macro list of token types and their string representations +#define OBJECT_LIST \ + X(NULL_OBJ, "NULL") \ + X(INTEGER_OBJ, "INTEGER") \ + X(BOOLEAN_OBJ, "BOOLEAN") + + // Define the TokenType enum using the X-macro + enum class type { +#define X(name, str) name, + OBJECT_LIST +#undef X + }; + + std::ostream& operator<<(std::ostream&, type); +} // namespace object