diff --git a/cmake/fmt.cmake b/cmake/fmt.cmake index 2e325cc..f3cbe3e 100644 --- a/cmake/fmt.cmake +++ b/cmake/fmt.cmake @@ -2,7 +2,7 @@ include(FetchContent) FetchContent_Declare(fmt GIT_REPOSITORY https://github.com/fmtlib/fmt.git - GIT_TAG 8.1.1 + GIT_TAG 11.0.2 ) FetchContent_MakeAvailable(fmt) diff --git a/source/ast/callable_expression.cpp b/source/ast/callable_expression.cpp index 67da00d..8201256 100644 --- a/source/ast/callable_expression.cpp +++ b/source/ast/callable_expression.cpp @@ -1,6 +1,7 @@ #include "callable_expression.hpp" #include +#include callable_expression::callable_expression(std::vector params) : parameters {std::move(params)} diff --git a/source/ast/function_expression.cpp b/source/ast/function_expression.cpp index c316a43..d3cb82a 100644 --- a/source/ast/function_expression.cpp +++ b/source/ast/function_expression.cpp @@ -1,6 +1,7 @@ #include "function_expression.hpp" #include +#include function_expression::function_expression(std::vector&& parameters, statement* body) : callable_expression(std::move(parameters)) diff --git a/source/ast/hash_literal_expression.cpp b/source/ast/hash_literal_expression.cpp index b3a9704..e51e90b 100644 --- a/source/ast/hash_literal_expression.cpp +++ b/source/ast/hash_literal_expression.cpp @@ -4,6 +4,7 @@ #include "hash_literal_expression.hpp" #include +#include auto hash_literal_expression::string() const -> std::string { diff --git a/source/ast/util.hpp b/source/ast/util.hpp index 2fce9cc..44d94c0 100644 --- a/source/ast/util.hpp +++ b/source/ast/util.hpp @@ -6,6 +6,7 @@ #include #include +#include template auto join(const std::vector& nodes, std::string_view sep = {}) -> std::string diff --git a/source/code/code.hpp b/source/code/code.hpp index d245728..2c22c7a 100644 --- a/source/code/code.hpp +++ b/source/code/code.hpp @@ -46,6 +46,11 @@ enum class opcodes : uint8_t auto operator<<(std::ostream& ostream, opcodes opcode) -> std::ostream&; +template<> +struct fmt::formatter : ostream_formatter +{ +}; + using operands = std::vector; using instructions = std::vector; diff --git a/source/compiler/compiler.cpp b/source/compiler/compiler.cpp index b55f91c..5e76c8c 100644 --- a/source/compiler/compiler.cpp +++ b/source/compiler/compiler.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include "eval/object.hpp" diff --git a/source/compiler/symbol_table.cpp b/source/compiler/symbol_table.cpp index 1970082..c423376 100644 --- a/source/compiler/symbol_table.cpp +++ b/source/compiler/symbol_table.cpp @@ -29,13 +29,18 @@ auto operator<<(std::ostream& ost, symbol_scope scope) -> std::ostream& return ost; } +template<> +struct fmt::formatter : ostream_formatter +{ +}; + auto operator<<(std::ostream& ost, const symbol& sym) -> std::ostream& { return ost << fmt::format("symbol{{{}, {}, {}}}", sym.name, sym.scope, sym.index); } symbol_table::symbol_table(symbol_table* outer) - : m_parent(std::move(outer)) + : m_parent(outer) { } @@ -44,7 +49,7 @@ auto symbol_table::define(const std::string& name) -> symbol using enum symbol_scope; return m_store[name] = symbol { .name = name, - .scope = m_parent ? local : global, + .scope = (m_parent != nullptr) ? local : global, .index = m_defs++, }; } @@ -73,7 +78,7 @@ auto symbol_table::resolve(const std::string& name) -> std::optional if (m_store.contains(name)) { return m_store[name]; } - if (m_parent) { + if (m_parent != nullptr) { auto maybe_symbol = m_parent->resolve(name); if (!maybe_symbol.has_value()) { return maybe_symbol; diff --git a/source/eval/ast_eval.cpp b/source/eval/ast_eval.cpp index 62bc2ec..d06fa08 100644 --- a/source/eval/ast_eval.cpp +++ b/source/eval/ast_eval.cpp @@ -18,7 +18,9 @@ #include #include #include +#include #include +#include #include #include "environment.hpp" @@ -364,7 +366,8 @@ const builtin_function_expression builtin_len { } return make_error("argument of type {} to len() is not supported", maybe_string_or_array->type()); }}; -const builtin_function_expression builtin_puts {{"puts"}, + +const builtin_function_expression builtin_puts {"puts", {"str"}, [](const array_object::array& arguments) -> const object* { diff --git a/source/eval/object.cpp b/source/eval/object.cpp index 9351c73..1ce96d7 100644 --- a/source/eval/object.cpp +++ b/source/eval/object.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include auto operator<<(std::ostream& ostrm, object::object_type type) -> std::ostream& { @@ -57,7 +59,7 @@ auto integer_object::hash_key() const -> hash_key_type auto builtin_object::inspect() const -> std::string { - return fmt::format("builtin {}(){{...}}", builtin->name); + return fmt::format("builtin {}({}){{...}}", builtin->name, fmt::join(builtin->parameters, ", ")); } auto function_object::inspect() const -> std::string @@ -80,8 +82,6 @@ auto array_object::inspect() const -> std::string return strm.str(); } -namespace -{ auto operator<<(std::ostream& strm, const hashable_object::hash_key_type& t) -> std::ostream& { std::visit( @@ -94,8 +94,6 @@ auto operator<<(std::ostream& strm, const hashable_object::hash_key_type& t) -> return strm; } -} // namespace - auto hash_object::inspect() const -> std::string { std::stringstream strm; diff --git a/source/eval/object.hpp b/source/eval/object.hpp index 068a175..a09348d 100644 --- a/source/eval/object.hpp +++ b/source/eval/object.hpp @@ -15,15 +15,6 @@ #include "eval/environment.hpp" -// helper type for std::visit -template -struct overloaded : T... -{ - using T::operator()...; -}; -template -overloaded(T...) -> overloaded; - struct object { enum class object_type : std::uint8_t @@ -71,6 +62,11 @@ struct object auto operator<<(std::ostream& ostrm, object::object_type type) -> std::ostream&; +template<> +struct fmt::formatter : ostream_formatter +{ +}; + struct hashable_object : object { using hash_key_type = std::variant; @@ -80,6 +76,8 @@ struct hashable_object : object [[nodiscard]] virtual auto hash_key() const -> hash_key_type = 0; }; +auto operator<<(std::ostream& strm, const hashable_object::hash_key_type& t) -> std::ostream&; + struct integer_object : hashable_object { explicit integer_object(int64_t val) diff --git a/source/lexer/token_type.hpp b/source/lexer/token_type.hpp index df9676d..1b5b7cd 100644 --- a/source/lexer/token_type.hpp +++ b/source/lexer/token_type.hpp @@ -1,10 +1,11 @@ #pragma once +#include #include #include -enum class token_type +enum class token_type : std::uint8_t { // special tokens illegal, @@ -56,3 +57,8 @@ enum class token_type }; auto operator<<(std::ostream& ostream, token_type type) -> std::ostream&; + +template<> +struct fmt::formatter : ostream_formatter +{ +}; diff --git a/source/overloaded.hpp b/source/overloaded.hpp new file mode 100644 index 0000000..088b9f3 --- /dev/null +++ b/source/overloaded.hpp @@ -0,0 +1,10 @@ +#pragma once + +// helper type for std::visit +template +struct overloaded : T... +{ + using T::operator()...; +}; +template +overloaded(T...) -> overloaded; diff --git a/source/parser/parser.cpp b/source/parser/parser.cpp index f9f0a8b..8ae40ae 100644 --- a/source/parser/parser.cpp +++ b/source/parser/parser.cpp @@ -22,7 +22,9 @@ #include #include #include +#include #include +#include namespace { diff --git a/source/vm/vm.cpp b/source/vm/vm.cpp index 62b73e6..8881a58 100644 --- a/source/vm/vm.cpp +++ b/source/vm/vm.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include vm::vm(frames frames, const constants* consts, constants* globals)