Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- fs.readFile('input.file', 'utf8', (err, data) => {
- const lines = data.split('\n');
- let index = 0;
- const initializationExpression = new RegExp(/(?:Var|,)\s*([a-zA-Z]\w*\s*=\s*[\d\?])/g);
- const operationExpression = new RegExp(/[a-zA-Z].*?=\s*[a-zA-Z].*?\+\s*[a-zA-Z]\w*/);
- const outputExpression = new RegExp(/^Output\s([a-zA-Z]*)/);
- const matches = {
- initializations: [],
- operations: [],
- outputs: [],
- }
- for (let line of lines){
- let match = null;
- while((match = initializationExpression.exec(line)) !== null) {
- matches.initializations.push(match[1]);
- }
- if (operationExpression.test(line)) {
- matches.operations.push(line);
- }
- let output = null;
- if ((output = outputExpression.exec(line)) !== null) {
- matches.outputs.push(output[1]);
- }
- }
- let op = '(function () {\n';
- matches.initializations.forEach(initialization => {
- op = `${op.length ? op + '\tlet ' + initialization + ';\n' : 'let ' + initialization + ';\n'}`;
- });
- matches.operations.forEach(operation => {
- op = `${op}\n\t${operation};`;
- });
- op = `${op}\n\n\treturn ${matches.outputs[0]};\n})()`;
- fs.writeFileSync('output.file', op);
- console.log(eval(op));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement