OP_FORPREP, OP_FORLOOP: Support for numerical for loops

Sun, 23 May 2010 13:28:45 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 23 May 2010 13:28:45 +0100
changeset 34
2c3d73c76d0f
parent 33
6cc1b0a8dd97
child 35
3de1d63ffdf7

OP_FORPREP, OP_FORLOOP: Support for numerical for loops

lvm.js file | annotate | diff | comparison | revisions
--- a/lvm.js	Sun May 23 13:28:23 2010 +0100
+++ b/lvm.js	Sun May 23 13:28:45 2010 +0100
@@ -11,6 +11,8 @@
 var OP_NEWTABLE = 10;
 var OP_CALL = 28;
 var OP_RETURN = 30;
+var OP_FORLOOP = 31;
+var OP_FORPREP = 32;
 var OP_CLOSURE = 36;
 
 var debugMode = false;
@@ -391,6 +393,22 @@
 						this.frame.reg.splice(0,oldFrame.retAt+i);
 				}
 				break;
+			case OP_FORPREP:
+				this.frame.pc+=(INS_sBx(instruction));
+				var A = INS_A(instruction);
+				this.frame.reg[A].value -= this.frame.reg[A+2].value;
+				this.frame.reg[A+3] = new LValue("number", null);
+				break;
+			case OP_FORLOOP:
+				var A = INS_A(instruction);
+				var RA = this.frame.reg[A];
+				RA.value += this.frame.reg[A+2].value;
+				if(RA.value <= this.frame.reg[A+1].value)
+				{
+					this.frame.pc += INS_sBx(instruction);
+					this.frame.reg[A+3].value = RA.value;
+				}
+				break;
 			default:
 				throw "Unhandled opcode: "+INS_OPCODE(instruction);
 			}

mercurial