return function() { let min = Math.min; var max = Math.max; var huge = Number.MAX_SAFE_INTEGER; let self = { state: { strings: [], slots: {} } }; self.counters = { "loop": 1, } function match(self, counters) { console.log(Object.entries(counters).filter(([,v])=>v>0).map(([k,v]) => `${k} :: ${v}`).join('\n') ); console.log(self.state.strings); console.log(self.state.slots); if (counters["push string"] > 0) { self.state.strings.push(self.string_constants[counters["push string"]-1]); counters["push string"] = 0; return true; } if (counters["dx"] > 0 && counters["-dx"] > 0) { let acc = huge; acc = min(acc, counters["dx"] || 0); acc = min(acc, counters["-dx"] || 0); counters["dx"] = max((counters["dx"] || 0) - acc, 0); counters["-dx"] = max((counters["-dx"] || 0) - acc, 0); return true; } if (counters["dy"] > 0 && counters["-dy"] > 0) { let acc = huge; acc = min(acc, counters["dy"] || 0); acc = min(acc, counters["-dy"] || 0); counters["dy"] = max((counters["dy"] || 0) - acc, 0); counters["-dy"] = max((counters["-dy"] || 0) - acc, 0); return true; } if (counters["get dy"] > 0 && counters["W pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dy"] || 0); acc = min(acc, counters["W pressed"] || 0); counters["get dy"] = max((counters["get dy"] || 0) - acc, 0); counters["W pressed"] = max((counters["W pressed"] || 0) - acc, 0); counters["-dy"] = (counters["-dy"] || 0) + acc * 1; counters["get dy"] = (counters["get dy"] || 0) + acc * 1; return true; } if (counters["get dy"] > 0 && counters["S pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dy"] || 0); acc = min(acc, counters["S pressed"] || 0); counters["get dy"] = max((counters["get dy"] || 0) - acc, 0); counters["S pressed"] = max((counters["S pressed"] || 0) - acc, 0); counters["dy"] = (counters["dy"] || 0) + acc * 1; return true; } if (counters["get dx"] > 0 && counters["A pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dx"] || 0); acc = min(acc, counters["A pressed"] || 0); counters["get dx"] = max((counters["get dx"] || 0) - acc, 0); counters["A pressed"] = max((counters["A pressed"] || 0) - acc, 0); counters["-dx"] = (counters["-dx"] || 0) + acc * 1; counters["get dx"] = (counters["get dx"] || 0) + acc * 1; return true; } if (counters["get dx"] > 0 && counters["D pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dx"] || 0); acc = min(acc, counters["D pressed"] || 0); counters["get dx"] = max((counters["get dx"] || 0) - acc, 0); counters["D pressed"] = max((counters["D pressed"] || 0) - acc, 0); counters["dx"] = (counters["dx"] || 0) + acc * 1; return true; } if (counters["loop"] > 0) { let acc = counters["loop"] || 0; counters["loop"] = max((counters["loop"] || 0) - acc, 0); counters["input"] = (counters["input"] || 0) + acc * 1; counters["player"] = (counters["player"] || 0) + acc * 1; counters["enemies"] = (counters["enemies"] || 0) + acc * 1; counters["cull"] = (counters["cull"] || 0) + acc * 1; counters["re-loop"] = (counters["re-loop"] || 0) + acc * 1; return true; } if (counters["input"] > 0) { let acc = counters["input"] || 0; counters["input"] = max((counters["input"] || 0) - acc, 0); counters["poll"] = (counters["poll"] || 0) + acc * 1; return true; } if (counters["player"] > 0) { let acc = counters["player"] || 0; counters["player"] = max((counters["player"] || 0) - acc, 0); counters["get dx:max"] = (counters["get dx:max"] || 0) + acc * 1; counters["get dy:max"] = (counters["get dy:max"] || 0) + acc * 1; return true; } if (counters["update enemy"] > 0) { let acc = counters["update enemy"] || 0; counters["update enemy"] = max((counters["update enemy"] || 0) - acc, 0); counters["track player"] = (counters["track player"] || 0) + acc * 1; return true; } if (counters["enemies"] > 0 && counters["last enemy"] > 0) { let acc = huge; acc = min(acc, counters["enemies"] || 0); acc = min(acc, counters["last enemy"] || 0); counters["enemies"] = max((counters["enemies"] || 0) - acc, 0); counters["last enemy"] = max((counters["last enemy"] || 0) - acc, 0); return true; } if (counters["enemies"] > 0) { let acc = counters["enemies"] || 0; counters["enemies"] = max((counters["enemies"] || 0) - acc, 0); counters["update enemy"] = (counters["update enemy"] || 0) + acc * 1; counters["next enemy"] = (counters["next enemy"] || 0) + acc * 1; return true; } if (counters["cull"] > 0) { let acc = counters["cull"] || 0; counters["cull"] = max((counters["cull"] || 0) - acc, 0); counters["remove dead enemies"] = (counters["remove dead enemies"] || 0) + acc * 1; return true; } if (counters["re-loop"] > 0) { let acc = counters["re-loop"] || 0; counters["re-loop"] = max((counters["re-loop"] || 0) - acc, 0); counters["loop"] = (counters["loop"] || 0) + acc * 1; return true; } if (counters["get dy"] > 0 && counters["W pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dy"] || 0); acc = min(acc, counters["W pressed"] || 0); counters["get dy"] = max((counters["get dy"] || 0) - acc, 0); counters["W pressed"] = max((counters["W pressed"] || 0) - acc, 0); counters["-dy"] = (counters["-dy"] || 0) + acc * 1; counters["get dy"] = (counters["get dy"] || 0) + acc * 1; return true; } if (counters["get dy"] > 0 && counters["S pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dy"] || 0); acc = min(acc, counters["S pressed"] || 0); counters["get dy"] = max((counters["get dy"] || 0) - acc, 0); counters["S pressed"] = max((counters["S pressed"] || 0) - acc, 0); counters["dy"] = (counters["dy"] || 0) + acc * 1; return true; } if (counters["get dx"] > 0 && counters["A pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dx"] || 0); acc = min(acc, counters["A pressed"] || 0); counters["get dx"] = max((counters["get dx"] || 0) - acc, 0); counters["A pressed"] = max((counters["A pressed"] || 0) - acc, 0); counters["-dx"] = (counters["-dx"] || 0) + acc * 1; counters["get dx"] = (counters["get dx"] || 0) + acc * 1; return true; } if (counters["get dx"] > 0 && counters["D pressed"] > 0) { let acc = huge; acc = min(acc, counters["get dx"] || 0); acc = min(acc, counters["D pressed"] || 0); counters["get dx"] = max((counters["get dx"] || 0) - acc, 0); counters["D pressed"] = max((counters["D pressed"] || 0) - acc, 0); counters["dx"] = (counters["dx"] || 0) + acc * 1; return true; } if (counters["loop"] > 0) { let acc = counters["loop"] || 0; counters["loop"] = max((counters["loop"] || 0) - acc, 0); counters["input"] = (counters["input"] || 0) + acc * 1; counters["player"] = (counters["player"] || 0) + acc * 1; counters["enemies"] = (counters["enemies"] || 0) + acc * 1; counters["cull"] = (counters["cull"] || 0) + acc * 1; counters["re-loop"] = (counters["re-loop"] || 0) + acc * 1; return true; } if (counters["input"] > 0) { let acc = counters["input"] || 0; counters["input"] = max((counters["input"] || 0) - acc, 0); counters["poll"] = (counters["poll"] || 0) + acc * 1; return true; } if (counters["player"] > 0) { let acc = counters["player"] || 0; counters["player"] = max((counters["player"] || 0) - acc, 0); counters["get dx:max"] = (counters["get dx:max"] || 0) + acc * 1; counters["get dy:max"] = (counters["get dy:max"] || 0) + acc * 1; return true; } if (counters["update enemy"] > 0) { let acc = counters["update enemy"] || 0; counters["update enemy"] = max((counters["update enemy"] || 0) - acc, 0); counters["track player"] = (counters["track player"] || 0) + acc * 1; return true; } if (counters["enemies"] > 0 && counters["last enemy"] > 0) { let acc = huge; acc = min(acc, counters["enemies"] || 0); acc = min(acc, counters["last enemy"] || 0); counters["enemies"] = max((counters["enemies"] || 0) - acc, 0); counters["last enemy"] = max((counters["last enemy"] || 0) - acc, 0); return true; } if (counters["enemies"] > 0) { let acc = counters["enemies"] || 0; counters["enemies"] = max((counters["enemies"] || 0) - acc, 0); counters["update enemy"] = (counters["update enemy"] || 0) + acc * 1; counters["next enemy"] = (counters["next enemy"] || 0) + acc * 1; return true; } if (counters["cull"] > 0) { let acc = counters["cull"] || 0; counters["cull"] = max((counters["cull"] || 0) - acc, 0); counters["remove dead enemies"] = (counters["remove dead enemies"] || 0) + acc * 1; return true; } if (counters["re-loop"] > 0) { let acc = counters["re-loop"] || 0; counters["re-loop"] = max((counters["re-loop"] || 0) - acc, 0); counters["loop"] = (counters["loop"] || 0) + acc * 1; return true; } return false; } self.run = function() { let counters = self.counters; while(match(self, counters)){}; }; self.log_state = function() { return Object.entries(self.counters).map(([k, v]) => `${k} :: ${v}`).join('\n'); }; return self; };