Main+Env: Add more native functions
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <memory> // std::static_pointer_cast
|
||||
#include <string>
|
||||
|
||||
#include "ruc/file.h"
|
||||
#include "ruc/format/format.h"
|
||||
|
||||
#include "ast.h"
|
||||
@@ -371,6 +372,58 @@ ADD_FUNCTION(
|
||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
||||
});
|
||||
|
||||
ADD_FUNCTION(
|
||||
"read-string",
|
||||
{
|
||||
if (nodes.size() != 1) {
|
||||
Error::the().add(format("wrong number of arguments: read-string, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!is<String>(nodes.front().get())) {
|
||||
Error::the().add(format("wrong argument type: string, '{}'", nodes.front()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string input = std::static_pointer_cast<String>(nodes.front())->data();
|
||||
|
||||
return read(input);
|
||||
});
|
||||
|
||||
ADD_FUNCTION(
|
||||
"slurp",
|
||||
{
|
||||
if (nodes.size() != 1) {
|
||||
Error::the().add(format("wrong number of arguments: slurp, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!is<String>(nodes.front().get())) {
|
||||
Error::the().add(format("wrong argument type: string, '{}'", nodes.front()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string path = std::static_pointer_cast<String>(nodes.front())->data();
|
||||
|
||||
auto file = ruc::File(path);
|
||||
|
||||
return makePtr<String>(file.data());
|
||||
});
|
||||
|
||||
|
||||
ADD_FUNCTION(
|
||||
"eval",
|
||||
{
|
||||
if (nodes.size() != 1) {
|
||||
Error::the().add(format("wrong number of arguments: eval, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return eval(nodes.front(), nullptr);
|
||||
});
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void installFunctions(EnvironmentPtr env)
|
||||
{
|
||||
for (const auto& [name, lambda] : s_functions) {
|
||||
|
||||
@@ -53,6 +53,10 @@ auto read(std::string_view input) -> ASTNodePtr
|
||||
|
||||
auto eval(ASTNodePtr ast, EnvironmentPtr env) -> ASTNodePtr
|
||||
{
|
||||
if (env == nullptr) {
|
||||
env = s_outer_env;
|
||||
}
|
||||
|
||||
Eval eval(ast, env);
|
||||
eval.eval();
|
||||
|
||||
@@ -76,6 +80,8 @@ static auto rep(std::string_view input, EnvironmentPtr env) -> std::string
|
||||
|
||||
static std::string_view lambdaTable[] = {
|
||||
"(def! not (fn* (cond) (if cond false true)))",
|
||||
"(def! load-file (fn* (filename) \
|
||||
(eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))",
|
||||
};
|
||||
|
||||
static auto installLambdas(EnvironmentPtr env) -> void
|
||||
|
||||
Reference in New Issue
Block a user