SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var LineReader = require('node-line-reader').LineReader;
  2. const fs = require('fs');
  3.  
  4. let registers = [{name: 'ACC', val: 0}, {name: 'R0', val: 0}];
  5. let mem = [6,9,4,2,7,8,1];
  6. let marks = [];
  7. let counter = 0;
  8.  
  9. fs.readFile('code.txt', {encoding: 'utf-8'}, function(err, data) {
  10.     if (!err) {
  11.         let code = data.replace(/,/g,'').replace(/\r/g,'').split('\n');
  12.         let filtered = code.filter((val, i, arr) => {
  13.             return val != '';
  14.         });
  15.         runProgram(filtered);
  16.     } else {
  17.         console.log(err);
  18.     }
  19. });
  20.  
  21. const runProgram = (sourceCode) => {
  22.     while(counter < sourceCode.length - 1) {
  23.         if (sourceCode[counter].includes(':')) {
  24.             marks.push({pos: counter, val: sourceCode[counter].replace(/:/g,'')});
  25.         } else {
  26.             processCommand(sourceCode[counter], sourceCode[counter - 1]);
  27.         }
  28.         counter++;
  29.     }
  30.     console.log(registers);
  31. }
  32.  
  33. const processCommand = (line, previousLine) => {
  34.     let data = line.split(' ');
  35.     let prevData = previousLine.split(' ');
  36.     switch(data[0]) {
  37.         case 'MOV' : processMov(data);
  38.         break;
  39.         case 'ADD' : processAdd(data);
  40.         break;
  41.         case 'DEC' : processDec(data);
  42.         break;
  43.         case 'JNE' : processJne(data, processCmp(prevData));
  44.         break;
  45.     }
  46. }
  47.  
  48. const processJne = (data, condition) => {
  49.     if (!condition) {
  50.         marks.forEach((mark, i) => {
  51.             if (mark.val === 'loop') { counter = mark.pos }
  52.         });
  53.     }
  54. }
  55.  
  56. const processCmp = (data) => {
  57.     let regValue = registers[findRegister(data[1])].val;
  58.     return regValue <= data[2];
  59. }
  60.  
  61. const processDec = (data) => {
  62.     let index = findRegister(data[1]);
  63.     registers[index].val -= 1;
  64. }
  65.  
  66. const processAdd = (data) => {
  67.     registers[0].val += memAccess(data[1]);
  68. }
  69.  
  70. const processMov = (data) => {
  71.     if (data[2].includes('[') && data[2].includes(']')) {
  72.         registers[findRegister(data[1])].val = memAccess(data[2]);
  73.     } else {
  74.         registers[findRegister(data[1])].val = parseInt(data[2]);
  75.     }
  76. }
  77.  
  78. const memAccess = (sector) => {
  79.     const numbers = /^[0-9]+$/
  80.     let sectorData = sector.replace(/\[|\]/g,'');
  81.     if (sectorData.match(numbers)) {
  82.         return mem[parseInt(sectorData, 10)];
  83.     }
  84.     else {
  85.         let reg = registers[findRegister(sectorData)];
  86.         return mem[reg.val];
  87.     }
  88. }
  89.  
  90. const findRegister = (name) => {
  91.     let index = -1;
  92.     registers.forEach((register, i) => {
  93.         if (register.name === name) { index = i; }
  94.     });
  95.     return index;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top