Correcting incorrect comment in vvc related to parsing ASCII strings.
[vvhitespace] / vv_compiler.c
index 3153eee..e53f70d 100644 (file)
@@ -25,6 +25,48 @@ print_usage(char ** argv)
     );
 }
 
     );
 }
 
+/* Builds an ASCII string on the stack using VVS PUSH_IMMEDIATE commands.    */
+/* The syntax: A"test" results in six PUSH_IMMEDIATE commands for the four   */
+/*   letters, newline, and null-terminator.                                  */
+/* Expects 'input' to present a double-quoted ('"') ASCII string.            */
+/* The 'A' has already been chomped.                                         */
+void
+parse_ascii_string(FILE * input, FILE * output)
+{
+    uint8_t temp_byte;
+    fread(&temp_byte, 1, 1, input);
+    if (temp_byte != '"') {
+        /* Die here since we walk the string backward and look for '"' as a terminator. */
+        fprintf(stderr, "ERROR: Expected double-quote to begin string.\n");
+        exit(EXIT_FAILURE);
+    }
+
+    /* 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;
+    temp_byte = '\0';
+
+    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. */
+        while (index) {
+            ((temp_byte >> (index-1)) & 1) ? (temp_output = '\t') : (temp_output = ' ');
+            fwrite(&temp_output, 1, 1, output);
+            index--;
+        }
+        /* Third, close the number with a newline. */
+        temp_output = '\n';
+        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);
+    }
+}
+
 int
 main(int argc, char ** argv)
 {
 int
 main(int argc, char ** argv)
 {
@@ -90,11 +132,15 @@ main(int argc, char ** argv)
                 temp_byte = '\v';
                 fwrite(&temp_byte, 1, 1, output);
                 break;
                 temp_byte = '\v';
                 fwrite(&temp_byte, 1, 1, output);
                 break;
+            case 'a':
+            case 'A':
+                parse_ascii_string(input, output);
+                break;
             case '\n':
                 /* Intentionally empty */
                 break;
             default:
             case '\n':
                 /* Intentionally empty */
                 break;
             default:
-                /* The first non-[tTsSnNvV] character begins a comment lasting until end of line. */
+                /* 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;
                 }
                 while (fread(&temp_byte, 1, 1, input)) {
                     if (temp_byte == '\n') break;
                 }
@@ -108,7 +154,5 @@ main(int argc, char ** argv)
     fclose(input);
     fclose(output);
 
     fclose(input);
     fclose(output);
 
-    printf("Successfully converted source code.\n");
-
     exit(EXIT_SUCCESS);
 }
     exit(EXIT_SUCCESS);
 }