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
\.settings/language\.settings\.xml
utils_src/readprintwc/out

View file

@ -130,7 +130,7 @@ struct cons_pointer add_2( struct stack_frame *frame, struct cons_pointer arg1,
print( stderr, arg1 );
fputws( L"; arg2 = ", stderr );
print( stderr, arg2 );
fputws( L")\n", stderr);
fputws( L")\n", stderr );
#endif
if ( zerop( arg1 ) ) {
@ -264,7 +264,7 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
print( stderr, arg1 );
fputws( L"; arg2 = ", stderr );
print( stderr, arg2 );
fputws( L")\n", stderr);
fputws( L")\n", stderr );
#endif
if ( zerop( arg1 ) ) {
@ -406,8 +406,8 @@ struct cons_pointer inverse( struct stack_frame *frame,
case RATIOTV:
result = make_ratio( frame,
make_integer( 0 -
to_long_int( cell.payload.
ratio.dividend ) ),
to_long_int( cell.payload.ratio.
dividend ) ),
cell.payload.ratio.divisor );
break;
case REALTV:

View file

@ -58,12 +58,12 @@ struct cons_pointer simplify_ratio( struct stack_frame *frame,
struct cons_pointer arg ) {
struct cons_pointer result = arg;
if (ratiop(arg)) {
if ( ratiop( arg ) ) {
int64_t ddrv =
pointer2cell( pointer2cell( arg ).payload.ratio.dividend ).
payload.integer.value, drrv =
pointer2cell( pointer2cell( arg ).payload.ratio.divisor ).
payload.integer.value, gcd = greatest_common_divisor( ddrv, drrv );
pointer2cell( pointer2cell( arg ).payload.ratio.dividend ).payload.
integer.value, drrv =
pointer2cell( pointer2cell( arg ).payload.ratio.divisor ).payload.
integer.value, gcd = greatest_common_divisor( ddrv, drrv );
if ( gcd > 1 ) {
if ( drrv / gcd == 1 ) {
@ -75,9 +75,10 @@ struct cons_pointer simplify_ratio( struct stack_frame *frame,
}
}
} else {
result = lisp_throw(
c_string_to_lisp_string( "Shouldn't happen: bad arg to simplify_ratio" ),
frame );
result =
lisp_throw( make_cons( c_string_to_lisp_string
( "Shouldn't happen: bad arg to simplify_ratio" ),
arg ), frame );
}
return result;
@ -100,10 +101,10 @@ struct cons_pointer add_ratio_ratio( struct stack_frame *frame,
print( stderr, arg1 );
fputws( L"; arg2 = ", stderr );
print( stderr, arg2 );
fputws( L")\n", stderr);
fputws( L")\n", stderr );
#endif
if ( ratiop(arg1) && ratiop(arg2)) {
if ( ratiop( arg1 ) && ratiop( arg2 ) ) {
struct cons_space_object cell1 = pointer2cell( arg1 );
struct cons_space_object cell2 = pointer2cell( arg2 );
int64_t dd1v =
@ -147,8 +148,11 @@ struct cons_pointer add_ratio_ratio( struct stack_frame *frame,
dec_ref( r );
}
} else {
result = lisp_throw(
c_string_to_lisp_string( "Shouldn't happen: bad arg to add_ratio_ratio" ),
result =
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 );
}
@ -172,7 +176,7 @@ struct cons_pointer add_integer_ratio( struct stack_frame *frame,
struct cons_pointer ratarg ) {
struct cons_pointer result;
if (integerp(intarg) && ratiop(ratarg)) {
if ( integerp( intarg ) && ratiop( ratarg ) ) {
struct cons_pointer one = make_integer( 1 ),
ratio = make_ratio( frame, intarg, one );
@ -181,8 +185,11 @@ struct cons_pointer add_integer_ratio( struct stack_frame *frame,
dec_ref( one );
dec_ref( ratio );
} else {
result = lisp_throw(
c_string_to_lisp_string( "Shouldn't happen: bad arg to add_integer_ratio" ),
result =
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 );
}
@ -198,10 +205,10 @@ struct cons_pointer divide_ratio_ratio( struct stack_frame *frame,
struct cons_pointer arg1,
struct cons_pointer arg2 ) {
struct cons_pointer i = make_ratio( frame,
pointer2cell( arg2 ).payload.
ratio.divisor,
pointer2cell( arg2 ).payload.
ratio.dividend ), result =
pointer2cell( arg2 ).payload.ratio.
divisor,
pointer2cell( arg2 ).payload.ratio.
dividend ), result =
multiply_ratio_ratio( frame, arg1, i );
dec_ref( i );
@ -226,9 +233,9 @@ struct cons_pointer multiply_ratio_ratio( struct
print( stderr, arg1 );
fputws( L"; arg2 = ", stderr );
print( stderr, arg2 );
fputws( L")\n", stderr);
fputws( L")\n", stderr );
#endif
if ( ratiop(arg1) && ratiop(arg2)) {
if ( ratiop( arg1 ) && ratiop( arg2 ) ) {
struct cons_space_object cell1 = pointer2cell( arg1 );
struct cons_space_object cell2 = pointer2cell( arg2 );
int64_t dd1v =
@ -241,7 +248,8 @@ struct cons_pointer multiply_ratio_ratio( struct
pointer2cell( cell2.payload.ratio.divisor ).payload.integer.value,
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 ) );
result = simplify_ratio( frame, unsimplified );
@ -249,8 +257,9 @@ struct cons_pointer multiply_ratio_ratio( struct
dec_ref( unsimplified );
}
} else {
result = lisp_throw(
c_string_to_lisp_string( "Shouldn't happen: bad arg to multiply_ratio_ratio" ),
result =
lisp_throw( c_string_to_lisp_string
( "Shouldn't happen: bad arg to multiply_ratio_ratio" ),
frame );
}
@ -267,7 +276,7 @@ struct cons_pointer multiply_integer_ratio( struct stack_frame *frame,
struct cons_pointer ratarg ) {
struct cons_pointer result;
if (integerp(intarg) && ratiop(ratarg)) {
if ( integerp( intarg ) && ratiop( ratarg ) ) {
struct cons_pointer one = make_integer( 1 ),
ratio = make_ratio( frame, intarg, one );
result = multiply_ratio_ratio( frame, ratio, ratarg );
@ -275,8 +284,9 @@ struct cons_pointer multiply_integer_ratio( struct stack_frame *frame,
dec_ref( one );
dec_ref( ratio );
} else {
result = lisp_throw(
c_string_to_lisp_string( "Shouldn't happen: bad arg to multiply_integer_ratio" ),
result =
lisp_throw( c_string_to_lisp_string
( "Shouldn't happen: bad arg to multiply_integer_ratio" ),
frame );
}

View file

@ -12,37 +12,37 @@
#define __ratio_h
struct cons_pointer simplify_ratio( struct stack_frame *frame,
struct cons_pointer arg ) ;
struct cons_pointer arg );
struct cons_pointer add_ratio_ratio( struct stack_frame *frame,
struct cons_pointer arg1,
struct cons_pointer arg2 ) ;
struct cons_pointer arg2 );
struct cons_pointer add_integer_ratio( struct stack_frame *frame,
struct cons_pointer intarg,
struct cons_pointer ratarg ) ;
struct cons_pointer ratarg );
struct cons_pointer divide_ratio_ratio( struct stack_frame *frame,
struct cons_pointer arg1,
struct cons_pointer arg2 ) ;
struct cons_pointer arg2 );
struct cons_pointer multiply_ratio_ratio( struct
stack_frame
*frame, struct
cons_pointer arg1, struct
cons_pointer arg2 ) ;
cons_pointer arg2 );
struct cons_pointer multiply_integer_ratio( struct stack_frame *frame,
struct cons_pointer intarg,
struct cons_pointer ratarg ) ;
struct cons_pointer ratarg );
struct cons_pointer subtract_ratio_ratio( struct stack_frame *frame,
struct cons_pointer arg1,
struct cons_pointer arg2 ) ;
struct cons_pointer arg2 );
struct cons_pointer make_ratio( struct stack_frame *frame,
struct cons_pointer dividend,
struct cons_pointer divisor ) ;
struct cons_pointer divisor );
#endif

View file

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

View file

@ -136,10 +136,10 @@ void dump_object( FILE * output, struct cons_pointer pointer ) {
case RATIOTV:
fwprintf( output,
L"\t\tRational cell: value %ld/%ld, count %u\n",
pointer2cell( cell.payload.ratio.dividend ).
payload.integer.value,
pointer2cell( cell.payload.ratio.divisor ).
payload.integer.value, cell.count );
pointer2cell( cell.payload.ratio.dividend ).payload.
integer.value,
pointer2cell( cell.payload.ratio.divisor ).payload.
integer.value, cell.count );
break;
case READTV:
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,
cell_b->payload.string.cdr )
|| ( end_of_string( cell_a->payload.string.cdr )
&& end_of_string( cell_b->payload.
string.cdr ) ) );
&& end_of_string( cell_b->payload.string.
cdr ) ) );
break;
case INTEGERTV:
result =

View file

@ -172,7 +172,7 @@ void log_binding( struct cons_pointer name, struct cons_pointer val ) {
#ifdef DEBUG
fputws( L"\n\tBinding ", stderr );
print( stderr, name );
fputws( L" to ", stderr);
fputws( L" to ", stderr );
print( stderr, val );
fputws( L"\"\n", stderr );
#endif
@ -657,20 +657,20 @@ lisp_read( struct stack_frame *frame, struct cons_pointer env ) {
/**
* reverse a sequence.
*/
struct cons_pointer c_reverse( struct cons_pointer arg) {
struct cons_pointer c_reverse( struct cons_pointer arg ) {
struct cons_pointer result = NIL;
for (struct cons_pointer p = arg; sequencep(p); p = c_cdr(p)) {
struct cons_space_object o = pointer2cell(p);
switch (o.tag.value) {
for ( struct cons_pointer p = arg; sequencep( p ); p = c_cdr( p ) ) {
struct cons_space_object o = pointer2cell( p );
switch ( o.tag.value ) {
case CONSTV:
result = make_cons(o.payload.cons.car, result);
result = make_cons( o.payload.cons.car, result );
break;
case STRINGTV:
result = make_string(o.payload.string.character, result);
result = make_string( o.payload.string.character, result );
break;
case SYMBOLTV:
result = make_symbol(o.payload.string.character, result);
result = make_symbol( o.payload.string.character, result );
break;
}
}
@ -683,8 +683,9 @@ struct cons_pointer c_reverse( struct cons_pointer arg) {
* (reverse sequence)
* 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 ) {
return c_reverse( frame->arg[0]);
struct cons_pointer lisp_reverse( struct stack_frame *frame,
struct cons_pointer env ) {
return c_reverse( frame->arg[0] );
}

View file

@ -40,7 +40,7 @@ struct cons_pointer c_car( struct cons_pointer arg );
*/
struct cons_pointer c_cdr( struct cons_pointer arg );
struct cons_pointer c_reverse( struct cons_pointer arg);
struct cons_pointer c_reverse( struct cons_pointer arg );
/**
* Useful building block; evaluate this single form in the context of this

View file

@ -118,7 +118,12 @@ struct cons_pointer print( FILE * output, struct cons_pointer pointer ) {
case EXCEPTIONTV:
fwprintf( output, L"\n%sException: ",
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;
case FUNCTIONTV:
fwprintf( output, L"(Function)" );
@ -132,8 +137,8 @@ struct cons_pointer print( FILE * output, struct cons_pointer pointer ) {
case LAMBDATV:
print( output, make_cons( c_string_to_lisp_symbol( "lambda" ),
make_cons( cell.payload.lambda.args,
cell.payload.lambda.
body ) ) );
cell.payload.
lambda.body ) ) );
break;
case NILTV:
fwprintf( output, L"nil" );
@ -141,8 +146,8 @@ struct cons_pointer print( FILE * output, struct cons_pointer pointer ) {
case NLAMBDATV:
print( output, make_cons( c_string_to_lisp_symbol( "nlambda" ),
make_cons( cell.payload.lambda.args,
cell.payload.lambda.
body ) ) );
cell.payload.
lambda.body ) ) );
break;
case RATIOTV:
print( output, cell.payload.ratio.dividend );

View file

@ -86,7 +86,7 @@ struct cons_pointer read_continuation( struct stack_frame *frame, FILE * input,
case '"':
result = read_string( input, fgetwc( input ) );
break;
case '-': {
case '-':{
wint_t next = fgetwc( input );
ungetwc( next, input );
if ( iswdigit( next ) ) {
@ -119,8 +119,9 @@ struct cons_pointer read_continuation( struct stack_frame *frame, FILE * input,
result = read_symbol( input, c );
} else {
result =
make_exception( c_string_to_lisp_string
make_exception( make_cons( c_string_to_lisp_string
( "Unrecognised start of input character" ),
make_string( c, NIL ) ),
frame );
}
break;
@ -142,14 +143,11 @@ struct cons_pointer read_number( struct stack_frame *frame, FILE * input,
int64_t dividend = 0;
int places_of_decimals = 0;
wint_t c;
bool negative = initial == btowc( '-');
bool negative = initial == btowc( '-' );
if (negative) {
if ( negative ) {
initial = fgetwc( input );
}
#ifdef DEBUG
fwprintf( stderr, L"read_number starting '%c' (%d)\n", initial, initial );
#endif
@ -193,7 +191,7 @@ struct cons_pointer read_number( struct stack_frame *frame, FILE * input,
if ( seen_period ) {
long double rv = ( long double )
( accumulator / pow( 10, places_of_decimals ) );
if (negative) {
if ( negative ) {
rv = 0 - rv;
}
#ifdef DEBUG
@ -205,7 +203,7 @@ struct cons_pointer read_number( struct stack_frame *frame, FILE * input,
make_ratio( frame, make_integer( dividend ),
make_integer( accumulator ) );
} else {
if (negative) {
if ( negative ) {
accumulator = 0 - accumulator;
}
result = make_integer( accumulator );

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);
}
}
}