From 419d28f4f2e31a0f6179cc09052d97cd75236944 Mon Sep 17 00:00:00 2001 From: FelixBrendel Date: Tue, 31 Mar 2020 12:01:05 +0200 Subject: [PATCH] slime version 0.2 --- src/globals.cpp | 2 +- src/io.cpp | 2 + src/testing.cpp | 367 +++++++++++++++++++++--------------------------- todo.org | 8 +- 4 files changed, 170 insertions(+), 209 deletions(-) diff --git a/src/globals.cpp b/src/globals.cpp index 2884051..1eac348 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -1,6 +1,6 @@ namespace Slime { #define v_major 0 -#define v_minor 1 +#define v_minor 2 #define STRINGIZE2(s) #s #define STRINGIZE(s) STRINGIZE2(s) #define VERSION_STRING "v" STRINGIZE(v_major) "." STRINGIZE(v_minor) " - built on " __DATE__ " " __TIME__ diff --git a/src/io.cpp b/src/io.cpp index e01ef4c..826c1db 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -324,6 +324,8 @@ namespace Slime { string_builder.dealloc(); }; + if (!node) return _strdup(""); + switch (node->type) { case (Lisp_Object_Type::Nil): return _strdup("()"); case (Lisp_Object_Type::T): return _strdup("t"); diff --git a/src/testing.cpp b/src/testing.cpp index b626219..e4481de 100644 --- a/src/testing.cpp +++ b/src/testing.cpp @@ -317,205 +317,169 @@ namespace Slime { return pass; } - proc test_built_in_add() -> testresult { - char exp_string[] = "(+ 10 4)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string); - Lisp_Object* result; - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Number); - assert_equal_f64(result->value.number, 14); - - return pass; - } - - proc test_built_in_substract() -> testresult { - char exp_string[] = "(- 10 4)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string); - Lisp_Object* result; - - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Number); - assert_equal_f64(result->value.number, 6); - + proc test_string_copy() -> testresult { + Lisp_Object* str = Memory::create_lisp_object("Hello World"); + Lisp_Object* cpy = Memory::copy_lisp_object(str); + assert_not_equal_int(str, cpy); + assert_not_equal_int(str->value.string.data, cpy->value.string.data); return pass; } - - proc test_built_in_multiply() -> testresult { - char exp_string[] = "(* 10 4)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string); - Lisp_Object* result; - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Number); - assert_equal_f64(result->value.number, 40); - - return pass; - } - - - proc test_built_in_divide() -> testresult { - char exp_string[] = "(/ 20 4)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string); - Lisp_Object* result; - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Number); - assert_equal_f64(result->value.number, 5); - - return pass; - } - - - proc test_built_in_if() -> testresult { - char exp_string1[] = "(if 1 4 5)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string1); - Lisp_Object* result; - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Number); - assert_equal_f64(result->value.number, 4); - - char exp_string2[] = "(if () 4 5)"; - expression = Parser::parse_single_expression(exp_string2); - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Number); - assert_equal_f64(result->value.number, 5); - - return pass; - } - - proc test_built_in_and() -> testresult { - char exp_string1[] = "(and 1 \"asd\" 4)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string1); - Lisp_Object* result; - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::T); - - // a false case - char exp_string2[] = "(and () \"asd\" 4)"; - expression = Parser::parse_single_expression(exp_string2); - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Nil); - - return pass; - } - - proc test_built_in_or() -> testresult { - char exp_string1[] = "(or \"asd\" nil)"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string1); - Lisp_Object* result; - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::T); - - // a false case - char exp_string2[] = "(or () ())"; - expression = Parser::parse_single_expression(exp_string2); - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Nil); - - return pass; - } - - - proc test_built_in_not() -> testresult { - char exp_string1[] = "(not ())"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string1); - Lisp_Object* result; - try result = eval_expr(expression); - - // a true case - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::T); - - // a false case - char exp_string2[] = "(not \"asd xD\")"; - expression = Parser::parse_single_expression(exp_string2); - try result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Nil); - - return pass; - } - - proc test_built_in_type() -> testresult { - // Environment* env; - // try env = get_root_environment(); - - // normal type testing - char exp_string1[] = "(begin (define a 10)(type a))"; - Lisp_Object* expression = Parser::parse_single_expression(exp_string1); - Lisp_Object* result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Keyword); - assert_equal_string(result->value.symbol, "number"); - - // setting user type - char exp_string2[] = "(begin (set-type! a :my-type)(type a))"; - expression = Parser::parse_single_expression(exp_string2); - result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Keyword); - assert_equal_string(result->value.symbol, "my-type"); - - // // trying to set invalid user type - // char exp_string3[] = "(begin (set-type! a \"wrong tpye\")(type a))"; - // expression = Parser::parse_single_expression(exp_string3); - // assert_no_error(); - - // ignore_logging { - // dont_break_on_errors { - // result = eval_expr(expression); - // } - // } - - // assert_error(); - // delete_error(); - - // deleting user type - char exp_string4[] = "(begin (delete-type! a)(type a))"; - expression = Parser::parse_single_expression(exp_string4); - result = eval_expr(expression); - - assert_no_error(); - assert_not_null(result); - assert_equal_type(result, Lisp_Object_Type::Keyword); - assert_equal_string(result->value.symbol, "number"); - + proc test_simple_stuff() -> testresult { + { // built in add + char exp_string[] = "(+ 10 4)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string); + Lisp_Object* result; + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Number); + assert_equal_f64(result->value.number, 14); + } + { // built in subtract + char exp_string[] = "(- 10 4)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string); + Lisp_Object* result; + + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Number); + assert_equal_f64(result->value.number, 6); + } + { // built in multiply + char exp_string[] = "(* 10 4)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string); + Lisp_Object* result; + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Number); + assert_equal_f64(result->value.number, 40); + } + { // built in divide + char exp_string[] = "(/ 20 4)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string); + Lisp_Object* result; + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Number); + assert_equal_f64(result->value.number, 5); + } + { // built in if + char exp_string1[] = "(if 1 4 5)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string1); + Lisp_Object* result; + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Number); + assert_equal_f64(result->value.number, 4); + + char exp_string2[] = "(if () 4 5)"; + expression = Parser::parse_single_expression(exp_string2); + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Number); + assert_equal_f64(result->value.number, 5); + } + { // built in and + char exp_string1[] = "(and 1 \"asd\" 4)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string1); + Lisp_Object* result; + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::T); + + // a false case + char exp_string2[] = "(and () \"asd\" 4)"; + expression = Parser::parse_single_expression(exp_string2); + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Nil); + } + { // built in or + char exp_string1[] = "(or \"asd\" nil)"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string1); + Lisp_Object* result; + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::T); + + // a false case + char exp_string2[] = "(or () ())"; + expression = Parser::parse_single_expression(exp_string2); + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Nil); + } + { // buit in not + char exp_string1[] = "(not ())"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string1); + Lisp_Object* result; + try result = eval_expr(expression); + + // a true case + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::T); + + // a false case + char exp_string2[] = "(not \"asd xD\")"; + expression = Parser::parse_single_expression(exp_string2); + try result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Nil); + } + { // built in type + // normal type testing + char exp_string1[] = "(begin (define a 10)(type a))"; + Lisp_Object* expression = Parser::parse_single_expression(exp_string1); + Lisp_Object* result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Keyword); + assert_equal_string(result->value.symbol, "number"); + + // setting user type + char exp_string2[] = "(begin (set-type! a :my-type)(type a))"; + expression = Parser::parse_single_expression(exp_string2); + result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Keyword); + assert_equal_string(result->value.symbol, "my-type"); + + // deleting user type + char exp_string4[] = "(begin (delete-type! a)(type a))"; + expression = Parser::parse_single_expression(exp_string4); + result = eval_expr(expression); + + assert_no_error(); + assert_not_null(result); + assert_equal_type(result, Lisp_Object_Type::Keyword); + assert_equal_string(result->value.symbol, "number"); + } return pass; } @@ -604,15 +568,8 @@ namespace Slime { invoke_test(test_parse_expression); printf("\n-- Built ins --\n"); - invoke_test(test_built_in_add); - invoke_test(test_built_in_substract); - invoke_test(test_built_in_multiply); - invoke_test(test_built_in_divide); - invoke_test(test_built_in_if); - invoke_test(test_built_in_and); - invoke_test(test_built_in_or); - invoke_test(test_built_in_not); - // invoke_test(test_built_in_type); + invoke_test(test_simple_stuff); + invoke_test(test_string_copy); printf("\n-- Memory management --\n"); invoke_test(test_singular_t_and_nil); diff --git a/todo.org b/todo.org index 56bc597..63ee385 100644 --- a/todo.org +++ b/todo.org @@ -20,14 +20,16 @@ CLOSED: [2020-03-29 So 21:27] * DONE use better type names: u32, .. CLOSED: [2020-03-31 Di 11:36] -* TODO when copying LO, check if string itself is being copied +* DONE when copying LO, check if string itself is being copied + CLOSED: [2020-03-31 Di 11:58] +* DONE update header files + CLOSED: [2020-03-31 Di 11:58] * TODO define-syntax-shorthand (define-syntax-shorthand [ vector ] ) (define-syntax-shorthand { hash-map } ) * TODO doc generation -* TODO assert list_length for arguemns of macros +* TODO assert list length for arguemns of macros ??? -* TODO update header files * TODO source code locations * TODO function let (let fac ([n 10])