new functions, doc updates

This commit is contained in:
vaclavt
2022-02-25 00:04:14 +01:00
parent c16b062267
commit 0f184461fe
5 changed files with 70 additions and 17 deletions

55
ml.cpp
View File

@@ -1153,6 +1153,30 @@ MlValue parse_csv(std::vector<MlValue> args, MlEnvironment &env) {
return csv.parseCSV(args[0].as_string());
}
// Reads in the printed representation of a Lisp object from input-stream, builds a corresponding Lisp object, and returns the object
MlValue read(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 0)
throw MlError(MlValue("read", read), env, TOO_MANY_ARGS);
std::string line;
std::getline(std::cin, line);
return run(line, env);
}
// Read line from stdin
MlValue read_line(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 0)
throw MlError(MlValue("read-line", read_line), env, TOO_MANY_ARGS);
std::string line;
std::getline(std::cin, line);
return MlValue::string(line);
}
// Get the contents of a file
MlValue read_file(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -1346,6 +1370,7 @@ MlValue is_dir(std::vector<MlValue> args, MlEnvironment &env) {
return MlValue(is_path_dir(args[0].as_string()));
}
// starts tcp listening server
MlValue tcp_server(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -1369,6 +1394,7 @@ MlValue tcp_server(std::vector<MlValue> args, MlEnvironment &env) {
return MlValue((long)r);
}
// tcp client
MlValue tcp_client(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -1389,6 +1415,31 @@ MlValue tcp_client(std::vector<MlValue> args, MlEnvironment &env) {
}
}
// get environment variable
MlValue get_env(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 1)
throw MlError(MlValue("get-env", get_env), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
if (const char* env_p = std::getenv(args[0].as_string().c_str()))
return MlValue::string(env_p);
else
return MlValue::nil();
}
// set environment variable
MlValue set_env(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 2)
throw MlError(MlValue("set-env", set_env), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
setenv(args[0].as_string().c_str(), args[1].as_string().c_str(), true);
return args[1];
}
// Read a file and execute its code
MlValue include(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -2160,6 +2211,8 @@ std::map <const std::string, Builtin> builtin_funcs
std::make_pair("input", builtin::input),
std::make_pair("random", builtin::random),
std::make_pair("include", builtin::include),
std::make_pair("read", builtin::read),
std::make_pair("read-line", builtin::read_line),
std::make_pair("read-file", builtin::read_file),
std::make_pair("read-file-lines", builtin::read_file_lines),
std::make_pair("write-file", builtin::write_file),
@@ -2170,6 +2223,8 @@ std::map <const std::string, Builtin> builtin_funcs
std::make_pair("is-dir?", builtin::is_dir),
std::make_pair("tcp-server", builtin::tcp_server),
std::make_pair("tcp-client", builtin::tcp_client),
std::make_pair("get-env", builtin::get_env),
std::make_pair("set-env", builtin::set_env),
// parsing operations
std::make_pair("parse-csv", builtin::parse_csv),