Compare commits
5
Commits
11f0553b5a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79ede83a99 | ||
|
|
287c2457e7 | ||
|
|
092ada8479 | ||
|
|
b68839902c | ||
|
|
f87eb4d934 |
+50
-46
@@ -11,6 +11,7 @@ endif()
|
|||||||
# Options
|
# Options
|
||||||
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
|
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
|
||||||
option(BLAZE_BUILD_EXAMPLES "Build the Blaze example programs" ${BLAZE_STANDALONE})
|
option(BLAZE_BUILD_EXAMPLES "Build the Blaze example programs" ${BLAZE_STANDALONE})
|
||||||
|
option(BLAZE_BUILD_TESTS "Build the Blaze test programs" ${BLAZE_STANDALONE})
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
|
|
||||||
@@ -55,18 +56,19 @@ endif()
|
|||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
# Library
|
# Dependencies
|
||||||
|
|
||||||
add_subdirectory("vendor/ruc")
|
add_subdirectory("vendor/ruc")
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
# Application target
|
# Library target
|
||||||
|
|
||||||
# Define source files
|
# Define source files
|
||||||
file(GLOB_RECURSE PROJECT_SOURCES "src/*.cpp")
|
file(GLOB_RECURSE LIBRARY_SOURCES "src/*.cpp")
|
||||||
|
|
||||||
add_executable(${PROJECT} ${PROJECT_SOURCES})
|
add_library(${PROJECT} ${LIBRARY_SOURCES})
|
||||||
target_include_directories(${PROJECT} PRIVATE "src")
|
target_include_directories(${PROJECT} PUBLIC
|
||||||
|
"src")
|
||||||
target_link_libraries(${PROJECT} readline ruc)
|
target_link_libraries(${PROJECT} readline ruc)
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
@@ -78,53 +80,55 @@ add_custom_target(${PROJECT}-lisp
|
|||||||
add_dependencies(${PROJECT} ${PROJECT}-lisp)
|
add_dependencies(${PROJECT} ${PROJECT}-lisp)
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
# Execute target
|
# Example target
|
||||||
|
|
||||||
add_custom_target(run
|
if (BLAZE_BUILD_EXAMPLES)
|
||||||
COMMAND ${PROJECT} -c
|
add_subdirectory("example")
|
||||||
DEPENDS ${PROJECT})
|
endif()
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
# Test targets
|
# Test targets
|
||||||
|
|
||||||
function(make_test_target target_name step_name)
|
if (BLAZE_BUILD_TESTS)
|
||||||
add_custom_target(${target_name}
|
function(make_test_target target_name step_name)
|
||||||
COMMAND ../vendor/mal/runtest.py --deferrable --optional ../tests/${step_name}.mal -- ./${PROJECT})
|
add_custom_target(${target_name}
|
||||||
add_dependencies(${target_name} ${PROJECT})
|
COMMAND ../vendor/mal/runtest.py --deferrable --optional ../tests/${step_name}.mal -- ./${PROJECT})
|
||||||
endfunction()
|
add_dependencies(${target_name} ${PROJECT})
|
||||||
|
endfunction()
|
||||||
|
|
||||||
make_test_target("test0" "step0_repl")
|
make_test_target("test0" "step0_repl")
|
||||||
make_test_target("test1" "step1_read_print")
|
make_test_target("test1" "step1_read_print")
|
||||||
make_test_target("test2" "step2_eval")
|
make_test_target("test2" "step2_eval")
|
||||||
make_test_target("test3" "step3_env")
|
make_test_target("test3" "step3_env")
|
||||||
make_test_target("test4" "step4_if_fn_do")
|
make_test_target("test4" "step4_if_fn_do")
|
||||||
make_test_target("test5" "step5_tco")
|
make_test_target("test5" "step5_tco")
|
||||||
make_test_target("test6" "step6_file")
|
make_test_target("test6" "step6_file")
|
||||||
make_test_target("test7" "step7_quote")
|
make_test_target("test7" "step7_quote")
|
||||||
make_test_target("test8" "step8_macros")
|
make_test_target("test8" "step8_macros")
|
||||||
make_test_target("test9" "step9_try")
|
make_test_target("test9" "step9_try")
|
||||||
make_test_target("testA" "stepA_mal")
|
make_test_target("testA" "stepA_mal")
|
||||||
|
|
||||||
function(make_host_test_target target_name step_name)
|
function(make_host_test_target target_name step_name)
|
||||||
add_custom_target(${target_name}
|
add_custom_target(${target_name}
|
||||||
COMMAND ../vendor/mal/runtest.py --deferrable --optional ../tests/${step_name}.mal -- ./${PROJECT} ../mal/${step_name}.mal)
|
COMMAND ../vendor/mal/runtest.py --deferrable --optional ../tests/${step_name}.mal -- ./${PROJECT} ../mal/${step_name}.mal)
|
||||||
add_dependencies(${target_name} ${PROJECT})
|
add_dependencies(${target_name} ${PROJECT})
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
make_host_test_target("host_test0" "step0_repl")
|
make_host_test_target("host_test0" "step0_repl")
|
||||||
make_host_test_target("host_test1" "step1_read_print")
|
make_host_test_target("host_test1" "step1_read_print")
|
||||||
make_host_test_target("host_test2" "step2_eval")
|
make_host_test_target("host_test2" "step2_eval")
|
||||||
make_host_test_target("host_test3" "step3_env")
|
make_host_test_target("host_test3" "step3_env")
|
||||||
make_host_test_target("host_test4" "step4_if_fn_do")
|
make_host_test_target("host_test4" "step4_if_fn_do")
|
||||||
# make_host_test_target("host_test5" "step5_tco") # disabled
|
# make_host_test_target("host_test5" "step5_tco") # disabled
|
||||||
make_host_test_target("host_test6" "step6_file")
|
make_host_test_target("host_test6" "step6_file")
|
||||||
make_host_test_target("host_test7" "step7_quote")
|
make_host_test_target("host_test7" "step7_quote")
|
||||||
make_host_test_target("host_test8" "step8_macros")
|
make_host_test_target("host_test8" "step8_macros")
|
||||||
make_host_test_target("host_test9" "step9_try")
|
make_host_test_target("host_test9" "step9_try")
|
||||||
make_host_test_target("host_testA" "stepA_mal")
|
make_host_test_target("host_testA" "stepA_mal")
|
||||||
|
|
||||||
add_custom_target(perf
|
add_custom_target(perf
|
||||||
COMMAND ./${PROJECT} ../tests/perf1.mal
|
COMMAND ./${PROJECT} ../tests/perf1.mal
|
||||||
COMMAND ./${PROJECT} ../tests/perf2.mal
|
COMMAND ./${PROJECT} ../tests/perf2.mal
|
||||||
COMMAND ./${PROJECT} ../tests/perf3.mal)
|
COMMAND ./${PROJECT} ../tests/perf3.mal)
|
||||||
add_dependencies(perf ${PROJECT})
|
add_dependencies(perf ${PROJECT})
|
||||||
|
endif()
|
||||||
|
|||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
build/compile_commands.json
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# ------------------------------------------
|
||||||
|
# User config between these lines
|
||||||
|
|
||||||
|
# Set project name
|
||||||
|
set(EXAMPLE "repl")
|
||||||
|
|
||||||
|
# ------------------------------------------
|
||||||
|
|
||||||
|
project(${EXAMPLE} CXX)
|
||||||
|
|
||||||
|
# Define game source files
|
||||||
|
file(GLOB_RECURSE EXAMPLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
|
||||||
|
|
||||||
|
add_executable(${EXAMPLE} ${EXAMPLE_SOURCES})
|
||||||
|
target_include_directories(${EXAMPLE} PRIVATE
|
||||||
|
"src")
|
||||||
|
target_link_libraries(${EXAMPLE} ${PROJECT})
|
||||||
|
|
||||||
|
# ------------------------------------------
|
||||||
|
|
||||||
|
# Add 'make run' target
|
||||||
|
add_custom_target(run
|
||||||
|
COMMAND ${EXAMPLE} -c
|
||||||
|
DEPENDS ${PROJECT}
|
||||||
|
WORKING_DIRECTORY "..")
|
||||||
@@ -13,14 +13,21 @@
|
|||||||
#include "ruc/format/color.h"
|
#include "ruc/format/color.h"
|
||||||
#include "ruc/format/print.h"
|
#include "ruc/format/print.h"
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "repl.h"
|
#include "blaze/repl.h"
|
||||||
#include "settings.h"
|
#include "blaze/settings.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
|
static auto cleanup(int signal) -> void
|
||||||
|
{
|
||||||
|
print("\033[0m\n");
|
||||||
|
Repl::cleanup();
|
||||||
|
std::exit(signal);
|
||||||
|
}
|
||||||
|
|
||||||
auto main(int argc, char* argv[]) -> int
|
auto main(int argc, char* argv[]) -> int
|
||||||
{
|
{
|
||||||
bool dump_lexer = false;
|
bool dump_lexer = false;
|
||||||
@@ -39,17 +46,17 @@ auto main(int argc, char* argv[]) -> int
|
|||||||
arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No);
|
arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No);
|
||||||
arg_parser.parse(argc, argv);
|
arg_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
Repl::init();
|
||||||
|
|
||||||
|
// Signal callbacks
|
||||||
|
std::signal(SIGINT, cleanup);
|
||||||
|
std::signal(SIGTERM, cleanup);
|
||||||
|
|
||||||
// Set settings
|
// Set settings
|
||||||
g_outer_env->set("*DUMP-LEXER*", makePtr<Constant>(dump_lexer));
|
g_outer_env->set("*DUMP-LEXER*", makePtr<Constant>(dump_lexer));
|
||||||
g_outer_env->set("*DUMP-READER*", makePtr<Constant>(dump_reader));
|
g_outer_env->set("*DUMP-READER*", makePtr<Constant>(dump_reader));
|
||||||
g_outer_env->set("*PRETTY-PRINT*", makePtr<Constant>(pretty_print));
|
g_outer_env->set("*PRETTY-PRINT*", makePtr<Constant>(pretty_print));
|
||||||
|
|
||||||
// Signal callbacks
|
|
||||||
std::signal(SIGINT, Repl::cleanup);
|
|
||||||
std::signal(SIGTERM, Repl::cleanup);
|
|
||||||
|
|
||||||
Environment::loadFunctions();
|
|
||||||
Environment::installFunctions(g_outer_env);
|
|
||||||
Repl::makeArgv(g_outer_env, arguments);
|
Repl::makeArgv(g_outer_env, arguments);
|
||||||
|
|
||||||
if (arguments.size() > 0) {
|
if (arguments.size() > 0) {
|
||||||
@@ -73,6 +80,8 @@ auto main(int argc, char* argv[]) -> int
|
|||||||
print("\033[0m");
|
print("\033[0m");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Repl::cleanup();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10,12 +10,12 @@
|
|||||||
#include <utility> // std::move
|
#include <utility> // std::move
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "printer.h"
|
#include "blaze/printer.h"
|
||||||
#include "types.h"
|
#include "blaze/types.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -11,16 +11,18 @@
|
|||||||
#include <functional> // std::function
|
#include <functional> // std::function
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory> // std::shared_ptr
|
#include <memory> // std::make_shared, std::shared_ptr
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <typeinfo> // typeid
|
#include <typeinfo> // typeid
|
||||||
|
#include <utility> // std::forward
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "ruc/format/formatter.h"
|
#include "ruc/format/formatter.h"
|
||||||
|
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
|
#include "blaze/to-from-hashmap.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -77,14 +79,13 @@ protected:
|
|||||||
#define WITH_META(Type) \
|
#define WITH_META(Type) \
|
||||||
virtual ValuePtr withMetaImpl(ValuePtr meta) const override \
|
virtual ValuePtr withMetaImpl(ValuePtr meta) const override \
|
||||||
{ \
|
{ \
|
||||||
return makePtr<Type>(*this, meta); \
|
return std::make_shared<Type>(*this, meta); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define WITH_NO_META() \
|
#define WITH_NO_META() \
|
||||||
virtual ValuePtr withMetaImpl(ValuePtr meta) const override \
|
virtual ValuePtr withMetaImpl(ValuePtr) const override \
|
||||||
{ \
|
{ \
|
||||||
(void)meta; \
|
return nullptr; \
|
||||||
return nullptr; \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
@@ -195,6 +196,20 @@ public:
|
|||||||
HashMap(const HashMap& that, ValuePtr meta);
|
HashMap(const HashMap& that, ValuePtr meta);
|
||||||
virtual ~HashMap() = default;
|
virtual ~HashMap() = default;
|
||||||
|
|
||||||
|
static HashMapPtr create(const Elements& elements)
|
||||||
|
{
|
||||||
|
return std::make_shared<HashMap>(elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Customization Point
|
||||||
|
template<typename T>
|
||||||
|
static HashMapPtr create(T value)
|
||||||
|
{
|
||||||
|
HashMapPtr hash_map;
|
||||||
|
to_hash_map(hash_map, std::forward<T>(value));
|
||||||
|
return hash_map;
|
||||||
|
}
|
||||||
|
|
||||||
static std::string getKeyString(ValuePtr key);
|
static std::string getKeyString(ValuePtr key);
|
||||||
|
|
||||||
bool exists(const std::string& key);
|
bool exists(const std::string& key);
|
||||||
@@ -222,6 +237,11 @@ public:
|
|||||||
String(char character);
|
String(char character);
|
||||||
virtual ~String() = default;
|
virtual ~String() = default;
|
||||||
|
|
||||||
|
static ValuePtr create(const std::string& data)
|
||||||
|
{
|
||||||
|
return std::make_shared<String>(data);
|
||||||
|
}
|
||||||
|
|
||||||
const std::string& data() const { return m_data; }
|
const std::string& data() const { return m_data; }
|
||||||
size_t size() const { return m_data.size(); }
|
size_t size() const { return m_data.size(); }
|
||||||
bool empty() const { return m_data.empty(); }
|
bool empty() const { return m_data.empty(); }
|
||||||
@@ -287,6 +307,11 @@ public:
|
|||||||
Decimal(double decimal);
|
Decimal(double decimal);
|
||||||
virtual ~Decimal() = default;
|
virtual ~Decimal() = default;
|
||||||
|
|
||||||
|
static ValuePtr create(float value)
|
||||||
|
{
|
||||||
|
return std::make_shared<Decimal>(value);
|
||||||
|
}
|
||||||
|
|
||||||
double decimal() const { return m_decimal; }
|
double decimal() const { return m_decimal; }
|
||||||
|
|
||||||
WITH_NO_META();
|
WITH_NO_META();
|
||||||
+9
-5
@@ -11,11 +11,11 @@
|
|||||||
#include "ruc/file.h"
|
#include "ruc/file.h"
|
||||||
#include "ruc/format/format.h"
|
#include "ruc/format/format.h"
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "repl.h"
|
#include "blaze/repl.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -79,12 +79,16 @@ EnvironmentPtr Environment::create(const ValuePtr lambda, ValueVector&& argument
|
|||||||
|
|
||||||
void Environment::loadFunctions()
|
void Environment::loadFunctions()
|
||||||
{
|
{
|
||||||
|
s_function_parts.clear();
|
||||||
|
s_lambdas.clear();
|
||||||
|
|
||||||
loadCollectionAccess();
|
loadCollectionAccess();
|
||||||
loadCollectionConstructor();
|
loadCollectionConstructor();
|
||||||
loadCollectionModify();
|
loadCollectionModify();
|
||||||
loadCompare();
|
loadCompare();
|
||||||
loadConvert();
|
loadConvert();
|
||||||
loadFormat();
|
loadFormat();
|
||||||
|
loadMath();
|
||||||
loadMeta();
|
loadMeta();
|
||||||
loadMutable();
|
loadMutable();
|
||||||
loadOperators();
|
loadOperators();
|
||||||
+3
-2
@@ -11,8 +11,8 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -51,6 +51,7 @@ private:
|
|||||||
static void loadCompare();
|
static void loadCompare();
|
||||||
static void loadConvert();
|
static void loadConvert();
|
||||||
static void loadFormat();
|
static void loadFormat();
|
||||||
|
static void loadMath();
|
||||||
static void loadMeta();
|
static void loadMeta();
|
||||||
static void loadMutable();
|
static void loadMutable();
|
||||||
static void loadOperators();
|
static void loadOperators();
|
||||||
+4
-4
@@ -7,10 +7,10 @@
|
|||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <memory> // std:static_pointer_cast
|
#include <memory> // std:static_pointer_cast
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
Vendored
+4
-4
@@ -7,10 +7,10 @@
|
|||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <memory> // std:static_pointer_cast
|
#include <memory> // std:static_pointer_cast
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
+6
-6
@@ -8,12 +8,12 @@
|
|||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <memory> // std::static_pointer_cast
|
#include <memory> // std::static_pointer_cast
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "repl.h"
|
#include "blaze/repl.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -8,9 +8,9 @@
|
|||||||
#include <functional> // std::function
|
#include <functional> // std::function
|
||||||
#include <memory> // std::static_pointer_cast
|
#include <memory> // std::static_pointer_cast
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -8,9 +8,9 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <system_error> // std::errc
|
#include <system_error> // std::errc
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -7,13 +7,13 @@
|
|||||||
#include <iterator> // std::next
|
#include <iterator> // std::next
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "reader.h"
|
|
||||||
#include "ruc/format/print.h"
|
#include "ruc/format/print.h"
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "printer.h"
|
#include "blaze/printer.h"
|
||||||
#include "util.h"
|
#include "blaze/reader.h"
|
||||||
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
Vendored
+82
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 Riyyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cmath> // std::sin
|
||||||
|
#include <limits> // sdt::numeric_limits
|
||||||
|
#include <memory> // std::static_pointer_cast
|
||||||
|
|
||||||
|
#include "blaze/ast.h"
|
||||||
|
#include "blaze/env/macro.h"
|
||||||
|
#include "blaze/util.h"
|
||||||
|
|
||||||
|
namespace blaze {
|
||||||
|
|
||||||
|
void Environment::loadMath()
|
||||||
|
{
|
||||||
|
#define MATH_MAX_MIN(variant, limit, operator) \
|
||||||
|
{ \
|
||||||
|
CHECK_ARG_COUNT_AT_LEAST(#variant, SIZE(), 1); \
|
||||||
|
\
|
||||||
|
int64_t number = std::numeric_limits<int64_t>::limit(); \
|
||||||
|
double decimal = std::numeric_limits<double>::limit(); \
|
||||||
|
\
|
||||||
|
for (auto it = begin; it != end; ++it) { \
|
||||||
|
IS_VALUE(Numeric, (*it)); \
|
||||||
|
if (is<Number>(it->get())) { \
|
||||||
|
auto it_numeric = std::static_pointer_cast<Number>(*it)->number(); \
|
||||||
|
if (it_numeric operator number) { \
|
||||||
|
number = it_numeric; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
auto it_numeric = std::static_pointer_cast<Decimal>(*it)->decimal(); \
|
||||||
|
if (it_numeric operator decimal) { \
|
||||||
|
decimal = it_numeric; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (number operator decimal) { \
|
||||||
|
return makePtr<Number>(number); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
return makePtr<Decimal>(decimal); \
|
||||||
|
}
|
||||||
|
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"max", "number...",
|
||||||
|
"Return largest of all arguments, where NUMBER is a number or decimal.",
|
||||||
|
MATH_MAX_MIN(max, lowest, >));
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"min", "number...",
|
||||||
|
"Return smallest of all arguments, where NUMBER is a number or decimal.",
|
||||||
|
MATH_MAX_MIN(min, max, <));
|
||||||
|
|
||||||
|
#define MATH_COS_SIN(variant) \
|
||||||
|
{ \
|
||||||
|
CHECK_ARG_COUNT_IS(#variant, SIZE(), 1); \
|
||||||
|
\
|
||||||
|
auto value = *begin; \
|
||||||
|
IS_VALUE(Numeric, value); \
|
||||||
|
if (is<Number>(begin->get())) { \
|
||||||
|
return makePtr<Decimal>(std::variant((double)std::static_pointer_cast<Number>(value)->number())); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
return makePtr<Decimal>(std::variant(std::static_pointer_cast<Decimal>(value)->decimal())); \
|
||||||
|
}
|
||||||
|
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"cos", "arg",
|
||||||
|
"Return the cosine of ARG.",
|
||||||
|
MATH_COS_SIN(cos));
|
||||||
|
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"sin", "arg",
|
||||||
|
"Return the sine of ARG.",
|
||||||
|
MATH_COS_SIN(sin));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace blaze
|
||||||
@@ -4,11 +4,11 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -7,12 +7,12 @@
|
|||||||
#include <algorithm> // std::copy
|
#include <algorithm> // std::copy
|
||||||
#include <memory> // std::static_pointer_cast
|
#include <memory> // std::static_pointer_cast
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "repl.h"
|
#include "blaze/repl.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -7,9 +7,9 @@
|
|||||||
#include <cstdint> // int64_t
|
#include <cstdint> // int64_t
|
||||||
#include <memory> // std::static_pointer_cast
|
#include <memory> // std::static_pointer_cast
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
Vendored
+78
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 Riyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <chrono> // std::chrono::sytem_clock
|
||||||
|
#include <cstdint> // int64_t
|
||||||
|
#include <filesystem> // std::filesystem::current_path
|
||||||
|
|
||||||
|
#include "ruc/file.h"
|
||||||
|
|
||||||
|
#include "blaze/ast.h"
|
||||||
|
#include "blaze/env/macro.h"
|
||||||
|
#include "blaze/error.h"
|
||||||
|
#include "blaze/forward.h"
|
||||||
|
#include "blaze/util.h"
|
||||||
|
|
||||||
|
namespace blaze {
|
||||||
|
|
||||||
|
void Environment::loadOther()
|
||||||
|
{
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"pwd", "",
|
||||||
|
"Return the full filename of the current working directory.",
|
||||||
|
{
|
||||||
|
CHECK_ARG_COUNT_IS("pwd", SIZE(), 0);
|
||||||
|
|
||||||
|
auto path = std::filesystem::current_path().string();
|
||||||
|
return makePtr<String>(path);
|
||||||
|
});
|
||||||
|
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"slurp", "",
|
||||||
|
"Read file contents",
|
||||||
|
{
|
||||||
|
CHECK_ARG_COUNT_IS("slurp", SIZE(), 1);
|
||||||
|
|
||||||
|
VALUE_CAST(node, String, (*begin));
|
||||||
|
std::string path = node->data();
|
||||||
|
|
||||||
|
auto file = ruc::File(path);
|
||||||
|
|
||||||
|
return makePtr<String>(file.data());
|
||||||
|
});
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
|
||||||
|
// (throw x)
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"throw", "",
|
||||||
|
"",
|
||||||
|
{
|
||||||
|
CHECK_ARG_COUNT_IS("throw", SIZE(), 1);
|
||||||
|
|
||||||
|
Error::the().add(*begin);
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
})
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
|
||||||
|
// (time-ms)
|
||||||
|
ADD_FUNCTION(
|
||||||
|
"time-ms", "",
|
||||||
|
"",
|
||||||
|
{
|
||||||
|
CHECK_ARG_COUNT_IS("time-ms", SIZE(), 0);
|
||||||
|
|
||||||
|
int64_t elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
std::chrono::system_clock::now().time_since_epoch())
|
||||||
|
.count();
|
||||||
|
|
||||||
|
return makePtr<Number>(elapsed);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace blaze
|
||||||
@@ -4,9 +4,9 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/env/macro.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -6,12 +6,10 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "ruc/file.h"
|
#include "blaze/ast.h"
|
||||||
|
#include "blaze/env/macro.h"
|
||||||
#include "ast.h"
|
#include "blaze/repl.h"
|
||||||
#include "env/macro.h"
|
#include "blaze/util.h"
|
||||||
#include "repl.h"
|
|
||||||
#include "util.h"
|
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -31,22 +29,6 @@ void Environment::loadRepl()
|
|||||||
return Repl::read(input);
|
return Repl::read(input);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Read file contents
|
|
||||||
ADD_FUNCTION(
|
|
||||||
"slurp",
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
{
|
|
||||||
CHECK_ARG_COUNT_IS("slurp", SIZE(), 1);
|
|
||||||
|
|
||||||
VALUE_CAST(node, String, (*begin));
|
|
||||||
std::string path = node->data();
|
|
||||||
|
|
||||||
auto file = ruc::File(path);
|
|
||||||
|
|
||||||
return makePtr<String>(file.data());
|
|
||||||
});
|
|
||||||
|
|
||||||
// Prompt readline
|
// Prompt readline
|
||||||
ADD_FUNCTION(
|
ADD_FUNCTION(
|
||||||
"readline",
|
"readline",
|
||||||
+7
-3
@@ -4,15 +4,19 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iterator> // std::distance
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
|
#include "blaze/forward.h"
|
||||||
|
|
||||||
#define ADD_FUNCTION(name, signature, documentation, lambda) \
|
#define ADD_FUNCTION(name, signature, documentation, lambda) \
|
||||||
Environment::registerFunction( \
|
blaze::Environment::registerFunction( \
|
||||||
{ name, \
|
{ name, \
|
||||||
signature, \
|
signature, \
|
||||||
documentation, \
|
documentation, \
|
||||||
[](ValueVectorConstIt begin, ValueVectorConstIt end) -> blaze::ValuePtr lambda });
|
[](blaze::ValueVectorConstIt begin, blaze::ValueVectorConstIt end) -> blaze::ValuePtr lambda });
|
||||||
|
|
||||||
#define SIZE() std::distance(begin, end)
|
#define SIZE() std::distance(begin, end)
|
||||||
@@ -10,8 +10,8 @@
|
|||||||
|
|
||||||
#include "ruc/singleton.h"
|
#include "ruc/singleton.h"
|
||||||
|
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "lexer.h"
|
#include "blaze/lexer.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -16,16 +16,16 @@
|
|||||||
#include "ruc/format/format.h"
|
#include "ruc/format/format.h"
|
||||||
#include "ruc/format/print.h"
|
#include "ruc/format/print.h"
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "eval.h"
|
#include "blaze/eval.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "macro.h"
|
#include "blaze/macro.h"
|
||||||
#include "printer.h"
|
#include "blaze/printer.h"
|
||||||
#include "settings.h"
|
#include "blaze/settings.h"
|
||||||
#include "types.h"
|
#include "blaze/types.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -10,13 +10,13 @@
|
|||||||
#include <span> // std::span
|
#include <span> // std::span
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "eval.h"
|
#include "blaze/eval.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "types.h"
|
#include "blaze/types.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "forward.h" // EnvironmentPtr
|
#include "blaze/forward.h" // EnvironmentPtr
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -15,7 +15,9 @@ namespace blaze {
|
|||||||
// Types
|
// Types
|
||||||
|
|
||||||
class Value;
|
class Value;
|
||||||
|
class HashMap;
|
||||||
typedef std::shared_ptr<Value> ValuePtr;
|
typedef std::shared_ptr<Value> ValuePtr;
|
||||||
|
typedef std::shared_ptr<HashMap> HashMapPtr;
|
||||||
typedef std::vector<ValuePtr> ValueVector;
|
typedef std::vector<ValuePtr> ValueVector;
|
||||||
typedef ValueVector::iterator ValueVectorIt;
|
typedef ValueVector::iterator ValueVectorIt;
|
||||||
typedef ValueVector::reverse_iterator ValueVectorReverseIt;
|
typedef ValueVector::reverse_iterator ValueVectorReverseIt;
|
||||||
@@ -11,8 +11,8 @@
|
|||||||
#include "ruc/format/print.h"
|
#include "ruc/format/print.h"
|
||||||
#include "ruc/genericlexer.h"
|
#include "ruc/genericlexer.h"
|
||||||
|
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "lexer.h"
|
#include "blaze/lexer.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -4,7 +4,9 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "eval.h"
|
#pragma once
|
||||||
|
|
||||||
|
#include "blaze/eval.h"
|
||||||
|
|
||||||
#define CONCAT(a, b) CONCAT_IMPL(a, b)
|
#define CONCAT(a, b) CONCAT_IMPL(a, b)
|
||||||
#define CONCAT_IMPL(a, b) a##b
|
#define CONCAT_IMPL(a, b) a##b
|
||||||
@@ -11,13 +11,13 @@
|
|||||||
#include "ruc/format/color.h"
|
#include "ruc/format/color.h"
|
||||||
#include "ruc/format/format.h"
|
#include "ruc/format/format.h"
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "lexer.h"
|
#include "blaze/lexer.h"
|
||||||
#include "printer.h"
|
#include "blaze/printer.h"
|
||||||
#include "settings.h"
|
#include "blaze/settings.h"
|
||||||
#include "types.h"
|
#include "blaze/types.h"
|
||||||
#include "util.h"
|
#include "blaze/util.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -6,9 +6,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "ast.h"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "blaze/ast.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
// Serializer -> return to string
|
// Serializer -> return to string
|
||||||
@@ -12,14 +12,14 @@
|
|||||||
#include <system_error> // std::errc
|
#include <system_error> // std::errc
|
||||||
#include <utility> // std::move
|
#include <utility> // std::move
|
||||||
|
|
||||||
#include "error.h"
|
|
||||||
#include "ruc/format/color.h"
|
#include "ruc/format/color.h"
|
||||||
#include "ruc/meta/assert.h"
|
#include "ruc/meta/assert.h"
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "reader.h"
|
#include "blaze/error.h"
|
||||||
#include "settings.h"
|
#include "blaze/reader.h"
|
||||||
#include "types.h"
|
#include "blaze/settings.h"
|
||||||
|
#include "blaze/types.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -10,8 +10,8 @@
|
|||||||
#include <memory> // std::shared_ptr
|
#include <memory> // std::shared_ptr
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "ast.h"
|
#include "blaze/ast.h"
|
||||||
#include "lexer.h"
|
#include "blaze/lexer.h"
|
||||||
|
|
||||||
#define INDENTATION_WIDTH 2
|
#define INDENTATION_WIDTH 2
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "ruc/format/color.h"
|
#include "ruc/format/color.h"
|
||||||
#include "ruc/format/print.h"
|
#include "ruc/format/print.h"
|
||||||
|
|
||||||
#include "readline.h"
|
#include "blaze/readline.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -11,26 +11,32 @@
|
|||||||
|
|
||||||
#include "ruc/format/print.h"
|
#include "ruc/format/print.h"
|
||||||
|
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "eval.h"
|
#include "blaze/eval.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "lexer.h"
|
#include "blaze/lexer.h"
|
||||||
#include "printer.h"
|
#include "blaze/printer.h"
|
||||||
#include "reader.h"
|
#include "blaze/reader.h"
|
||||||
#include "readline.h"
|
#include "blaze/readline.h"
|
||||||
#include "repl.h"
|
#include "blaze/repl.h"
|
||||||
#include "settings.h"
|
#include "blaze/settings.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
Readline g_readline;
|
Readline g_readline;
|
||||||
EnvironmentPtr g_outer_env = Environment::create();
|
EnvironmentPtr g_outer_env;
|
||||||
|
|
||||||
auto Repl::cleanup(int signal) -> void
|
auto Repl::init() -> void
|
||||||
{
|
{
|
||||||
::print("\033[0m\n");
|
g_outer_env = Environment::create();
|
||||||
std::exit(signal);
|
Environment::loadFunctions();
|
||||||
|
Environment::installFunctions(g_outer_env);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto Repl::cleanup() -> void
|
||||||
|
{
|
||||||
|
g_outer_env = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Repl::readline(const std::string& prompt) -> ValuePtr
|
auto Repl::readline(const std::string& prompt) -> ValuePtr
|
||||||
@@ -10,14 +10,16 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "readline.h"
|
#include "blaze/readline.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
class Repl {
|
class Repl {
|
||||||
public:
|
public:
|
||||||
static auto cleanup(int signal) -> void;
|
static auto init() -> void;
|
||||||
|
static auto cleanup() -> void;
|
||||||
|
|
||||||
static auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr;
|
static auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr;
|
||||||
static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void;
|
static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void;
|
||||||
static auto print(ValuePtr value) -> std::string;
|
static auto print(ValuePtr value) -> std::string;
|
||||||
@@ -8,10 +8,10 @@
|
|||||||
|
|
||||||
#include "ruc/meta/assert.h"
|
#include "ruc/meta/assert.h"
|
||||||
|
|
||||||
#include "env/environment.h"
|
#include "blaze/env/environment.h"
|
||||||
#include "forward.h"
|
#include "blaze/forward.h"
|
||||||
#include "settings.h"
|
#include "blaze/settings.h"
|
||||||
#include "types.h"
|
#include "blaze/types.h"
|
||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 Riyyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility> // std::forward
|
||||||
|
|
||||||
|
#include "ruc/meta/odr.h"
|
||||||
|
|
||||||
|
namespace blaze {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
// struct hashMapConstructor {
|
||||||
|
// template<typename HashMap>
|
||||||
|
// static void construct(HashMap& hash_map, bool boolean)
|
||||||
|
// {
|
||||||
|
// //..
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// template<typename HashMap, typename T>
|
||||||
|
// void toHashMap(HashMap& hash_map, const T& value)
|
||||||
|
// {
|
||||||
|
// hashMapConstructor::construct(hash_map, value);
|
||||||
|
// }
|
||||||
|
|
||||||
|
struct toHashMapFunction {
|
||||||
|
template<typename HashMapPtr, typename T>
|
||||||
|
auto operator()(HashMapPtr& hash_map, T&& value) const
|
||||||
|
{
|
||||||
|
return to_hash_map(hash_map, std::forward<T>(value));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// Anonymous namespace prevents multiple definition of the reference
|
||||||
|
namespace {
|
||||||
|
// Function object
|
||||||
|
constexpr const auto& to_hash_map = ruc::detail::staticConst<detail::toHashMapFunction>; // NOLINT(misc-definitions-in-headers,clang-diagnostic-unused-variable)
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
} // namespace blaze
|
||||||
|
|
||||||
|
// Customization Points
|
||||||
|
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
|
||||||
|
|
||||||
|
// blaze::to_hash_map is a function object, the type of which is
|
||||||
|
// blaze::detail::toHashMapFunction. In the blaze::detail namespace are the to_hash_map
|
||||||
|
// free functions. The function call operator of toHashMapFunction makes an
|
||||||
|
// unqualified call to to_hash_map which, since it shares the detail namespace with
|
||||||
|
// the to_hash_map free functions, will consider those in addition to any overloads
|
||||||
|
// that are found by argument-dependent lookup.
|
||||||
|
|
||||||
|
// Variable templates are linked externally, therefor every translation unit
|
||||||
|
// will see the same address for detail::staticConst<detail::toHashMapFunction>.
|
||||||
|
// Since blaze::to_hash_map is a reference to the variable template, it too will have
|
||||||
|
// the same address in all translation units.
|
||||||
@@ -10,8 +10,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "error.h"
|
#include "blaze/error.h"
|
||||||
#include "types.h"
|
#include "blaze/types.h"
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
@@ -34,10 +34,10 @@
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_IS_IMPL(name, size, expected, result) \
|
#define CHECK_ARG_COUNT_IS_IMPL(name, size, expected, result) \
|
||||||
if (size != expected) { \
|
if (size != expected) { \
|
||||||
Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
blaze::Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
||||||
return result; \
|
return result; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_IS_3(name, size, expected) \
|
#define CHECK_ARG_COUNT_IS_3(name, size, expected) \
|
||||||
@@ -52,10 +52,10 @@
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_AT_LEAST_IMPL(name, size, min, result) \
|
#define CHECK_ARG_COUNT_AT_LEAST_IMPL(name, size, min, result) \
|
||||||
if (size < min) { \
|
if (size < min) { \
|
||||||
Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
blaze::Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
||||||
return result; \
|
return result; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_AT_LEAST_3(name, size, min) \
|
#define CHECK_ARG_COUNT_AT_LEAST_3(name, size, min) \
|
||||||
@@ -70,10 +70,10 @@
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_BETWEEN_IMPL(name, size, min, max, result) \
|
#define CHECK_ARG_COUNT_BETWEEN_IMPL(name, size, min, max, result) \
|
||||||
if (size < min || size > max) { \
|
if (size < min || size > max) { \
|
||||||
Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
blaze::Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
||||||
return result; \
|
return result; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_BETWEEN_4(name, size, min, max) \
|
#define CHECK_ARG_COUNT_BETWEEN_4(name, size, min, max) \
|
||||||
@@ -88,10 +88,10 @@
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_EVEN_IMPL(name, size, result) \
|
#define CHECK_ARG_COUNT_EVEN_IMPL(name, size, result) \
|
||||||
if (size % 2 != 0) { \
|
if (size % 2 != 0) { \
|
||||||
Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
blaze::Error::the().add(::format("wrong number of arguments: {}, {}", name, size)); \
|
||||||
return result; \
|
return result; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK_ARG_COUNT_EVEN_2(name, size) \
|
#define CHECK_ARG_COUNT_EVEN_2(name, size) \
|
||||||
@@ -106,10 +106,10 @@
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
#define IS_VALUE_IMPL(type, value, result) \
|
#define IS_VALUE_IMPL(type, value, result) \
|
||||||
if (!is<type>(value.get())) { \
|
if (!is<type>(value.get())) { \
|
||||||
Error::the().add(::format("wrong argument type: {}, {}", #type, value)); \
|
blaze::Error::the().add(::format("wrong argument type: {}, {}", #type, value)); \
|
||||||
return result; \
|
return result; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define IS_VALUE_2(type, value) \
|
#define IS_VALUE_2(type, value) \
|
||||||
Vendored
-51
@@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2023 Riyi
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <chrono> // std::chrono::sytem_clock
|
|
||||||
#include <cstdint> // int64_t
|
|
||||||
|
|
||||||
#include "ast.h"
|
|
||||||
#include "env/macro.h"
|
|
||||||
#include "error.h"
|
|
||||||
#include "forward.h"
|
|
||||||
#include "util.h"
|
|
||||||
|
|
||||||
namespace blaze {
|
|
||||||
|
|
||||||
void Environment::loadOther()
|
|
||||||
{
|
|
||||||
// (throw x)
|
|
||||||
ADD_FUNCTION(
|
|
||||||
"throw",
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
{
|
|
||||||
CHECK_ARG_COUNT_IS("throw", SIZE(), 1);
|
|
||||||
|
|
||||||
Error::the().add(*begin);
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
})
|
|
||||||
|
|
||||||
// -----------------------------------------
|
|
||||||
|
|
||||||
// (time-ms)
|
|
||||||
ADD_FUNCTION(
|
|
||||||
"time-ms",
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
{
|
|
||||||
CHECK_ARG_COUNT_IS("time-ms", SIZE(), 0);
|
|
||||||
|
|
||||||
int64_t elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
||||||
std::chrono::system_clock::now().time_since_epoch())
|
|
||||||
.count();
|
|
||||||
|
|
||||||
return makePtr<Number>(elapsed);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace blaze
|
|
||||||
Reference in New Issue
Block a user