local min, max, huge = math.min, math.max, math.huge local machine = {} machine.counters = { ["run current frame"] = 0, ["step timer"] = 0, ["clear the screen"] = 0, ["draw scene"] = 0, ["present current frame"] = 0, ["sleep for 1ms"] = 0, ["next cycle"] = 0, ["run main loop"] = 1, ["@poll inputs"] = 0, ["@quit love"] = 0, ["@step timer"] = 0, ["@clear screen"] = 0, ["@text input"] = 0, ["@text received"] = 0, ["@quit"] = 0, ["@push text"] = 0, ["@draw buffer"] = 0, ["@present"] = 0, ["@is backspace pressed"] = 0, ["@backspace was pressed"] = 0, ["@delete character"] = 0, ["@is new line pressed"] = 0, ["@new line was pressed"] = 0, ["@insert new line"] = 0, ["handling inputs"] = 0, ["handle text input"] = 0, ["@exit code"] = 0, ["handle return or enter"] = 0, ["check for text input event"] = 0, ["push text to buffer"] = 0, ["check for backspace"] = 0, ["delete character from buffer"] = 0, ["check for new line"] = 0, ["insert new line"] = 0, ["@sleep"] = 0, ["drawing scene"] = 0, ["draw buffer"] = 0, ["handle backspace"] = 0, ["poll inputs"] = 0, ["handle inputs"] = 0, } function machine.on_poll_inputs(counters) end function machine.on_quit_love(counters) end function machine.on_step_timer(counters) end function machine.on_clear_screen(counters) end function machine.on_present(counters) end function machine.on_sleep(counters, sleep) end function machine.on_text_input(counters) end function machine.on_push_text_to_buffer(counters) end function machine.on_draw_buffer(counters) end function machine.on_is_backspace_pressed(counters) end function machine.on_delete_character(counters) end function machine.on_is_new_line_pressed(counters) end function machine.on_insert_new_line(counters) end function match(self, counters) if counters["@poll inputs"] > 0 then self.on_poll_inputs(counters) counters["@poll inputs"] = 0 return true end if counters["@quit love"] > 0 then counters["@exit code"] = self.on_quit_love(counters) counters["@quit love"] = 0 return true end if counters["@step timer"] > 0 then self.on_step_timer(counters) counters["@step timer"] = 0 return true end if counters["@clear screen"] > 0 then self.on_clear_screen(counters) counters["@clear screen"] = 0 return true end if counters["@present"] > 0 then self.on_present(counters) counters["@present"] = 0 return true end if counters["@sleep"] > 0 then self.on_sleep(counters, counters["@sleep"]) counters["@sleep"] = 0 return true end if counters["@text input"] > 0 then counters["@text received"] = self.on_text_input(counters) counters["@text input"] = 0 return true end if counters["@push text"] > 0 then self.on_push_text_to_buffer(counters) counters["@push text"] = 0 return true end if counters["@draw buffer"] > 0 then self.on_draw_buffer(counters) counters["@draw buffer"] = 0 return true end if counters["@is backspace pressed"] > 0 then counters["@backspace was pressed"] = self.on_is_backspace_pressed(counters) counters["@is backspace pressed"] = 0 return true end if counters["@delete character"] > 0 then self.on_delete_character(counters) counters["@delete character"] = 0 return true end if counters["@is new line pressed"] > 0 then counters["@new line was pressed"] = self.on_is_new_line_pressed(counters) counters["@is new line pressed"] = 0 return true end if counters["@insert new line"] > 0 then self.on_insert_new_line(counters) counters["@insert new line"] = 0 return true end if counters["handling inputs"] > 0 then acc = counters["handling inputs"] counters["handling inputs"] = max(counters["handling inputs"] - acc, 0) counters["handle text input"] = counters["handle text input"] + acc * 1 counters["handle backspace"] = counters["handle backspace"] + acc * 1 counters["handle return or enter"] = counters["handle return or enter"] + acc * 1 return true end if counters["handle text input"] > 0 then acc = counters["handle text input"] counters["handle text input"] = max(counters["handle text input"] - acc, 0) counters["check for text input event"] = counters["check for text input event"] + acc * 1 counters["push text to buffer"] = counters["push text to buffer"] + acc * 1 return true end if counters["check for text input event"] > 0 then acc = counters["check for text input event"] counters["check for text input event"] = max(counters["check for text input event"] - acc, 0) counters["@text input"] = counters["@text input"] + acc * 1 return true end if counters["push text to buffer"] > 0 and counters["@text received"] > 0 then local acc = huge acc = min(acc, counters["@text received"]) acc = min(acc, counters["push text to buffer"]) counters["push text to buffer"] = max(counters["push text to buffer"] - acc, 0) counters["@text received"] = max(counters["@text received"] - acc, 0) counters["@push text"] = counters["@push text"] + acc * 1 counters["handle text input"] = counters["handle text input"] + acc * 1 return true end if counters["push text to buffer"] > 0 then acc = counters["push text to buffer"] counters["push text to buffer"] = max(counters["push text to buffer"] - acc, 0) return true end if counters["handle backspace"] > 0 then acc = counters["handle backspace"] counters["handle backspace"] = max(counters["handle backspace"] - acc, 0) counters["check for backspace"] = counters["check for backspace"] + acc * 1 counters["delete character from buffer"] = counters["delete character from buffer"] + acc * 1 return true end if counters["check for backspace"] > 0 then acc = counters["check for backspace"] counters["check for backspace"] = max(counters["check for backspace"] - acc, 0) counters["@is backspace pressed"] = counters["@is backspace pressed"] + acc * 1 return true end if counters["delete character from buffer"] > 0 and counters["@backspace was pressed"] > 0 then local acc = huge acc = min(acc, counters["@backspace was pressed"]) acc = min(acc, counters["delete character from buffer"]) counters["delete character from buffer"] = max(counters["delete character from buffer"] - acc, 0) counters["@backspace was pressed"] = max(counters["@backspace was pressed"] - acc, 0) counters["@delete character"] = counters["@delete character"] + acc * 1 return true end if counters["delete character from buffer"] > 0 then acc = counters["delete character from buffer"] counters["delete character from buffer"] = max(counters["delete character from buffer"] - acc, 0) return true end if counters["handle return or enter"] > 0 then acc = counters["handle return or enter"] counters["handle return or enter"] = max(counters["handle return or enter"] - acc, 0) counters["check for new line"] = counters["check for new line"] + acc * 1 counters["insert new line"] = counters["insert new line"] + acc * 1 return true end if counters["check for new line"] > 0 then acc = counters["check for new line"] counters["check for new line"] = max(counters["check for new line"] - acc, 0) counters["@is new line pressed"] = counters["@is new line pressed"] + acc * 1 return true end if counters["insert new line"] > 0 and counters["@new line was pressed"] > 0 then local acc = huge acc = min(acc, counters["@new line was pressed"]) acc = min(acc, counters["insert new line"]) counters["insert new line"] = max(counters["insert new line"] - acc, 0) counters["@new line was pressed"] = max(counters["@new line was pressed"] - acc, 0) counters["@insert new line"] = counters["@insert new line"] + acc * 1 return true end if counters["insert new line"] > 0 then acc = counters["insert new line"] counters["insert new line"] = max(counters["insert new line"] - acc, 0) return true end if counters["drawing scene"] > 0 then acc = counters["drawing scene"] counters["drawing scene"] = max(counters["drawing scene"] - acc, 0) counters["draw buffer"] = counters["draw buffer"] + acc * 1 return true end if counters["draw buffer"] > 0 then acc = counters["draw buffer"] counters["draw buffer"] = max(counters["draw buffer"] - acc, 0) counters["@draw buffer"] = counters["@draw buffer"] + acc * 1 return true end if counters["run main loop"] > 0 then acc = counters["run main loop"] counters["run main loop"] = max(counters["run main loop"] - acc, 0) counters["run current frame"] = counters["run current frame"] + acc * 1 counters["poll inputs"] = counters["poll inputs"] + acc * 1 counters["handle inputs"] = counters["handle inputs"] + acc * 1 return true end if counters["poll inputs"] > 0 then acc = counters["poll inputs"] counters["poll inputs"] = max(counters["poll inputs"] - acc, 0) counters["@poll inputs"] = counters["@poll inputs"] + acc * 1 return true end if counters["handle inputs"] > 0 then acc = counters["handle inputs"] counters["handle inputs"] = max(counters["handle inputs"] - acc, 0) counters["handling inputs"] = counters["handling inputs"] + acc * 1 return true end if counters["run current frame"] > 0 and counters["@quit"] > 0 then local acc = huge acc = min(acc, counters["run current frame"]) acc = min(acc, counters["@quit"]) counters["run current frame"] = max(counters["run current frame"] - acc, 0) counters["@quit"] = max(counters["@quit"] - acc, 0) counters["@quit love"] = counters["@quit love"] + acc * 1 return true end if counters["run current frame"] > 0 then acc = counters["run current frame"] counters["run current frame"] = max(counters["run current frame"] - acc, 0) counters["step timer"] = counters["step timer"] + acc * 1 counters["clear the screen"] = counters["clear the screen"] + acc * 1 counters["draw scene"] = counters["draw scene"] + acc * 1 counters["present current frame"] = counters["present current frame"] + acc * 1 counters["sleep for 1ms"] = counters["sleep for 1ms"] + acc * 1 counters["next cycle"] = counters["next cycle"] + acc * 1 return true end if counters["step timer"] > 0 then acc = counters["step timer"] counters["step timer"] = max(counters["step timer"] - acc, 0) counters["@step timer"] = counters["@step timer"] + acc * 1 return true end if counters["clear the screen"] > 0 then acc = counters["clear the screen"] counters["clear the screen"] = max(counters["clear the screen"] - acc, 0) counters["@clear screen"] = counters["@clear screen"] + acc * 1 return true end if counters["draw scene"] > 0 then acc = counters["draw scene"] counters["draw scene"] = max(counters["draw scene"] - acc, 0) counters["drawing scene"] = counters["drawing scene"] + acc * 1 return true end if counters["present current frame"] > 0 then acc = counters["present current frame"] counters["present current frame"] = max(counters["present current frame"] - acc, 0) counters["@present"] = counters["@present"] + acc * 1 return true end if counters["sleep for 1ms"] > 0 then acc = counters["sleep for 1ms"] counters["sleep for 1ms"] = max(counters["sleep for 1ms"] - acc, 0) counters["@sleep"] = counters["@sleep"] + acc * 1 return true end if counters["next cycle"] > 0 then acc = counters["next cycle"] counters["next cycle"] = max(counters["next cycle"] - acc, 0) counters["run main loop"] = counters["run main loop"] + acc * 1 return true end return false end function machine:run() local counters = self.counters while match(self, counters) do end end return machine