error description less verbose, some params checking added, comments

This commit is contained in:
VaclavT 2021-03-02 23:13:05 +01:00
parent 3868a32168
commit 903c8fd49d
1 changed files with 22 additions and 4 deletions

26
ml.cpp
View File

@ -635,7 +635,8 @@ MlError::~MlError() {
}
std::string MlError::description() {
return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" + msg + "\"";
// return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" + msg + "\"";
return "error: the expression `" + cause->debug() + "` with message \"" + msg + "\"";
}
void MlEnvironment::combine(MlEnvironment const &other) {
@ -1088,9 +1089,9 @@ namespace builtin {
HttpClient client;
if (args.size() == 2) {
for (const auto &pair_list: args[1].as_list()[0].as_list()) {
for (const auto &hdr_val_pair: args[1].as_list()) {
// TODO check its 2 string elements list
const auto &pair = pair_list.as_list();
const auto &pair = hdr_val_pair.as_list();
headers[pair[0].as_string()] = pair[1].as_string();
}
}
@ -1462,6 +1463,7 @@ namespace builtin {
if (args.size() != 1)
throw MlError(MlValue("head", head), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> list = args[0].as_list();
if (list.empty())
throw MlError(MlValue("head", head), env, INDEX_OUT_OF_RANGE);
@ -1551,6 +1553,9 @@ namespace builtin {
MlValue map_list(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 2)
throw MlError(MlValue("map_list", map_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result, l = args[1].as_list(), tmp;
for (size_t i = 0; i < l.size(); i++) {
tmp.push_back(l[i]);
@ -1565,6 +1570,9 @@ namespace builtin {
MlValue filter_list(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 2)
throw MlError(MlValue("filter_list", filter_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result, l = args[1].as_list(), tmp;
for (size_t i = 0; i < l.size(); i++) {
tmp.push_back(l[i]);
@ -1575,9 +1583,14 @@ namespace builtin {
return MlValue(result);
}
// >>> (reduce (lambda (x y) (+ (* x 10) y)) 0 '(1 2 3 4))
// => 1234
MlValue reduce_list(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 3)
throw MlError(MlValue("reduce_list", reduce_list), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> l = args[2].as_list(), tmp;
MlValue acc = args[1];
for (size_t i = 0; i < l.size(); i++) {
@ -1589,9 +1602,14 @@ namespace builtin {
return acc;
}
// >>> (range 1 5)
// => (1 2 3 4)
MlValue range(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 2)
throw MlError(MlValue("range", range), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result;
MlValue low = args[0], high = args[1];
if (low.get_type_name() != INT_TYPE && low.get_type_name() != FLOAT_TYPE)
@ -1782,7 +1800,7 @@ int main(int argc, const char **argv) {
try {
load_std_lib(env);
// for xcode profiling
run(read_file_contents("/Users/vaclavt/Development/mlisp/tests/test.lsp"), env);
// run(read_file_contents("/Users/vaclavt/Development/mlisp/tests/test.lsp"), env);
if (argc == 1 || (argc == 2 && std::string(argv[1]) == "-i"))
repl(env);