Still not working, but I have increasing confidence I'm on the right track.

This commit is contained in:
Simon Brooke 2026-02-28 18:09:48 +00:00
parent a1c377bc7c
commit bcb227a5f9
10 changed files with 110 additions and 108 deletions

View file

@ -296,7 +296,8 @@ struct cons_pointer add_2( struct stack_frame *frame,
to_long_double( arg2 ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"+"),
result =
throw_exception( c_string_to_lisp_symbol( L"+" ),
c_string_to_lisp_string
( L"Cannot add: not a number" ),
frame_pointer );
@ -320,7 +321,8 @@ struct cons_pointer add_2( struct stack_frame *frame,
to_long_double( arg2 ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"+"),
result =
throw_exception( c_string_to_lisp_symbol( L"+" ),
c_string_to_lisp_string
( L"Cannot add: not a number" ),
frame_pointer );
@ -334,7 +336,7 @@ struct cons_pointer add_2( struct stack_frame *frame,
break;
default:
result = exceptionp( arg2 ) ? arg2 :
throw_exception( c_string_to_lisp_symbol( L"+"),
throw_exception( c_string_to_lisp_symbol( L"+" ),
c_string_to_lisp_string
( L"Cannot add: not a number" ),
frame_pointer );
@ -431,7 +433,7 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
break;
default:
result =
throw_exception( c_string_to_lisp_symbol( L"*"),
throw_exception( c_string_to_lisp_symbol( L"*" ),
make_cons
( c_string_to_lisp_string
( L"Cannot multiply: argument 2 is not a number: " ),
@ -458,7 +460,7 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
break;
default:
result =
throw_exception( c_string_to_lisp_symbol( L"*"),
throw_exception( c_string_to_lisp_symbol( L"*" ),
make_cons
( c_string_to_lisp_string
( L"Cannot multiply: argument 2 is not a number" ),
@ -472,7 +474,7 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
to_long_double( arg2 ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"*"),
result = throw_exception( c_string_to_lisp_symbol( L"*" ),
make_cons( c_string_to_lisp_string
( L"Cannot multiply: argument 1 is not a number" ),
c_type( arg1 ) ),
@ -626,7 +628,7 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
to_long_double( arg2 ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"-"),
result = throw_exception( c_string_to_lisp_symbol( L"-" ),
c_string_to_lisp_string
( L"Cannot subtract: not a number" ),
frame_pointer );
@ -657,7 +659,7 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
to_long_double( arg2 ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"-"),
result = throw_exception( c_string_to_lisp_symbol( L"-" ),
c_string_to_lisp_string
( L"Cannot subtract: not a number" ),
frame_pointer );
@ -669,7 +671,7 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
make_real( to_long_double( arg1 ) - to_long_double( arg2 ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"-"),
result = throw_exception( c_string_to_lisp_symbol( L"-" ),
c_string_to_lisp_string
( L"Cannot subtract: not a number" ),
frame_pointer );
@ -741,7 +743,7 @@ struct cons_pointer lisp_divide( struct
to_long_double( frame->arg[1] ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"/"),
result = throw_exception( c_string_to_lisp_symbol( L"/" ),
c_string_to_lisp_string
( L"Cannot divide: not a number" ),
frame_pointer );
@ -772,7 +774,7 @@ struct cons_pointer lisp_divide( struct
to_long_double( frame->arg[1] ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"/"),
result = throw_exception( c_string_to_lisp_symbol( L"/" ),
c_string_to_lisp_string
( L"Cannot divide: not a number" ),
frame_pointer );
@ -785,7 +787,7 @@ struct cons_pointer lisp_divide( struct
to_long_double( frame->arg[1] ) );
break;
default:
result = throw_exception( c_string_to_lisp_symbol( L"/"),
result = throw_exception( c_string_to_lisp_symbol( L"/" ),
c_string_to_lisp_string
( L"Cannot divide: not a number" ),
frame_pointer );

View file

@ -114,9 +114,8 @@ struct cons_pointer add_ratio_ratio( struct cons_pointer arg1,
cell1->payload.ratio.divisor ) );
r = make_ratio( dividend, divisor, true );
} else {
r = throw_exception( c_string_to_lisp_symbol( L"+"),
make_cons(
c_string_to_lisp_string
r = throw_exception( c_string_to_lisp_symbol( L"+" ),
make_cons( c_string_to_lisp_string
( L"Shouldn't happen: bad arg to add_ratio_ratio" ),
make_cons( arg1,
make_cons( arg2, NIL ) ) ),
@ -156,7 +155,7 @@ struct cons_pointer add_integer_ratio( struct cons_pointer intarg,
dec_ref( ratio );
} else {
result =
throw_exception( c_string_to_lisp_symbol( L"+"),
throw_exception( c_string_to_lisp_symbol( L"+" ),
make_cons( c_string_to_lisp_string
( L"Shouldn't happen: bad arg to add_integer_ratio" ),
make_cons( intarg,
@ -237,7 +236,7 @@ struct cons_pointer multiply_ratio_ratio( struct
release_integer( divisor );
} else {
result =
throw_exception( c_string_to_lisp_symbol( L"*"),
throw_exception( c_string_to_lisp_symbol( L"*" ),
c_string_to_lisp_string
( L"Shouldn't happen: bad arg to multiply_ratio_ratio" ),
NIL );
@ -273,7 +272,7 @@ struct cons_pointer multiply_integer_ratio( struct cons_pointer intarg,
release_integer( one );
} else {
result =
throw_exception( c_string_to_lisp_symbol( L"*"),
throw_exception( c_string_to_lisp_symbol( L"*" ),
c_string_to_lisp_string
( L"Shouldn't happen: bad arg to multiply_integer_ratio" ),
NIL );
@ -342,7 +341,7 @@ struct cons_pointer make_ratio( struct cons_pointer dividend,
}
} else {
result =
throw_exception( c_string_to_lisp_symbol( L"make_ratio"),
throw_exception( c_string_to_lisp_symbol( L"make_ratio" ),
c_string_to_lisp_string
( L"Dividend and divisor of a ratio must be integers" ),
NIL );

View file

@ -293,6 +293,8 @@ int main( int argc, char *argv[] ) {
*/
bind_symbol_value( privileged_symbol_nil, NIL, true );
bind_value( L"t", TRUE, true );
bind_symbol_value( privileged_keyword_location, TRUE, true );
bind_symbol_value( privileged_keyword_payload, TRUE, true );
/*
* standard input, output, error and sink streams
@ -413,7 +415,8 @@ int main( int argc, char *argv[] ) {
bind_function( L"keys",
L"`(keys store)`: Return a list of all keys in this `store`.",
&lisp_keys );
bind_function( L"list", L"`(list args...): Return a list of these `args`.",
bind_function( L"list",
L"`(list args...)`: Return a list of these `args`.",
&lisp_list );
bind_function( L"mapcar",
L"`(mapcar function sequence)`: Apply `function` to each element of `sequence` in turn, and return a sequence of the results.",

View file

@ -508,8 +508,8 @@ lisp_read_char( struct stack_frame *frame, struct cons_pointer frame_pointer,
if ( readp( frame->arg[0] ) ) {
result =
make_string( url_fgetwc
( pointer2cell( frame->arg[0] ).payload.stream.
stream ), NIL );
( pointer2cell( frame->arg[0] ).payload.
stream.stream ), NIL );
}
return result;

View file

@ -167,7 +167,7 @@ struct cons_pointer read_continuation( struct stack_frame *frame,
if ( url_feof( input ) ) {
result =
throw_exception( c_string_to_lisp_symbol( L"read"),
throw_exception( c_string_to_lisp_symbol( L"read" ),
c_string_to_lisp_string
( L"End of file while reading" ), frame_pointer );
} else {
@ -178,7 +178,7 @@ struct cons_pointer read_continuation( struct stack_frame *frame,
/* skip all characters from semi-colon to the end of the line */
break;
case EOF:
result = throw_exception( c_string_to_lisp_symbol( L"read"),
result = throw_exception( c_string_to_lisp_symbol( L"read" ),
c_string_to_lisp_string
( L"End of input while reading" ),
frame_pointer );
@ -268,7 +268,7 @@ struct cons_pointer read_continuation( struct stack_frame *frame,
result = read_symbol_or_key( input, SYMBOLTV, c );
} else {
result =
throw_exception(c_string_to_lisp_symbol( L"read"),
throw_exception( c_string_to_lisp_symbol( L"read" ),
make_cons( c_string_to_lisp_string
( L"Unrecognised start of input character" ),
make_string( c, NIL ) ),
@ -316,7 +316,7 @@ struct cons_pointer read_number( struct stack_frame *frame,
switch ( c ) {
case LPERIOD:
if ( seen_period || !nilp( dividend ) ) {
return throw_exception( c_string_to_lisp_symbol( L"read"),
return throw_exception( c_string_to_lisp_symbol( L"read" ),
c_string_to_lisp_string
( L"Malformed number: too many periods" ),
frame_pointer );
@ -328,7 +328,7 @@ struct cons_pointer read_number( struct stack_frame *frame,
break;
case LSLASH:
if ( seen_period || !nilp( dividend ) ) {
return throw_exception( c_string_to_lisp_symbol( L"read"),
return throw_exception( c_string_to_lisp_symbol( L"read" ),
c_string_to_lisp_string
( L"Malformed number: dividend of rational must be integer" ),
frame_pointer );

View file

@ -39,8 +39,6 @@ struct cons_pointer privileged_keyword_location = NIL;
*/
struct cons_pointer privileged_keyword_payload = NIL;
/**
* True if the value of the tag on the cell at this `pointer` is this `value`,
* or, if the tag of the cell is `VECP`, if the value of the tag of the
@ -49,11 +47,11 @@ struct cons_pointer privileged_keyword_payload = NIL;
bool check_tag( struct cons_pointer pointer, uint32_t value ) {
bool result = false;
struct cons_space_object cell = pointer2cell( pointer );
result = cell.tag.value == value;
struct cons_space_object *cell = &pointer2cell( pointer );
result = cell->tag.value == value;
if ( result == false ) {
if ( cell.tag.value == VECTORPOINTTV ) {
if ( cell->tag.value == VECTORPOINTTV ) {
struct vector_space_object *vec = pointer_to_vso( pointer );
if ( vec != NULL ) {

View file

@ -114,10 +114,10 @@ void dump_object( URL_FILE *output, struct cons_pointer pointer ) {
case RATIOTV:
url_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:
url_fputws( L"\t\tInput stream; metadata: ", output );

View file

@ -161,6 +161,10 @@ struct cons_pointer make_stack_frame( struct cons_pointer previous,
env );
frame->more = more;
inc_ref( more );
for ( ; !nilp( args ); args = c_cdr( args ) ) {
frame->args++;
}
}
}
debug_print( L"make_stack_frame: returning\n", DEBUG_STACK );

View file

@ -311,16 +311,16 @@ struct cons_pointer interned( struct cons_pointer key,
map->payload.hashmap.buckets[bucket_no] );
} else {
result =
throw_exception( c_string_to_lisp_symbol( L"interned?"),
make_cons
( c_string_to_lisp_string
throw_exception( c_string_to_lisp_symbol
( L"interned?" ),
make_cons( c_string_to_lisp_string
( L"Unexpected store type: " ),
c_type( store ) ), NIL );
}
break;
default:
result =
throw_exception( c_string_to_lisp_symbol( L"interned?"),
throw_exception( c_string_to_lisp_symbol( L"interned?" ),
make_cons
( c_string_to_lisp_string
( L"Unexpected store type: " ),
@ -329,7 +329,7 @@ struct cons_pointer interned( struct cons_pointer key,
}
} else {
result =
throw_exception( c_string_to_lisp_symbol( L"interned?"),
throw_exception( c_string_to_lisp_symbol( L"interned?" ),
make_cons
( c_string_to_lisp_string
( L"Unexpected key type: " ), c_type( key ) ),
@ -392,11 +392,12 @@ struct cons_pointer c_assoc( struct cons_pointer key,
result = hashmap_get( entry_ptr, key );
break;
default:
throw_exception( c_string_to_lisp_symbol( L"assoc"),
c_append
( c_string_to_lisp_string
throw_exception( c_string_to_lisp_symbol
( L"assoc" ),
c_append( c_string_to_lisp_string
( L"Store entry is of unknown type: " ),
c_type( entry_ptr ) ), NIL );
c_type( entry_ptr ) ),
NIL );
}
// #ifdef DEBUG
@ -417,7 +418,7 @@ struct cons_pointer c_assoc( struct cons_pointer key,
// debug_print( L"`\n", DEBUG_BIND );
// #endif
result =
throw_exception( c_string_to_lisp_symbol(L"assoc"),
throw_exception( c_string_to_lisp_symbol( L"assoc" ),
c_append
( c_string_to_lisp_string
( L"Store is of unknown type: " ),

View file

@ -337,22 +337,22 @@ struct cons_pointer maybe_fixup_exception_location( struct cons_pointer r,
fn_pointer ) {
struct cons_pointer result = r;
if ( exceptionp( result ) && (functionp( fn_pointer) || specialp(fn_pointer))) {
if ( exceptionp( result )
&& ( functionp( fn_pointer ) || specialp( fn_pointer ) ) ) {
struct cons_space_object *fn_cell = &pointer2cell( fn_pointer );
struct cons_pointer payload =
pointer2cell( result ).payload.exception.payload;
/* TODO: should name_key also be a privileged keyword? */
struct cons_pointer name_key =
c_string_to_lisp_keyword( L"name" );
struct cons_pointer name_key = c_string_to_lisp_keyword( L"name" );
switch ( get_tag_value( payload ) ) {
case NILTV:
case CONSTV:
case HASHTV:
{
if ( nilp( c_assoc( privileged_keyword_location ,
payload ) )) {
if ( nilp( c_assoc( privileged_keyword_location,
payload ) ) ) {
pointer2cell( result ).payload.exception.payload =
set( privileged_keyword_location,
c_assoc( name_key,
@ -363,17 +363,16 @@ struct cons_pointer maybe_fixup_exception_location( struct cons_pointer r,
break;
default:
pointer2cell( result ).payload.exception.payload =
make_cons(
make_cons( privileged_keyword_location,
make_cons( make_cons( privileged_keyword_location,
c_assoc( name_key,
fn_cell->payload.function.meta ) ),
make_cons(
make_cons( privileged_keyword_payload,
payload ) ,
NIL ));
fn_cell->payload.function.
meta ) ),
make_cons( make_cons
( privileged_keyword_payload,
payload ), NIL ) );
}
dec_ref( name_key);
dec_ref( name_key );
}
return result;
@ -421,10 +420,7 @@ c_apply( struct stack_frame *frame, struct cons_pointer frame_pointer,
get_stack_frame( next_pointer );
result = maybe_fixup_exception_location( ( *
( fn_cell->
payload.
function.
executable ) )
( fn_cell->payload.function.executable ) )
( next,
next_pointer,
env ),
@ -498,10 +494,7 @@ c_apply( struct stack_frame *frame, struct cons_pointer frame_pointer,
result = next_pointer;
} else {
result = maybe_fixup_exception_location( ( *
( fn_cell->
payload.
special.
executable ) )
( fn_cell->payload.special.executable ) )
( get_stack_frame( next_pointer ), next_pointer, env ), fn_pointer );
debug_print( L"Special form returning: ", DEBUG_EVAL );
debug_print_object( result, DEBUG_EVAL );
@ -1385,7 +1378,8 @@ lisp_exception( struct stack_frame *frame, struct cons_pointer frame_pointer,
return exceptionp( message ) ? message : throw_exception( message,
frame->arg[1],
frame->previous );
frame->
previous );
}
/**
@ -1569,13 +1563,14 @@ struct cons_pointer c_append( struct cons_pointer l1, struct cons_pointer l2 ) {
if ( pointer2cell( l1 ).tag.value == pointer2cell( l2 ).tag.value ) {
if ( nilp( c_cdr( l1 ) ) ) {
return
make_string_like_thing( ( pointer2cell( l1 ).payload.
string.character ), l2,
make_string_like_thing( ( pointer2cell( l1 ).
payload.string.character ),
l2,
pointer2cell( l1 ).tag.value );
} else {
return
make_string_like_thing( ( pointer2cell( l1 ).payload.
string.character ),
make_string_like_thing( ( pointer2cell( l1 ).
payload.string.character ),
c_append( c_cdr( l1 ), l2 ),
pointer2cell( l1 ).tag.value );
}