Kaynağa Gözat

Add ability to jump to labels

With this update, jnz and jz have a new parameter, offset. This
numeric value, if provided, is added to the address that the label
resolves to.
ApisNecros 1 ay önce
ebeveyn
işleme
0d95a8b089
1 değiştirilmiş dosya ile 29 ekleme ve 2 silme
  1. 29 2
      yaict

+ 29 - 2
yaict

@@ -169,10 +169,10 @@ function insertOutput(outputParameter) {
  * @param {string} testingParameter The parameter to test. Could be an immediate value or an address
  * @param {string} jumpDestination The address to jump to if the test passes
  */
-function insertTestJump(opcode, testingParameter, jumpDestination) {
+function insertTestJump(opcode, testingParameter, jumpDestination, offset) {
     tape[tapePointer++] = getParameterMode(jumpDestination) + getParameterMode(testingParameter) + opcode;
     tape[tapePointer++] = testingParameter;
-    tape[tapePointer++] = jumpDestination;
+    tape[tapePointer++] = validateJumpDestination(jumpDestination, offset);
 }
 
 /**
@@ -246,6 +246,33 @@ function isLabelStart(parameter) {
     return /^\w+:$/.test(parameter);
 }
 
+/**
+ * Confirm that a label exists
+ *
+ * @param {string} labelName The possible label name to check
+ *
+ * @returns {boolean}
+ */
+function validateLabel(labelName) {
+    return labelDictionary.hasOwnProperty(labelName);
+}
+
+/**
+ * Get the address to jump to
+ * 
+ * @param {string} jumpDestination The destination to jump to. Could be either a memory address or 
+ * @param {number} [offset] An offset for the jump destination. Only used if the jump destination is a label
+ *
+ * @returns {string|number} The address to jump to
+ */
+function validateJumpDestination(jumpDestination, offset = 0) {
+    if (isAddress(jumpDestination)) { return jumpDestination; }
+    if (validateLabel(jumpDestination)) {
+        return labelDictionary[jumpDestination] + offset
+    }
+    return;
+}
+
 /**
  * Get the memory address a value represents
  *