Reworked the test suite so I can create tests for the stdlib.
[vvhitespace] / vv_interpreter.c
index 8f98fec..6dde3e8 100644 (file)
 #include <stdint.h>
 #include <sys/select.h>
 #include <getopt.h>
 #include <stdint.h>
 #include <sys/select.h>
 #include <getopt.h>
+#include <termios.h>
 
 #define VERSION 1
 
 
 #define VERSION 1
 
-#define STACKSIZE 1024
-#define HEAPSIZE  1024
-#define RETURNSTACKSIZE 1024
+#define HEAPSIZE        1024    /* Size of heap in words        */
+#define DATASTACKSIZE   1024    /* Size of stack in words       */
+#define RETURNSTACKSIZE 1024    /* Max subroutine call depth    */
 
 void
 print_usage(char ** argv)
 {
 
 void
 print_usage(char ** argv)
 {
-    printf( "Whitespace Interpreter v%d (www.subgeniuskitty.com)\n"
+    printf( "VVhitespace Interpreter v%d (www.subgeniuskitty.com)\n"
             "Usage: %s -i <file>\n"
             "  -h         Help (prints this message)\n"
             "Usage: %s -i <file>\n"
             "  -h         Help (prints this message)\n"
-            "  -i <file>  Specify a Whitespace source file to interpret.\n"
+            "  -i <file>  Specify a VVhitespace source file to interpret.\n"
             , VERSION, argv[0]
     );
 }
             , VERSION, argv[0]
     );
 }
@@ -46,29 +47,55 @@ stdin_empty(void)
     return 1;
 }
 
     return 1;
 }
 
+void
+set_terminal_mode(void)
+{
+    struct termios options;
+    tcgetattr(STDIN_FILENO, &options);
+    /* Create a cbreak-like environment through the following options. */
+    options.c_lflag &= ~ECHO; /* Disable echoing of input characters. */
+    options.c_lflag &= ~ICANON; /* Disable cooked/line-oriented mode. */
+    options.c_cc[VMIN] = 1;
+    options.c_cc[VTIME] = 0;
+    tcsetattr(STDIN_FILENO, TCSANOW, &options);
+}
+
+void
+unset_terminal_mode(void)
+{
+    struct termios options;
+    tcgetattr(STDIN_FILENO, &options);
+    /* Undo the changes made in set_terminal_mode(). */
+    options.c_lflag |= ECHO; /* Enable echoing of input characters. */
+    options.c_lflag |= ICANON; /* Enable cooked/line-oriented mode. */
+    options.c_cc[VMIN] = 1; /* Default value from /usr/src/sys/sys/ttydefaults.h */
+    options.c_cc[VTIME] = 0; /* Default value from /usr/src/sys/sys/ttydefaults.h */
+    tcsetattr(STDIN_FILENO, TCSANOW, &options);
+}
+
 void
 ws_die(size_t * pc, char * msg)
 {
 void
 ws_die(size_t * pc, char * msg)
 {
-    printf("\n");
     printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg);
     fflush(stdout);
     printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg);
     fflush(stdout);
+    unset_terminal_mode();
     exit(EXIT_FAILURE);
 }
 
 void
     exit(EXIT_FAILURE);
 }
 
 void
-stack_push(int32_t ** sp, int32_t word)
+stack_push(int64_t ** sp, int64_t word)
 {
     *((*sp)++) = word;
 }
 
 {
     *((*sp)++) = word;
 }
 
-int32_t
-stack_pop(int32_t ** sp)
+int64_t
+stack_pop(int64_t ** sp)
 {
     return *(--(*sp));
 }
 
 {
     return *(--(*sp));
 }
 
-int32_t
-stack_peek(int32_t ** sp, size_t offset)
+int64_t
+stack_peek(int64_t ** sp, size_t offset)
 /* offset=0 peeks TOS, offset=1 peeks NOS, etc. */
 {
     return *((*sp)-offset-1);
 /* offset=0 peeks TOS, offset=1 peeks NOS, etc. */
 {
     return *((*sp)-offset-1);
@@ -80,6 +107,10 @@ next_code_byte(uint8_t * code, size_t * pc)
     return code[(*pc)++];
 }
 
     return code[(*pc)++];
 }
 
