diff --git a/TODO_StateMachine.md b/TODO_StateMachine.md index 91fd7b81..3010dc3c 100644 --- a/TODO_StateMachine.md +++ b/TODO_StateMachine.md @@ -24,7 +24,7 @@ - `$pass_input` statement - The next `$switch` will use the current input -#### Sample +### Sample ``` class Calculator { @@ -148,3 +148,125 @@ func Main(): string return calculator.Value; // "46" } ``` + +### Generated +``` +class Calculator +{ +    ... // ignored everything before $state_machine + + enum Input + { + Invalid = 0, + Digit = 1, + Dot = 2, + Add = 3, + Mul = 4, + Equal = 5, + Clear = 6, + } + + enum State + { + Start = 0, + Digit = 1, + Integer = 2, + Numver = 3, + Calculate = 4, + } + + [cpp:Private] + var input: Input = ::Calculator::Input::Invalid; + + [cpp:Private] + var coroutine: ::system::Coroutine^ = null; + + [cpp:Private] + var i: int = 0; + + [cpp:Private] + void Resume(): void + { + } + + void Digit(i: int): void { input = ::Calculator::Input::Digit; i = i; Resume(); } + void Dot(): void { input = ::Calculator::Input::Dot; Resume(); } + void Add(): void { input = ::Calculator::Input::Add; Resume(); } + void Mul(): void { input = ::Calculator::Input::Mul; Resume(); } + void Equal(): void { input = ::Calculator::Input::Equal; Resume(); } + void Clear(): void { input = ::Calculator::Input::Clear; Resume(); } + + void RunStateMachine(): void + { + } + + $state_machine + { + $state Digits() + { + $switch(pass) + { + case Digit(i) + { + Value = Value & i; + $goto_state Digits(); + } + } + } + + $state Integer(newNumber: bool) + { + $switch(pass) + { + case Digit(i) + { + if (newNumber) + { + Value = i; + } + else + { + Value = Value & i; + } + $goto_state Digits(); + } + } + } + + $state Number() + { + $push_state Integer(true); + $switch(pass_and_return) + { + case Dot() + { + Value = Value & "."; + } + } + $push_state Integer(false); + } + + $state Calculate() + { + $push_state Number(); + $switch + { + case Add(): {Calculate(); op = "+";} + case Mul(): {Calculate(); op = "-";} + case Equal(): {Calculate(); op = "=";} + case Clear(): + { + valueFirst = ""; + op = ""; + Value = "0"; + } + } + } + + $state + { + $goto_state Calculate(); + } + } +} +```