Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. const fs = require('fs');
  2. fs.readFile('input.file', 'utf8', (err, data) => {
  3. const lines = data.split('\n');
  4. let index = 0;
  5. const initializationExpression = new RegExp(/(?:Var|,)\s*([a-zA-Z]\w*\s*=\s*[\d\?])/g);
  6. const operationExpression = new RegExp(/[a-zA-Z].*?=\s*[a-zA-Z].*?\+\s*[a-zA-Z]\w*/);
  7. const outputExpression = new RegExp(/^Output\s([a-zA-Z]*)/);
  8.  
  9. const matches = {
  10. initializations: [],
  11. operations: [],
  12. outputs: [],
  13. }
  14. for (let line of lines){
  15. let match = null;
  16. while((match = initializationExpression.exec(line)) !== null) {
  17. matches.initializations.push(match[1]);
  18. }
  19. if (operationExpression.test(line)) {
  20. matches.operations.push(line);
  21. }
  22. let output = null;
  23. if ((output = outputExpression.exec(line)) !== null) {
  24. matches.outputs.push(output[1]);
  25. }
  26. }
  27. let op = '(function () {\n';
  28. matches.initializations.forEach(initialization => {
  29. op = `${op.length ? op + '\tlet ' + initialization + ';\n' : 'let ' + initialization + ';\n'}`;
  30. });
  31. matches.operations.forEach(operation => {
  32. op = `${op}\n\t${operation};`;
  33. });
  34. op = `${op}\n\n\treturn ${matches.outputs[0]};\n})()`;
  35. fs.writeFileSync('output.file', op);
  36.  
  37. console.log(eval(op));
  38. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement