Can now read files from the filesystem.

This commit is contained in:
Simon Brooke 2019-01-27 18:54:23 +00:00
parent d9acb277bf
commit 3470f27585
3 changed files with 34 additions and 7 deletions

1
hi Normal file
View file

@ -0,0 +1 @@
Hello, this is used by `slurp.sh` test, please do not remove.

View file

@ -31,7 +31,7 @@ char *lisp_string_to_c_string( struct cons_pointer s ) {
int len = 0;
for ( struct cons_pointer c = s; !nilp( c );
c = pointer2cell( c ).payload.string.cdr ) {
c = pointer2cell( c ).payload.string.cdr ) {
len++;
}
@ -49,6 +49,10 @@ char *lisp_string_to_c_string( struct cons_pointer s ) {
free( buffer );
}
debug_print(L"lisp_string_to_c_string( ", DEBUG_IO);
debug_print_object( s, DEBUG_IO);
debug_printf( DEBUG_IO, L") => '%s'\n", result);
return result;
}
@ -110,6 +114,10 @@ lisp_open( struct stack_frame *frame, struct cons_pointer frame_pointer,
}
free( url );
if ( pointer2cell(result).payload.stream.stream == NULL) {
result = NIL;
}
}
return result;
@ -158,18 +166,23 @@ struct cons_pointer
lisp_slurp( struct stack_frame *frame, struct cons_pointer frame_pointer,
struct cons_pointer env ) {
struct cons_pointer result = NIL;
struct cons_pointer cdr = NIL;
if ( readp( frame->arg[0] ) ) {
URL_FILE *stream = pointer2cell( frame->arg[0] ).payload.stream.stream;
struct cons_pointer cursor = make_string( url_fgetwc( stream ), NIL);
result = cursor;
for ( wint_t c = url_fgetwc( stream ); c != -1;
for ( wint_t c = url_fgetwc( stream ); !url_feof(stream);
c = url_fgetwc( stream ) ) {
cdr = make_string( ( ( wchar_t ) c ), cdr );
debug_print(L"slurp: cursor is: ", DEBUG_IO);
debug_dump_object( cursor, DEBUG_IO);
debug_print(L"; result is: ", DEBUG_IO);
debug_dump_object( result, DEBUG_IO);
debug_println( DEBUG_IO);
if ( nilp( result ) ) {
result = cdr;
}
struct cons_space_object * cell = &pointer2cell(cursor);
cursor = make_string( ( wchar_t ) c , NIL);
cell->payload.string.cdr = cursor;
}
}

13
unit-tests/slurp.sh Executable file
View file

@ -0,0 +1,13 @@
#!/bin/bash
expected='"Hello, this is used by `slurp.sh` test, please do not remove.'
actual=`echo '(slurp (open "hi"))' | target/psse | tail -2 | head -1`
if [ "${expected}" = "${actual}" ]
then
echo "OK"
exit 0
else
echo "Fail: expected '$expected', got '$actual'"
exit 1
fi