better boolean handling..

still T symbol should be implemented
This commit is contained in:
VaclavT 2021-05-05 21:43:08 +02:00
parent ea76cdf0be
commit 783aa6976b
3 changed files with 16 additions and 13 deletions

20
ml.cpp
View File

@ -72,6 +72,8 @@ MlValue::MlValue(long i) : type(INT) { stack_data.i = i; }
MlValue::MlValue(double f) : type(FLOAT) { stack_data.f = f; }
MlValue::MlValue(bool b) { if (b) {type = INT; stack_data.i = 1;} else {type = NIL;} }
MlValue::MlValue(const std::vector<MlValue> &list) : type(LIST), list(list) {}
MlValue MlValue::quote(const MlValue &quoted) {
@ -1240,22 +1242,20 @@ namespace builtin {
MlValue is_file(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
// TODO add support for more params and list params
if (args.size() != 1)
throw MlError(MlValue("is-file?", is_file), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue((long)is_path_file(args[0].as_string()));
return MlValue(is_path_file(args[0].as_string()));
}
// is_path directory
MlValue is_dir(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
// TODO add support for more params and list params
if (args.size() != 1)
throw MlError(MlValue("is-dir?", is_dir), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue((long)is_path_dir(args[0].as_string()));
return MlValue(is_path_dir(args[0].as_string()));
}
// Read a file and execute its code
@ -1349,7 +1349,7 @@ namespace builtin {
if (args.size() != 2)
throw MlError(MlValue("=", eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(long(args[0] == args[1]));
return MlValue(args[0] == args[1]);
}
// Are two values not equal?
@ -1358,7 +1358,7 @@ namespace builtin {
if (args.size() != 2)
throw MlError(MlValue("!=", neq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(long(args[0] != args[1]));
return MlValue(args[0] != args[1]);
}
// Is one number greater than another?
@ -1367,7 +1367,7 @@ namespace builtin {
if (args.size() != 2)
throw MlError(MlValue(">", greater), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(long(args[0] > args[1]));
return MlValue(args[0] > args[1]);
}
// Is one number less than another?
@ -1376,7 +1376,7 @@ namespace builtin {
if (args.size() != 2)
throw MlError(MlValue("<", less), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(long(args[0] < args[1]));
return MlValue(args[0] < args[1]);
}
// Is one number greater than or equal to another?
@ -1385,7 +1385,7 @@ namespace builtin {
if (args.size() != 2)
throw MlError(MlValue(">=", greater_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(long(args[0] >= args[1]));
return MlValue(args[0] >= args[1]);
}
// Is one number less than or equal to another?
@ -1394,7 +1394,7 @@ namespace builtin {
if (args.size() != 2)
throw MlError(MlValue("<=", less_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(long(args[0] <= args[1]));
return MlValue(args[0] <= args[1]);
}
// Get the type name of a value

3
ml.h
View File

@ -110,6 +110,9 @@ public:
// Constructs a floating point value
MlValue(double f);
// Constructs a bool value
MlValue(bool b);
// Constructs a list
MlValue(const std::vector<MlValue> &list);

View File

@ -1,5 +1,3 @@
; it is expected to be called from parent directory ie: ml -f tests/test.lisp
(include "/usr/local/var/mlisp/ut.lsp")
;; prepare some code to be used in tests
@ -55,7 +53,7 @@
(ut::define-test "result of (string-split \"split me by space\" \"\\s+\")" '(ut::assert-equal '("split" "me" "by" "space") (string-split "split me by space" "\\s+")))
(ut::define-test "result of (string-upcase \"abcABCD\")" '(ut::assert-equal "ABCABCD" (string-upcase "abcABCD")))
(ut::define-test "result of (string-downcase \"abcABCD\")" '(ut::assert-equal "!abcabcd" (string-downcase "abcABCD")))
(ut::define-test "result of (string-downcase \"abcABCD\")" '(ut::assert-equal "abcabcd" (string-downcase "abcABCD")))
(ut::define-test "result of (write-file \"/tmp/file\" \"write-file test\")" '(ut::assert-equal 1 (write-file "/tmp/file" "write-file test\n")))
(ut::define-test "result of (is-file? \"/tmp/file\")" '(ut::assert-true (is-file? "/tmp/file")))
@ -75,6 +73,8 @@
(ut::define-test "result of (get-env \"HOME\")" '(ut::assert-equal "/Users/vaclavt" (get-env "HOME")))
(ut::define-test "result of (!= nil nil)" '(ut::assert-false (!= nil nil)))
;(ut::define-test "result of " '(ut::assert-true )
(ut::run-tests)