From 5b41fd07c44c20e93ea179aad9d50dfce964d59c Mon Sep 17 00:00:00 2001
From: David Schmenk <dschmenk@gmail.com>
Date: Sun, 4 Mar 2018 13:15:02 -0800
Subject: [PATCH] More work on for/next

---
 src/samplesrc/rogue.combat.pla | 57 ++++++++++----------
 src/toolsrc/codegen.c          | 25 ++++-----
 src/toolsrc/codegen.pla        |  7 +--
 src/toolsrc/lex.pla            | 11 ++--
 src/toolsrc/parse.pla          | 27 +++++-----
 src/vmsrc/apple/plvm02.s       | 40 +++++++-------
 src/vmsrc/plvm.c               | 95 +++++++++++++++++-----------------
 7 files changed, 132 insertions(+), 130 deletions(-)

diff --git a/src/samplesrc/rogue.combat.pla b/src/samplesrc/rogue.combat.pla
index 8e88b1a..57154ee 100644
--- a/src/samplesrc/rogue.combat.pla
+++ b/src/samplesrc/rogue.combat.pla
@@ -162,38 +162,37 @@ export def fight(player, enemy)
     if toupper(conio:getkey()) == 'R'
       conio:echo(ECHO_OFF)
       return 1
+    fin
+    //
+    // Turn player in random direction
+    //
+    player->angle = conio:rnd() & 7
+    //
+    // Calculate attack (with a little random variation)
+    //
+    p_atck = player->skill + player->energy / 10 - enemy->power / 25  + (conio:rnd() & 7)
+    e_atck = enemy->power  - player->skill / 5   - player->energy / 20 + (conio:rnd() & 7)
+    if enemy->life > p_atck
+      enemy->life = enemy->life - p_atck
     else
+      win
+      enemy->life = 0
+      p_atck      = player->skill + enemy->power / 3
+      if p_atck > 100 // Limit skill
+        p_atck = 100
+      fin
+      player->skill = p_atck
       //
-      // Turn player in random direction
+      // Unlink dead enemy from entities list
       //
-      player->angle = conio:rnd() & 7
-      //
-      // Calculate attack (with a little random variation)
-      //
-      p_atck = player->skill + player->energy / 10 - enemy->power / 25  + (conio:rnd() & 7)
-      e_atck = enemy->power  - player->skill / 5   - player->energy / 20 + (conio:rnd() & 7)
-      if enemy->life > p_atck
-        enemy->life = enemy->life - p_atck
-      else
-        win
-        enemy->life = 0
-        p_atck      = player->skill + enemy->power / 3
-        if p_atck > 100 // Limit skill
-          p_atck = 100
-        fin
-        player->skill = p_atck
-        //
-        // Unlink dead enemy from entities list
-        //
-        if enemy == entities
-          entities = enemy=>next_other
-        fin
-    if enemy=>next_other
-          enemy=>next_other=>prev_other = enemy=>prev_other
-    fin
-    if enemy=>prev_other
-          enemy=>prev_other=>next_other = enemy=>next_other
-    fin
+      if enemy == entities
+        entities = enemy=>next_other
+      fin
+      if enemy=>next_other
+        enemy=>next_other=>prev_other = enemy=>prev_other
+      fin
+      if enemy=>prev_other
+        enemy=>prev_other=>next_other = enemy=>next_other
       fin
       if player->health > e_atck
         player->health = player->health - e_atck
