From 74fa59f9152de89b53e1441f3949abd30f5e4fdf Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 7 May 2023 16:06:16 +0400 Subject: [PATCH] func "arcana/builtin" --- Makefile | 1 + src/builtins.c | 12 ++++++++++ tests/arcana/builtin.scm | 51 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/arcana/builtin.scm diff --git a/Makefile b/Makefile index b9b8906..fbb349b 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ repl: arcana-lisp test: arcana-lisp $(CAT) tests/arcana.scm | ./arcana-lisp + $(CAT) tests/arcana/builtin.scm | ./arcana-lisp $(CAT) tests/arcana/tokenize.scm | ./arcana-lisp $(CAT) tests/arithm_ops.scm | ./arcana-lisp $(CAT) tests/basic_data_structs.scm | ./arcana-lisp diff --git a/src/builtins.c b/src/builtins.c index 57ac19b..98bd8b8 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -16,6 +16,7 @@ static struct Object *func_assert_equal(size_t args_count, struct Object **args_ static struct Object *func_assert_false(size_t args_count, struct Object **args_array); static struct Object *func_assert_true(size_t args_count, struct Object **args_array); // Arcana Lisp internals +static struct Object *func_arcana_SLASH_builtin(size_t args_count, struct Object **args_array); static struct Object *func_arcana_SLASH_parse(size_t args_count, struct Object **args_array); static struct Object *func_arcana_SLASH_tokenize(size_t args_count, struct Object **args_array); static struct Object *func_arcana_SLASH_typeof(size_t args_count, struct Object **args_array); @@ -56,6 +57,7 @@ static struct Object builtins[] = { { .type = TYPE_PROCEDURE, .procedure = { "assert-false", func_assert_false } }, { .type = TYPE_PROCEDURE, .procedure = { "assert-true", func_assert_true } }, // Arcana Lisp internals + { .type = TYPE_PROCEDURE, .procedure = { "arcana/builtin", func_arcana_SLASH_builtin } }, { .type = TYPE_PROCEDURE, .procedure = { "arcana/parse", func_arcana_SLASH_parse } }, { .type = TYPE_PROCEDURE, .procedure = { "arcana/tokenize", func_arcana_SLASH_tokenize } }, { .type = TYPE_PROCEDURE, .procedure = { "arcana/typeof", func_arcana_SLASH_typeof } }, @@ -139,6 +141,16 @@ struct Object *func_assert_true( * Arcana Lisp internals * *************************/ +struct Object *func_arcana_SLASH_builtin( + size_t args_count, + struct Object **args_array +) { + assert(args_count == 1); + struct Object *const name = args_array[0]; + assert(Object_is_symbol(name)); + return builtins_get(name->s); +} + struct Object *func_arcana_SLASH_parse( size_t args_count, struct Object **args_array diff --git a/tests/arcana/builtin.scm b/tests/arcana/builtin.scm new file mode 100644 index 0000000..4d76b2c --- /dev/null +++ b/tests/arcana/builtin.scm @@ -0,0 +1,51 @@ +(begin + (assert-true (null? (arcana/builtin 'foobar))) + + ; Testing some builtin procedures for sanity + (assert-equal '(1 2 3) ((arcana/builtin 'list) 1 2 3)) + (assert-true ((arcana/builtin 'boolean?) #false)) + (assert-true ((arcana/builtin 'number?) 123)) + (assert-equal "123456" ((arcana/builtin 'number->string) 123456)) + (assert-equal 'foobar ((arcana/builtin 'string->symbol) "foobar")) + (assert-equal "foobar" ((arcana/builtin 'symbol->string) 'foobar)) + + ; Assertions + (assert-true (procedure? (arcana/builtin 'assert-equal))) + (assert-true (procedure? (arcana/builtin 'assert-false))) + (assert-true (procedure? (arcana/builtin 'assert-true))) + ; Arcana Lisp internals + (assert-true (procedure? (arcana/builtin 'arcana/builtin))) + (assert-true (procedure? (arcana/builtin 'arcana/parse))) + (assert-true (procedure? (arcana/builtin 'arcana/tokenize))) + (assert-true (procedure? (arcana/builtin 'arcana/typeof))) + ; Basic data structures + (assert-true (procedure? (arcana/builtin 'car))) + (assert-true (procedure? (arcana/builtin 'cdr))) + (assert-true (procedure? (arcana/builtin 'cons))) + (assert-true (procedure? (arcana/builtin 'list))) + ; Type predicates + (assert-true (procedure? (arcana/builtin 'boolean?))) + (assert-true (procedure? (arcana/builtin 'char?))) + (assert-true (procedure? (arcana/builtin 'null?))) + (assert-true (procedure? (arcana/builtin 'number?))) + (assert-true (procedure? (arcana/builtin 'pair?))) + (assert-true (procedure? (arcana/builtin 'procedure?))) + (assert-true (procedure? (arcana/builtin 'string?))) + (assert-true (procedure? (arcana/builtin 'symbol?))) + ; Equivalence predicates + (assert-true (procedure? (arcana/builtin 'equal?))) + ; Type conversion + (assert-true (procedure? (arcana/builtin 'number->string))) + (assert-true (procedure? (arcana/builtin 'string->symbol))) + (assert-true (procedure? (arcana/builtin 'symbol->string))) + ; Logical operators + (assert-true (procedure? (arcana/builtin 'not))) + ; Arithmetic operators + (assert-true (procedure? (arcana/builtin '=))) + (assert-true (procedure? (arcana/builtin '+))) + (assert-true (procedure? (arcana/builtin '-))) + ; IO + (assert-true (procedure? (arcana/builtin 'display))) + (assert-true (procedure? (arcana/builtin 'displayln))) + (assert-true (procedure? (arcana/builtin 'newline))) +)