Added final tests. All VVhitespace commands now have a basic test.
[vvhitespace] / vv_interpreter.c
index 1c6e207..ca2f772 100644 (file)
@@ -11,6 +11,7 @@
 #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
 
@@ -46,12 +47,38 @@ 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);
 }
 
     exit(EXIT_FAILURE);
 }
 
@@ -80,23 +107,35 @@ 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)
 {
-    // TODO: Check for invalid label and die.
     uint16_t label = 0;
     uint8_t c;
     while ((c = code[(*pc)++]) != '\n') {
         label = label << 1;
         if (c == ' ') label++;
     }
     uint16_t label = 0;
     uint8_t c;
     while ((c = code[(*pc)++]) != '\n') {
         label = label << 1;
         if (c == ' ') label++;
     }
+    // TODO: Where should I handle attempts to access an unitialized label?
+    //       For now, leave it undefined in a nasal demon sense.
     return label;
 }
 
 void
 populate_labels(uint32_t * labels, uint8_t * code, size_t code_size)
 {
     return label;
 }
 
 void
 populate_labels(uint32_t * labels, uint8_t * code, size_t code_size)
 {
-    // TODO
+    size_t cp = 0;
+    while (cp <= code_size) {
+        if (code[cp] == '\v') {
+            uint16_t temp_label = parse_label(code, &cp);
+            labels[temp_label] = cp;
+        }
+        cp++;
+    }
 }
 
 void
 }
 
 void
@@ -211,21 +250,26 @@ process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l
     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. */
-                        // TODO: Verify this is a label, but do nothing else.
-                        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. */
-                        *((*rsp)++) = *pc;
-                        *pc = labels[parse_label(code, pc)];
+                        {
+                            size_t temp_pc = labels[parse_label(code, pc)];
+                            *((*rsp)++) = *pc;
+                            *pc = temp_pc;
+                        }
                         break;
                     case '\n':
                         /* Jump unconditionally to a label. */
                         break;
                     case '\n':
                         /* Jump unconditionally to a label. */
@@ -242,10 +286,12 @@ 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 */
+                        /* TODO: Does WS pop or peek the TOS? */
                         if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)];
                         break;
                     case '\t':
                         /* Jump to a label if TOS < 0. */
                         if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)];
                         break;
                     case '\t':
                         /* Jump to a label if TOS < 0. */
+                        /* TODO: Does WS pop or peek the TOS? */
                         if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)];
                         break;
                     case '\n':
                         if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)];
                         break;
                     case '\n':
@@ -268,9 +314,21 @@ void
 process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
 {
     switch (next_code_byte(code,pc)) {
 process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
 {
     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;
-        default: ws_die(pc, "malformed heap IMP");                              break;
+        case ' ' :
+            /* Store to heap */
+            {
+                int32_t value = stack_pop(sp);
+                int32_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;
     }
 }
 
     }
 }
 
@@ -282,9 +340,9 @@ process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
             /* Output */
             {
                 switch (next_code_byte(code,pc)) {
             /* 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;
-                    default: ws_die(pc, "malformed output IMP");                          break;
+                    case ' ' : /* Output char from TOS  */ printf("%c", stack_pop(sp));     break;
+                    case '\t': /* Output digit from TOS */ printf("%c", stack_pop(sp)+'0'); break;
+                    default  : ws_die(pc, "malformed output IMP");                          break;
                 }
                 fflush(stdout);
             }
                 }
                 fflush(stdout);
             }
@@ -296,8 +354,8 @@ 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;
-                    default: ws_die(pc, "malformed input IMP");             break;
+                    case ' ' : /* Input character */ *(*hp + *(--(*sp))) = c; break;
+                    default  : ws_die(pc, "malformed input IMP");             break;
                 }
             }
             break;
                 }
             }
             break;
@@ -373,14 +431,17 @@ main(int argc, char ** argv)
     /*
      * 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) {
             fprintf(stderr, "SIM_ERROR: PC Overrun\n    Requested PC: %lu\n    Max Address: %lu\n",
                 pc, ws_code_size-1);
             exit(EXIT_FAILURE);
     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);
             exit(EXIT_FAILURE);
+            unset_terminal_mode();
         }
         }
+// TODO: Have the SIGTERM signal handler and normal term point return the value
+// on TOS so I can do rudimentary automated tests.
 
         /* Decode the IMPs */
         switch (ws_code_space[pc++]) {
 
         /* Decode the IMPs */
         switch (ws_code_space[pc++]) {
@@ -411,7 +472,7 @@ main(int argc, char ** argv)
                     }
                 }
                 break;
                     }
                 }
                 break;
-            default: ws_die(pc, "unexpected VTab"); break;
+            default: ws_die(&pc, "unexpected VTab"); break;
         }
     }
 
         }
     }
 
@@ -419,4 +480,5 @@ main(int argc, char ** argv)
     printf("Program executed.\n");
 
     exit(EXIT_SUCCESS);
     printf("Program executed.\n");
 
     exit(EXIT_SUCCESS);
+    unset_terminal_mode();
 }
 }