An open API service indexing awesome lists of open source software.

https://github.com/zefir1990/turing-machine


https://github.com/zefir1990/turing-machine

Last synced: 6 days ago
JSON representation

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.