UNPKG

brauliovm.github.io

Version:
61 lines (46 loc) 1.31 kB
//import TuringMachine from '../TuringMachine/index.js'; class Program { constructor(name, link, rules){ this.name = name; this.link = link; this.machine = new TuringMachine(0, -1); // Q0 and HALT for(let rule of rules){ this.machine.addRule(...rule); } } } // transform Q[0] ... Q[1] and HALT constants into numbers const HALT = "HALT", STATE_PREFIX = "Q", RIGHT = "RIGHT", LEFT = "LEFT"; /* TSON == TuringSon, similar to JSON but with state names and stuff */ function getProgramsFromTson(tson){ var machines_data = JSON.parse(transformToValidJson(tson)); let programs = []; for(let machine_data of machines_data){ programs.push(new Program(machine_data.name, machine_data.link, machine_data.rules)); } return programs; } function transformToValidJson(text){ text = text.split(HALT).join(-1); // we assign HALT state to -1 text = text.split(RIGHT).join(1); text = text.split(LEFT).join(-1); let i = 0, currentState = STATE_PREFIX + i; while (text.indexOf(currentState) != -1){ text = text.split(currentState).join(i); i++; currentState = STATE_PREFIX + i; } return text; } const PROGRAMS_URL = "/data/programs.tson"; export function getPrograms(){ return fetch(PROGRAMS_URL).then( (response) => response.text() ).then(getProgramsFromTson); }