made our wrapper objects for all of our primitve
datatypes: ints, bools and null
This commit is contained in:
9
src/object/boolean.cpp
Normal file
9
src/object/boolean.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "boolean.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace object {
|
||||
std::string boolean::inspect() {
|
||||
return std::to_string(value);
|
||||
}
|
||||
} // namespace object
|
11
src/object/boolean.hpp
Normal file
11
src/object/boolean.hpp
Normal file
@@ -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
|
9
src/object/integers.cpp
Normal file
9
src/object/integers.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "integers.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace object {
|
||||
std::string integer::inspect() {
|
||||
return std::to_string(value);
|
||||
}
|
||||
} // namespace object
|
11
src/object/integers.hpp
Normal file
11
src/object/integers.hpp
Normal file
@@ -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
|
7
src/object/null.cpp
Normal file
7
src/object/null.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "null.hpp"
|
||||
|
||||
namespace object {
|
||||
std::string null::inspect() {
|
||||
return "null";
|
||||
}
|
||||
} // namespace object
|
10
src/object/null.hpp
Normal file
10
src/object/null.hpp
Normal file
@@ -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
|
13
src/object/object.hpp
Normal file
13
src/object/object.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "type.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace object {
|
||||
struct object {
|
||||
::object::type type;
|
||||
virtual std::string inspect() = 0;
|
||||
};
|
||||
|
||||
} // namespace object
|
23
src/object/type.cpp
Normal file
23
src/object/type.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "type.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace object {
|
||||
|
||||
// Array mapping enum values to their string representations
|
||||
constexpr std::
|
||||
array<std::string_view, static_cast<size_t>(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<size_t>(type);
|
||||
if (idx < tokenTypeStrings.size())
|
||||
return os << tokenTypeStrings[idx];
|
||||
return os << "Unknown";
|
||||
}
|
||||
} // namespace object
|
21
src/object/type.hpp
Normal file
21
src/object/type.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
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
|
Reference in New Issue
Block a user