From 7d0b6bec97c49a587312b2b76752c9471c5983d2 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 12 Dec 2018 11:48:24 +0000 Subject: [PATCH] Colourised print. --- .gitignore | 8 +++++++ src/conspage.c | 9 +++----- src/consspaceobject.c | 2 +- src/init.c | 1 + src/lispops.c | 7 ++++--- src/print.c | 49 +++++++++++++++++++++++++++++++++++++------ src/print.h | 1 + 7 files changed, 61 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 30afce2..6840d19 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,11 @@ post-scarcity\.iml doc/ log* + +\.cproject + +\.gdb_history + +\.project + +\.settings/language\.settings\.xml diff --git a/src/conspage.c b/src/conspage.c index 9845284..a88b62a 100644 --- a/src/conspage.c +++ b/src/conspage.c @@ -73,11 +73,9 @@ 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: @@ -122,8 +120,7 @@ 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 1b6173f..2d1464e 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/init.c b/src/init.c index 8043117..67a5fbb 100644 --- a/src/init.c +++ b/src/init.c @@ -53,6 +53,7 @@ int main( int argc, char *argv[] ) { break; case 'p': show_prompt = true; + print_use_colours = true; break; default: fwprintf( stderr, L"Unexpected option %c\n", option ); diff --git a/src/lispops.c b/src/lispops.c index 9ecd602..9815816 100644 --- a/src/lispops.c +++ b/src/lispops.c @@ -114,12 +114,13 @@ lisp_lambda( struct stack_frame *frame, struct cons_pointer lexpr, struct cons_pointer new_env = env; } else { char *buffer = malloc( 1024 ); + struct cons_space_object not_lambda = pointer2cell( should_be_lambda ); memset( buffer, '\0', 1024 ); sprintf( buffer, "Expected lambda, but found cell with tag %d (%c%c%c%c)", - fn_cell.tag.value, fn_cell.tag.bytes[0], - fn_cell.tag.bytes[1], fn_cell.tag.bytes[2], - fn_cell.tag.bytes[3] ); + not_lambda.tag.value, not_lambda.tag.bytes[0], + not_lambda.tag.bytes[1], not_lambda.tag.bytes[2], + not_lambda.tag.bytes[3] ); struct cons_pointer message = c_string_to_lisp_string( buffer ); free( buffer ); result = lisp_throw( message, frame ); diff --git a/src/print.c b/src/print.c index ee5a5b3..31971a2 100644 --- a/src/print.c +++ b/src/print.c @@ -22,6 +22,8 @@ #include "integer.h" #include "print.h" +int print_use_colours = 0; + void print_string_contents( FILE * output, struct cons_pointer pointer ) { if ( stringp( pointer ) || symbolp( pointer ) ) { struct cons_space_object *cell = &pointer2cell( pointer ); @@ -66,9 +68,19 @@ print_list_contents( FILE * output, struct cons_pointer pointer, } void print_list( FILE * output, struct cons_pointer pointer ) { - fputwc( btowc( '(' ), output ); + if ( print_use_colours ) { + fwprintf( output, L"%s(%s", "\x1B[31m", "\x1B[39m" ); + } else { + fputws( L"(", output ); + }; + print_list_contents( output, pointer, false ); - fputwc( btowc( ')' ), output ); + if ( print_use_colours ) { + fwprintf( output, L"%s)%s", "\x1B[31m", "\x1B[39m" ); + } else { + fputws( L")", output ); + } + } void print( FILE * output, struct cons_pointer pointer ) { @@ -84,11 +96,19 @@ void print( FILE * output, struct cons_pointer pointer ) { print_list( output, pointer ); break; case EXCEPTIONTV: - fwprintf( output, L"\nException: " ); + fwprintf( output, L"\n%sException: ", + print_use_colours ? "\x1B[31m" : "" ); print_string_contents( output, cell.payload.exception.message ); + fputws( L"\x1B[39m", output ); break; case INTEGERTV: - fwprintf( output, L"%ld", cell.payload.integer.value ); + if ( print_use_colours ) { + fputws( L"\x1B[34m", output ); + } + fwprintf( output, L"%ld%", cell.payload.integer.value ); + if ( print_use_colours ) { + fputws( L"\x1B[39m", output ); + } break; case LAMBDATV: fwprintf( output, L"lambda" /* "λ" */ ); @@ -109,14 +129,30 @@ void print( FILE * output, struct cons_pointer pointer ) { buffer[i] = '\0'; } } + if ( print_use_colours ) { + fputws( L"\x1B[34m", output ); + } fwprintf( output, L"%s", buffer ); + if ( print_use_colours ) { + fputws( L"\x1B[39m", output ); + } free( buffer ); break; case STRINGTV: + if ( print_use_colours ) { + fputws( L"\x1B[36m", output ); + } print_string( output, pointer ); + if ( print_use_colours ) { + fputws( L"\x1B[39m", output ); + } break; case SYMBOLTV: + if ( print_use_colours ) + fputws( L"\x1B[1;33m", output ); print_string_contents( output, pointer ); + if ( print_use_colours ) + fputws( L"\x1B[0;39m", output ); break; case TRUETV: fwprintf( output, L"t" ); @@ -129,9 +165,10 @@ void print( FILE * output, struct cons_pointer pointer ) { break; default: fwprintf( stderr, - L"Error: Unrecognised tag value %d (%c%c%c%c)\n", + L"%sError: Unrecognised tag value %d (%c%c%c%c)%s\n", + "\x1B[31m", cell.tag.value, cell.tag.bytes[0], cell.tag.bytes[1], - cell.tag.bytes[2], cell.tag.bytes[3] ); + cell.tag.bytes[2], cell.tag.bytes[3], "\x1B[39m" ); break; } } diff --git a/src/print.h b/src/print.h index a3fb4ab..7ee9c80 100644 --- a/src/print.h +++ b/src/print.h @@ -15,5 +15,6 @@ #define __print_h void print( FILE * output, struct cons_pointer pointer ); +extern int print_use_colours; #endif