Ver código fonte

Move string to decimal conversion to last step

To make using variables easier, I've removed all of the calls to
`strToDecimal` to the end of the transpilation process.
ApisNecros 3 semanas atrás
pai
commit
8b447e8270
1 arquivos alterados com 26 adições e 19 exclusões
  1. 26 19
      yaict

+ 26 - 19
yaict

@@ -6,7 +6,7 @@ const fs = require("node:fs");
  * The tape of integers
  * @type {number[]}
  */
-const tape = [];
+let tape = [];
 /**
  * The current address of the tape being modified
  * 
@@ -65,6 +65,9 @@ function transpileAssembly(assembly) {
         // Call the function for this instruction, and pass the rest of the line as parameters
         ASM_DICTIONARY[instruction](...line);
     }
+
+    // Clean the values on the tape
+    tape = tape.map((inst) => strToDecimal(inst));
 }
 
 /* ASSEMBLY HANDLERS */
@@ -76,7 +79,7 @@ function transpileAssembly(assembly) {
  * @param {number} value
  */
 function loadToAddress(address, value) {
-    tape[strToDecimal(address)] = strToDecimal(value);
+    tape[strToDecimal(address)] = value;
 }
 
 /**
@@ -95,10 +98,10 @@ function insertArithmetic(opCode, operand1, operand2, outputDestination) {
     // Add the parameter mode for the two operands
     opCode = getParameterMode(operand2) + getParameterMode(operand1) + opCode;
 
-    tape[tapePointer++] = strToDecimal(opCode);
-    tape[tapePointer++] = strToDecimal(operand1);
-    tape[tapePointer++] = strToDecimal(operand2);
-    tape[tapePointer++] = strToDecimal(outputDestination);
+    tape[tapePointer++] = opCode;
+    tape[tapePointer++] = operand1;
+    tape[tapePointer++] = operand2;
+    tape[tapePointer++] = outputDestination;
 }
 
 /*
@@ -113,8 +116,8 @@ function insertInput() {
  * @param {string} outputParameter The parameter of the value to output. May be an immediate value or an address
  */
 function insertOutput(outputParameter) {
-    tape[tapePointer++] = strToDecimal(getParameterMode(outputParameter) + OP_CODE_DICTIONARY.OUTPUT);
-    tape[tapePointer++] = strToDecimal(outputParameter);
+    tape[tapePointer++] = getParameterMode(outputParameter) + OP_CODE_DICTIONARY.OUTPUT;
+    tape[tapePointer++] = outputParameter;
 }
 
 /**
@@ -125,9 +128,9 @@ function insertOutput(outputParameter) {
  * @param {string} jumpDestination The address to jump to if the test passes
  */
 function insertTestJump(opcode, testingParameter, jumpDestination) {
-    tape[tapePointer++] = strToDecimal(getParameterMode(jumpDestination) + getParameterMode(testingParameter) + opcode);
-    tape[tapePointer++] = strToDecimal(testingParameter);
-    tape[tapePointer++] = strToDecimal(jumpDestination);
+    tape[tapePointer++] = getParameterMode(jumpDestination) + getParameterMode(testingParameter) + opcode;
+    tape[tapePointer++] = testingParameter;
+    tape[tapePointer++] = jumpDestination;
 }
 
 /**
@@ -142,10 +145,10 @@ function insertLessThanCheck(operand1, operand2, outputDestination) {
         throw new WrongParameterModeError();
     }
 
-    tape[tapePointer++] = strToDecimal(getParameterMode(operand2) + getParameterMode(operand1) + OP_CODE_DICTIONARY.LESS_THAN);
-    tape[tapePointer++] = strToDecimal(operand1);
-    tape[tapePointer++] = strToDecimal(operand2);
-    tape[tapePointer++] = strToDecimal(outputDestination);
+    tape[tapePointer++] = getParameterMode(operand2) + getParameterMode(operand1) + OP_CODE_DICTIONARY.LESS_THAN;
+    tape[tapePointer++] = operand1;
+    tape[tapePointer++] = operand2;
+    tape[tapePointer++] = outputDestination;
 }
 
 /**
@@ -160,10 +163,10 @@ function insertEqualityCheck(operand1, operand2, outputDestination) {
         throw new WrongParameterModeError();
     }
 
-    tape[tapePointer++] = strToDecimal(getParameterMode(operand2) + getParameterMode(operand1) + OP_CODE_DICTIONARY.EQUALS);
-    tape[tapePointer++] = strToDecimal(operand1);
-    tape[tapePointer++] = strToDecimal(operand2);
-    tape[tapePointer++] = strToDecimal(outputDestination);
+    tape[tapePointer++] = getParameterMode(operand2) + getParameterMode(operand1) + OP_CODE_DICTIONARY.EQUALS;
+    tape[tapePointer++] = operand1;
+    tape[tapePointer++] = operand2;
+    tape[tapePointer++] = outputDestination;
 }
 
 /**
@@ -215,6 +218,10 @@ function getParameterMode(parameter) {
  * @returns {number}
  */
 function strToDecimal(decimalValue) {
+    if (typeof decimalValue == "number") {
+        return decimalValue;
+    }
+
     return parseInt(decimalValue.replace(/^0d/, ""), 10);
 }