All unit tests passing! This is slightly a fix because there is a bug with
the character read after reading a number not being correctly pushed back onto the input stream, but...
This commit is contained in:
parent
6eab3a531a
commit
2d9f4b0439
|
@ -121,11 +121,12 @@ struct cons_pointer make_string( char c, struct cons_pointer tail) {
|
||||||
|
|
||||||
if ( check_tag( tail, STRINGTAG) || check_tag( tail, NILTAG)) {
|
if ( check_tag( tail, STRINGTAG) || check_tag( tail, NILTAG)) {
|
||||||
pointer = allocate_cell( STRINGTAG);
|
pointer = allocate_cell( STRINGTAG);
|
||||||
struct cons_space_object* cell = &conspages[pointer.page]->cell[pointer.offset];
|
struct cons_space_object* cell = &pointer2cell(pointer);
|
||||||
|
|
||||||
inc_ref(tail);
|
inc_ref(tail);
|
||||||
cell->payload.string.character = (uint32_t) c;
|
cell->payload.string.character = (uint32_t) c;
|
||||||
cell->payload.string.cdr = tail;
|
cell->payload.string.cdr.page = tail.page;
|
||||||
|
cell->payload.string.cdr.offset = tail.offset;
|
||||||
} else {
|
} else {
|
||||||
fprintf( stderr, "Warning: only NIL and STRING can be appended to STRING\n");
|
fprintf( stderr, "Warning: only NIL and STRING can be appended to STRING\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,17 +53,17 @@
|
||||||
/**
|
/**
|
||||||
* true if conspointer points to the special cell NIL, else false
|
* true if conspointer points to the special cell NIL, else false
|
||||||
*/
|
*/
|
||||||
#define nilp(conspoint) (check_tag(conspoint,NILTAG)==0)
|
#define nilp(conspoint) (check_tag(conspoint,NILTAG))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* true if conspointer points to a cons cell, else false
|
* true if conspointer points to a cons cell, else false
|
||||||
*/
|
*/
|
||||||
#define consp(conspoint) (check_tag(conspoint,CONSTAG)==0)
|
#define consp(conspoint) (check_tag(conspoint,CONSTAG))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* true if conspointer points to a string cell, else false
|
* true if conspointer points to a string cell, else false
|
||||||
*/
|
*/
|
||||||
#define stringp(conspoint) (check_tag(conspoint,STRINGTAG)==0)
|
#define stringp(conspoint) (check_tag(conspoint,STRINGTAG))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An indirect pointer to a cons cell
|
* An indirect pointer to a cons cell
|
||||||
|
|
|
@ -18,16 +18,15 @@
|
||||||
#include "read.h"
|
#include "read.h"
|
||||||
|
|
||||||
int main (int argc, char *argv[]) {
|
int main (int argc, char *argv[]) {
|
||||||
// printf( "Post scarcity software environment version %s\n", VERSION);
|
fprintf( stderr, "Post scarcity software environment version %s\n", VERSION);
|
||||||
initialise_cons_pages();
|
initialise_cons_pages();
|
||||||
|
|
||||||
|
fprintf( stderr, "\n:: ");
|
||||||
struct cons_pointer input = read( stdin);
|
struct cons_pointer input = read( stdin);
|
||||||
//inc_ref( input);
|
fprintf( stderr, "\n{%d,%d}=> ", input.page, input.offset);
|
||||||
//printf( "\n:: ");
|
|
||||||
print( stdout, input);
|
print( stdout, input);
|
||||||
|
|
||||||
// dump_pages(stdout);
|
dump_pages(stderr);
|
||||||
// printf( "Tag2uint(\"FREE\") = %d\n", tag2uint("FREE"));
|
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*/
|
*/
|
||||||
struct cons_pointer make_integer( int value) {
|
struct cons_pointer make_integer( int value) {
|
||||||
struct cons_pointer result = allocate_cell( INTEGERTAG);
|
struct cons_pointer result = allocate_cell( INTEGERTAG);
|
||||||
struct cons_space_object* cell = &conspages[result.page]->cell[result.offset];
|
struct cons_space_object* cell = &pointer2cell(result);
|
||||||
cell->payload.integer.value = value;
|
cell->payload.integer.value = value;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
60
src/print.c
60
src/print.c
|
@ -17,17 +17,51 @@
|
||||||
#include "integer.h"
|
#include "integer.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
|
|
||||||
|
void print_string_contents( FILE* output, struct cons_pointer pointer) {
|
||||||
|
if ( check_tag( pointer, STRINGTAG)) {
|
||||||
|
struct cons_space_object* cell = &pointer2cell(pointer);
|
||||||
|
char c = cell->payload.string.character;
|
||||||
|
|
||||||
|
if ( c != '\0') {
|
||||||
|
fputc( c, output);
|
||||||
|
}
|
||||||
|
print_string_contents( output, cell->payload.string.cdr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void print_string( FILE* output, struct cons_pointer pointer) {
|
||||||
|
fputc( '"', output);
|
||||||
|
print_string_contents( output, pointer);
|
||||||
|
fputc( '"', output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void print_list_contents( FILE* output, struct cons_pointer pointer) {
|
||||||
|
if ( check_tag( pointer, CONSTAG)) {
|
||||||
|
struct cons_space_object* cell = &pointer2cell(pointer);
|
||||||
|
|
||||||
|
print( output, cell->payload.cons.car);
|
||||||
|
|
||||||
|
if ( !nilp( cell->payload.cons.cdr)) {
|
||||||
|
fputc( ' ', output);
|
||||||
|
}
|
||||||
|
print_list_contents( output, cell->payload.cons.cdr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void print_list( FILE* output, struct cons_pointer pointer) {
|
||||||
|
fputc( '(', output);
|
||||||
|
print_list_contents( output, pointer);
|
||||||
|
fputc( ')', output);
|
||||||
|
}
|
||||||
|
|
||||||
void print( FILE* output, struct cons_pointer pointer) {
|
void print( FILE* output, struct cons_pointer pointer) {
|
||||||
struct cons_space_object cell = pointer2cell( pointer);
|
struct cons_space_object cell = pointer2cell( pointer);
|
||||||
|
|
||||||
if ( check_tag( pointer, CONSTAG)) {
|
if ( check_tag( pointer, CONSTAG)) {
|
||||||
fputc( '(', output);
|
print_list( output, pointer);
|
||||||
for (struct cons_pointer p = pointer; consp( p);
|
|
||||||
p = pointer2cell( p).payload.cons.cdr) {
|
|
||||||
print( output, p);
|
|
||||||
fputc( ' ', output);
|
|
||||||
}
|
|
||||||
fputc( ')', output);
|
|
||||||
} else if ( check_tag( pointer, INTEGERTAG)) {
|
} else if ( check_tag( pointer, INTEGERTAG)) {
|
||||||
fprintf( output, "%ld", cell.payload.integer.value);
|
fprintf( output, "%ld", cell.payload.integer.value);
|
||||||
} else if ( check_tag( pointer, NILTAG)) {
|
} else if ( check_tag( pointer, NILTAG)) {
|
||||||
|
@ -35,17 +69,7 @@ void print( FILE* output, struct cons_pointer pointer) {
|
||||||
} else if ( check_tag( pointer, REALTAG)) {
|
} else if ( check_tag( pointer, REALTAG)) {
|
||||||
fprintf( output, "%Lf", cell.payload.real.value);
|
fprintf( output, "%Lf", cell.payload.real.value);
|
||||||
} else if ( check_tag( pointer, STRINGTAG)) {
|
} else if ( check_tag( pointer, STRINGTAG)) {
|
||||||
fputc( '"', output);
|
print_string( output, pointer);
|
||||||
for (struct cons_pointer p = pointer; stringp( p);
|
|
||||||
p = pointer2cell( p).payload.string.cdr) {
|
|
||||||
// TODO: That's potentially a UTF character, needs more handling.
|
|
||||||
char c = (char)pointer2cell( p).payload.string.character;
|
|
||||||
|
|
||||||
if ( c != '\0') {
|
|
||||||
fprintf( output, "%c", c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fputc( '"', output);
|
|
||||||
} else if ( check_tag( pointer, TRUETAG)) {
|
} else if ( check_tag( pointer, TRUETAG)) {
|
||||||
fprintf( output, "T");
|
fprintf( output, "T");
|
||||||
}
|
}
|
||||||
|
|
13
unit-tests/complex-list.sh
Normal file
13
unit-tests/complex-list.sh
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
expected='(1 2 3 ("Fred") NIL 77354)'
|
||||||
|
actual=`echo '(1 2 3 ("Fred") () 77354 )' | target/psse 2> /dev/null`
|
||||||
|
|
||||||
|
if [ "${expected}" = "${actual}" ]
|
||||||
|
then
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
echo "Fail: expected '${expected}', got '${actual}'"
|
||||||
|
exit 1
|
||||||
|
fi
|
|
@ -1,9 +1,9 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
expected=\"Fred\"
|
expected='"Fred"'
|
||||||
actual=`echo ${expected} | target/psse 2> /dev/null`
|
actual=`echo ${expected} | target/psse 2> /dev/null`
|
||||||
|
|
||||||
if [ "${expected}" = "{$actual}" ]
|
if [ "${expected}" = "${actual}" ]
|
||||||
then
|
then
|
||||||
echo "OK"
|
echo "OK"
|
||||||
exit 0
|
exit 0
|
||||||
|
|
14
unit-tests/string-allocation.sh
Normal file
14
unit-tests/string-allocation.sh
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
value='"Fred"'
|
||||||
|
expected="String cell: character 'F'"
|
||||||
|
echo ${value} | target/psse 2>&1 | grep "${expected}" > /dev/null
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]
|
||||||
|
then
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
echo "Expected '${expected}', not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
|
@ -1,9 +1,9 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
expected="\"Strings should be able to include spaces (and other stuff)!\""
|
expected='"Strings should be able to include spaces (and other stuff)!"'
|
||||||
actual=`echo ${expected} | target/psse 2> /dev/null`
|
actual=`echo ${expected} | target/psse 2> /dev/null`
|
||||||
|
|
||||||
if [ "${expected}" = "{$actual}" ]
|
if [ "${expected}" = "${actual}" ]
|
||||||
then
|
then
|
||||||
echo "OK"
|
echo "OK"
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue