Colourised print.
This commit is contained in:
parent
9bfc9074b0
commit
7d0b6bec97
8
.gitignore
vendored
8
.gitignore
vendored
|
@ -20,3 +20,11 @@ post-scarcity\.iml
|
|||
doc/
|
||||
|
||||
log*
|
||||
|
||||
\.cproject
|
||||
|
||||
\.gdb_history
|
||||
|
||||
\.project
|
||||
|
||||
\.settings/language\.settings\.xml
|
||||
|
|
|
@ -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} );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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 );
|
||||
|
|
49
src/print.c
49
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,5 +15,6 @@
|
|||
#define __print_h
|
||||
|
||||
void print( FILE * output, struct cons_pointer pointer );
|
||||
extern int print_use_colours;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue