* (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
print_usage(char ** argv
)
printf( "VVhitespace Interpreter v%d (www.subgeniuskitty.com)\n"
"Usage: %s -i <file> -o <file>\n"
" -h Help (prints this message)\n"
" -i <file> Specify a pseudo-VVhitespace source file as input.\n"
" -o <file> Specify location for VVhitespace output.\n"
/* Builds an ASCII string on the stack using VVS PUSH_IMMEDIATE commands. */
/* The syntax: A"test" results in five PUSH_IMMEDIATE commands for the four */
/* letters, and null-terminator. */
/* Expects 'input' to present a double-quoted ('"') ASCII string. */
/* The 'A' has already been chomped. */
parse_ascii_string(FILE * input
, FILE * output
)
fread(&temp_byte
, 1, 1, input
);
/* Die here since we walk the string backward and look for '"' as a terminator. */
fprintf(stderr
, "ERROR: Expected double-quote to begin string.\n");
/* Put the letters on the stack in reverse. Don't forget to put a null-terminator first. */
for (fread(&temp_byte
,1,1,input
); temp_byte
!= '"'; fread(&temp_byte
,1,1,input
)) continue;
while (temp_byte
!= '"') {
/* First, push three spaces to start a PUSH_IMMEDIATE command for a positive number. */
uint8_t temp_output
= ' ';
for(int i
=0;i
<3;i
++) fwrite(&temp_output
, 1, 1, output
);
/* Second, push the ASCII character, 7 bits total, most significant bit first. */
/* Remember, [Tab]=1 and [Space]=0. */
uint8_t index
= 7; /* 7 bits needed per ASCII character. */
((temp_byte
>> (index
-1)) & 1) ? (temp_output
= '\t') : (temp_output
= ' ');
fwrite(&temp_output
, 1, 1, output
);
/* Third, close the number with a newline. */
fwrite(&temp_output
, 1, 1, output
);
/* Read the next byte to prepare for the loop test. */
fseek(input
, -2, SEEK_CUR
);
fread(&temp_byte
, 1, 1, input
);
/* Advance the stream pointer to the end of the string before returning. */
for (fread(&temp_byte
,1,1,input
); temp_byte
!= '"'; fread(&temp_byte
,1,1,input
)) continue;
main(int argc
, char ** argv
)
* Process command line arguments
FILE * input
= NULL
, * output
= NULL
;
while ((c
= getopt(argc
,argv
,"i:o:h")) != -1) {
if ((input
= fopen(optarg
, "r")) == NULL
) {
fprintf(stderr
, "ERROR: %s: %s\n", optarg
, strerror(errno
));
if ((output
= fopen(optarg
, "w+")) == NULL
) {
fprintf(stderr
, "ERROR: %s: %s\n", optarg
, strerror(errno
));
fprintf(stderr
, "ERROR: Must specify a pseudo-VVhitespace source file with -i flag.\n");
fprintf(stderr
, "ERROR: Must specify destination for VVhitespace source file with -o flag.\n");
while (fread(&temp_byte
, 1, 1, input
)) {
fwrite(&temp_byte
, 1, 1, output
);
fwrite(&temp_byte
, 1, 1, output
);
fwrite(&temp_byte
, 1, 1, output
);
fwrite(&temp_byte
, 1, 1, output
);
parse_ascii_string(input
, output
);
/* Intentionally empty */
/* The first non-[tTsSnNvVaA] character begins a comment lasting until end of line. */
while (fread(&temp_byte
, 1, 1, input
)) {
if (temp_byte
== '\n') break;