Reorganized wumpus, combining `wump_ui.pvvs` and `wump_game.pvvs`.
[vvhitespace] / vv_interpreter.c
index 1d5d906..1e42baf 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
 /*
  * (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
- * All rights reserved.
+ * See LICENSE.txt file for copyright and license details.
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
@@ -15,9 +15,9 @@
 
 #define VERSION 1
 
 
 #define VERSION 1
 
-#define HEAPSIZE        1024    /* Size of heap in words        */
-#define DATASTACKSIZE   1024    /* Size of stack in words       */
-#define RETURNSTACKSIZE 1024    /* Max subroutine call depth    */
+#define HEAPSIZE        65536   /* Size of heap in words        */
+#define DATASTACKSIZE   65536   /* Size of stack in words       */
+#define RETURNSTACKSIZE 65536   /* Max subroutine call depth    */
 
 void
 print_usage(char ** argv)
 
 void
 print_usage(char ** argv)
@@ -126,7 +126,10 @@ parse_label(uint8_t * code, size_t * pc)
 uint16_t
 check_label(size_t * labels, uint16_t label, size_t * pc)
 {
 uint16_t
 check_label(size_t * labels, uint16_t label, size_t * pc)
 {
-    if(!labels[label]) ws_die(pc, "uninitialized label (forgot an include?)");
+    if (!labels[label]) {
+        fprintf(stderr, "Trying to process label 0x%X.\n", label);
+        ws_die(pc, "uninitialized label (forgot an include?)");
+    }
     return label;
 }
 
     return label;
 }
 
@@ -159,14 +162,26 @@ process_imp_stack(uint8_t * code, size_t * pc, int64_t ** sp)
 
                 /* 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. */
-                int64_t number = 0;
+                uint64_t number = 0; /* Unsigned to accomodate magnitude of most negative number. */
                 uint8_t temp;
                 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++;
                 }
                 uint8_t temp;
                 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++;
                 }
-                stack_push(sp, number*sign);
+                /* Without temporarily casting to something >64-bit, the most negative */
+                /* number will overflow when performing 'number*sign'. Instead, we     */
+                /* pick off the most negative number as a special case.                */
+                if (number == (1ULL << 63) && sign == -1) {
+                    /* C parses negative integer literals first as signed positive */
+                    /* integer literals, then applying a unary negation operator.  */
+                    /* Thus, the most negative value is unreachable directly.      */
+                    int64_t number_temp = -9223372036854775807LL; /* First store -((2^63)-1)  */
+                    number_temp--;                                /* Now turn it into -(2^63) */
+                    stack_push(sp, number_temp);
+                } else {
+                    stack_push(sp, number*sign);
+                }
             }
             break;
         case '\n':
             }
             break;
         case '\n':
@@ -238,7 +253,7 @@ process_imp_arithmetic(uint8_t * code, size_t * pc, int64_t ** sp)
                     case '\t':
                         /* Modulo */
                         temp = stack_pop(sp);
                     case '\t':
                         /* Modulo */
                         temp = stack_pop(sp);
-                        stack_push(sp, stack_pop(sp)%temp);
+                        stack_push(sp, llabs(stack_pop(sp) % llabs(temp)));
                         break;
                     default: ws_die(pc, "malformed arithmetic IMP"); break;
                 }
                         break;
                     default: ws_die(pc, "malformed arithmetic IMP"); break;
                 }
@@ -440,8 +455,8 @@ main(int argc, char ** argv)
         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);
         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();
             unset_terminal_mode();
+            exit(EXIT_FAILURE);
         }
 
         /* Decode the IMPs */
         }
 
         /* Decode the IMPs */