Skip to content

Instantly share code, notes, and snippets.

@thomasloven
Last active March 31, 2025 06:30
Show Gist options
  • Save thomasloven/f571549c08393a3687dbc5aa90af26c4 to your computer and use it in GitHub Desktop.
Save thomasloven/f571549c08393a3687dbc5aa90af26c4 to your computer and use it in GitHub Desktop.
A Home Assistant script which interprets brainfuck. Because science isn't about why, it's about why not!
sequence:
- variables:
jumplist: []
cells: "{{[0]*10}}"
cell_ptr: 0
ins: 0
program: >-
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.-
output: ""
jumps: "{{[0]*(program|length)}}"
- repeat:
sequence:
- variables:
instruction: "{{program[ins]}}"
- choose:
- conditions:
- condition: template
value_template: "{{instruction == \"[\"}}"
sequence:
- variables:
jumplist: "{{jumplist + [ins]}}"
- conditions:
- condition: template
value_template: "{{instruction == \"]\"}}"
sequence:
- action: system_log.write
data:
level: error
message: Jump back from {{ins}} to {{jumplist[-1]}} {{jumplist}}
enabled: false
- variables:
opening: "{{jumplist[-1]}}"
jumplist: "{{jumplist[:-1]}}"
jumps: >-
{{jumps[:opening] + [ins] + jumps[opening+1:ins] +
[opening] + jumps[ins+1:]}}
- action: system_log.write
data:
level: error
message: "{{jumps}}"
enabled: false
- variables:
ins: "{{ins + 1}}"
while:
- condition: template
value_template: "{{(ins|int(0)) < (program|length)-1}}"
- variables:
ins: 0
- repeat:
sequence:
- variables:
instruction: "{{program[ins]}}"
- choose:
- conditions:
- condition: template
value_template: "{{instruction == \">\"}}"
sequence:
- variables:
cell_ptr: "{{cell_ptr + 1}}"
- conditions:
- condition: template
value_template: "{{instruction == \"<\"}}"
sequence:
- variables:
cell_ptr: "{{cell_ptr - 1}}"
- conditions:
- condition: template
value_template: "{{instruction == \"+\"}}"
sequence:
- variables:
cells: >-
{{cells[:cell_ptr] + [cells[cell_ptr]+1] +
cells[cell_ptr+1:]}}
- conditions:
- condition: template
value_template: "{{instruction == \"-\"}}"
sequence:
- variables:
cells: >-
{{cells[:cell_ptr] + [cells[cell_ptr]-1] +
cells[cell_ptr+1:]}}
- conditions:
- condition: template
value_template: "{{instruction == \".\"}}"
sequence:
- variables:
output: >-
{{output +
'_!.#$%&.()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[#]^_#abcdefghijklmnopqrstuvwxyz{|}~'[cells[cell_ptr]-32]}}
- conditions:
- condition: template
value_template: "{{instruction == \"[\" and cells[cell_ptr] == 0}}"
sequence:
- variables:
ins: "{{jumps[ins]-1}}"
- conditions:
- condition: template
value_template: "{{(instruction == \"]\") and (cells[cell_ptr] != 0)}}"
sequence:
- variables:
ins: "{{jumps[ins]-1}}"
- action: system_log.write
data:
level: error
message: "Instruction ({{ins}}): {{program[ins]}} ({{cell_ptr}}) {{cells}}"
enabled: false
- variables:
ins: "{{ins+1}}"
while:
- condition: template
value_template: "{{(ins|int(0)) < (program|length)-1}}"
enabled: true
- action: system_log.write
data:
level: error
message: "output: {{output}}"
- action: persistent_notification.create
data:
message: "{{output}}"
enabled: true
alias: bf
description: ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment