Better exceptions, investigation on failure to read/print wide chars.

This commit is contained in:
Simon Brooke 2018-12-25 15:32:45 +00:00
parent 9ff2f14c7d
commit ae8ba67ed7
13 changed files with 217 additions and 183 deletions

2
.gitignore vendored
View file

@ -28,3 +28,5 @@ log*
\.project \.project
\.settings/language\.settings\.xml \.settings/language\.settings\.xml
utils_src/readprintwc/out

View file

@ -406,8 +406,8 @@ struct cons_pointer inverse( struct stack_frame *frame,
case RATIOTV: case RATIOTV:
result = make_ratio( frame, result = make_ratio( frame,
make_integer( 0 - make_integer( 0 -
to_long_int( cell.payload. to_long_int( cell.payload.ratio.
ratio.dividend ) ), dividend ) ),
cell.payload.ratio.divisor ); cell.payload.ratio.divisor );
break; break;
case REALTV: case REALTV:

View file

@ -60,10 +60,10 @@ struct cons_pointer simplify_ratio( struct stack_frame *frame,
if ( ratiop( arg ) ) { if ( ratiop( arg ) ) {
int64_t ddrv = int64_t ddrv =
pointer2cell( pointer2cell( arg ).payload.ratio.dividend ). pointer2cell( pointer2cell( arg ).payload.ratio.dividend ).payload.
payload.integer.value, drrv = integer.value, drrv =
pointer2cell( pointer2cell( arg ).payload.ratio.divisor ). pointer2cell( pointer2cell( arg ).payload.ratio.divisor ).payload.
payload.integer.value, gcd = greatest_common_divisor( ddrv, drrv ); integer.value, gcd = greatest_common_divisor( ddrv, drrv );
if ( gcd > 1 ) { if ( gcd > 1 ) {
if ( drrv / gcd == 1 ) { if ( drrv / gcd == 1 ) {
@ -75,9 +75,10 @@ struct cons_pointer simplify_ratio( struct stack_frame *frame,
} }
} }
} else { } else {
result = lisp_throw( result =
c_string_to_lisp_string( "Shouldn't happen: bad arg to simplify_ratio" ), lisp_throw( make_cons( c_string_to_lisp_string
frame ); ( "Shouldn't happen: bad arg to simplify_ratio" ),
arg ), frame );
} }
return result; return result;
@ -147,8 +148,11 @@ struct cons_pointer add_ratio_ratio( struct stack_frame *frame,
dec_ref( r ); dec_ref( r );
} }
} else { } else {
result = lisp_throw( result =
c_string_to_lisp_string( "Shouldn't happen: bad arg to add_ratio_ratio" ), lisp_throw( make_cons( c_string_to_lisp_string
( "Shouldn't happen: bad arg to add_ratio_ratio" ),
make_cons( arg1,
make_cons( arg2, NIL ) ) ),
frame ); frame );
} }
@ -181,8 +185,11 @@ struct cons_pointer add_integer_ratio( struct stack_frame *frame,
dec_ref( one ); dec_ref( one );
dec_ref( ratio ); dec_ref( ratio );
} else { } else {
result = lisp_throw( result =
c_string_to_lisp_string( "Shouldn't happen: bad arg to add_integer_ratio" ), lisp_throw( make_cons( c_string_to_lisp_string
( "Shouldn't happen: bad arg to add_integer_ratio" ),
make_cons( intarg,
make_cons( ratarg, NIL ) ) ),
frame ); frame );
} }
@ -198,10 +205,10 @@ struct cons_pointer divide_ratio_ratio( struct stack_frame *frame,
struct cons_pointer arg1, struct cons_pointer arg1,
struct cons_pointer arg2 ) { struct cons_pointer arg2 ) {
struct cons_pointer i = make_ratio( frame, struct cons_pointer i = make_ratio( frame,
pointer2cell( arg2 ).payload. pointer2cell( arg2 ).payload.ratio.
ratio.divisor, divisor,
pointer2cell( arg2 ).payload. pointer2cell( arg2 ).payload.ratio.
ratio.dividend ), result = dividend ), result =
multiply_ratio_ratio( frame, arg1, i ); multiply_ratio_ratio( frame, arg1, i );
dec_ref( i ); dec_ref( i );
@ -241,7 +248,8 @@ struct cons_pointer multiply_ratio_ratio( struct
pointer2cell( cell2.payload.ratio.divisor ).payload.integer.value, pointer2cell( cell2.payload.ratio.divisor ).payload.integer.value,
ddrv = dd1v * dd2v, drrv = dr1v * dr2v; ddrv = dd1v * dd2v, drrv = dr1v * dr2v;
struct cons_pointer unsimplified = make_ratio( frame, make_integer( ddrv ), struct cons_pointer unsimplified =
make_ratio( frame, make_integer( ddrv ),
make_integer( drrv ) ); make_integer( drrv ) );
result = simplify_ratio( frame, unsimplified ); result = simplify_ratio( frame, unsimplified );
@ -249,8 +257,9 @@ struct cons_pointer multiply_ratio_ratio( struct
dec_ref( unsimplified ); dec_ref( unsimplified );
} }
} else { } else {
result = lisp_throw( result =
c_string_to_lisp_string( "Shouldn't happen: bad arg to multiply_ratio_ratio" ), lisp_throw( c_string_to_lisp_string
( "Shouldn't happen: bad arg to multiply_ratio_ratio" ),
frame ); frame );
} }
@ -275,8 +284,9 @@ struct cons_pointer multiply_integer_ratio( struct stack_frame *frame,
dec_ref( one ); dec_ref( one );
dec_ref( ratio ); dec_ref( ratio );
} else { } else {
result = lisp_throw( result =
c_string_to_lisp_string( "Shouldn't happen: bad arg to multiply_integer_ratio" ), lisp_throw( c_string_to_lisp_string
( "Shouldn't happen: bad arg to multiply_integer_ratio" ),
frame ); frame );
} }

