<commit_before>"use strict";

let indentString = require('indent-string');

let argv = require('minimist')(process.argv.slice(2));
let fs = require('fs')

function indent(str, i = 1) {
  return indentString(str, ' ', 2 * i);
}

function parseTuringSyntax(source) {
  let states = {};
  let startState = null;
  let successState = null;
  let lines = source.split("\n");
  for (let line of lines) {
    line = line.trim();
    if (line[0] === ';') { }
    else if (line === '') { }
    else {
      let [state, read, replace, move, nextState] = line.split(' ');
      if (states[state] === undefined) {
        states[state] = [];
      }
      states[state].push({
        read,
        replace,
        move,
        nextState
      })
    }
  }
  return {
    states,
    startState,
    successState
  }
}

function transpile(syntax) {
  let source = '';
  source += "var DEBUG = false;\n";
  source += "var input = input.split('');\n"// + JSON.stringify('110'.split('')) + ";\n";
  source += "var tapePosition = " + JSON.stringify(0) + ";\n";
  source += "function statehalt() { }\n";
  source += "function statehaltAccept() { }\n";
  source += `function goLeft() {
  if (DEBUG) console.log('Go left');
  tapePosition--;
  if (tapePosition === -1) {
    tapePosition += 1;
    input.unshift(' ');
  } else if (tapePosition < 0) {
    throw "Invalid tape position when going left";
  }
}
`;
  source += `function goRight() {
  if (DEBUG) console.log('Go right');
  tapePosition++;
  if (tapePosition === input.length) {
    input.push(' ');
  } else if (tapePosition > input.length) {
    throw "Invalid tape position when going right";
  }
}
`;
  source += `function replace(replace) {
  if (DEBUG) console.log('Replacing with ', replace);
  if (tapePosition >= 0 && tapePosition < input.length) {
    input[tapePosition] = replace;
  } else if (tapePosition === input.length) {
    input.push(replace);
  } else {
    throw "Invalid tape position when replacing";
  }
}
`;  source += `function state(replace) {
  if (DEBUG) console.log('Replacing with ', replace);
  if (tapePosition >= 0 && tapePosition < input.length) {
    input[tapePosition] = replace;
  } else if (tapePosition === input.length) {
    input.push(replace);
  } else {
    throw "Invalid tape position when replacing";<commit_msg>Slightly faster performance<commit_after>"use strict";

let indentString = require('indent-string');

let argv = require('minimist')(process.argv.slice(2));
let fs = require('fs')

function indent(str, i = 1) {
  return indentString(str, ' ', 2 * i);
}

function parseTuringSyntax(source) {
  let states = {};
  let startState = null;
  let successState = null;
  let lines = source.split("\n");
  for (let line of lines) {
    line = line.trim();
    if (line[0] === ';') { }
    else if (line === '') { }
    else {
      let [state, read, replace, move, nextState] = line.split(' ');
      if (states[state] === undefined) {
        states[state] = [];
      }
      states[state].push({
        read,
        replace,
        move,
        nextState
      })
    }
  }
  return {
    states,
    startState,
    successState
  }
}

function transpile(syntax) {
  let source = '';
  source += "var DEBUG = false;\n";
  source += "var input = input.split('');\n"// + JSON.stringify('110'.split('')) + ";\n";
  source += "var tapePosition = " + JSON.stringify(0) + ";\n";
  source += "function statehalt() { }\n";
  source += "function statehaltAccept() { }\n";
  source += `function goLeft() {
  if (DEBUG) console.log('Go left');
  tapePosition--;
  if (tapePosition === -1) {
    tapePosition += 1;
    input.unshift(' ');
  } else if (tapePosition < 0) {
    throw "Invalid tape position when going left";
  }
}
`;
  source += `function goRight() {
  if (DEBUG) console.log('Go right');
  tapePosition++;
  if (tapePosition === input.length) {
    input.push(' ');
  } else if (tapePosition > input.length) {
    throw "Invalid tape position when going right";
  }
}
`;
  source += `function replace(replace) {
  if (DEBUG) console.log('Replacing with ', replace);
  input[tapePosition] = replace;
}
`;  source += `function state(replace) {
  if (DEBUG) console.log('Replacing with ', replace);
  if (tapePosition >= 0 && tapePosition < input.length) {
    input[tapePosition] = replace;
  } else if (tapePosition === input.length) {
    input.push(replace);
  } else {
    throw "Invalid tape position when replacing";
{'fraction_non_alphanumeric': 0.17494356659142213, 'fraction_numerical': 0.0056433408577878106, 'mean_word_length': 3.0838709677419356, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2032055', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1436, 'n_words': 372}