-
Notifications
You must be signed in to change notification settings - Fork 34
Description
To be a truly powerful educational tool, KidCode needs a debugger. This would allow users to pause execution, inspect the state of variables, and step through their code line by line to understand how it works.
Building this feature requires significant changes to the execution loop and state management.
Proposed Plan:
-
Introduce an
ExecutionContext:- Create a class that holds the state of an execution:
PAUSED,RUNNING,STEPPING,TERMINATED. - The
KidCodeEngineandEvaluatormust be aware of this context. The stop signalSupplier<Boolean>should be enhanced or replaced by this context object.
- Create a class that holds the state of an execution:
-
Modify the Evaluator for Pausing:
- The
Evaluatormust check theExecutionContextstate before executing eachStatement. - If the state is
PAUSED(e.g., at a breakpoint), the execution thread mustwait(). It will be resumed by an external signal (notify()). - If the state is
STEPPING, it should execute one statement and then immediately re-enter thePAUSEDstate.
- The
-
Breakpoint Management:
- The UI (web and desktop) needs a way for users to set breakpoints on specific line numbers.
- This information (
Set<Integer> breakpoints) must be passed to theKidCodeEngineat the start of execution. - Before executing a statement, the
Evaluatormust check if the current statement's line number is in the set of breakpoints. If so, it transitions theExecutionContexttoPAUSED.
-
Variable Inspection:
- When paused, the UI needs a way to request the current state of all variables.
- This requires adding a method to the
Environmentto dump all key-value pairs in the current scope. An API endpoint or WebSocket message would be needed to fetch this data from a paused execution context.
-
UI Controls:
- The desktop and web UIs need new buttons: "Debug", "Continue", "Step Over", "Stop".
- These buttons will interact with the
ExecutionContextto change its state (e.g., "Continue" sets the state toRUNNINGand notifies the waiting thread).