+/*
+ * In addition to returning the parsed label, this function advances the PC to
+ * the next instruction.
+ */
 uint16_t
 parse_label(uint8_t * code, size_t * pc)
 {
 uint16_t
 parse_label(uint8_t * code, size_t * pc)
 {
@@ -87,30 +118,54 @@ parse_label(uint8_t * code, size_t * pc)
     uint8_t c;
     while ((c = code[(*pc)++]) != '\n') {
         label = label << 1;
     uint8_t c;
     while ((c = code[(*pc)++]) != '\n') {
         label = label << 1;
-        if (c == ' ') label++;
+        if (c == '\t') label++;
+    }
+    return label;
+}
+
+uint16_t
+check_label(size_t * labels, uint16_t label, size_t * pc)
+{
+    if (!labels[label]) {
+        fprintf(stderr, "Trying to process label 0x%X.\n", label);
+        ws_die(pc, "uninitialized label (forgot an include?)");
     }
     return label;
 }
 
 void
     }
     return label;
 }
 
 void
-process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
+populate_labels(size_t * labels, uint8_t * code, size_t code_size)
+{
+    size_t cp = 0;
+    while (cp <= code_size) {
+        if (code[cp++] == '\v') {
+            uint16_t temp_label = parse_label(code, &cp);
+            labels[temp_label] = cp;
+        }
+    }
+}
+
+void
+process_imp_stack(uint8_t * code, size_t * pc, int64_t ** sp)
 {
     switch (next_code_byte(code,pc)) {
         case ' ':
             /* Push number onto TOS. */
             {
                 /* First, pick off the sign */
 {
     switch (next_code_byte(code,pc)) {
         case ' ':
             /* Push number onto TOS. */
             {
                 /* First, pick off the sign */
-                int32_t sign = 0;
+                int64_t sign = 0;
                 switch (next_code_byte(code,pc)) {
                     case ' ' :  sign = 1;                       break;
                     case '\t':  sign = -1;                      break;
                 switch (next_code_byte(code,pc)) {
                     case ' ' :  sign = 1;                       break;
                     case '\t':  sign = -1;                      break;
-                    case '\n':  ws_die(pc, "expected sign");    break;
+                    default  :  ws_die(pc, "expected sign");    break;
                 }
 
                 /* Now, construct the number and push to TOS. */
                 /* I'm assuming the numbers are read MSb first. */
                 }
 
                 /* Now, construct the number and push to TOS. */
                 /* I'm assuming the numbers are read MSb first. */
-                int32_t temp, number = 0;
+                int64_t number = 0;
+                uint8_t temp;
                 while ((temp = next_code_byte(code,pc)) != '\n') {
                 while ((temp = next_code_byte(code,pc)) != '\n') {
+                    if (temp == '\v') ws_die(pc, "non-binary digit in number");
                     number <<= 1;
                     if (temp == '\t') number++;
                 }
                     number <<= 1;
                     if (temp == '\t') number++;
                 }
@@ -128,8 +183,8 @@ process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
                         /* Swap TOS and NOS. */
                     case '\t':
                         {
                         /* Swap TOS and NOS. */
                     case '\t':
                         {
-                            int32_t t1 = stack_pop(sp);
-                            int32_t t2 = stack_pop(sp);
+                            int64_t t1 = stack_pop(sp);
+                            int64_t t2 = stack_pop(sp);
                             stack_push(sp, t1);
                             stack_push(sp, t2);
                         }
                             stack_push(sp, t1);
                             stack_push(sp, t2);
                         }
@@ -138,17 +193,20 @@ process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
                     case '\n':
                         stack_pop(sp);
                         break;
                     case '\n':
                         stack_pop(sp);
                         break;
+                    default:
+                        ws_die(pc, "malformed stack IMP");
+                        break;
                 }
             }
             break;
                 }
             }
             break;
-        case '\t': ws_die(pc, "malformed stack IMP"); break;
+        default: ws_die(pc, "malformed stack IMP"); break;
     }
 }
 
 void
     }
 }
 
 void
