post-scarcity/src/c/ops/repl.c

102 lines
2.8 KiB
C

/**
* repl.c
*
* Post Scarcity Soctware Environment
*
* First cut at a top level read-eval-print loop.
*
* Copyright (c): 17 Apr 2026 Simon Brooke <simon@journeyman.cc>
* Licensed under GPL version 2.0, or, at your option, any later version.
*/
#include <signal.h>
#include <stdio.h>
#include <wchar.h>
#include "debug.h"
#include "io/fopen.h"
#include "io/io.h"
#include "io/print.h"
#include "io/read.h"
#include "memory/node.h"
#include "memory/pointer.h"
#include "memory/pso.h"
#include "memory/pso2.h"
#include "memory/pso4.h"
#include "memory/tags.h"
#include "payloads/cons.h"
#include "payloads/function.h"
#include "payloads/stack.h"
#include "ops/assoc.h"
#include "ops/eval_apply.h"
#include "ops/truth.h"
/**
* @brief Handle an interrupt signal.
*
* @param dummy
*/
void int_handler( int dummy ) {
wprintf( L"TODO: handle ctrl-C in a more interesting way\n" );
}
/**
* Very simple read/eval/print loop for bootstrapping.
*/
void c_repl( bool show_prompt ) {
// todo: issue #21: must have stack frame passed in.
signal( SIGINT, int_handler );
debug_print( L"Entered repl\n", DEBUG_REPL, 0 );
struct pso_pointer env = consp( oblist ) ? oblist : make_cons( oblist, nil );
struct pso_pointer input_stream = c_assoc( lisp_io_in, env );
struct pso_pointer output_stream = c_assoc( lisp_io_out, env );
if ( !readp( input_stream ) ) {
debug_print( L"Invalid read stream: ", DEBUG_IO, 0 );
debug_print_object( input_stream, DEBUG_IO, 0 );
input_stream = lisp_stdin;
}
if ( !writep( output_stream ) ) {
debug_print( L"Invalid write stream: ", DEBUG_IO, 0 );
debug_print_object( output_stream, DEBUG_IO, 0 );
output_stream = lisp_stdout;
}
while ( readp( input_stream ) &&
!url_feof( stream_get_url_file( input_stream ) ) ) {
if ( show_prompt )
c_princ( c_assoc( lisp_io_prompt, env ), output_stream );
/* bottom of stack */
struct pso_pointer frame_pointer = make_frame( 1, nil, input_stream );
if ( nilp( frame_pointer ) )
break;
struct pso_pointer input = read(
#ifndef MANAGED_POINTER_ONLY
pointer_to_pso4( frame_pointer ),
#endif
frame_pointer, env );
frame_pointer = make_frame( 1, frame_pointer, input );
if ( nilp( frame_pointer ) )
break;
struct pso_pointer result = eval(
#ifndef MANAGED_POINTER_ONLY
pointer_to_pso4( frame_pointer ),
#endif
frame_pointer, oblist );
c_print( result, output_stream );
dec_ref( frame_pointer );
}
debug_print( L"Leaving repl\n", DEBUG_REPL, 0 );
}