diff --git a/src/toolsrc/codegen.c b/src/toolsrc/codegen.c
index 8076a86..e9208f9 100755
--- a/src/toolsrc/codegen.c
+++ b/src/toolsrc/codegen.c
@@ -798,37 +798,37 @@ void emit_brne(int tag)
 void emit_brgt(int tag)
 {
     emit_pending_seq();
-    printf("\t%s\t$88\t\t\t; BRGT\t_B%03d\n", DB, tag);
+    printf("\t%s\t$80\t\t\t; BRGT\t_B%03d\n", DB, tag);
     printf("\t%s\t_B%03d-*\n", DW, tag);
 }
 void emit_brlt(int tag)
 {
     emit_pending_seq();
-    printf("\t%s\t$8A\t\t\t; BRLT\t_B%03d\n", DB, tag);
-    printf("\t%s\t_B%03d-*\n", DW, tag);
-}
-void emit_addbrle(int tag)
-{
-    emit_pending_seq();
-    printf("\t%s\t$80\t\t\t; BRLE\t_B%03d\n", DB, tag);
+    printf("\t%s\t$82\t\t\t; BRLT\t_B%03d\n", DB, tag);
     printf("\t%s\t_B%03d-*\n", DW, tag);
 }
 void emit_incbrle(int tag)
 {
     emit_pending_seq();
-    printf("\t%s\t$82\t\t\t; INCBRLE\t_B%03d\n", DB, tag);
+    printf("\t%s\t$84\t\t\t; INCBRLE\t_B%03d\n", DB, tag);
     printf("\t%s\t_B%03d-*\n", DW, tag);
 }
-void emit_subbrge(int tag)
+void emit_addbrle(int tag)
 {
     emit_pending_seq();
-    printf("\t%s\t$84\t\t\t; BRGE\t_B%03d\n", DB, tag);
+    printf("\t%s\t$86\t\t\t; BRLE\t_B%03d\n", DB, tag);
     printf("\t%s\t_B%03d-*\n", DW, tag);
 }
 void emit_decbrge(int tag)
 {
     emit_pending_seq();
-    printf("\t%s\t$86\t\t\t; DECBRGE\t_B%03d\n", DB, tag);
+    printf("\t%s\t$88\t\t\t; DECBRGE\t_B%03d\n", DB, tag);
+    printf("\t%s\t_B%03d-*\n", DW, tag);
+}
+void emit_subbrge(int tag)
+{
+    emit_pending_seq();
+    printf("\t%s\t$8A\t\t\t; BRGE\t_B%03d\n", DB, tag);
     printf("\t%s\t_B%03d-*\n", DW, tag);
 }
 void emit_call(int tag, int type)
@@ -1277,6 +1277,7 @@ int crunch_seq(t_opseq **seq, int pass)
                             op->code = SUBI_CODE;
                             freeops  = 1;
                         }