-process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp)
+process_imp_arithmetic(uint8_t * code, size_t * pc, int64_t ** sp)
 {
 {
-    int32_t temp;
+    int64_t temp;
     switch (next_code_byte(code,pc)) {
         case ' ':
             {
     switch (next_code_byte(code,pc)) {
         case ' ':
             {
@@ -166,6 +224,9 @@ process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp)
                         /* Multiplication */
                         stack_push(sp, stack_pop(sp)*stack_pop(sp));
                         break;
                         /* Multiplication */
                         stack_push(sp, stack_pop(sp)*stack_pop(sp));
                         break;
+                    default:
+                        ws_die(pc, "malformed arithmetic IMP");
+                        break;
                 }
             }
             break;
                 }
             }
             break;
@@ -182,39 +243,47 @@ process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp)
                         temp = stack_pop(sp);
                         stack_push(sp, stack_pop(sp)%temp);
                         break;
                         temp = stack_pop(sp);
                         stack_push(sp, stack_pop(sp)%temp);
                         break;
-                    case '\n': ws_die(pc, "malformed arithmetic IMP"); break;
+                    default: ws_die(pc, "malformed arithmetic IMP"); break;
                 }
             }
             break;
                 }
             }
             break;
-        case '\n': ws_die(pc, "malformed arithmetic IMP"); break;
+        default: ws_die(pc, "malformed arithmetic IMP"); break;
     }
 }
 
 void
     }
 }
 
 void
-process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * labels,
-                        uint32_t ** rsp)
+process_imp_flowcontrol(uint8_t * code, size_t * pc, int64_t ** sp, size_t * labels,
+                        size_t ** rsp)
 {
 {
+    size_t temp_pc;
     switch (next_code_byte(code,pc)) {
         case '\n':
             /* Technically another LF is required but we ignore it. */
     switch (next_code_byte(code,pc)) {
         case '\n':
             /* Technically another LF is required but we ignore it. */
-            printf("\n");
             fflush(stdout);
             fflush(stdout);
+            unset_terminal_mode();
             exit(EXIT_SUCCESS);
         case ' ':
             {
                 switch (next_code_byte(code,pc)) {
                     case ' ':
                         /* Mark a location in the program. */
             exit(EXIT_SUCCESS);
         case ' ':
             {
                 switch (next_code_byte(code,pc)) {
                     case ' ':
                         /* Mark a location in the program. */
-                        labels[parse_label(code, pc)] = *pc;
+                        if (next_code_byte(code,pc) != '\v') ws_die(pc,"expected vtab, "
+                                "perhaps a whitespace program, rather than vvhitespace?");
+                        /* Jump to next instruction since labels were parsed during startup. */
+                        parse_label(code, pc);
                         break;
                     case '\t':
                         /* Call a subroutine. */
                         break;
                     case '\t':
                         /* Call a subroutine. */
+                        temp_pc = labels[check_label(labels, parse_label(code, pc), pc)];
                         *((*rsp)++) = *pc;
                         *((*rsp)++) = *pc;
-                        *pc = labels[parse_label(code, pc)];
+                        *pc = temp_pc;
                         break;
                     case '\n':
                         /* Jump unconditionally to a label. */
                         break;
                     case '\n':
                         /* Jump unconditionally to a label. */
-                        *pc = labels[parse_label(code, pc)];
+                        *pc = labels[check_label(labels, parse_label(code, pc), pc)];
+                        break;
+                    default:
+                        ws_die(pc, "malformed flow control IMP");
                         break;
                 }
             }
                         break;
                 }
             }
@@ -224,43 +293,63 @@ process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l
                 switch (next_code_byte(code,pc)) {
                     case ' ':
                         /* Jump to a label if TOS == 0 */
                 switch (next_code_byte(code,pc)) {
                     case ' ':
                         /* Jump to a label if TOS == 0 */
-                        if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)];
+                        temp_pc = labels[check_label(labels, parse_label(code, pc), pc)];
+                        if (stack_pop(sp) == 0) *pc = temp_pc;
                         break;
                     case '\t':
                         /* Jump to a label if TOS < 0. */
                         break;
                     case '\t':
                         /* Jump to a label if TOS < 0. */
-                        if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)];
+                        temp_pc = labels[check_label(labels, parse_label(code, pc), pc)];
+                        if (stack_pop(sp) < 0) *pc = temp_pc;
                         break;
                     case '\n':
                         /* Return from subroutine. */
                         *pc = *(--(*rsp));
                         break;
                         break;
                     case '\n':
                         /* Return from subroutine. */
                         *pc = *(--(*rsp));
                         break;
+                    default:
+                        ws_die(pc, "malformed flow control IMP");
+                        break;
                 }
             }
             break;
                 }
             }
             break;
