From 434c17eb0e2cc71d398a60895991e9b88af45cc2 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 12 Dec 2018 21:12:49 +0000 Subject: [PATCH] And now the interpreter works (I think) correctly! --- src/conspage.c | 9 ++++++--- src/consspaceobject.c | 2 +- src/equal.c | 4 ++-- src/lispops.c | 9 ++++----- unit-tests/intepreter.sh | 12 ++++++++++++ 5 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 unit-tests/intepreter.sh diff --git a/src/conspage.c b/src/conspage.c index e016c86..0b13baf 100644 --- a/src/conspage.c +++ b/src/conspage.c @@ -73,9 +73,11 @@ void make_cons_page( ) { strncpy( &cell->tag.bytes[0], TRUETAG, TAGLENGTH ); cell->count = MAXREFERENCE; cell->payload.free.car = ( struct cons_pointer ) { - 0, 1}; + 0, 1 + }; cell->payload.free.cdr = ( struct cons_pointer ) { - 0, 1}; + 0, 1 + }; fwprintf( stderr, L"Allocated special cell T\n" ); break; } @@ -110,7 +112,8 @@ void dump_pages( FILE * output ) { for ( int j = 0; j < CONSPAGESIZE; j++ ) { dump_object( output, ( struct cons_pointer ) { - i, j} ); + i, j + } ); } } } diff --git a/src/consspaceobject.c b/src/consspaceobject.c index defc56f..4ecd054 100644 --- a/src/consspaceobject.c +++ b/src/consspaceobject.c @@ -63,7 +63,7 @@ void dec_ref( struct cons_pointer pointer ) { } } -void dump_string_cell( FILE * output, wchar_t * prefix, +void dump_string_cell( FILE * output, wchar_t *prefix, struct cons_pointer pointer ) { struct cons_space_object cell = pointer2cell( pointer ); if ( cell.payload.string.character == 0 ) { diff --git a/src/equal.c b/src/equal.c index d06903f..ef0b897 100644 --- a/src/equal.c +++ b/src/equal.c @@ -78,8 +78,8 @@ bool equal( struct cons_pointer a, struct cons_pointer b ) { && ( equal( cell_a->payload.string.cdr, cell_b->payload.string.cdr ) || ( end_of_string( cell_a->payload.string.cdr ) - && end_of_string( cell_b->payload.string. - cdr ) ) ); + && end_of_string( cell_b->payload. + string.cdr ) ) ); break; case INTEGERTV: case REALTV: diff --git a/src/lispops.c b/src/lispops.c index b2f7800..8529bd9 100644 --- a/src/lispops.c +++ b/src/lispops.c @@ -128,11 +128,10 @@ eval_lambda( struct cons_space_object cell, struct stack_frame *frame, struct cons_pointer new_env = env; struct cons_pointer args = cell.payload.lambda.args; struct cons_pointer body = cell.payload.lambda.body; - struct cons_pointer vals = frame->arg[0]; - while ( consp( args ) && consp( vals ) ) { + for ( int i = 0; i < args_in_frame && consp( args ); i++ ) { struct cons_pointer arg = c_car( args ); - struct cons_pointer val = c_car( vals ); + struct cons_pointer val = frame->arg[i]; print( stderr, c_string_to_lisp_string( "\n\tBinding " ) ); print( stderr, arg ); print( stderr, c_string_to_lisp_string( " to " ) ); @@ -141,8 +140,6 @@ eval_lambda( struct cons_space_object cell, struct stack_frame *frame, new_env = make_cons( make_cons( arg, val ), new_env ); args = c_cdr( args ); - vals = c_cdr( vals ); - } while ( !nilp( body ) ) { @@ -194,6 +191,8 @@ c_apply( struct stack_frame *frame, struct cons_pointer env ) { { struct stack_frame *next = make_stack_frame( frame, args, env ); + fputws( L"Stack frame for lambda\n", stderr ); + dump_frame( stderr, next ); result = eval_lambda( fn_cell, next, env ); free_stack_frame( next ); } diff --git a/unit-tests/intepreter.sh b/unit-tests/intepreter.sh new file mode 100644 index 0000000..9eb2a06 --- /dev/null +++ b/unit-tests/intepreter.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +expected='6' +actual=`echo "(apply '(lambda (x y z) (/ (* y z) x)) '(2 3 4))" | target/psse 2> /dev/null | head -2 | tail -1` + +if [ "${expected}" = "${actual}" ] +then + echo "OK" +else + echo "Fail: expected '${expected}', got '${actual}'" + exit 1 +fi