Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gervinfung/brainfuck
Brainfuck implementation in TypeScript
https://github.com/gervinfung/brainfuck
brainfuck interpreter nodejs
Last synced: 29 days ago
JSON representation
Brainfuck implementation in TypeScript
- Host: GitHub
- URL: https://github.com/gervinfung/brainfuck
- Owner: GervinFung
- Created: 2023-06-11T10:56:28.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-03T16:17:14.000Z (over 1 year ago)
- Last Synced: 2024-10-04T16:41:45.765Z (3 months ago)
- Topics: brainfuck, interpreter, nodejs
- Language: TypeScript
- Homepage:
- Size: 79.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Brainfuck Interpreter
This is a brainfuck interpreter that does the following
1. Tokenization
2. Parse Tokens as Nodes
3. Optimize aodesFrom here, it can either
1. Execute code on the fly
2. Output the instructions in another languageThe first option was completed
I might just output the Brainfuck code to Javascript
# Process
## Tokenization
Brainfuck actually doesn't need Tokenization strictly speaking, all I did in Tokenization phase is to identify pair bracket(s), if there is mismatch of brackets, error will be thrown. This will help in parsing the operations inside brackets as loop. Also, it tokenize similar operation
Notice that Brainfuck contains only 8 instructions, and each instruction has their own 'sibling'
`'+', '-', '>', '<', '[', ']', '.', and ','`
Instruction would be tokenzied as either of the following type```ts
{
type Operation = {
type: 'operation';
operation: 'plus' | 'minus';
};type Arrow = {
type: 'arrow';
direction: 'left' | 'right';
};type Bracket = {
type: 'bracket';
direction: 'left' | 'right';
};type Punctuation = {
type: 'punctuation';
punctuation: 'dot' | 'comma';
};
}
```## Parsing
Parsing involves removing unused tokens and creating loop nodes for operations inside paired brackets.
## Optimization
Optimization involves the following techniques:
1. Redundant move elimination:
Removes unnecessary operations that reset the value to 0 or shift the value when it reaches 255.2. Negate the value if negative:
Determines the sign of the operation (to left or to right) and assigns a corresponding value (-1 or 1).So, the following will transform to
```json
{
"type": "arrow",
"direction": "left"
}
```this
```json
{
"type": "arrow",
"index": -1
}
```3. Constant folding:
Replaces repetitive operations with a condensed representation.Consider the Brainfuck instructions below
`+++++<<<---++++>>>`
it will be transformed as the following pseudocode through constant folding
`5[+], 3[<], 3[-], 3[>]`
4. Combine Arrow and Operation Movement:
Combines consecutive arrow and operation movements into a single node for efficiency.Take the pseudocode from previous example,
`5[+], 3[<], 3[-], 3[>]`
it will be tranform to the json structure below
```json
{
"optimized": [
{ "type": "operation", "value": 5 },
{ "type": "arrow-operation", "index": -3, "value": 1 },
{ "type": "arrow", "index": 3 }
]
}
```# Execution
## Interpret
At the time of writing, I only support interpreting, running the code without emitting any code
## Code Emission
I might support code emission through JavaScript or WASM (maybe, who knows?)