+        default:
+            ws_die(pc, "malformed flow control IMP");
+            break;
     }
 }
 
 void
     }
 }
 
 void
-process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
+process_imp_heap(uint8_t * code, size_t * pc, int64_t ** sp, int64_t ** hp)
 {
     switch (next_code_byte(code,pc)) {
 {
     switch (next_code_byte(code,pc)) {
-        case ' ' : /* Store to heap      */ *(*hp + *((*sp)-1)) = **sp; *sp -= 2; break;
-        case '\t': /* Retrieve from heap */ **sp = *(*hp + **sp);                 break;
-        case '\n': ws_die(pc, "malformed heap IMP");                              break;
+        case ' ' :
+            /* Store to heap */
+            {
+                int64_t value = stack_pop(sp);
+                int64_t addr  = stack_pop(sp);
+                *(*hp + addr) = value;
+            }
+            break;
+        case '\t':
+            /* Retrieve from heap */
+            stack_push(sp, *(*hp + stack_pop(sp)));
+            break;
+        default:
+            ws_die(pc, "malformed heap IMP");
+            break;
     }
 }
 
 void
     }
 }
 
 void
-process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
+process_imp_io(uint8_t * code, size_t * pc, int64_t ** sp, int64_t ** hp)
 {
     switch (next_code_byte(code,pc)) {
         case ' ':
             /* Output */
             {
                 switch (next_code_byte(code,pc)) {
 {
     switch (next_code_byte(code,pc)) {
         case ' ':
             /* Output */
             {
                 switch (next_code_byte(code,pc)) {
-                    case ' ' : /* Output character from TOS */ printf("%c", stack_pop(sp)); break;
-                    case '\t': /* Output number from TOS    */ printf("%d", stack_pop(sp)); break;
-                    case '\n': ws_die(pc, "malformed output IMP");                          break;
+                    case ' ' : /* Output char from TOS  */ printf("%c", (uint8_t) stack_pop(sp));     break;
+                    case '\t': /* Output digit from TOS */ printf("%c", (uint8_t) stack_pop(sp)+'0'); break;
+                    default  : ws_die(pc, "malformed output IMP");                          break;
                 }
                 fflush(stdout);
             }
                 }
                 fflush(stdout);
             }
@@ -272,17 +361,15 @@ process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
                 char c = getchar();
                 switch (next_code_byte(code,pc)) {
                     case '\t': /* Input digit     */ c -= '0';                /* fallthrough */
                 char c = getchar();
                 switch (next_code_byte(code,pc)) {
                     case '\t': /* Input digit     */ c -= '0';                /* fallthrough */
-                    case ' ' : /* Input character */ *(*hp + *((*sp)--)) = c; break;
-                    case '\n': ws_die(pc, "malformed input IMP");             break;
+                    case ' ' : /* Input character */ *(*hp + *(--(*sp))) = c; break;
+                    default  : ws_die(pc, "malformed input IMP");             break;
                 }
             }
             break;
                 }
             }
             break;
-        case '\n': ws_die(pc, "malformed i/o IMP"); break;
+        default: ws_die(pc, "malformed i/o IMP"); break;
     }
 }
 
     }
 }
 
