https://github.com/zefir1990/turing-machine
https://github.com/zefir1990/turing-machine
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/zefir1990/turing-machine
- Owner: zefir1990
- License: mit
- Created: 2025-11-02T11:54:20.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-11-02T12:41:38.000Z (7 months ago)
- Last Synced: 2026-02-21T21:20:31.187Z (3 months ago)
- Language: Dart
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ง Dart Turing Machine Emulator
A minimalistic **Turing Machine emulator** implemented entirely in **Dart**, featuring an extendable opcode system, infinite tape simulation, and several sample "programs" (tapes) that demonstrate printing, arithmetic, conditional branching, user input, and even a **self-replicating quine**.
---
## ๐ Features
- ๐งพ **Finite State Control** โ Centralized control flow handling all instructions.
- โพ๏ธ **Infinite Tape** โ Implemented as a sparse map for unbounded memory access.
- ๐ช **Tape Head** โ Supports moving, reading, and writing across infinite cells.
- ๐งฉ **Opcodes System** โ Extensible instruction set (print, increment, input, copy, etc).
- ๐ **Non-deterministic Option** โ Optional chaos mode for random opcode substitution.
- ๐ง **Delegate Interface** โ Clean separation between logic and memory management.
- ๐ก **Example Programs**:
- **Hello World**
- **Count to Sixteen**
- **Count Down to Zero**
- **Number Guessing Game**
- **Quine (self-replicating program)**
---
## ๐งฉ Opcodes
| Opcode | Description |
|--------|--------------|
| `stop` | Halts the machine |
| `print` | Prints the next symbol |
| `increment next` | Increments the numeric value of the next symbol |
| `decrement next` | Decrements the numeric value of the next symbol |
| `if previous not equal` | Conditional execution block |
| `move to index` | Moves the tape head to a specific index |
| `copy from index to index` | Copies a value from one cell to another |
| `input to next` | Reads user input and writes it to the next cell |
| `generate random number from zero to next and write after` | Writes a random integer to tape |
---
## ๐งฎ Example Tapes
The `main()` function defines and runs several tapes sequentially:
### ๐จ๏ธ 1. Hello World
Prints โhello worldโ and stops.
```dart
helloWorldTape.write(symbol: "print", at: 0);
helloWorldTape.write(symbol: "hello world", at: 1);
helloWorldTape.write(symbol: "stop", at: 2);
```
### ๐ข 2. Count to Sixteen
Increments numbers from 0 to 16 and prints each value.
### ๐ป 3. Count Down to Zero
Decrements numbers from 16 to 0 and prints each value.
### ๐ฒ 4. Guess the Number
Asks user to guess a random number between 0 and 4.
### ๐ช 5. Quine
A self-replicating Turing program that outputs its own tape source.
---
## โ๏ธ Running the Emulator
### ๐งฑ Requirements
- Dart SDK โฅ 3.0
Install from [dart.dev](https://dart.dev/get-dart)
### โถ๏ธ Run
```bash
dart run main.dart
```
Youโll see several demo tapes execute in sequence, printing results to the console.
---
## ๐ง Architecture Overview
```text
FiniteStateControl
โโ handles opcodes and logic
โ
โโโ delegate โ TuringMachine (implements FiniteStateControlDelegate)
โ โโโ TapeHead (controls current position)
โ โโโ InfiniteTape (Map-based memory)
```
---
## ๐ Debug Options
Enable various debug flags at the top of the file:
```dart
const DEBUG_PRINT_ENABLED = true;
const DEBUG_MODE_ENABLED = true;
const DEBUG_PRINT_TAPE = true;
```
- `DEBUG_PRINT_ENABLED` โ Log every symbol and opcode
- `DEBUG_MODE_ENABLED` โ Step-by-step execution (press Enter to continue)
- `DEBUG_PRINT_TAPE` โ Print tape contents after each operation
---
## ๐งช Extending the Machine
To add a new opcode:
1. Define it as a new constant:
```dart
const OPCODE_NEW_OPERATION = "new operation";
```
2. Add it to the `opcodes` list.
3. Implement its behavior in `FiniteStateControl.handle()`.
---
## ๐งฐ Example Output
```
---Next Tape---
hello world
---Next Tape---
16
15
...
0
Finished!
---Next Tape---
Guess number (0-4):
> 2
Wrong!
Number: 3
---Next Tape---
????????????????
(copying and printing itself...)
```
---
## ๐ License
MIT License
Copyright (c) 2025
Free to use, modify, and experiment.
---
## ๐งโ๐ป Author
Developed by **Ilia Prokhorov**
A playful exploration of computation theory, recursion, and chaosโimplemented in Dart.