View file

@ -111,6 +111,7 @@ int main( int argc, char *argv[] ) {
*/ */
bind_special( "cond", &lisp_cond ); bind_special( "cond", &lisp_cond );
bind_special( "lambda", &lisp_lambda ); bind_special( "lambda", &lisp_lambda );
/* bind_special( "λ", &lisp_lambda ); */
bind_special( "nlambda", &lisp_nlambda ); bind_special( "nlambda", &lisp_nlambda );
bind_special( "progn", &lisp_progn ); bind_special( "progn", &lisp_progn );
bind_special( "quote", &lisp_quote ); bind_special( "quote", &lisp_quote );

View file

@ -136,10 +136,10 @@ void dump_object( FILE * output, struct cons_pointer pointer ) {
case RATIOTV: case RATIOTV:
fwprintf( output, fwprintf( output,
L"\t\tRational cell: value %ld/%ld, count %u\n", L"\t\tRational cell: value %ld/%ld, count %u\n",
pointer2cell( cell.payload.ratio.dividend ). pointer2cell( cell.payload.ratio.dividend ).payload.
payload.integer.value, integer.value,
pointer2cell( cell.payload.ratio.divisor ). pointer2cell( cell.payload.ratio.divisor ).payload.
payload.integer.value, cell.count ); integer.value, cell.count );
break; break;
case READTV: case READTV:
fwprintf( output, L"\t\tInput stream\n" ); fwprintf( output, L"\t\tInput stream\n" );

View file

@ -80,8 +80,8 @@ bool equal( struct cons_pointer a, struct cons_pointer b ) {
&& ( equal( cell_a->payload.string.cdr, && ( equal( cell_a->payload.string.cdr,
cell_b->payload.string.cdr ) cell_b->payload.string.cdr )
|| ( end_of_string( cell_a->payload.string.cdr ) || ( end_of_string( cell_a->payload.string.cdr )
&& end_of_string( cell_b->payload. && end_of_string( cell_b->payload.string.
string.cdr ) ) ); cdr ) ) );
break; break;
case INTEGERTV: case INTEGERTV:
result = result =

View file

@ -683,7 +683,8 @@ struct cons_pointer c_reverse( struct cons_pointer arg) {
* (reverse sequence) * (reverse sequence)
* Return a sequence like this sequence but with the members in the reverse order. * Return a sequence like this sequence but with the members in the reverse order.
*/ */
struct cons_pointer lisp_reverse( struct stack_frame *frame, struct cons_pointer env ) { struct cons_pointer lisp_reverse( struct stack_frame *frame,
struct cons_pointer env ) {
return c_reverse( frame->arg[0] ); return c_reverse( frame->arg[0] );
} }

View file

@ -118,7 +118,12 @@ struct cons_pointer print( FILE * output, struct cons_pointer pointer ) {
case EXCEPTIONTV: case EXCEPTIONTV:
fwprintf( output, L"\n%sException: ", fwprintf( output, L"\n%sException: ",
print_use_colours ? "\x1B[31m" : "" ); print_use_colours ? "\x1B[31m" : "" );
print_string_contents( output, cell.payload.exception.message ); if ( stringp( cell.payload.exception.message ) ) {
print_string_contents( output,
cell.payload.exception.message );
} else {
print( output, cell.payload.exception.message );
}
break; break;
case FUNCTIONTV: case FUNCTIONTV:
fwprintf( output, L"(Function)" ); fwprintf( output, L"(Function)" );
@ -132,8 +137,8 @@ struct cons_pointer print( FILE * output, struct cons_pointer pointer ) {
case LAMBDATV: case LAMBDATV:
print( output, make_cons( c_string_to_lisp_symbol( "lambda" ), print( output, make_cons( c_string_to_lisp_symbol( "lambda" ),
make_cons( cell.payload.lambda.args, make_cons( cell.payload.lambda.args,
cell.payload.lambda. cell.payload.
body ) ) ); lambda.body ) ) );
break; break;
case NILTV: case NILTV:
fwprintf( output, L"nil" ); fwprintf( output, L"nil" );
@ -141,8 +146,8 @@ struct cons_pointer print( FILE * output, struct cons_pointer pointer ) {
case NLAMBDATV: case NLAMBDATV:
print( output, make_cons( c_string_to_lisp_symbol( "nlambda" ), print( output, make_cons( c_string_to_lisp_symbol( "nlambda" ),
make_cons( cell.payload.lambda.args, make_cons( cell.payload.lambda.args,
cell.payload.lambda. cell.payload.
body ) ) ); lambda.body ) ) );
break; break;
case RATIOTV: case RATIOTV:
print( output, cell.payload.ratio.dividend ); print( output, cell.payload.ratio.dividend );

View file

@ -119,8 +119,9 @@ struct cons_pointer read_continuation( struct stack_frame *frame, FILE * input,
result = read_symbol( input, c ); result = read_symbol( input, c );
} else { } else {
result = result =
make_exception( c_string_to_lisp_string make_exception( make_cons( c_string_to_lisp_string
( "Unrecognised start of input character" ), ( "Unrecognised start of input character" ),
make_string( c, NIL ) ),
frame ); frame );
} }
break; break;
@ -147,9 +148,6 @@ struct cons_pointer read_number( struct stack_frame *frame, FILE * input,
if ( negative ) { if ( negative ) {
initial = fgetwc( input ); initial = fgetwc( input );
} }
#ifdef DEBUG #ifdef DEBUG
fwprintf( stderr, L"read_number starting '%c' (%d)\n", initial, initial ); fwprintf( stderr, L"read_number starting '%c' (%d)\n", initial, initial );
#endif #endif

View file

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
int main( int argc, char *argv[] ) {
fwide( stdin, 1 );
fwide( stdout, 1 );
for (wchar_t c = fgetwc( stdin ); !feof( stdin); c = fgetwc( stdin )) {
if (c != '\n') {
fwprintf( stdout, L"Read character %d, %C\t", (int)c, c);
fputwc( c, stdout);
fputws(L"\n", stdout);
}
}
}