-/* TODO: Continue cleanup here */
-
 int
 main(int argc, char ** argv)
 {
 int
 main(int argc, char ** argv)
 {
@@ -307,50 +394,57 @@ main(int argc, char ** argv)
         }
     }
     if (input == NULL) {
         }
     }
     if (input == NULL) {
-        fprintf(stderr, "ERROR: Must specify a Whitespace source file with -f flag.\n");
+        fprintf(stderr, "ERROR: Must specify a VVhitespace source file with -f flag.\n");
         print_usage(argv);
         exit(EXIT_FAILURE);
     }
 
     /*
         print_usage(argv);
         exit(EXIT_FAILURE);
     }
 
     /*
-     * Read just the Whitespace source code into memory.
+     * Read just the VVhitespace source code into memory, stripping comment characters.
      * We will use the array indices as addresses for the virtual PC when jumping to labels.
      */
     size_t ws_code_size = 0;
     uint8_t temp_byte;
     while (fread(&temp_byte, 1, 1, input)) {
      * We will use the array indices as addresses for the virtual PC when jumping to labels.
      */
     size_t ws_code_size = 0;
     uint8_t temp_byte;
     while (fread(&temp_byte, 1, 1, input)) {
-        if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n') ws_code_size++;
+        if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') {
+            ws_code_size++;
+        }
     }
     rewind(input);
     uint8_t * ws_code_space = malloc(ws_code_size);
     ws_code_size = 0;
     while (fread(&temp_byte, 1, 1, input)) {
     }
     rewind(input);
     uint8_t * ws_code_space = malloc(ws_code_size);
     ws_code_size = 0;
     while (fread(&temp_byte, 1, 1, input)) {
-        if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n') ws_code_space[ws_code_size++] = temp_byte;
+        if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') {
+            ws_code_space[ws_code_size++] = temp_byte;
+        }
     }
     fclose(input);
 
     /*
      * Setup a stack and heap.
     }
     fclose(input);
 
     /*
      * Setup a stack and heap.
-     * Assume a 32-bit word size.
      */
      */
-    int32_t * hp = malloc(HEAPSIZE*4);
-    int32_t * sp = malloc(STACKSIZE*4);
+    int64_t * hp = malloc(HEAPSIZE*sizeof(int64_t));
+    int64_t * sp = malloc(DATASTACKSIZE*sizeof(int64_t));
 
     /* 
      * Setup the return stack and the label array.
      */
 
     /* 
      * Setup the return stack and the label array.
      */
-    uint32_t * rsp = malloc(RETURNSTACKSIZE*4);
-    uint32_t labels[65536];
+    size_t * rsp = malloc(RETURNSTACKSIZE*sizeof(size_t));
+    size_t labels[65536] = {0}; /* 65536 = 2^16 => Holds all possible labels.               */
+                                /* Default value of zero indicates an uninitialized label.  */
+    populate_labels(labels, ws_code_space, ws_code_size);
 
     /*
      * Main Loop
      */
 
     /*
      * Main Loop
      */
-
+    set_terminal_mode();
     size_t pc = 0; /* Virtual program counter. Operates in the ws_code_space[] address space. */
     while (1) {
         if (pc >= ws_code_size) {
     size_t pc = 0; /* Virtual program counter. Operates in the ws_code_space[] address space. */
     while (1) {
         if (pc >= ws_code_size) {
-            fprintf(stderr, "SIM_ERROR: PC Overrun\n    Requested PC: %lu\n    Max Address: %lu\n", pc, ws_code_size-1);
+            fprintf(stderr, "SIM_ERROR: PC Overrun\n    Requested PC: %lu\n    Max Address: %lu\n",
+                pc, ws_code_size-1);
             exit(EXIT_FAILURE);
             exit(EXIT_FAILURE);
+            unset_terminal_mode();
         }
 
         /* Decode the IMPs */
         }
 
         /* Decode the IMPs */
@@ -382,11 +476,7 @@ main(int argc, char ** argv)
                     }
                 }
                 break;
                     }
                 }
                 break;
+            default: ws_die(&pc, "unexpected byte"); break;
         }
     }
         }
     }
-
-    printf("\n");
-    printf("Program executed.\n");
-
-    exit(EXIT_SUCCESS);
 }
 }