Skip to content

Commit

Permalink
feat: support puts() on array and hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm committed Dec 14, 2024
1 parent 2d0464e commit de5e0c4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion source/compiler/ast_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ auto identifier::compile(compiler& comp) const -> void
if (!maybe_symbol.has_value()) {
throw std::runtime_error(fmt::format("undefined variable {}", value));
}
auto symbol = maybe_symbol.value();
const auto& symbol = maybe_symbol.value();
comp.load_symbol(symbol);
}

Expand Down
2 changes: 1 addition & 1 deletion source/eval/ast_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ auto array_expression::eval(environment* env) const -> const object*
{
array_object::array arr;
for (const auto& element : elements) {
auto* evaluated = element->eval(env);
const auto* evaluated = element->eval(env);
if (evaluated->is_error()) {
return evaluated;
}
Expand Down
49 changes: 49 additions & 0 deletions source/eval/object.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <sstream>
#include <string>

#include "object.hpp"

#include <ast/builtin_function_expression.hpp>
#include <ast/callable_expression.hpp>
#include <fmt/format.h>
#include <fmt/ostream.h>

auto operator<<(std::ostream& ostrm, object::object_type type) -> std::ostream&
{
Expand Down Expand Up @@ -62,6 +64,53 @@ auto function_object::inspect() const -> std::string
return callable->string();
}

auto array_object::inspect() const -> std::string
{
std::stringstream str;
str << "[";
for (bool first = true; const auto* const element : elements) {
if (!first) {
str << ", ";
}
str << fmt::format("{}", element->inspect());
first = false;
}
str << "]";
return str.str();
}

namespace
{
auto operator<<(std::ostream& strm, const hashable_object::hash_key_type& t) -> std::ostream&
{
std::visit(
overloaded {
[&](int64_t val) { strm << val; },
[&](const std::string& val) { strm << '"' << val << '"'; },
[&](bool val) { strm << val; },
},
t);
return strm;
}

} // namespace

auto hash_object::inspect() const -> std::string
{
std::stringstream str;
str << "{";
for (bool first = true; const auto& [key, value] : pairs) {
if (!first) {
str << ", ";
}
str << key;
str << ": " << value->inspect();
first = false;
}
str << "}";
return str.str();
}

namespace
{
const boolean_object false_obj {/*val=*/false};
Expand Down
4 changes: 2 additions & 2 deletions source/eval/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct array_object : object

[[nodiscard]] auto type() const -> object_type override { return object_type::array; }

[[nodiscard]] auto inspect() const -> std::string override { return "todo"; }
[[nodiscard]] auto inspect() const -> std::string override;

[[nodiscard]] auto equals_to(const object* other) const -> bool override
{
Expand Down Expand Up @@ -234,7 +234,7 @@ struct hash_object : object

[[nodiscard]] auto type() const -> object_type override { return object_type::hash; }

[[nodiscard]] auto inspect() const -> std::string override { return "todo"; }
[[nodiscard]] auto inspect() const -> std::string override;

[[nodiscard]] auto equals_to(const object* other) const -> bool override
{
Expand Down
4 changes: 2 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ auto run_repl(const command_line_args& opts) -> int
auto machine = vm::create_with_state(cmplr.byte_code(), &globals);
machine.run();
const auto* result = machine.last_popped();
if (!result->is(object::object_type::null)) {
if (result != nullptr && !result->is(object::object_type::null)) {
std::cout << result->inspect() << "\n";
}
} else {
const auto* result = prgrm->eval(global_env);
if (!result->is(object::object_type::null)) {
if (result != nullptr && !result->is(object::object_type::null)) {
std::cout << result->inspect() << "\n";
}
}
Expand Down

0 comments on commit de5e0c4

Please sign in to comment.