2022-08-18 22:48:11 +02:00
|
|
|
#ifndef Musique_Header_HH
|
|
|
|
#define Musique_Header_HH
|
2022-04-24 15:27:09 +02:00
|
|
|
|
2022-05-24 02:58:15 +02:00
|
|
|
#include <chrono>
|
2022-04-24 15:27:09 +02:00
|
|
|
#include <cstdint>
|
2022-05-02 20:06:40 +02:00
|
|
|
#include <cstring>
|
2022-08-21 15:52:54 +02:00
|
|
|
#include <functional>
|
2022-09-23 13:18:55 +02:00
|
|
|
#include <iostream>
|
2022-05-21 23:54:21 +02:00
|
|
|
#include <memory>
|
2022-05-02 14:50:04 +02:00
|
|
|
#include <optional>
|
2022-05-07 20:52:09 +02:00
|
|
|
#include <span>
|
2022-04-27 14:58:02 +02:00
|
|
|
#include <string_view>
|
2022-05-02 14:50:04 +02:00
|
|
|
#include <variant>
|
2022-04-24 15:27:09 +02:00
|
|
|
|
2022-05-24 03:35:15 +02:00
|
|
|
#include <midi.hh>
|
|
|
|
#include <tl/expected.hpp>
|
|
|
|
|
2022-05-08 00:03:22 +02:00
|
|
|
#if defined(__cpp_lib_source_location)
|
|
|
|
#include <source_location>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// To make sure, that we don't collide with <cassert> macro
|
|
|
|
#ifdef assert
|
|
|
|
#undef assert
|
|
|
|
#endif
|
|
|
|
|
2022-05-17 16:10:56 +02:00
|
|
|
using namespace std::string_literals;
|
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
2022-04-24 15:27:09 +02:00
|
|
|
using u8 = std::uint8_t;
|
|
|
|
using u16 = std::uint16_t;
|
|
|
|
using u32 = std::uint32_t;
|
|
|
|
using u64 = std::uint64_t;
|
|
|
|
|
|
|
|
using i8 = std::int8_t;
|
|
|
|
using i16 = std::int16_t;
|
|
|
|
using i32 = std::int32_t;
|
|
|
|
using i64 = std::int64_t;
|
|
|
|
|
2022-04-27 13:48:50 +02:00
|
|
|
using usize = std::size_t;
|
|
|
|
using isize = std::ptrdiff_t;
|
2022-04-24 16:09:55 +02:00
|
|
|
|
2022-08-21 20:01:59 +02:00
|
|
|
/// \brief Location describes code position in `file line column` format.
|
|
|
|
/// It's used both to represent position in source files provided
|
|
|
|
// to interpreter and internal interpreter usage.
|
|
|
|
struct Location
|
|
|
|
{
|
|
|
|
std::string_view filename = "<unnamed>"; ///< File that location is pointing to
|
|
|
|
usize line = 1; ///< Line number (1 based) that location is pointing to
|
|
|
|
usize column = 1; ///< Column number (1 based) that location is pointing to
|
|
|
|
|
|
|
|
/// Advances line and column numbers based on provided rune
|
|
|
|
///
|
|
|
|
/// If rune is newline, then column is reset to 1, and line number is incremented.
|
|
|
|
/// Otherwise column number is incremented.
|
|
|
|
///
|
|
|
|
/// @param rune Rune from which column and line numbers advancements are made.
|
|
|
|
Location& advance(u32 rune);
|
|
|
|
|
|
|
|
bool operator==(Location const& rhs) const = default;
|
|
|
|
|
|
|
|
//! Creates location at default filename with specified line and column number
|
|
|
|
static Location at(usize line, usize column);
|
|
|
|
|
|
|
|
// Used to describe location of function call in interpreter (internal use only)
|
|
|
|
#if defined(__cpp_lib_source_location)
|
|
|
|
static Location caller(std::source_location loc = std::source_location::current());
|
|
|
|
#elif (__has_builtin(__builtin_FILE) and __has_builtin(__builtin_LINE))
|
|
|
|
static Location caller(char const* file = __builtin_FILE(), usize line = __builtin_LINE());
|
|
|
|
#else
|
|
|
|
#error Cannot implement Location::caller function
|
|
|
|
/// Returns location of call in interpreter source code.
|
|
|
|
///
|
|
|
|
/// Example of reporting where `foo()` was beeing called:
|
|
|
|
/// @code
|
|
|
|
/// void foo(Location loc = Location::caller()) { std::cout << loc << '\n'; }
|
|
|
|
/// @endcode
|
|
|
|
static Location caller();
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, Location const& location);
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Error handling related functions and definitions
|
2022-04-24 16:09:55 +02:00
|
|
|
namespace errors
|
|
|
|
{
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When user puts emoji in the source code
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Unrecognized_Character
|
2022-04-24 16:09:55 +02:00
|
|
|
{
|
2022-05-29 22:39:46 +02:00
|
|
|
u32 invalid_character;
|
|
|
|
};
|
2022-05-07 20:52:09 +02:00
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When parser was expecting code but encountered end of file
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Unexpected_Empty_Source
|
|
|
|
{
|
2022-08-21 20:01:59 +02:00
|
|
|
enum { Block_Without_Closing_Bracket } reason;
|
|
|
|
std::optional<Location> start;
|
2022-05-29 22:39:46 +02:00
|
|
|
};
|
2022-05-16 02:18:53 +02:00
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When user passed numeric literal too big for numeric type
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Failed_Numeric_Parsing
|
|
|
|
{
|
|
|
|
std::errc reason;
|
|
|
|
};
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When user forgot semicolon or brackets
|
2022-05-29 23:54:05 +02:00
|
|
|
struct Expected_Expression_Separator_Before
|
|
|
|
{
|
|
|
|
std::string_view what;
|
|
|
|
};
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When some keywords are not allowed in given context
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Unexpected_Keyword
|
|
|
|
{
|
|
|
|
std::string_view keyword;
|
|
|
|
};
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When user tried to use operator that was not defined
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Undefined_Operator
|
|
|
|
{
|
|
|
|
std::string_view op;
|
|
|
|
};
|
|
|
|
|
2022-09-21 13:36:58 +02:00
|
|
|
/// When user tries to use operator with wrong arity of arguments
|
|
|
|
struct Wrong_Arity_Of
|
|
|
|
{
|
|
|
|
/// Type of operation
|
|
|
|
enum Type { Operator, Function } type;
|
|
|
|
|
|
|
|
/// Name of operation
|
|
|
|
std::string_view name;
|
|
|
|
|
|
|
|
/// Arity that was expected by given operation
|
|
|
|
size_t expected_arity;
|
|
|
|
|
|
|
|
/// Arit that user provided
|
|
|
|
size_t actual_arity;
|
|
|
|
};
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When user tried to call something that can't be called
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Not_Callable
|
|
|
|
{
|
|
|
|
std::string_view type;
|
|
|
|
};
|
|
|
|
|
2022-06-14 13:24:15 +02:00
|
|
|
/// When user provides literal where identifier should be
|
2022-06-02 21:39:22 +02:00
|
|
|
struct Literal_As_Identifier
|
|
|
|
{
|
|
|
|
std::string_view type_name;
|
|
|
|
std::string_view source;
|
|
|
|
std::string_view context;
|
|
|
|
};
|
|
|
|
|
2022-06-14 13:24:15 +02:00
|
|
|
/// When user provides wrong type for given operation
|
|
|
|
struct Unsupported_Types_For
|
|
|
|
{
|
|
|
|
/// Type of operation
|
2022-09-04 17:46:04 +02:00
|
|
|
enum Type { Operator, Function } type;
|
2022-06-14 13:24:15 +02:00
|
|
|
|
|
|
|
/// Name of operation
|
|
|
|
std::string_view name;
|
|
|
|
|
|
|
|
/// Possible ways to use it correctly
|
|
|
|
std::vector<std::string> possibilities;
|
|
|
|
};
|
|
|
|
|
2022-08-18 20:38:21 +02:00
|
|
|
/// When user tries to use variable that has not been defined yet.
|
|
|
|
struct Missing_Variable
|
|
|
|
{
|
|
|
|
/// Name of variable
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
2022-08-19 16:06:33 +02:00
|
|
|
/// When user tries to invoke some MIDI action but haven't established MIDI connection
|
|
|
|
struct Operation_Requires_Midi_Connection
|
|
|
|
{
|
|
|
|
/// If its input or output connection missing
|
|
|
|
bool is_input;
|
|
|
|
|
|
|
|
/// Name of the operation that was beeing invoked
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
2022-09-04 13:16:40 +02:00
|
|
|
/// When user tries to get element from collection with index higher then collection size
|
|
|
|
struct Out_Of_Range
|
|
|
|
{
|
|
|
|
/// Index that was required by the user
|
|
|
|
size_t required_index;
|
|
|
|
|
|
|
|
/// Size of accessed collection
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2022-09-04 13:48:06 +02:00
|
|
|
struct Closing_Token_Without_Opening
|
|
|
|
{
|
|
|
|
enum {
|
|
|
|
Block = ']',
|
|
|
|
Paren = ')'
|
|
|
|
} type;
|
|
|
|
};
|
|
|
|
|
2022-09-04 19:44:33 +02:00
|
|
|
struct Arithmetic
|
|
|
|
{
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
Division_By_Zero,
|
|
|
|
Fractional_Modulo,
|
|
|
|
Unable_To_Calculate_Modular_Multiplicative_Inverse
|
|
|
|
} type;
|
|
|
|
};
|
|
|
|
|
2022-05-29 22:39:46 +02:00
|
|
|
/// Collection of messages that are considered internal and should not be printed to the end user.
|
|
|
|
namespace internal
|
|
|
|
{
|
2022-05-30 00:18:48 +02:00
|
|
|
/// When encountered token that was supposed to be matched in higher branch of the parser
|
2022-05-29 22:39:46 +02:00
|
|
|
struct Unexpected_Token
|
|
|
|
{
|
|
|
|
/// Type of the token
|
|
|
|
std::string_view type;
|
|
|
|
|
|
|
|
/// Source of the token
|
|
|
|
std::string_view source;
|
|
|
|
|
|
|
|
/// Where this token was encountered that was unexpected?
|
|
|
|
std::string_view when;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// All possible error types
|
2022-05-29 22:39:46 +02:00
|
|
|
using Details = std::variant<
|
2022-09-04 19:44:33 +02:00
|
|
|
Arithmetic,
|
2022-09-04 13:48:06 +02:00
|
|
|
Closing_Token_Without_Opening,
|
2022-05-29 23:54:05 +02:00
|
|
|
Expected_Expression_Separator_Before,
|
2022-05-29 22:39:46 +02:00
|
|
|
Failed_Numeric_Parsing,
|
2022-06-14 13:24:15 +02:00
|
|
|
Literal_As_Identifier,
|
2022-08-18 20:38:21 +02:00
|
|
|
Missing_Variable,
|
2022-05-29 22:39:46 +02:00
|
|
|
Not_Callable,
|
2022-08-19 16:06:33 +02:00
|
|
|
Operation_Requires_Midi_Connection,
|
2022-09-04 13:16:40 +02:00
|
|
|
Out_Of_Range,
|
2022-05-29 22:39:46 +02:00
|
|
|
Undefined_Operator,
|
|
|
|
Unexpected_Empty_Source,
|
|
|
|
Unexpected_Keyword,
|
|
|
|
Unrecognized_Character,
|
2022-06-14 13:24:15 +02:00
|
|
|
Unsupported_Types_For,
|
2022-09-21 13:36:58 +02:00
|
|
|
Wrong_Arity_Of,
|
2022-05-29 22:39:46 +02:00
|
|
|
internal::Unexpected_Token
|
|
|
|
>;
|
2022-04-24 16:09:55 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// All code related to pretty printing. Default mode is no_color
|
2022-05-29 23:34:20 +02:00
|
|
|
namespace pretty
|
|
|
|
{
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Mark start of printing an error
|
2022-05-29 23:34:20 +02:00
|
|
|
std::ostream& begin_error(std::ostream&);
|
2022-05-30 00:18:48 +02:00
|
|
|
|
|
|
|
/// Mark start of printing a path
|
2022-05-29 23:34:20 +02:00
|
|
|
std::ostream& begin_path(std::ostream&);
|
2022-05-30 00:18:48 +02:00
|
|
|
|
|
|
|
/// Mark start of printing a comment
|
2022-05-29 23:34:20 +02:00
|
|
|
std::ostream& begin_comment(std::ostream&);
|
2022-05-30 00:18:48 +02:00
|
|
|
|
|
|
|
/// Mark end of any above
|
2022-05-29 23:34:20 +02:00
|
|
|
std::ostream& end(std::ostream&);
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Switch to colorful output via ANSI escape sequences
|
2022-05-29 23:34:20 +02:00
|
|
|
void terminal_mode();
|
2022-05-30 00:18:48 +02:00
|
|
|
|
|
|
|
/// Switch to colorless output (default one)
|
2022-05-29 23:34:20 +02:00
|
|
|
void no_color_mode();
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Combine several lambdas into one for visiting std::variant
|
2022-05-29 22:39:46 +02:00
|
|
|
template<typename ...Lambdas>
|
|
|
|
struct Overloaded : Lambdas... { using Lambdas::operator()...; };
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Guards that program exits if condition does not hold
|
2022-05-08 00:03:22 +02:00
|
|
|
void assert(bool condition, std::string message, Location loc = Location::caller());
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Marks part of code that was not implemented yet
|
2022-05-24 00:48:15 +02:00
|
|
|
[[noreturn]] void unimplemented(std::string_view message = {}, Location loc = Location::caller());
|
2022-05-08 00:03:22 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Marks location that should not be reached
|
2022-05-08 00:03:22 +02:00
|
|
|
[[noreturn]] void unreachable(Location loc = Location::caller());
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Represents all recoverable error messages that interpreter can produce
|
2022-04-24 15:27:09 +02:00
|
|
|
struct Error
|
|
|
|
{
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Specific message details
|
2022-05-29 22:39:46 +02:00
|
|
|
errors::Details details;
|
2022-05-30 00:18:48 +02:00
|
|
|
|
|
|
|
/// Location that coused all this trouble
|
2022-05-02 14:50:04 +02:00
|
|
|
std::optional<Location> location = std::nullopt;
|
2022-04-24 16:09:55 +02:00
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Return self with new location
|
2022-05-07 20:52:09 +02:00
|
|
|
Error with(Location) &&;
|
2022-04-24 15:27:09 +02:00
|
|
|
};
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Error pretty printing
|
2022-05-08 00:03:22 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Error const& err);
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Returns if provided thingy is a given template
|
2022-05-17 02:35:51 +02:00
|
|
|
template<template<typename ...> typename Template, typename>
|
|
|
|
struct is_template : std::false_type {};
|
|
|
|
|
|
|
|
template<template<typename ...> typename Template, typename ...T>
|
|
|
|
struct is_template<Template, Template<T...>> : std::true_type {};
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Returns if provided thingy is a given template
|
2022-05-17 02:35:51 +02:00
|
|
|
template<template<typename ...> typename Template, typename T>
|
|
|
|
constexpr auto is_template_v = is_template<Template, T>::value;
|
|
|
|
|
2022-05-30 00:18:48 +02:00
|
|
|
/// Holds either T or Error
|
2022-04-24 15:27:09 +02:00
|
|
|
template<typename T>
|
2022-05-08 15:57:31 +02:00
|
|
|
struct [[nodiscard("This value may contain critical error, so it should NOT be ignored")]] Result : tl::expected<T, Error>
|
2022-04-27 13:48:50 +02:00
|
|
|
{
|
2022-05-02 15:13:12 +02:00
|
|
|
using Storage = tl::expected<T, Error>;
|
|
|
|
|
2022-05-08 00:03:22 +02:00
|
|
|
constexpr Result() = default;
|
2022-04-27 13:48:50 +02:00
|
|
|
|
2022-05-08 00:03:22 +02:00
|
|
|
template<typename ...Args> requires (not std::is_void_v<T>) && std::is_constructible_v<T, Args...>
|
|
|
|
constexpr Result(Args&& ...args)
|
|
|
|
: Storage( T{ std::forward<Args>(args)... } )
|
2022-05-02 15:13:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:03:22 +02:00
|
|
|
template<typename Arg> requires std::is_constructible_v<Storage, Arg>
|
|
|
|
constexpr Result(Arg &&arg)
|
|
|
|
: Storage(std::forward<Arg>(arg))
|
2022-04-27 13:48:50 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:03:22 +02:00
|
|
|
inline Result(Error error)
|
|
|
|
: Storage(tl::unexpected(std::move(error)))
|
2022-04-27 13:48:50 +02:00
|
|
|
{
|
|
|
|
}
|
2022-04-24 15:27:09 +02:00
|
|
|
|
2022-09-17 00:31:55 +02:00
|
|
|
template<typename Arg>
|
|
|
|
requires requires (Arg a) {
|
|
|
|
{ Error { .details = std::move(a) } };
|
|
|
|
}
|
|
|
|
inline Result(Arg a)
|
|
|
|
: Storage(tl::unexpected(Error { .details = std::move(a) }))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:03:22 +02:00
|
|
|
// Internal function used for definition of Try macro
|
|
|
|
inline auto value() &&
|
|
|
|
{
|
|
|
|
if constexpr (not std::is_void_v<T>) {
|
2022-05-21 23:12:50 +02:00
|
|
|
// NOTE This line in ideal world should be `return Storage::value()`
|
|
|
|
// but C++ does not infer that this is rvalue context.
|
|
|
|
// `std::add_rvalue_reference_t<Storage>::value()`
|
|
|
|
// also does not work, so this is probably the best way to express this:
|
|
|
|
return std::move(*static_cast<Storage*>(this)).value();
|
2022-05-08 00:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-08-21 19:12:12 +02:00
|
|
|
/// Fill error location if it's empty and we have an error
|
|
|
|
inline Result<T> with_location(Location location) &&
|
|
|
|
{
|
|
|
|
if (!Storage::has_value()) {
|
|
|
|
if (auto& target = Storage::error().location; !target || target == Location{}) {
|
|
|
|
target = location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-17 02:35:51 +02:00
|
|
|
inline tl::expected<T, Error> to_expected() &&
|
|
|
|
{
|
|
|
|
return *static_cast<Storage*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Map>
|
|
|
|
requires is_template_v<Result, std::invoke_result_t<Map, T&&>>
|
|
|
|
auto and_then(Map &&map) &&
|
|
|
|
{
|
|
|
|
return std::move(*static_cast<Storage*>(this)).and_then(
|
|
|
|
[map = std::forward<Map>(map)](T &&value) {
|
|
|
|
return std::move(map)(std::move(value)).to_expected();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
using Storage::and_then;
|
2022-08-21 14:49:39 +02:00
|
|
|
|
|
|
|
operator std::optional<Error>() &&
|
|
|
|
{
|
|
|
|
return Storage::has_value() ? std::nullopt : std::optional(Storage::error());
|
|
|
|
}
|
2022-05-08 00:03:22 +02:00
|
|
|
};
|
2022-04-24 15:27:09 +02:00
|
|
|
|
2022-09-23 13:59:35 +02:00
|
|
|
/// Abstraction over any value that are either value or error
|
|
|
|
///
|
|
|
|
/// Inspired by P2561R0
|
|
|
|
template<typename = void>
|
|
|
|
struct Try_Traits
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
static constexpr bool is_ok(T const& v) { return Try_Traits<T>::is_ok(v); }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static constexpr auto yield_value(T&& v) { return Try_Traits<T>::yield_value(std::forward<T>(v)); }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static constexpr auto yield_error(T&& v) { return Try_Traits<T>::yield_error(std::forward<T>(v)); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Try_Traits<std::optional<Error>>
|
|
|
|
{
|
|
|
|
using Value_Type = std::nullopt_t;
|
|
|
|
using Error_Type = Error;
|
|
|
|
|
|
|
|
static constexpr bool is_ok(std::optional<Error> const& o)
|
|
|
|
{
|
|
|
|
return not o.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::nullopt_t yield_value(std::optional<Error>&& err)
|
|
|
|
{
|
|
|
|
assert(not err.has_value(), "Trying to yield value from optional that contains error");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Error yield_error(std::optional<Error>&& err)
|
|
|
|
{
|
|
|
|
assert(err.has_value(), "Trying to yield value from optional that NOT constains error");
|
|
|
|
return std::move(*err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Try_Traits<Result<T>>
|
|
|
|
{
|
|
|
|
using Value_Type = T;
|
|
|
|
using Error_Type = Error;
|
|
|
|
|
|
|
|
static constexpr bool is_ok(Result<T> const& o)
|
|
|
|
{
|
|
|
|
return o.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto yield_value(Result<T> val)
|
|
|
|
{
|
|
|
|
assert(val.has_value(), "Trying to yield value from expected that contains error");
|
|
|
|
if constexpr (std::is_void_v<T>) {
|
|
|
|
} else {
|
|
|
|
return std::move(*val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Error yield_error(Result<T>&& val)
|
|
|
|
{
|
|
|
|
assert(not val.has_value(), "Trying to yield error from expected with value");
|
|
|
|
return std::move(val.error());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Shorthand for forwarding error values with Result type family.
|
|
|
|
///
|
|
|
|
/// This implementation requires C++ language extension: statement expressions
|
|
|
|
/// It's supported by GCC and Clang, other compilers i don't know.
|
|
|
|
/// Inspired by SerenityOS TRY macro
|
2022-09-23 13:59:35 +02:00
|
|
|
#define Try(Value) \
|
|
|
|
({ \
|
|
|
|
auto try_value = (Value); \
|
|
|
|
using Trait [[maybe_unused]] = Try_Traits<std::decay_t<decltype(try_value)>>; \
|
|
|
|
if (not Trait::is_ok(try_value)) [[unlikely]] \
|
|
|
|
return Trait::yield_error(std::move(try_value)); \
|
|
|
|
Trait::yield_value(std::move(try_value)); \
|
2022-04-24 15:27:09 +02:00
|
|
|
})
|
|
|
|
|
2022-05-24 17:39:07 +02:00
|
|
|
/// Drop in replacement for bool when C++ implcit conversions stand in your way
|
|
|
|
struct Explicit_Bool
|
|
|
|
{
|
|
|
|
bool value;
|
|
|
|
|
|
|
|
constexpr Explicit_Bool(bool b) : value(b)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Explicit_Bool(auto &&) = delete;
|
|
|
|
|
|
|
|
constexpr operator bool() const
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// All unicode related operations
|
2022-04-27 14:37:21 +02:00
|
|
|
namespace unicode
|
|
|
|
{
|
|
|
|
inline namespace special_runes
|
|
|
|
{
|
2022-05-08 00:03:22 +02:00
|
|
|
[[maybe_unused]] constexpr u32 Rune_Error = 0xfffd;
|
|
|
|
[[maybe_unused]] constexpr u32 Rune_Self = 0x80;
|
|
|
|
[[maybe_unused]] constexpr u32 Max_Bytes = 4;
|
2022-04-27 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// is_digit returns true if `digit` is ASCII digit
|
2022-04-27 14:37:21 +02:00
|
|
|
bool is_digit(u32 digit);
|
2022-05-02 19:24:29 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// is_space return true if `space` is ASCII blank character
|
2022-04-27 14:37:21 +02:00
|
|
|
bool is_space(u32 space);
|
2022-05-02 19:24:29 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// is_letter returns true if `letter` is considered a letter by Unicode
|
2022-05-02 19:00:11 +02:00
|
|
|
bool is_letter(u32 letter);
|
2022-05-02 19:24:29 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// is_identifier returns true if `letter` is valid character for identifier.
|
|
|
|
///
|
|
|
|
/// It's modifier by is_first_character flag to determine some character classes
|
|
|
|
/// allowance like numbers, which are only allowed NOT at the front of the identifier
|
2022-05-02 19:24:29 +02:00
|
|
|
enum class First_Character : bool { Yes = true, No = false };
|
|
|
|
bool is_identifier(u32 letter, First_Character is_first_character);
|
2022-04-27 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// utf8 encoding and decoding
|
2022-04-27 13:48:50 +02:00
|
|
|
namespace utf8
|
|
|
|
{
|
2022-04-27 14:37:21 +02:00
|
|
|
using namespace unicode::special_runes;
|
2022-04-27 13:48:50 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Decodes rune and returns remaining string
|
2022-05-02 14:50:04 +02:00
|
|
|
auto decode(std::string_view s) -> std::pair<u32, std::string_view>;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Returns length of the first rune in the provided string
|
2022-05-02 14:50:04 +02:00
|
|
|
auto length(std::string_view s) -> usize;
|
|
|
|
|
|
|
|
struct Print { u32 rune; };
|
2022-04-27 13:48:50 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 14:50:04 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, utf8::Print const& print);
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Lexical token representation for Musique language
|
2022-04-24 15:27:09 +02:00
|
|
|
struct Token
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Type of Token
|
2022-04-24 15:27:09 +02:00
|
|
|
enum class Type
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
Symbol, ///< like repeat or choose or chord
|
|
|
|
Keyword, ///< like true, false, nil
|
|
|
|
Operator, ///< like "+", "-", "++", "<"
|
|
|
|
Chord, ///< chord or single note literal, like "c125"
|
|
|
|
Numeric, ///< numeric literal (floating point or integer)
|
|
|
|
Parameter_Separator, ///< "|" separaters arguments from block body
|
|
|
|
Expression_Separator, ///< ";" separates expressions. Used mainly to separate calls, like `foo 1 2; bar 3 4`
|
|
|
|
Open_Block, ///< "[" delimits anonymous block of code (potentially a function)
|
|
|
|
Close_Block, ///< "]" delimits anonymous block of code (potentially a function)
|
|
|
|
Open_Paren, ///< "(" used in arithmetic or as function invocation sarrounding
|
|
|
|
Close_Paren ///< ")" used in arithmetic or as function invocation sarrounding
|
2022-04-24 15:27:09 +02:00
|
|
|
};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Type of token
|
2022-04-24 16:09:55 +02:00
|
|
|
Type type;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Matched source code to the token type
|
2022-04-24 15:27:09 +02:00
|
|
|
std::string_view source;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Location of encountered token
|
2022-04-27 14:58:02 +02:00
|
|
|
Location location;
|
2022-04-24 15:27:09 +02:00
|
|
|
};
|
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
static constexpr usize Keywords_Count = 5;
|
|
|
|
static constexpr usize Operators_Count = 17;
|
2022-06-03 00:52:55 +02:00
|
|
|
|
2022-05-29 22:46:20 +02:00
|
|
|
std::string_view type_name(Token::Type type);
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Token debug printing
|
2022-04-24 15:27:09 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Token const& tok);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Token type debug printing
|
2022-05-02 19:42:57 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Token::Type type);
|
2022-04-24 15:27:09 +02:00
|
|
|
|
2022-06-09 01:48:02 +02:00
|
|
|
struct Lines
|
|
|
|
{
|
|
|
|
static Lines the;
|
|
|
|
|
|
|
|
/// Region of lines in files
|
|
|
|
std::unordered_map<std::string, std::vector<std::string_view>> lines;
|
|
|
|
|
|
|
|
/// Add lines from file
|
|
|
|
void add_file(std::string filename, std::string_view source);
|
|
|
|
|
|
|
|
/// Add single line into file (REPL usage)
|
2022-08-21 20:24:49 +02:00
|
|
|
void add_line(std::string const& filename, std::string_view source, unsigned line_number);
|
2022-06-09 01:48:02 +02:00
|
|
|
|
|
|
|
/// Print selected region
|
|
|
|
void print(std::ostream& os, std::string const& file, unsigned first_line, unsigned last_line) const;
|
|
|
|
};
|
|
|
|
|
2022-05-29 22:39:46 +02:00
|
|
|
/// Explicit marker of the end of file
|
|
|
|
struct End_Of_File {};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Lexer takes source code and turns it into list of tokens
|
|
|
|
///
|
|
|
|
/// It allows for creating sequence of tokens by using next_token() method.
|
|
|
|
/// On each call to next_token() when source is non empty token is lexed and
|
|
|
|
/// source is beeing advanced by removing matched token from string.
|
2022-04-24 15:27:09 +02:00
|
|
|
struct Lexer
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Source that is beeing lexed
|
2022-04-24 15:27:09 +02:00
|
|
|
std::string_view source;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Location in source of the last rune
|
|
|
|
///
|
|
|
|
/// Used only for rewinding
|
2022-04-27 13:48:50 +02:00
|
|
|
u32 last_rune_length = 0;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Start of the token that is currently beeing matched
|
2022-04-27 14:37:21 +02:00
|
|
|
char const* token_start = nullptr;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Bytes matched so far
|
2022-04-27 14:37:21 +02:00
|
|
|
usize token_length = 0;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Location of the start of a token that is currently beeing matched
|
2022-04-27 14:58:02 +02:00
|
|
|
Location token_location{};
|
2022-04-27 14:37:21 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Current location of Lexer in source
|
2022-04-27 14:58:02 +02:00
|
|
|
Location location{};
|
2022-04-24 15:27:09 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Previous location of Lexer in source
|
|
|
|
///
|
|
|
|
/// Used only for rewinding
|
|
|
|
Location prev_location{};
|
|
|
|
|
2022-05-29 22:39:46 +02:00
|
|
|
/// Try to tokenize next token.
|
|
|
|
auto next_token() -> Result<std::variant<Token, End_Of_File>>;
|
2022-04-27 13:48:50 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Skip whitespace and comments from the beggining of the source
|
|
|
|
///
|
|
|
|
/// Utility function for next_token()
|
2022-05-02 21:23:01 +02:00
|
|
|
void skip_whitespace_and_comments();
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Finds next rune in source
|
2022-04-27 14:37:21 +02:00
|
|
|
auto peek() const -> u32;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Finds next rune in source and returns it, advancing the string
|
2022-04-27 14:37:21 +02:00
|
|
|
auto consume() -> u32;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// For test beeing
|
|
|
|
/// callable, current rune is passed to test
|
|
|
|
/// integral, current rune is tested for equality with test
|
|
|
|
/// string, current rune is tested for beeing in it
|
|
|
|
/// otherwise, current rune is tested for beeing in test
|
|
|
|
///
|
|
|
|
/// When testing above yields truth, current rune is consumed.
|
|
|
|
/// Returns if rune was consumed
|
2022-05-07 18:39:29 +02:00
|
|
|
auto consume_if(auto test) -> bool;
|
2022-04-27 13:48:50 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Consume two runes with given tests otherwise backtrack
|
2022-05-07 18:39:29 +02:00
|
|
|
auto consume_if(auto first, auto second) -> bool;
|
2022-05-02 21:23:01 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Goes back last rune
|
2022-04-27 13:48:50 +02:00
|
|
|
void rewind();
|
2022-04-27 14:37:21 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Marks begin of token
|
2022-04-27 14:37:21 +02:00
|
|
|
void start();
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Marks end of token and returns it's matching source
|
2022-04-27 14:37:21 +02:00
|
|
|
std::string_view finish();
|
2022-04-24 15:27:09 +02:00
|
|
|
};
|
2022-05-07 20:52:09 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Representation of a node in program tree
|
2022-05-07 20:52:09 +02:00
|
|
|
struct Ast
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Constructs binary operator
|
2022-05-09 19:42:02 +02:00
|
|
|
static Ast binary(Token, Ast lhs, Ast rhs);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Constructs block
|
2022-05-17 14:09:40 +02:00
|
|
|
static Ast block(Location location, Ast seq = sequence({}));
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Constructs call expression
|
2022-05-10 15:25:17 +02:00
|
|
|
static Ast call(std::vector<Ast> call);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Constructs block with parameters
|
2022-05-17 14:09:40 +02:00
|
|
|
static Ast lambda(Location location, Ast seq = sequence({}), std::vector<Ast> parameters = {});
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Constructs constants, literals and variable identifiers
|
2022-05-16 16:58:31 +02:00
|
|
|
static Ast literal(Token);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Constructs sequence of operations
|
2022-05-10 16:03:30 +02:00
|
|
|
static Ast sequence(std::vector<Ast> call);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Constructs variable declaration
|
2022-05-16 16:58:31 +02:00
|
|
|
static Ast variable_declaration(Location loc, std::vector<Ast> lvalues, std::optional<Ast> rvalue);
|
2022-05-07 20:52:09 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Available ASt types
|
2022-05-07 20:52:09 +02:00
|
|
|
enum class Type
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
Binary, ///< Binary operator application like `1` + `2`
|
|
|
|
Block, ///< Block expressions like `[42; hello]`
|
|
|
|
Lambda, ///< Block expression beeing functions like `[i|i+1]`
|
|
|
|
Call, ///< Function call application like `print 42`
|
|
|
|
Literal, ///< Compile time known constant like `c` or `1`
|
|
|
|
Sequence, ///< Several expressions sequences like `42`, `42; 32`
|
|
|
|
Variable_Declaration, ///< Declaration of a variable with optional value assigment like `var x = 10` or `var y`
|
2022-05-07 20:52:09 +02:00
|
|
|
};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Type of AST node
|
2022-05-07 20:52:09 +02:00
|
|
|
Type type;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Location that introduced this node
|
2022-05-15 22:22:26 +02:00
|
|
|
Location location;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Associated token
|
2022-05-07 20:52:09 +02:00
|
|
|
Token token;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Child nodes
|
2022-05-10 16:49:36 +02:00
|
|
|
std::vector<Ast> arguments{};
|
2022-05-07 20:52:09 +02:00
|
|
|
};
|
|
|
|
|
2022-05-09 19:42:02 +02:00
|
|
|
bool operator==(Ast const& lhs, Ast const& rhs);
|
2022-05-10 16:49:36 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Ast::Type type);
|
2022-05-09 19:42:02 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Ast const& tree);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Pretty print program tree for debugging purposes
|
2022-05-10 16:49:36 +02:00
|
|
|
void dump(Ast const& ast, unsigned indent = 0);
|
2022-05-09 19:42:02 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Source code to program tree converter
|
|
|
|
///
|
|
|
|
/// Intended to be used by library user only by Parser::parse() static function.
|
2022-05-07 20:52:09 +02:00
|
|
|
struct Parser
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// List of tokens yielded from source
|
2022-05-07 20:52:09 +02:00
|
|
|
std::vector<Token> tokens;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Current token id (offset in tokens array)
|
2022-05-07 20:52:09 +02:00
|
|
|
unsigned token_id = 0;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Parses whole source code producing Ast or Error
|
|
|
|
/// using Parser structure internally
|
2022-08-21 20:24:49 +02:00
|
|
|
static Result<Ast> parse(std::string_view source, std::string_view filename, unsigned line_number = 0);
|
2022-05-07 20:52:09 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Parse sequence, collection of expressions
|
2022-05-10 16:03:30 +02:00
|
|
|
Result<Ast> parse_sequence();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Parse either infix expression or variable declaration
|
2022-05-07 20:52:09 +02:00
|
|
|
Result<Ast> parse_expression();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Parse infix expression
|
2022-05-10 15:25:17 +02:00
|
|
|
Result<Ast> parse_infix_expression();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
2022-06-20 05:45:28 +02:00
|
|
|
/// Parse right hand size of infix expression
|
|
|
|
Result<Ast> parse_rhs_of_infix_expression(Ast lhs);
|
|
|
|
|
2022-06-11 17:55:52 +02:00
|
|
|
/// Parse either index expression or atomic expression
|
|
|
|
Result<Ast> parse_index_expression();
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Parse function call, literal etc
|
2022-05-10 15:25:17 +02:00
|
|
|
Result<Ast> parse_atomic_expression();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Parse variable declaration
|
2022-05-16 16:58:31 +02:00
|
|
|
Result<Ast> parse_variable_declaration();
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Utility function for identifier parsing
|
2022-05-16 16:58:31 +02:00
|
|
|
Result<Ast> parse_identifier_with_trailing_separators();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Utility function for identifier parsing
|
2022-05-10 16:49:36 +02:00
|
|
|
Result<Ast> parse_identifier();
|
2022-05-07 20:52:09 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Peek current token
|
2022-05-10 15:25:17 +02:00
|
|
|
Result<Token> peek() const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Peek type of the current token
|
2022-05-10 14:20:23 +02:00
|
|
|
Result<Token::Type> peek_type() const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Consume current token
|
2022-05-07 20:52:09 +02:00
|
|
|
Token consume();
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Tests if current token has given type
|
2022-05-07 20:52:09 +02:00
|
|
|
bool expect(Token::Type type) const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Tests if current token has given type and source
|
2022-05-16 16:58:31 +02:00
|
|
|
bool expect(Token::Type type, std::string_view lexeme) const;
|
2022-09-18 14:50:20 +02:00
|
|
|
|
|
|
|
// Tests if current token has given type and the next token has given type and source
|
|
|
|
bool expect(Token::Type t1, Token::Type t2, std::string_view lexeme_for_t2) const;
|
2022-05-07 20:52:09 +02:00
|
|
|
};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Number type supporting integer and fractional constants
|
|
|
|
///
|
|
|
|
/// \invariant gcd(num, den) == 1, after any operation
|
2022-05-15 23:03:09 +02:00
|
|
|
struct Number
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Type that represents numerator and denominator values
|
2022-05-16 00:06:27 +02:00
|
|
|
using value_type = i64;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
value_type num = 0; ///< Numerator of a fraction beeing represented
|
|
|
|
value_type den = 1; ///< Denominator of a fraction beeing represented
|
2022-05-15 23:03:09 +02:00
|
|
|
|
2022-05-16 00:06:27 +02:00
|
|
|
constexpr Number() = default;
|
|
|
|
constexpr Number(Number const&) = default;
|
|
|
|
constexpr Number(Number &&) = default;
|
|
|
|
constexpr Number& operator=(Number const&) = default;
|
|
|
|
constexpr Number& operator=(Number &&) = default;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
explicit Number(value_type v); ///< Creates Number as fraction v / 1
|
|
|
|
Number(value_type num, value_type den); ///< Creates Number as fraction num / den
|
2022-05-16 00:06:27 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
auto as_int() const -> value_type; ///< Returns self as int
|
|
|
|
auto simplify() const -> Number; ///< Returns self, but with gcd(num, den) == 1
|
|
|
|
void simplify_inplace(); ///< Update self, to have gcd(num, den) == 1
|
2022-05-15 23:03:09 +02:00
|
|
|
|
|
|
|
bool operator==(Number const&) const;
|
|
|
|
bool operator!=(Number const&) const;
|
|
|
|
std::strong_ordering operator<=>(Number const&) const;
|
|
|
|
|
|
|
|
Number operator+(Number const& rhs) const;
|
|
|
|
Number& operator+=(Number const& rhs);
|
|
|
|
Number operator-(Number const& rhs) const;
|
|
|
|
Number& operator-=(Number const& rhs);
|
|
|
|
Number operator*(Number const& rhs) const;
|
|
|
|
Number& operator*=(Number const& rhs);
|
2022-09-04 19:44:33 +02:00
|
|
|
Result<Number> operator/(Number const& rhs) const;
|
|
|
|
Result<Number> operator%(Number const& rhs) const;
|
2022-05-16 00:06:27 +02:00
|
|
|
|
2022-08-21 00:43:04 +02:00
|
|
|
Number floor() const; ///< Return number rounded down to nearest integer
|
|
|
|
Number ceil() const; ///< Return number rounded up to nearest integer
|
|
|
|
Number round() const; ///< Return number rounded to nearest integer
|
|
|
|
|
2022-09-04 19:44:33 +02:00
|
|
|
Result<Number> inverse() const; ///< Return number raised to power -1
|
|
|
|
Result<Number> pow(Number n) const; ///< Return number raised to power `n`.
|
2022-08-21 00:43:04 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Parses source contained by token into a Number instance
|
2022-05-16 00:06:27 +02:00
|
|
|
static Result<Number> from(Token token);
|
2022-05-15 23:03:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, Number const& num);
|
|
|
|
|
2022-05-22 00:34:02 +02:00
|
|
|
struct Env;
|
2022-05-17 14:09:40 +02:00
|
|
|
struct Interpreter;
|
2022-05-22 00:34:02 +02:00
|
|
|
struct Value;
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-05-22 05:29:41 +02:00
|
|
|
using Intrinsic = Result<Value>(*)(Interpreter &i, std::vector<Value>);
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-05-24 17:39:07 +02:00
|
|
|
/// Lazy Array / Continuation / Closure type thingy
|
2022-05-22 05:29:41 +02:00
|
|
|
struct Block
|
2022-05-17 14:09:40 +02:00
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Location of definition / creation
|
2022-05-17 14:09:40 +02:00
|
|
|
Location location;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Names of expected parameters
|
2022-05-17 14:09:40 +02:00
|
|
|
std::vector<std::string> parameters;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Body that will be executed
|
2022-05-17 14:09:40 +02:00
|
|
|
Ast body;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Context from which block was created. Used for closures
|
2022-05-22 00:34:02 +02:00
|
|
|
std::shared_ptr<Env> context;
|
2022-05-17 14:09:40 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Calling block
|
2022-05-17 14:09:40 +02:00
|
|
|
Result<Value> operator()(Interpreter &i, std::vector<Value> params);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Indexing block
|
2022-09-23 13:18:55 +02:00
|
|
|
Result<Value> index(Interpreter &i, unsigned position) const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Count of elements in block
|
2022-05-24 19:09:37 +02:00
|
|
|
usize size() const;
|
2022-05-17 14:09:40 +02:00
|
|
|
};
|
|
|
|
|
2022-09-11 16:19:10 +02:00
|
|
|
/// Representation of musical note or musical pause
|
2022-05-24 01:40:16 +02:00
|
|
|
struct Note
|
|
|
|
{
|
|
|
|
/// Base of a note, like `c` (=0), `c#` (=1) `d` (=2)
|
2022-09-11 16:19:10 +02:00
|
|
|
/// Or nullopt where there is no note - case when we have pause
|
|
|
|
std::optional<i32> base = std::nullopt;
|
2022-05-24 01:40:16 +02:00
|
|
|
|
|
|
|
/// Octave in MIDI acceptable range (from -1 to 9 inclusive)
|
|
|
|
std::optional<i8> octave = std::nullopt;
|
|
|
|
|
|
|
|
/// Length of playing note
|
|
|
|
std::optional<Number> length = std::nullopt;
|
|
|
|
|
|
|
|
/// Create Note from string
|
|
|
|
static std::optional<Note> from(std::string_view note);
|
|
|
|
|
|
|
|
/// Extract midi note number
|
|
|
|
std::optional<u8> into_midi_note() const;
|
|
|
|
|
|
|
|
/// Extract midi note number, but when octave is not present use provided default
|
|
|
|
u8 into_midi_note(i8 default_octave) const;
|
2022-05-24 01:58:17 +02:00
|
|
|
|
|
|
|
bool operator==(Note const&) const;
|
2022-05-25 03:14:08 +02:00
|
|
|
|
2022-06-02 23:17:02 +02:00
|
|
|
std::partial_ordering operator<=>(Note const&) const;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Simplify note by adding base to octave if octave is present
|
2022-05-25 03:14:08 +02:00
|
|
|
void simplify_inplace();
|
2022-05-24 01:40:16 +02:00
|
|
|
};
|
|
|
|
|
2022-05-25 03:14:08 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Note note);
|
2022-05-24 01:58:17 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Represantation of simultaneously played notes, aka musical chord
|
2022-05-25 00:08:48 +02:00
|
|
|
struct Chord
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
std::vector<Note> notes; ///< Notes composing a chord
|
2022-05-25 00:08:48 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Parse chord literal from provided source
|
2022-05-25 00:08:48 +02:00
|
|
|
static Chord from(std::string_view source);
|
|
|
|
|
|
|
|
bool operator==(Chord const&) const = default;
|
2022-09-17 13:10:24 +02:00
|
|
|
|
|
|
|
/// Fill length and octave or sequence multiple chords
|
|
|
|
Result<Value> operator()(Interpreter &i, std::vector<Value> args);
|
2022-05-25 00:08:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, Chord const& chord);
|
|
|
|
|
2022-05-24 17:39:07 +02:00
|
|
|
/// Eager Array
|
|
|
|
struct Array
|
|
|
|
{
|
|
|
|
/// Elements that are stored in array
|
|
|
|
std::vector<Value> elements;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Index element of an array
|
2022-09-23 13:18:55 +02:00
|
|
|
Result<Value> index(Interpreter &i, unsigned position) const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Count of elements
|
2022-05-24 19:09:37 +02:00
|
|
|
usize size() const;
|
|
|
|
|
|
|
|
bool operator==(Array const&) const = default;
|
2022-05-24 17:39:07 +02:00
|
|
|
};
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-05-24 19:09:37 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Array const& v);
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Representation of any value in language
|
2022-05-16 02:18:53 +02:00
|
|
|
struct Value
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Creates value from literal contained in Token
|
2022-05-16 02:18:53 +02:00
|
|
|
static Result<Value> from(Token t);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Create value holding provided boolean
|
|
|
|
///
|
|
|
|
/// Using Explicit_Bool to prevent from implicit casts
|
2022-05-24 17:39:07 +02:00
|
|
|
static Value from(Explicit_Bool b);
|
|
|
|
|
2022-09-05 22:30:11 +02:00
|
|
|
static Value from(Array &&array); ///< Create value of type array holding provided array
|
|
|
|
static Value from(Block &&l); ///< Create value of type block holding provided block
|
|
|
|
static Value from(Chord chord); ///< Create value of type music holding provided chord
|
|
|
|
static Value from(Note n); ///< Create value of type music holding provided note
|
|
|
|
static Value from(Number n); ///< Create value of type number holding provided number
|
|
|
|
static Value from(char const* s); ///< Create value of type symbol holding provided symbol
|
|
|
|
static Value from(std::string s); ///< Create value of type symbol holding provided symbol
|
|
|
|
static Value from(std::string_view s); ///< Create value of type symbol holding provided symbol
|
|
|
|
static Value from(std::vector<Value> &&array); ///< Create value of type array holding provided array
|
2022-05-16 02:18:53 +02:00
|
|
|
|
|
|
|
enum class Type
|
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
Nil, ///< Unit type, used for denoting emptiness and result of some side effect only functions
|
|
|
|
Bool, ///< Boolean type, used for logic computations
|
|
|
|
Number, ///< Number type, representing only rational numbers
|
|
|
|
Symbol, ///< Symbol type, used to represent identifiers
|
|
|
|
Intrinsic, ///< Intrinsic functions that are implemented in C++
|
|
|
|
Block, ///< Block type, containing block value (lazy array/closure/lambda like)
|
|
|
|
Array, ///< Array type, eager array
|
|
|
|
Music, ///< Music type,
|
2022-05-16 02:18:53 +02:00
|
|
|
};
|
|
|
|
|
2022-05-17 02:35:51 +02:00
|
|
|
Value() = default;
|
|
|
|
Value(Value const&) = default;
|
|
|
|
Value(Value &&) = default;
|
|
|
|
Value& operator=(Value const&) = default;
|
|
|
|
Value& operator=(Value &&) = default;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Contructs Intrinsic, used to simplify definition of intrinsics
|
2022-05-22 05:29:41 +02:00
|
|
|
inline Value(Intrinsic intr) : type{Type::Intrinsic}, intr(intr)
|
2022-05-17 02:35:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-16 02:18:53 +02:00
|
|
|
Type type = Type::Nil;
|
2022-05-24 01:40:16 +02:00
|
|
|
bool b;
|
|
|
|
Number n;
|
|
|
|
Intrinsic intr;
|
2022-05-22 05:29:41 +02:00
|
|
|
Block blk;
|
2022-05-25 00:08:48 +02:00
|
|
|
Chord chord;
|
2022-05-24 19:09:37 +02:00
|
|
|
Array array;
|
2022-05-16 02:18:53 +02:00
|
|
|
|
|
|
|
// TODO Most strings should not be allocated by Value, but reference to string allocated previously
|
|
|
|
// Wrapper for std::string is needed that will allocate only when needed, middle ground between:
|
|
|
|
// std::string - always owning string type
|
|
|
|
// std::string_view - not-owning string type
|
|
|
|
std::string s{};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Returns truth judgment for current type, used primarly for if function
|
2022-05-17 16:10:56 +02:00
|
|
|
bool truthy() const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Returns false judgment for current type, used primarly for if function
|
2022-05-17 16:10:56 +02:00
|
|
|
bool falsy() const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Calls contained value if it can be called
|
2022-05-17 16:10:56 +02:00
|
|
|
Result<Value> operator()(Interpreter &i, std::vector<Value> args);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Index contained value if it can be called
|
2022-09-23 13:18:55 +02:00
|
|
|
Result<Value> index(Interpreter &i, unsigned position) const;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Return elements count of contained value if it can be measured
|
2022-05-24 19:09:37 +02:00
|
|
|
usize size() const;
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-05-16 02:18:53 +02:00
|
|
|
bool operator==(Value const& other) const;
|
2022-06-02 23:17:02 +02:00
|
|
|
|
|
|
|
std::partial_ordering operator<=>(Value const& other) const;
|
2022-05-16 02:18:53 +02:00
|
|
|
};
|
|
|
|
|
2022-05-30 15:10:03 +02:00
|
|
|
template<Value::Type>
|
|
|
|
struct Member_For_Value_Type {};
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Bool>
|
|
|
|
{ static constexpr auto value = &Value::b; };
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Number>
|
|
|
|
{ static constexpr auto value = &Value::n; };
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Symbol>
|
|
|
|
{ static constexpr auto value = &Value::s; };
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Intrinsic>
|
|
|
|
{ static constexpr auto value = &Value::intr; };
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Block>
|
|
|
|
{ static constexpr auto value = &Value::blk; };
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Array>
|
|
|
|
{ static constexpr auto value = &Value::array; };
|
|
|
|
|
|
|
|
template<> struct Member_For_Value_Type<Value::Type::Music>
|
|
|
|
{ static constexpr auto value = &Value::chord; };
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Returns type name of Value type
|
2022-05-17 02:35:51 +02:00
|
|
|
std::string_view type_name(Value::Type t);
|
|
|
|
|
2022-05-16 02:18:53 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, Value const& v);
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Collection holding all variables in given scope.
|
2022-05-21 23:54:21 +02:00
|
|
|
struct Env : std::enable_shared_from_this<Env>
|
2022-05-17 02:35:51 +02:00
|
|
|
{
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Constructor of Env class
|
2022-05-21 23:54:21 +02:00
|
|
|
static std::shared_ptr<Env> make();
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Global scope that is beeing set by Interpreter
|
2022-05-21 23:54:21 +02:00
|
|
|
static std::shared_ptr<Env> global;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Variables in current scope
|
2022-05-17 02:35:51 +02:00
|
|
|
std::unordered_map<std::string, Value> variables;
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Parent scope
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> parent;
|
2022-05-17 02:35:51 +02:00
|
|
|
|
|
|
|
Env(Env const&) = delete;
|
|
|
|
Env(Env &&) = default;
|
|
|
|
Env& operator=(Env const&) = delete;
|
|
|
|
Env& operator=(Env &&) = default;
|
|
|
|
|
|
|
|
/// Defines new variable regardless of it's current existance
|
|
|
|
Env& force_define(std::string name, Value new_value);
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Finds variable in current or parent scopes
|
2022-05-17 02:35:51 +02:00
|
|
|
Value* find(std::string const& name);
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Create new scope with self as parent
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> enter();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
/// Leave current scope returning parent
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> leave();
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-05-21 23:54:21 +02:00
|
|
|
private:
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Ensure that all values of this class are behind shared_ptr
|
2022-05-21 23:54:21 +02:00
|
|
|
Env() = default;
|
2022-05-17 02:35:51 +02:00
|
|
|
};
|
2022-05-16 02:18:53 +02:00
|
|
|
|
2022-05-24 02:58:15 +02:00
|
|
|
/// Context holds default values for music related actions
|
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
/// Default note octave
|
|
|
|
i8 octave = 4;
|
|
|
|
|
|
|
|
/// Default note length
|
|
|
|
Number length = Number(1, 4);
|
|
|
|
|
|
|
|
/// Default BPM
|
|
|
|
unsigned bpm = 120;
|
|
|
|
|
|
|
|
/// Fills empty places in Note like octave and length with default values from context
|
|
|
|
Note fill(Note) const;
|
|
|
|
|
|
|
|
/// Converts length to seconds with current bpm
|
|
|
|
std::chrono::duration<float> length_to_duration(std::optional<Number> length) const;
|
|
|
|
};
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Given program tree evaluates it into Value
|
2022-05-16 02:18:53 +02:00
|
|
|
struct Interpreter
|
|
|
|
{
|
2022-05-24 03:35:15 +02:00
|
|
|
/// MIDI connection that is used to play music.
|
|
|
|
/// It's optional for simple interpreter testing.
|
|
|
|
midi::Connection *midi_connection = nullptr;
|
|
|
|
|
2022-05-22 16:43:36 +02:00
|
|
|
/// Operators defined for language
|
2022-05-22 05:29:41 +02:00
|
|
|
std::unordered_map<std::string, Intrinsic> operators;
|
2022-05-22 16:43:36 +02:00
|
|
|
|
|
|
|
/// Current environment (current scope)
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> env;
|
2022-05-16 02:18:53 +02:00
|
|
|
|
2022-05-24 02:58:15 +02:00
|
|
|
/// Context stack. `constext_stack.back()` is a current context.
|
|
|
|
/// There is always at least one context
|
|
|
|
std::vector<Context> context_stack;
|
|
|
|
|
2022-09-23 13:59:35 +02:00
|
|
|
std::function<std::optional<Error>(Interpreter&, Value)> default_action;
|
2022-08-21 15:52:54 +02:00
|
|
|
|
2022-06-19 00:27:02 +02:00
|
|
|
struct Incoming_Midi_Callbacks;
|
|
|
|
std::unique_ptr<Incoming_Midi_Callbacks> callbacks;
|
|
|
|
void register_callbacks();
|
|
|
|
|
2022-05-16 02:18:53 +02:00
|
|
|
Interpreter();
|
2022-05-17 02:35:51 +02:00
|
|
|
~Interpreter();
|
2022-05-16 02:18:53 +02:00
|
|
|
Interpreter(Interpreter const&) = delete;
|
|
|
|
Interpreter(Interpreter &&) = default;
|
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
/// Try to evaluate given program tree
|
2022-05-16 02:18:53 +02:00
|
|
|
Result<Value> eval(Ast &&ast);
|
2022-05-21 23:54:21 +02:00
|
|
|
|
2022-05-29 02:28:51 +02:00
|
|
|
// Enter scope by changing current environment
|
2022-05-21 23:54:21 +02:00
|
|
|
void enter_scope();
|
2022-05-29 02:28:51 +02:00
|
|
|
|
|
|
|
// Leave scope by changing current environment
|
2022-05-21 23:54:21 +02:00
|
|
|
void leave_scope();
|
2022-05-24 03:35:15 +02:00
|
|
|
|
|
|
|
/// Play note resolving any missing parameters with context via `midi_connection` member.
|
2022-09-23 13:59:35 +02:00
|
|
|
std::optional<Error> play(Chord);
|
2022-08-18 22:21:04 +02:00
|
|
|
|
|
|
|
/// Add to global interpreter scope all builtin function definitions
|
|
|
|
///
|
|
|
|
/// Invoked during construction
|
|
|
|
void register_builtin_functions();
|
|
|
|
|
|
|
|
/// Add to interpreter operators table all operators
|
|
|
|
///
|
|
|
|
/// Invoked during construction
|
|
|
|
void register_builtin_operators();
|
2022-05-16 02:18:53 +02:00
|
|
|
};
|
|
|
|
|
2022-05-07 20:52:09 +02:00
|
|
|
namespace errors
|
|
|
|
{
|
|
|
|
[[noreturn]]
|
|
|
|
void all_tokens_were_not_parsed(std::span<Token>);
|
|
|
|
}
|
2022-08-18 22:21:04 +02:00
|
|
|
|
|
|
|
/// Intrinsic implementation primitive providing a short way to check if arguments match required type signature
|
|
|
|
static inline bool typecheck(std::vector<Value> const& args, auto const& ...expected_types)
|
|
|
|
{
|
|
|
|
return (args.size() == sizeof...(expected_types)) &&
|
|
|
|
[&args, expected_types...]<std::size_t ...I>(std::index_sequence<I...>) {
|
|
|
|
return ((expected_types == args[I].type) && ...);
|
|
|
|
} (std::make_index_sequence<sizeof...(expected_types)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Intrinsic implementation primitive providing a short way to move values based on matched type signature
|
|
|
|
static inline bool typecheck_front(std::vector<Value> const& args, auto const& ...expected_types)
|
|
|
|
{
|
|
|
|
return (args.size() >= sizeof...(expected_types)) &&
|
|
|
|
[&args, expected_types...]<std::size_t ...I>(std::index_sequence<I...>) {
|
|
|
|
return ((expected_types == args[I].type) && ...);
|
|
|
|
} (std::make_index_sequence<sizeof...(expected_types)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Intrinsic implementation primitive providing a short way to move values based on matched type signature
|
|
|
|
template<auto ...Types>
|
|
|
|
static inline auto move_from(std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
return [&args]<std::size_t ...I>(std::index_sequence<I...>) {
|
|
|
|
return std::tuple { (std::move(args[I]).*(Member_For_Value_Type<Types>::value)) ... };
|
|
|
|
} (std::make_index_sequence<sizeof...(Types)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shape abstraction to define what types are required once
|
|
|
|
template<auto ...Types>
|
|
|
|
struct Shape
|
|
|
|
{
|
|
|
|
static inline auto move_from(std::vector<Value>& args) { return ::move_from<Types...>(args); }
|
|
|
|
static inline auto typecheck(std::vector<Value>& args) { return ::typecheck(args, Types...); }
|
|
|
|
static inline auto typecheck_front(std::vector<Value>& args) { return ::typecheck_front(args, Types...); }
|
2022-08-21 13:45:34 +02:00
|
|
|
|
|
|
|
static inline auto typecheck_and_move(std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
return typecheck(args) ? std::optional { move_from(args) } : std::nullopt;
|
|
|
|
}
|
2022-08-18 22:21:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns if type can be indexed
|
|
|
|
static constexpr bool is_indexable(Value::Type type)
|
|
|
|
{
|
|
|
|
return type == Value::Type::Array || type == Value::Type::Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns if type can be called
|
|
|
|
static constexpr bool is_callable(Value::Type type)
|
|
|
|
{
|
|
|
|
return type == Value::Type::Block || type == Value::Type::Intrinsic;
|
|
|
|
}
|
|
|
|
|
2022-09-05 22:30:11 +02:00
|
|
|
/// Flattens one layer: `[[[1], 2], 3]` becomes `[[1], 2, 3]`
|
|
|
|
Result<std::vector<Value>> flatten(Interpreter &i, std::span<Value>);
|
|
|
|
Result<std::vector<Value>> flatten(Interpreter &i, std::vector<Value>);
|
|
|
|
|
2022-09-05 23:17:40 +02:00
|
|
|
template<> struct std::hash<Token> { std::size_t operator()(Token const&) const; };
|
|
|
|
template<> struct std::hash<Ast> { std::size_t operator()(Ast const&) const; };
|
|
|
|
template<> struct std::hash<Number> { std::size_t operator()(Number const&) const; };
|
|
|
|
template<> struct std::hash<Value> { std::size_t operator()(Value const&) const; };
|
|
|
|
|
2022-09-23 13:18:55 +02:00
|
|
|
struct Value_Formatter
|
|
|
|
{
|
|
|
|
enum Context
|
|
|
|
{
|
|
|
|
Free,
|
|
|
|
Inside_Block
|
|
|
|
};
|
|
|
|
|
|
|
|
Context context = Free;
|
|
|
|
unsigned indent = 0;
|
|
|
|
|
|
|
|
Value_Formatter nest(Context nested = Free) const;
|
|
|
|
|
2022-09-23 13:59:35 +02:00
|
|
|
std::optional<Error> format(std::ostream& os, Interpreter &interpreter, Value const& value);
|
2022-09-23 13:18:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Result<std::string> format(Interpreter &i, Value const& value);
|
|
|
|
|
2022-08-18 22:48:11 +02:00
|
|
|
#endif
|