From 783aa6976be365095da9688b5b57b742cd624509 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Wed, 5 May 2021 21:43:08 +0200 Subject: [PATCH] better boolean handling.. still T symbol should be implemented --- ml.cpp | 20 ++++++++++---------- ml.h | 3 +++ tests/test.lsp | 6 +++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/ml.cpp b/ml.cpp index 563488a..b479735 100644 --- a/ml.cpp +++ b/ml.cpp @@ -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 &list) : type(LIST), list(list) {} MlValue MlValue::quote(const MlValue "ed) { @@ -1240,22 +1242,20 @@ namespace builtin { MlValue is_file(std::vector 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 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 diff --git a/ml.h b/ml.h index ec2fc5d..e0a96b3 100644 --- a/ml.h +++ b/ml.h @@ -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 &list); diff --git a/tests/test.lsp b/tests/test.lsp index 626fe7c..1ea1752 100644 --- a/tests/test.lsp +++ b/tests/test.lsp @@ -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)