+                        break;
                     case BINARY_CODE(AND_TOKEN):
                         if (op->val >= 0 && op->val <= 255)
                         {
diff --git a/src/toolsrc/codegen.pla b/src/toolsrc/codegen.pla
index 4ec8a89..4c72ea2 100644
--- a/src/toolsrc/codegen.pla
+++ b/src/toolsrc/codegen.pla
@@ -405,9 +405,10 @@ def idmatch(nameptr, len, idptr, idcnt)
 
     while idcnt
         if len == idptr->idname
-            for i = 1 to len
-                if nameptr->[i - 1] <> idptr->idname.[i]; break; fin
-            next
+            i = 1; while i <= len and nameptr->[i - 1] == idptr->idname.[i]; i++; loop
+            //for i = 1 to len
+            //    if nameptr->[i - 1] <> idptr->idname.[i]; break; fin
+            //next
             if i > len; return idptr; fin
         fin
         idptr = idptr + idptr->idname + t_id
diff --git a/src/toolsrc/lex.pla b/src/toolsrc/lex.pla
index 2a6474c..b20e863 100644
--- a/src/toolsrc/lex.pla
+++ b/src/toolsrc/lex.pla
@@ -36,11 +36,12 @@ def keymatch
     loop
     chrptr = tknptr - 1
     while keywrds[keypos] == tknlen
-        for i = 1 to tknlen
-            if ^(chrptr + i) <> keywrds[keypos + i]
-                break
-            fin
-        next
+        i = 1; while i <= tknlen and ^(chrptr + i) == keywrds[keypos + i]; i++; loop
+        //for i = 1 to tknlen
+        //    if ^(chrptr + i) <> keywrds[keypos + i]
+        //        break
+        //    fin
+        //next
         if i > tknlen
             return keywrds[keypos + keywrds[keypos] + 1]
         fin
diff --git a/src/toolsrc/parse.pla b/src/toolsrc/parse.pla
index 0e58b6c..95e3812 100644
--- a/src/toolsrc/parse.pla
+++ b/src/toolsrc/parse.pla
@@ -640,8 +640,8 @@ def parse_stmnt
             if token <> FIN_TKN; exit_err(ERR_MISS|ERR_CLOSE|ERR_STATE); fin
             break
         is WHILE_TKN
-            prev_for  = infor
-            infor    = FALSE
+            prev_for    = infor
+            infor       = FALSE
             tag_while   = new_tag(RELATIVE_FIXUP)
             tag_wend    = new_tag(RELATIVE_FIXUP)
             tag_prevcnt = cont_tag
@@ -665,11 +665,11 @@ def parse_stmnt
             emit_tag(tag_wend)
             break_tag = tag_prevbrk
             cont_tag  = tag_prevcnt
-            infor  = prev_for
+            infor     = prev_for
             break
         is REPEAT_TKN
-            prev_for  = infor
-            infor    = FALSE
+            prev_for    = infor
+            infor       = FALSE
             tag_repeat  = new_tag(RELATIVE_FIXUP)
             tag_prevbrk = break_tag
             break_tag   = new_tag(RELATIVE_FIXUP)
@@ -693,11 +693,11 @@ def parse_stmnt
             emit_seq(seq)
             emit_tag(break_tag)
             break_tag = tag_prevbrk
-            infor  = prev_for
+            infor     = prev_for
             break
         is FOR_TKN
-            prev_for  = infor
-            infor    = TRUE
+            prev_for    = infor
+            infor       = TRUE
             stack_loop  = stack_loop + 2
             tag_for     = new_tag(RELATIVE_FIXUP)
             tag_prevcnt = cont_tag
@@ -773,15 +773,14 @@ def parse_stmnt
                     emit_decbrge(tag_for)
                 fin
             fin
-
             emit_tag(break_tag)
-            break_tag   = tag_prevbrk
-            stack_loop  = stack_loop - 2
-            infor    = prev_for
+            break_tag  = tag_prevbrk
+            stack_loop = stack_loop - 2
+            infor      = prev_for
             break
         is CASE_TKN
-            prev_for  = infor
-            infor    = FALSE
+            prev_for    = infor
+            infor       = FALSE
             stack_loop++
             tag_prevbrk = break_tag
             break_tag   = new_tag(RELATIVE_FIXUP)
diff --git a/src/vmsrc/apple/plvm02.s b/src/vmsrc/apple/plvm02.s
index 4798177..c5ade84 100755
--- a/src/vmsrc/apple/plvm02.s
+++ b/src/vmsrc/apple/plvm02.s
@@ -203,7 +203,8 @@ OPTBL   !WORD   ZERO,ADD,SUB,MUL,DIV,MOD,INCR,DECR              ; 00 02 04 06 08
         !WORD   BRNCH,BRNE,CALL,ICAL,ENTER,LEAVE,RET,CFFB       ; 50 52 54 56 58 5A 5C 5E
         !WORD   LB,LW,LLB,LLW,LAB,LAW,DLB,DLW                   ; 60 62 64 66 68 6A 6C 6E
         !WORD   SB,SW,SLB,SLW,SAB,SAW,DAB,DAW                   ; 70 72 74 76 78 7A 7C 7E
-        !WORD   ADDBRLE,INCBRLE,SUBBRGE,DECBRGE,BRGT,BRLT       ; 80 82 84 86 88 8A 8C 8E
+        !WORD   BRGT,BRLT,INCBRLE,ADDBRLE,DECBRGE,SUBBRGE       ; 80 82 84 86 88 8A 8C 8E
+;*
 ;*
 ;* ENTER INTO BYTECODE INTERPRETER
 ;*
@@ -413,7 +414,7 @@ OPXTBL  !WORD   ZERO,ADD,SUB,MUL,DIV,MOD,INCR,DECR              ; 00 02 04 06 08
         !WORD   BRNCH,BRNE,CALLX,ICALX,ENTER,LEAVEX,RETX,CFFB   ; 50 52 54 56 58 5A 5C 5E
         !WORD   LBX,LWX,LLBX,LLWX,LABX,LAWX,DLB,DLW             ; 60 62 64 66 68 6A 6C 6E
         !WORD   SB,SW,SLB,SLW,SAB,SAW,DAB,DAW                   ; 70 72 74 76 78 7A 7C 7E
-        !WORD   ADDBRLE,INCBRLE,SUBBRGE,DECBRGE,BRGT,BRLT       ; 80 82 84 86 88 8A 8C 8E
+        !WORD   BRGT,BRLT,INCBRLE,ADDBRLE,DECBRGE,SUBBRGE       ; 80 82 84 86 88 8A 8C 8E
 ;*
 ;* ADD TOS TO TOS-1
 ;*
@@ -710,6 +711,17 @@ LOR     LDA     ESTKL,X
         STA     ESTKH+1,X
 +       JMP     DROP
 ;*
+;* LOGICAL NOT
+;*
+LNOT    LDA     ESTKL,X
+        ORA     ESTKH,X
+        BEQ     +
+        LDA     #$FF
++       EOR     #$FF
+        STA     ESTKL,X
+        STA     ESTKH,X
+        JMP     NEXTOP
+;*
 ;* DUPLICATE TOS
 ;*
 DUP     DEX
@@ -719,17 +731,6 @@ DUP     DEX
         STA     ESTKH,X
         JMP     NEXTOP
 ;*
-;* LOGICAL NOT
-;*
-LNOT    LDA     ESTKL,X
-        ORA     ESTKH,X
-        BEQ     +
-        LDA     #$00
-+       EOR     #$FF
-        STA     ESTKL,X
-        STA     ESTKH,X
-        JMP     NEXTOP
-;*
 ;* CONSTANT
 ;*
 ZERO
@@ -1385,13 +1386,12 @@ BRLT    LDA     ESTKL,X
         BNE     BRNCH           ; BMI     BRNCH
 +       BMI     NOBRNCH
         BPL     -
-DECBRGE LDA     ESTKL,X
-        SEC
-        SBC     #$01
-        STA     ESTKL,X
-        BCS     +
+DECBRGE DEC     ESTKL,X
+        LDA     ESTKL,X
+        CMP     #$FF
+        BNE     +
         DEC     ESTKH,X
-_BRGE   LDA     ESTKL,X         ; BRGE
+_BRGE   LDA     ESTKL,X
 +       CMP     ESTKL+1,X
         LDA     ESTKH,X
         SBC     ESTKH+1,X
@@ -1403,7 +1403,7 @@ _BRGE   LDA     ESTKL,X         ; BRGE
 INCBRLE INC     ESTKL,X
         BNE     _BRLE
         INC     ESTKH,X
-_BRLE   LDA     ESTKL+1,X       ; BRLE
+_BRLE   LDA     ESTKL+1,X
         CMP     ESTKL,X
         LDA     ESTKH+1,X
         SBC     ESTKH,X
diff --git a/src/vmsrc/plvm.c b/src/vmsrc/plvm.c
index 1aef4ab..a28178d 100755
--- a/src/vmsrc/plvm.c
+++ b/src/vmsrc/plvm.c
@@ -526,6 +526,7 @@ OPTBL:  DW  ZERO,ADD,SUB,MUL,DIV,MOD,INCR,DECR          ; 00 02 04 06 08 0A 0C 0
         DW  LB,LW,LLB,LLW,LAB,LAW,DLB,DLW               ; 60 62 64 66 68 6A 6C 6E
         DW  SB,SW,SLB,SLW,SAB,SAW,DAB,DAW               ; 70 72 74 76 78 7A 7C 7E
         DW  ADDBRLE,INCBRLE,SUBBRGE,DECBRGE,BRGT,BRLT   ; 80 82 84 86 88 8A 8C 8E
+        DW  BRGT,BRLT,INCBRLE,ADDBRLE,DECBRGE,SUBBRGE   ; 80 82 84 86 88 8A 8C 8E
 */
 void interp(code *ip)
 {
@@ -884,7 +885,47 @@ void interp(code *ip)
                 /*
                  * 0x80-0x8F
                  */
-            case 0x80: // ADDBRLE : TOS = TOS + TOS-1
+            case 0x80: // BRGT : TOS-1 > TOS ? IP += (IP)
+                val = POP;
+                if (TOS < val)
+                {
+                    POP;
+                    ip += WORD_PTR(ip);
+                }
+                else
+                {
+                    PUSH(val);
+                    ip += 2;
+                }
+                break;
+            case 0x82: // BRLT : TOS-1 < TOS ? IP += (IP)
+                val = POP;
+                if (TOS > val)
+                {
+                    POP;
+                    ip += WORD_PTR(ip);
+                }
+                else
+                {
+                    PUSH(val);
+                    ip += 2;
+                }
+                break;
+            case 0x84: // INCBRLE : TOS = TOS + 1
+                val = POP;
+                val++;
+                if (TOS >= val)
+                {
+                    PUSH(val);
+                    ip += WORD_PTR(ip);
+                }
+                else
+                {
+                    POP;
+                    ip += 2;
+                }
+                break;
+            case 0x86: // ADDBRLE : TOS = TOS + TOS-1
                 val = POP;
                 ea  = POP;
                 val = ea + val;
@@ -899,36 +940,7 @@ void interp(code *ip)
                     ip += 2;
                 }
                 break;
-            case 0x82: // INCBRLE : TOS = TOS + 1
-                val = POP;
-                val++;
-                if (TOS >= val)
-                {
-                    PUSH(val);
-                    ip += WORD_PTR(ip);
-                }
-                else
-                {
-                    POP;
-                    ip += 2;
-                }
-                break;
-            case 0x84: // SUBBRGE : TOS = TOS-1 - TOS
-                val = POP;
-                ea  = POP;
-                val = ea - val;
-                if (TOS <= val)
-                {
-                    PUSH(val);
-                    ip += WORD_PTR(ip);
-                }
-                else
-                {
-                    POP;
-                    ip += 2;
-                }
-                break;
-            case 0x86: // DECBRGE : TOS = TOS - 1
+            case 0x88: // DECBRGE : TOS = TOS - 1
                 val = POP;
                 val--;
                 if (TOS <= val)
@@ -942,29 +954,18 @@ void interp(code *ip)
                     ip += 2;
                 }
                 break;
-            case 0x88: // BRGT : TOS-1 > TOS ? IP += (IP)
+            case 0x8A: // SUBBRGE : TOS = TOS-1 - TOS
                 val = POP;
-                if (TOS < val)
+                ea  = POP;
+                val = ea - val;
+                if (TOS <= val)
                 {
-                    POP;
+                    PUSH(val);
                     ip += WORD_PTR(ip);
                 }
                 else
-                {
-                    PUSH(val);
-                    ip += 2;
-                }
-                break;
-            case 0x8A: // BRLT : TOS-1 < TOS ? IP += (IP)
-                val = POP;
-                if (TOS > val)
                 {
                     POP;
-                    ip += WORD_PTR(ip);
-                }
-                else
-                {
-                    PUSH(val);
                     ip += 2;
                 }
                 break;