Colourised print.

This commit is contained in:
Simon Brooke 2018-12-12 11:48:24 +00:00
parent 9bfc9074b0
commit 7d0b6bec97
7 changed files with 61 additions and 16 deletions

8
.gitignore vendored
View file

@ -20,3 +20,11 @@ post-scarcity\.iml
doc/ doc/
log* log*
\.cproject
\.gdb_history
\.project
\.settings/language\.settings\.xml

View file

@ -73,11 +73,9 @@ void make_cons_page( ) {
strncpy( &cell->tag.bytes[0], TRUETAG, TAGLENGTH ); strncpy( &cell->tag.bytes[0], TRUETAG, TAGLENGTH );
cell->count = MAXREFERENCE; cell->count = MAXREFERENCE;
cell->payload.free.car = ( struct cons_pointer ) { cell->payload.free.car = ( struct cons_pointer ) {
0, 1 0, 1};
};
cell->payload.free.cdr = ( struct cons_pointer ) { cell->payload.free.cdr = ( struct cons_pointer ) {
0, 1 0, 1};
};
fwprintf( stderr, L"Allocated special cell T\n" ); fwprintf( stderr, L"Allocated special cell T\n" );
break; break;
case 2: case 2:
@ -122,8 +120,7 @@ void dump_pages( FILE * output ) {
for ( int j = 0; j < CONSPAGESIZE; j++ ) { for ( int j = 0; j < CONSPAGESIZE; j++ ) {
dump_object( output, ( struct cons_pointer ) { dump_object( output, ( struct cons_pointer ) {
i, j i, j} );
} );
} }
} }
} }

View file

@ -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_pointer pointer ) {
struct cons_space_object cell = pointer2cell( pointer ); struct cons_space_object cell = pointer2cell( pointer );
if ( cell.payload.string.character == 0 ) { if ( cell.payload.string.character == 0 ) {

View file

@ -53,6 +53,7 @@ int main( int argc, char *argv[] ) {
break; break;
case 'p': case 'p':
show_prompt = true; show_prompt = true;
print_use_colours = true;
break; break;
default: default:
fwprintf( stderr, L"Unexpected option %c\n", option ); fwprintf( stderr, L"Unexpected option %c\n", option );

View file

@ -114,12 +114,13 @@ lisp_lambda( struct stack_frame *frame, struct cons_pointer lexpr,
struct cons_pointer new_env = env; struct cons_pointer new_env = env;
} else { } else {
char *buffer = malloc( 1024 ); char *buffer = malloc( 1024 );
struct cons_space_object not_lambda = pointer2cell( should_be_lambda );
memset( buffer, '\0', 1024 ); memset( buffer, '\0', 1024 );
sprintf( buffer, sprintf( buffer,
"Expected lambda, but found cell with tag %d (%c%c%c%c)", "Expected lambda, but found cell with tag %d (%c%c%c%c)",
fn_cell.tag.value, fn_cell.tag.bytes[0], not_lambda.tag.value, not_lambda.tag.bytes[0],
fn_cell.tag.bytes[1], fn_cell.tag.bytes[2], not_lambda.tag.bytes[1], not_lambda.tag.bytes[2],
fn_cell.tag.bytes[3] ); not_lambda.tag.bytes[3] );
struct cons_pointer message = c_string_to_lisp_string( buffer ); struct cons_pointer message = c_string_to_lisp_string( buffer );
free( buffer ); free( buffer );
result = lisp_throw( message, frame ); result = lisp_throw( message, frame );

View file

@ -22,6 +22,8 @@
#include "integer.h" #include "integer.h"
#include "print.h" #include "print.h"
int print_use_colours = 0;
void print_string_contents( FILE * output, struct cons_pointer pointer ) { void print_string_contents( FILE * output, struct cons_pointer pointer ) {
if ( stringp( pointer ) || symbolp( pointer ) ) { if ( stringp( pointer ) || symbolp( pointer ) ) {
struct cons_space_object *cell = &pointer2cell( 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 ) { 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 ); 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 ) { void print( FILE * output, struct cons_pointer pointer ) {
@ -84,11 +96,19 @@ void print( FILE * output, struct cons_pointer pointer ) {
print_list( output, pointer ); print_list( output, pointer );
break; break;
case EXCEPTIONTV: case EXCEPTIONTV:
fwprintf( output, L"\nException: " ); fwprintf( output, L"\n%sException: ",
print_use_colours ? "\x1B[31m" : "" );
print_string_contents( output, cell.payload.exception.message ); print_string_contents( output, cell.payload.exception.message );
fputws( L"\x1B[39m", output );
break; break;
case INTEGERTV: 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; break;
case LAMBDATV: case LAMBDATV:
fwprintf( output, L"lambda" /* "λ" */ ); fwprintf( output, L"lambda" /* "λ" */ );
@ -109,14 +129,30 @@ void print( FILE * output, struct cons_pointer pointer ) {
buffer[i] = '\0'; buffer[i] = '\0';
} }
} }
if ( print_use_colours ) {
fputws( L"\x1B[34m", output );
}
fwprintf( output, L"%s", buffer ); fwprintf( output, L"%s", buffer );
if ( print_use_colours ) {
fputws( L"\x1B[39m", output );
}
free( buffer ); free( buffer );
break; break;
case STRINGTV: case STRINGTV:
if ( print_use_colours ) {
fputws( L"\x1B[36m", output );
}
print_string( output, pointer ); print_string( output, pointer );
if ( print_use_colours ) {
fputws( L"\x1B[39m", output );
}
break; break;
case SYMBOLTV: case SYMBOLTV:
if ( print_use_colours )
fputws( L"\x1B[1;33m", output );
print_string_contents( output, pointer ); print_string_contents( output, pointer );
if ( print_use_colours )
fputws( L"\x1B[0;39m", output );
break; break;
case TRUETV: case TRUETV:
fwprintf( output, L"t" ); fwprintf( output, L"t" );
@ -129,9 +165,10 @@ void print( FILE * output, struct cons_pointer pointer ) {
break; break;
default: default:
fwprintf( stderr, 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.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; break;
} }
} }

View file

@ -15,5 +15,6 @@
#define __print_h #define __print_h
void print( FILE * output, struct cons_pointer pointer ); void print( FILE * output, struct cons_pointer pointer );
extern int print_use_colours;
#endif #endif