sprintf added, terminal colors

small code fixes and rearangements
This commit is contained in:
2021-03-09 22:57:16 +01:00
parent b4862ee196
commit 3f46ae9c94
10 changed files with 392 additions and 13 deletions

26
ml.cpp
View File

@@ -7,6 +7,7 @@
#include "clib/csvparser.h"
#include "clib/sslclient.h"
#include "clib/json11.h"
#include "clib/printf.h"
#include <cmath>
@@ -176,7 +177,7 @@ int MlValue::as_int() const {
// Get this item's floating point value
double MlValue::as_float() const {
return cast_to_int().stack_data.f;
return cast_to_float().stack_data.f;
}
// Get this item's string value
@@ -728,7 +729,7 @@ MlValue parse(std::string &s, int &ptr) {
skip_whitespace(s, ptr);
if (s[ptr] == ';')
throw std::runtime_error("this should never happen!");
throw std::runtime_error(INTERNAL_ERROR);
if (s == "") {
@@ -1084,8 +1085,7 @@ namespace builtin {
// TODO add support for more params specifying options
if (args.size() != 1)
throw MlError(MlValue("parse-json", parse_json), env,
args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
throw MlError(MlValue("parse-json", parse_json), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::string str = args[0].as_string();
std::string err;
@@ -1099,7 +1099,7 @@ namespace builtin {
return json.ivalualize();
}
// get current time as secs from epoch
// Get current time as secs from epoch
MlValue get_universal_time(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -1113,8 +1113,7 @@ namespace builtin {
eval_args(args, env);
if (args.size() != 2)
throw MlError(MlValue("date_to_str", date_to_str), env,
args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
throw MlError(MlValue("date_to_str", date_to_str), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue::string(date_to_string(args[0].as_int(), args[1].as_string()));
}
@@ -1498,7 +1497,7 @@ namespace builtin {
eval_args(args, env);
if (args.size() != 4)
throw MlError(MlValue("regex_search", regex_search), env, args.size() > 4 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
throw MlError(MlValue("string_pad", string_pad), env, args.size() > 4 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
// TODO validate len > 0 etc
return MlValue::string(string_padd(args[0].as_string(), args[1].as_int(), args[2].as_string()[0], (args[3].as_string()=="rpad")));
@@ -1522,6 +1521,16 @@ namespace builtin {
return MlValue::string(args[0].debug());
}
MlValue sprintf(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
std::string result;
if (args.size() < 1 || args.size() > 2)
throw MlError(MlValue("sprintf", sprintf), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue::string(mini_sprintf(args[0].as_string(), args.size()==2 ? args[1].as_list() : std::vector<MlValue> {} ));
}
// >>> (map (lambda (x) (+ x 10)) '(1 2 3 4 5 6))
// => (11 12 13 14 15 16)
MlValue map_list(std::vector<MlValue> args, MlEnvironment &env) {
@@ -1741,6 +1750,7 @@ MlValue MlEnvironment::get(const std::string &name) const {
// String operations
if (name == "debug") return MlValue("debug", builtin::debug);
if (name == "sprintf") return MlValue("sprintf", builtin::sprintf);
if (name == "display") return MlValue("display", builtin::display);
if (name == "replace") return MlValue("replace", builtin::replace);
if (name == "regex-search?") return MlValue("regex-search?", builtin::regex_search);