From d620542ee56c38a6e4878ea2e9e7f53e06368149 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Fri, 7 Dec 2018 20:53:09 +0000 Subject: [PATCH] Trying to get to the point where `make format` works the same on Linux and MacOS --- .gitignore | 2 + Makefile | 10 ++++- src/conspage.c | 13 ++++--- src/consspaceobject.c | 90 +++++++++++++++++++++---------------------- src/consspaceobject.h | 11 +++--- src/init.c | 2 +- src/lispops.c | 15 ++++---- src/peano.c | 33 ++++++++-------- src/print.c | 80 +++++++++++++++++++------------------- src/read.c | 19 +++++---- src/read.h | 2 +- src/repl.c | 60 ++++++++++++++--------------- src/stack.c | 30 +++++++-------- 13 files changed, 191 insertions(+), 176 deletions(-) diff --git a/.gitignore b/.gitignore index 15477a1..30afce2 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ src/\.#* post-scarcity\.iml doc/ + +log* diff --git a/Makefile b/Makefile index 1ba8422..b300b3c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ - TARGET ?= target/psse SRC_DIRS ?= ./src @@ -11,7 +10,10 @@ TESTS := $(shell find unit-tests -name *.sh) INC_DIRS := $(shell find $(SRC_DIRS) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) -INDENT_FLAGS := -kr -br -brf -brs -ce -cdw -npsl -nut -prs -l79 -ts2 + +INDENT_FLAGS := -nbad -bap -nbc -br -brf -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 \ +-d0 -di1 -nfc1 -i4 -ip0 -l75 -lp -npcs \ +-npsl -nsc -nsob -nss -nut -prs -l79 -ts2 VERSION := "0.0.2" @@ -25,7 +27,11 @@ doc: $(SRCS) Makefile doxygen format: $(SRCS) $(HDRS) Makefile +ifeq ($(shell uname -s), Darwin) + gindent $(INDENT_FLAGS) $(SRCS) $(HDRS) +else indent $(INDENT_FLAGS) $(SRCS) $(HDRS) +endif test: $(OBJS) $(TESTS) Makefile bash ./unit-tests.sh diff --git a/src/conspage.c b/src/conspage.c index 9b0d218..f044c93 100644 --- a/src/conspage.c +++ b/src/conspage.c @@ -55,7 +55,7 @@ void make_cons_page( ) { struct cons_space_object *cell = &conspages[initialised_cons_pages]->cell[i]; if ( initialised_cons_pages == 0 && i < 3 ) { - switch ( i) { + switch ( i ) { case 0: /* * initialise cell as NIL @@ -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; case 2: @@ -84,7 +86,7 @@ void make_cons_page( ) { */ strncpy( &cell->tag.bytes[0], LAMBDATAG, TAGLENGTH ); cell->count = MAXREFERENCE; - cell->payload.string.character = (wint_t)L'λ'; + cell->payload.string.character = ( wint_t ) L'λ'; cell->payload.free.cdr = NIL; fwprintf( stderr, L"Allocated special cell LAMBDA\n" ); break; @@ -120,7 +122,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 8c3de50..0c2260d 100644 --- a/src/consspaceobject.c +++ b/src/consspaceobject.c @@ -63,6 +63,29 @@ void dec_ref( struct cons_pointer pointer ) { } } +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 ) { + fwprintf( output, + L"\t\t%ls cell: termination; next at page %d offset %d, count %u\n", + prefix, + cell.payload.string.cdr.page, cell.payload.string.cdr.offset, + cell.count ); + } else { + fwprintf( output, + L"\t\t%ls cell: character '%lc' (%d) next at page %d offset %d, count %u\n", + prefix, + ( wint_t ) cell.payload.string.character, + cell.payload.string.character, + cell.payload.string.cdr.page, + cell.payload.string.cdr.offset, cell.count ); + fwprintf( output, L"\t\t value: " ); + print( output, pointer ); + fwprintf( output, L"\n" ); + } +} + /** * dump the object at this cons_pointer to this output stream. */ @@ -85,17 +108,16 @@ void dump_object( FILE * output, struct cons_pointer pointer ) { cell.payload.cons.cdr.page, cell.payload.cons.cdr.offset, cell.count ); break; - case EXCEPTIONTV: - fwprintf(output, L"\t\tException cell: "); - print(output, cell.payload.exception.message); - fwprintf( output, L"\n"); - /* TODO: dump the stack trace */ - for (struct stack_frame * frame = cell.payload.exception.frame; - frame != NULL; - frame = frame->previous){ - dump_frame(output, frame); - } - break; + case EXCEPTIONTV: + fwprintf( output, L"\t\tException cell: " ); + print( output, cell.payload.exception.message ); + fwprintf( output, L"\n" ); + /* TODO: dump the stack trace */ + for ( struct stack_frame * frame = cell.payload.exception.frame; + frame != NULL; frame = frame->previous ) { + dump_frame( output, frame ); + } + break; case FREETV: fwprintf( output, L"\t\tFree cell: next at page %d offset %d\n", cell.payload.cons.cdr.page, cell.payload.cons.cdr.offset ); @@ -105,40 +127,17 @@ void dump_object( FILE * output, struct cons_pointer pointer ) { L"\t\tInteger cell: value %ld, count %u\n", cell.payload.integer.value, cell.count ); break; - case READTV: - fwprintf( output, L"\t\tInput stream\n"); + case READTV: + fwprintf( output, L"\t\tInput stream\n" ); case REALTV: fwprintf( output, L"\t\tReal cell: value %Lf, count %u\n", cell.payload.real.value, cell.count ); break; case STRINGTV: - if (cell.payload.string.character == 0) { - fwprintf( output, - L"\t\tString cell: termination; next at page %d offset %d, count %u\n", - cell.payload.string.character, - cell.payload.string.cdr.page, - cell.payload.string.cdr.offset, cell.count ); - }else { - fwprintf( output, - L"\t\tString cell: character '%lc' (%d) next at page %d offset %d, count %u\n", - cell.payload.string.character, - cell.payload.string.character, - cell.payload.string.cdr.page, - cell.payload.string.cdr.offset, cell.count ); - fwprintf( output, L"\t\t value: " ); - print( output, pointer ); - fwprintf( output, L"\n" );} + dump_string_cell( output, L"String", pointer ); break; case SYMBOLTV: - fwprintf( output, - L"\t\tSymbol cell: character '%lc' (%d) next at page %d offset %d, count %u\n", - cell.payload.string.character, - cell.payload.string.character, - cell.payload.string.cdr.page, - cell.payload.string.cdr.offset, cell.count ); - fwprintf( output, L"\t\t value:" ); - print( output, pointer ); - fwprintf( output, L"\n" ); + dump_string_cell( output, L"Symbol", pointer ); break; } } @@ -168,7 +167,8 @@ struct cons_pointer make_cons( struct cons_pointer car, * @param message should be a lisp string describing the problem, but actually any cons pointer will do; * @param frame should be the frame in which the exception occurred. */ -struct cons_pointer make_exception( struct cons_pointer message, struct stack_frame * frame) { +struct cons_pointer make_exception( struct cons_pointer message, + struct stack_frame *frame ) { struct cons_pointer pointer = allocate_cell( EXCEPTIONTAG ); struct cons_space_object *cell = &pointer2cell( pointer ); @@ -259,26 +259,26 @@ make_special( struct cons_pointer src, struct cons_pointer ( *executable ) * Construct a cell which points to a stream open for reading. * @param input the C stream to wrap. */ -struct cons_pointer make_read_stream( FILE * input) { +struct cons_pointer make_read_stream( FILE * input ) { struct cons_pointer pointer = allocate_cell( READTAG ); struct cons_space_object *cell = &pointer2cell( pointer ); - cell->payload.stream.stream = input; + cell->payload.stream.stream = input; - return pointer; + return pointer; } /** * Construct a cell which points to a stream open for writeing. * @param output the C stream to wrap. */ -struct cons_pointer make_write_stream( FILE * output) { +struct cons_pointer make_write_stream( FILE * output ) { struct cons_pointer pointer = allocate_cell( WRITETAG ); struct cons_space_object *cell = &pointer2cell( pointer ); - cell->payload.stream.stream = output; + cell->payload.stream.stream = output; - return pointer; + return pointer; } /** diff --git a/src/consspaceobject.h b/src/consspaceobject.h index 05939c2..c17ded5 100644 --- a/src/consspaceobject.h +++ b/src/consspaceobject.h @@ -272,8 +272,8 @@ struct cons_payload { * Message should be a Lisp string; frame should be a pointer to an (unfreed) stack frame. */ struct exception_payload { - struct cons_pointer message; - struct stack_frame * frame; + struct cons_pointer message; + struct stack_frame *frame; }; /** @@ -461,7 +461,8 @@ struct cons_pointer make_cons( struct cons_pointer car, * @param message should be a lisp string describing the problem, but actually any cons pointer will do; * @param frame should be the frame in which the exception occurred. */ -struct cons_pointer make_exception( struct cons_pointer message, struct stack_frame * frame); +struct cons_pointer make_exception( struct cons_pointer message, + struct stack_frame *frame ); /** * Construct a cell which points to an executable Lisp special form. @@ -496,13 +497,13 @@ struct cons_pointer make_symbol( wint_t c, struct cons_pointer tail ); * Construct a cell which points to a stream open for reading. * @param input the C stream to wrap. */ -struct cons_pointer make_read_stream( FILE * input); +struct cons_pointer make_read_stream( FILE * input ); /** * Construct a cell which points to a stream open for writeing. * @param output the C stream to wrap. */ -struct cons_pointer make_write_stream( FILE * output); +struct cons_pointer make_write_stream( FILE * output ); /** diff --git a/src/init.c b/src/init.c index 83baa73..616261c 100644 --- a/src/init.c +++ b/src/init.c @@ -74,7 +74,7 @@ int main( int argc, char *argv[] ) { deep_bind( c_string_to_lisp_symbol( "nil" ), NIL ); deep_bind( c_string_to_lisp_symbol( "t" ), TRUE ); /* deep_bind( c_string_to_lisp_symbol( L"λ"), LAMBDA ); */ - deep_bind( c_string_to_lisp_symbol( "lambda"), LAMBDA ); + deep_bind( c_string_to_lisp_symbol( "lambda" ), LAMBDA ); /* * primitive function operations diff --git a/src/lispops.c b/src/lispops.c index 8019744..1b1eeea 100644 --- a/src/lispops.c +++ b/src/lispops.c @@ -475,7 +475,8 @@ lisp_cond( struct stack_frame *frame, struct cons_pointer env ) { done = true; } else { result = lisp_throw( c_string_to_lisp_string - ( "Arguments to `cond` must be lists" ), frame ); + ( "Arguments to `cond` must be lists" ), + frame ); } } /* TODO: if there are more than 8 clauses we need to continue into the @@ -495,11 +496,11 @@ lisp_throw( struct cons_pointer message, struct stack_frame *frame ) { struct cons_space_object cell = pointer2cell( message ); - if ( cell.tag.value == EXCEPTIONTV) { - result = message; - } else { - result = make_exception( message, frame); - } + if ( cell.tag.value == EXCEPTIONTV ) { + result = message; + } else { + result = make_exception( message, frame ); + } - return result; + return result; } diff --git a/src/peano.c b/src/peano.c index 43189ea..aec3104 100644 --- a/src/peano.c +++ b/src/peano.c @@ -144,9 +144,9 @@ lisp_subtract( struct stack_frame *frame, struct cons_pointer env ) { make_real( arg0.payload.real.value - numeric_value( frame->arg[1] ) ); } else { - /* TODO: throw an exception */ - lisp_throw( c_string_to_lisp_string - ( "Cannot subtract: not a number" ), frame ); + /* TODO: throw an exception */ + lisp_throw( c_string_to_lisp_string + ( "Cannot subtract: not a number" ), frame ); } // and if not nilp[frame->arg[2]) we also have an error. @@ -167,21 +167,20 @@ lisp_divide( struct stack_frame *frame, struct cons_pointer env ) { struct cons_space_object arg0 = pointer2cell( frame->arg[0] ); struct cons_space_object arg1 = pointer2cell( frame->arg[1] ); - if ( numberp(frame->arg[1]) && numeric_value(frame->arg[1]) == 0) { - lisp_throw( c_string_to_lisp_string - ( "Cannot divide: divisor is zero" ), frame ); - } else if ( integerp( frame->arg[0] ) && integerp( frame->arg[1] ) ) { - result = make_integer( arg0.payload.integer.value / - arg1.payload.integer.value ); - } else if ( numberp(frame->arg[0]) && numberp(frame->arg[1])) { - result = make_real( numeric_value(frame->arg[0]) / numeric_value(frame->arg[1])); + if ( numberp( frame->arg[1] ) && numeric_value( frame->arg[1] ) == 0 ) { + lisp_throw( c_string_to_lisp_string + ( "Cannot divide: divisor is zero" ), frame ); + } else if ( integerp( frame->arg[0] ) && integerp( frame->arg[1] ) ) { + result = make_integer( arg0.payload.integer.value / + arg1.payload.integer.value ); + } else if ( numberp( frame->arg[0] ) && numberp( frame->arg[1] ) ) { + result = + make_real( numeric_value( frame->arg[0] ) / + numeric_value( frame->arg[1] ) ); } else { - lisp_throw( c_string_to_lisp_string - ( "Cannot divide: not a number" ), frame ); + lisp_throw( c_string_to_lisp_string + ( "Cannot divide: not a number" ), frame ); } - return result; + return result; } - - - diff --git a/src/print.c b/src/print.c index 2eb943e..1fdcafd 100644 --- a/src/print.c +++ b/src/print.c @@ -78,45 +78,45 @@ void print( FILE * output, struct cons_pointer pointer ) { * statement can ultimately be replaced by a switch, which will be neater. */ switch ( cell.tag.value ) { - case CONSTV: - print_list( output, pointer ); - break; - case EXCEPTIONTV: - fwprintf( output, L"\nException: "); - print_string_contents( output, cell.payload.exception.message); - break; - case INTEGERTV: - fwprintf( output, L"%ld", cell.payload.integer.value ); - break; - case LAMBDATV: - fwprintf( output, L"lambda" /* "λ" */); - break; - case NILTV: - fwprintf( output, L"nil" ); - break; - case REALTV: - fwprintf( output, L"%Lf", cell.payload.real.value ); - break; - case STRINGTV: - print_string( output, pointer ); - break; - case SYMBOLTV: - print_string_contents( output, pointer ); - break; - case TRUETV: - fwprintf( output, L"t" ); - break; - case FUNCTIONTV: - fwprintf( output, L"(Function)" ); - break; - case SPECIALTV: - fwprintf( output, L"(Special form)" ); - break; - default: - fwprintf( stderr, - L"Error: Unrecognised tag value %d (%c%c%c%c)\n", - cell.tag.value, cell.tag.bytes[0], cell.tag.bytes[1], - cell.tag.bytes[2], cell.tag.bytes[3] ); - break; + case CONSTV: + print_list( output, pointer ); + break; + case EXCEPTIONTV: + fwprintf( output, L"\nException: " ); + print_string_contents( output, cell.payload.exception.message ); + break; + case INTEGERTV: + fwprintf( output, L"%ld", cell.payload.integer.value ); + break; + case LAMBDATV: + fwprintf( output, L"lambda" /* "λ" */ ); + break; + case NILTV: + fwprintf( output, L"nil" ); + break; + case REALTV: + fwprintf( output, L"%Lf", cell.payload.real.value ); + break; + case STRINGTV: + print_string( output, pointer ); + break; + case SYMBOLTV: + print_string_contents( output, pointer ); + break; + case TRUETV: + fwprintf( output, L"t" ); + break; + case FUNCTIONTV: + fwprintf( output, L"(Function)" ); + break; + case SPECIALTV: + fwprintf( output, L"(Special form)" ); + break; + default: + fwprintf( stderr, + L"Error: Unrecognised tag value %d (%c%c%c%c)\n", + cell.tag.value, cell.tag.bytes[0], cell.tag.bytes[1], + cell.tag.bytes[2], cell.tag.bytes[3] ); + break; } } diff --git a/src/read.c b/src/read.c index eb82c1f..ad74db6 100644 --- a/src/read.c +++ b/src/read.c @@ -32,7 +32,8 @@ */ struct cons_pointer read_number( FILE * input, wint_t initial ); -struct cons_pointer read_list( struct stack_frame *frame, FILE * input, wint_t initial ); +struct cons_pointer read_list( struct stack_frame *frame, FILE * input, + wint_t initial ); struct cons_pointer read_string( FILE * input, wint_t initial ); struct cons_pointer read_symbol( FILE * input, wint_t initial ); @@ -49,7 +50,8 @@ struct cons_pointer c_quote( struct cons_pointer arg ) { * treating this initial character as the first character of the object * representation. */ -struct cons_pointer read_continuation(struct stack_frame *frame, FILE * input, wint_t initial ) { +struct cons_pointer read_continuation( struct stack_frame *frame, FILE * input, + wint_t initial ) { struct cons_pointer result = NIL; wint_t c; @@ -58,10 +60,10 @@ struct cons_pointer read_continuation(struct stack_frame *frame, FILE * input, w c == '\0' || iswblank( c ) || iswcntrl( c ); c = fgetwc( input ) ); switch ( c ) { - case EOF: - result = lisp_throw( c_string_to_lisp_string - ( "End of input while reading" ), frame ); - break; + case EOF: + result = lisp_throw( c_string_to_lisp_string + ( "End of input while reading" ), frame ); + break; case '\'': result = c_quote( read_continuation( frame, input, fgetwc( input ) ) ); break; @@ -147,7 +149,8 @@ struct cons_pointer read_number( FILE * input, wint_t initial ) { * Read a list from this input stream, which no longer contains the opening * left parenthesis. */ -struct cons_pointer read_list( struct stack_frame *frame, FILE * input, wint_t initial ) { +struct cons_pointer read_list( struct stack_frame *frame, FILE * input, + wint_t initial ) { struct cons_pointer result = NIL; if ( initial != ')' ) { @@ -236,6 +239,6 @@ struct cons_pointer read_symbol( FILE * input, wint_t initial ) { /** * Read the next object on this input stream and return a cons_pointer to it. */ -struct cons_pointer read(struct stack_frame *frame, FILE * input ) { +struct cons_pointer read( struct stack_frame *frame, FILE * input ) { return read_continuation( frame, input, fgetwc( input ) ); } diff --git a/src/read.h b/src/read.h index 2577ba2..af7574b 100644 --- a/src/read.h +++ b/src/read.h @@ -14,6 +14,6 @@ /** * read the next object on this input stream and return a cons_pointer to it. */ -struct cons_pointer read(struct stack_frame *frame, FILE * input ); +struct cons_pointer read( struct stack_frame *frame, FILE * input ); #endif diff --git a/src/repl.c b/src/repl.c index 9df9234..f3a51bd 100644 --- a/src/repl.c +++ b/src/repl.c @@ -30,41 +30,42 @@ /** * Dummy up a Lisp read call with its own stack frame. */ -struct cons_pointer repl_read( struct cons_pointer stream_pointer) { - struct stack_frame *frame = make_empty_frame( NULL, oblist ); +struct cons_pointer repl_read( struct cons_pointer stream_pointer ) { + struct stack_frame *frame = make_empty_frame( NULL, oblist ); - frame->arg[0] = stream_pointer; - struct cons_pointer result = lisp_read( frame, oblist); - free_stack_frame( frame ); + frame->arg[0] = stream_pointer; + struct cons_pointer result = lisp_read( frame, oblist ); + free_stack_frame( frame ); - return result; + return result; } /** * Dummy up a Lisp eval call with its own stack frame. */ -struct cons_pointer repl_eval( struct cons_pointer input) { - struct stack_frame *frame = make_empty_frame( NULL, oblist ); +struct cons_pointer repl_eval( struct cons_pointer input ) { + struct stack_frame *frame = make_empty_frame( NULL, oblist ); - frame->arg[0] = NIL /* input */; - struct cons_pointer result = lisp_eval( frame, oblist); - free_stack_frame( frame ); + frame->arg[0] = NIL /* input */ ; + struct cons_pointer result = lisp_eval( frame, oblist ); + free_stack_frame( frame ); - return result; + return result; } /** * Dummy up a Lisp print call with its own stack frame. */ -struct cons_pointer repl_print( struct cons_pointer stream_pointer, struct cons_pointer value) { - struct stack_frame *frame = make_empty_frame( NULL, oblist ); +struct cons_pointer repl_print( struct cons_pointer stream_pointer, + struct cons_pointer value ) { + struct stack_frame *frame = make_empty_frame( NULL, oblist ); - frame->arg[0] = value; - frame->arg[1] = NIL /* stream_pointer */; - struct cons_pointer result = lisp_print( frame, oblist); - free_stack_frame( frame ); + frame->arg[0] = value; + frame->arg[1] = NIL /* stream_pointer */ ; + struct cons_pointer result = lisp_print( frame, oblist ); + free_stack_frame( frame ); - return result; + return result; } /** @@ -77,21 +78,20 @@ struct cons_pointer repl_print( struct cons_pointer stream_pointer, struct cons_ void repl( FILE * in_stream, FILE * out_stream, FILE * error_stream, bool show_prompt ) { - struct cons_pointer input_stream = make_read_stream(in_stream); - struct cons_pointer output_stream = make_write_stream(out_stream); + struct cons_pointer input_stream = make_read_stream( in_stream ); + struct cons_pointer output_stream = make_write_stream( out_stream ); - while ( !feof( pointer2cell(input_stream).payload.stream.stream ) ) { - if ( show_prompt ) { - fwprintf( out_stream, L"\n:: " ); - } + while ( !feof( pointer2cell( input_stream ).payload.stream.stream ) ) { + if ( show_prompt ) { + fwprintf( out_stream, L"\n:: " ); + } - struct cons_pointer val = repl_eval( repl_read( input_stream)); + struct cons_pointer val = repl_eval( repl_read( input_stream ) ); /* suppress the 'end of stream' exception */ - if ( exceptionp(val) && - !feof( pointer2cell( input_stream).payload.stream.stream ) ) - { - repl_print( output_stream, val); + if ( exceptionp( val ) && + !feof( pointer2cell( input_stream ).payload.stream.stream ) ) { + repl_print( output_stream, val ); } } } diff --git a/src/stack.c b/src/stack.c index 90a763a..7b4d2da 100644 --- a/src/stack.c +++ b/src/stack.c @@ -84,25 +84,25 @@ struct stack_frame *make_stack_frame( struct stack_frame *previous, struct stack_frame *arg_frame = make_empty_frame( previous, env ); arg_frame->arg[0] = cell.payload.cons.car; inc_ref( arg_frame->arg[0] ); - struct cons_pointer val = lisp_eval( arg_frame, env ); - if (pointer2cell(val).tag.value == EXCEPTIONTV) { - result->arg[0] = val; - break; - } else { - result->arg[i] = val; - } - inc_ref(val); + struct cons_pointer val = lisp_eval( arg_frame, env ); + if ( pointer2cell( val ).tag.value == EXCEPTIONTV ) { + result->arg[0] = val; + break; + } else { + result->arg[i] = val; + } + inc_ref( val ); free_stack_frame( arg_frame ); args = cell.payload.cons.cdr; } - if (!nilp( args)) { - /* - * TODO: this isn't right. These args should also each be evaled. - */ - result->more = args; - inc_ref( result->more ); - } + if ( !nilp( args ) ) { + /* + * TODO: this isn't right. These args should also each be evaled. + */ + result->more = args; + inc_ref( result->more ); + } return result; }