From 3abebe937cd55436e8d1bb8c9d1db741317a39b6 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 8 Sep 2021 13:47:36 +0100 Subject: [PATCH] Added `list` and unit test for it. --- src/init.c | 1 + src/ops/lispops.c | 13 +++++++++++ src/ops/lispops.h | 4 ++++ unit-tests/integer-allocation.sh | 4 ++-- unit-tests/list-test,sh | 38 ++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 unit-tests/list-test,sh diff --git a/src/init.c b/src/init.c index 5e8a55d..4fc922a 100644 --- a/src/init.c +++ b/src/init.c @@ -236,6 +236,7 @@ int main( int argc, char *argv[] ) { bind_function( L"hashmap", lisp_make_hashmap ); bind_function( L"inspect", &lisp_inspect ); bind_function( L"keys", &lisp_keys ); + bind_function( L"list", &lisp_list); bind_function( L"mapcar", &lisp_mapcar ); bind_function( L"meta", &lisp_metadata ); bind_function( L"metadata", &lisp_metadata ); diff --git a/src/ops/lispops.c b/src/ops/lispops.c index 0f058ed..d35c5a6 100644 --- a/src/ops/lispops.c +++ b/src/ops/lispops.c @@ -1453,6 +1453,19 @@ struct cons_pointer lisp_mapcar( struct stack_frame *frame, return result; } +struct cons_pointer lisp_list( struct stack_frame *frame, + struct cons_pointer frame_pointer, + struct cons_pointer env ) { + struct cons_pointer result = frame->more; + + for ( int a = nilp(result) ? frame->args - 1: args_in_frame - 1; + a >= 0; a-- ) { + result = make_cons(fetch_arg(frame, a), result); + } + + return result; +} + // /** // * Function; print the internal representation of the object indicated by `frame->arg[0]` to the // * (optional, defaults to the value of `*out*` in the environment) stream indicated by `frame->arg[1]`. diff --git a/src/ops/lispops.h b/src/ops/lispops.h index 582cd98..2724f89 100644 --- a/src/ops/lispops.h +++ b/src/ops/lispops.h @@ -211,4 +211,8 @@ struct cons_pointer lisp_append( struct stack_frame *frame, struct cons_pointer lisp_mapcar( struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env ); + + struct cons_pointer lisp_list( struct stack_frame *frame, + struct cons_pointer frame_pointer, + struct cons_pointer env ); #endif diff --git a/unit-tests/integer-allocation.sh b/unit-tests/integer-allocation.sh index ced92f2..b25bef3 100755 --- a/unit-tests/integer-allocation.sh +++ b/unit-tests/integer-allocation.sh @@ -1,8 +1,8 @@ #!/bin/bash value=354 -expected="Integer cell: value ${value}," -echo ${value} | target/psse -v5 2>&1 | grep "${expected}" > /dev/null +expected="(${value} \"INTR\")" +echo "(set! x $value)(list x (type x))" | target/psse 2>&1 | grep "${expected}" > /dev/null if [ $? -eq 0 ] then diff --git a/unit-tests/list-test,sh b/unit-tests/list-test,sh new file mode 100644 index 0000000..32f4797 --- /dev/null +++ b/unit-tests/list-test,sh @@ -0,0 +1,38 @@ +#!/bin/bash + +expected="(0 1 2 3 4 5 6 7 8 9 a b c d e f)" + +actual=`echo "(list 0 1 2 3 4 5 6 7 8 9 'a 'b 'c 'd 'e 'f)" | target/psse | tail -1` + +if [ "${expected}" = "${actual}" ] +then + echo "OK" +else + echo "Fail: expected '$expected', got '$actual'" + exit 1 +fi + +expected="(0 1 2 3 4)" + +actual=`echo "(list 0 1 2 3 4)" | target/psse | tail -1` + +if [ "${expected}" = "${actual}" ] +then + echo "OK" +else + echo "Fail: expected '$expected', got '$actual'" + exit 1 +fi + +expected="(0 1 2 3 4 5 6 7)" + +actual=`echo "(list 0 1 2 3 4 5 6 7)" | target/psse | tail -1` + +if [ "${expected}" = "${actual}" ] +then + echo "OK" + exit 0 +else + echo "Fail: expected '$expected', got '$actual'" + exit 1 +fi