brainfunc 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/brainfunc +1 -1
- data/bin/ibrainfunc +1 -1
- data/lib/brainfunc/repl.rb +8 -15
- data/lib/brainfunc/vm.rb +7 -45
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 127e1b439735aa67d950f37b652572444bd5a18adcbbf39c83eba03fc0e4c5c8
|
4
|
+
data.tar.gz: 3c8dd2547e787247408167d8b6d8b44d21504fa473b6859a4b3873d45a9bc2c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4da859f5322c499d2240230f6172e24139e6d07308a4ee114ee7ce1c1318689bff84727d8010f863532b949f54d52df58ecf6c780f01ade45ea2435c61b4d333
|
7
|
+
data.tar.gz: fef4a3ab248952d1dee8f05726dfca6b014aa0bb48012fc38b7f62da65305c8c727971fb2a0f6ff29c06fdc510031f27cb1ce2f2f7d973515c6e48abb052bba1
|
data/bin/brainfunc
CHANGED
data/bin/ibrainfunc
CHANGED
data/lib/brainfunc/repl.rb
CHANGED
@@ -1,26 +1,19 @@
|
|
1
1
|
module Brainfunc
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@machine = Brainfunc::VM.new
|
7
|
-
@state = {}
|
2
|
+
module Repl
|
3
|
+
Run = ->() {
|
8
4
|
puts("Brainfunc - Functional Brainfuck Interpreter")
|
9
|
-
end
|
10
|
-
|
11
|
-
def run
|
12
5
|
loop do
|
13
6
|
print("-> ")
|
14
|
-
case (instruction=gets.strip.chomp)
|
7
|
+
case (instruction = gets.strip.chomp)
|
15
8
|
when "quit"
|
16
|
-
|
9
|
+
break
|
17
10
|
when "exit"
|
18
|
-
|
11
|
+
break
|
19
12
|
else
|
20
|
-
|
21
|
-
pp @state
|
13
|
+
state = Brainfunc::VM::Exec.call(instruction, (state || {}))
|
22
14
|
end
|
23
15
|
end
|
24
|
-
|
16
|
+
exit(0)
|
17
|
+
}
|
25
18
|
end
|
26
19
|
end
|
data/lib/brainfunc/vm.rb
CHANGED
@@ -1,55 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Brainfunc
|
4
|
-
|
5
|
-
def ops
|
6
|
-
@ops ||= (Hash.new { ->(state) { state.merge({inst_p: state[:inst_p] + 1}) } }).merge({
|
7
|
-
?> => ->(state) { state.merge({data_p: state[:data_p]+1, inst_p: state[:inst_p] + 1}) },
|
8
|
-
?< => ->(state) { state.merge({data_p: state[:data_p]-1, inst_p: state[:inst_p] + 1}) },
|
9
|
-
?+ => ->(state) { state.merge({memory: state[:memory].merge(state[:data_p] => (state[:memory][state[:data_p]]+1 % 127).abs), inst_p: state[:inst_p] + 1}) },
|
10
|
-
?- => ->(state) { state.merge({memory: state[:memory].merge(state[:data_p] => (state[:memory][state[:data_p]]-1 % 127).abs), inst_p: state[:inst_p] + 1}) },
|
11
|
-
?. => ->(state) { print(state[:memory][state[:data_p]].chr); state.merge({inst_p: state[:inst_p] + 1}) },
|
12
|
-
?, => ->(state) { state.merge({memory: state[:memory].merge({ state[:data_p] => STDIN.getch.bytes[0] }), inst_p: state[:inst_p] + 1}) },
|
13
|
-
?[ => ->(state) {
|
14
|
-
state[:stack].push(state[:inst_p]) && ((
|
15
|
-
state[:memory][state[:data_p]].zero? && state.merge(
|
16
|
-
inst_p: state[:program]
|
17
|
-
.slice(state[:inst_p]..-1)
|
18
|
-
.filter_map.with_index { |el, index| index + 1 if el == "]" }[state[:stack].size]
|
19
|
-
)
|
20
|
-
) || state.merge({inst_p: state[:inst_p] + 1}))
|
21
|
-
},
|
22
|
-
?] => ->(state) {
|
23
|
-
(matching_paren=state[:stack].pop) && ((
|
24
|
-
!state[:memory][state[:data_p]].zero? &&
|
25
|
-
state.merge({inst_p: matching_paren})
|
26
|
-
) || state.merge({inst_p: state[:inst_p] + 1}))
|
27
|
-
},
|
28
|
-
?# => ->(state) { puts(debug(state)) || state.merge({inst_p: state[:inst_p] + 1}) }
|
29
|
-
})
|
30
|
-
end
|
4
|
+
require_relative "functions"
|
31
5
|
|
32
|
-
|
33
|
-
|
34
|
-
loop { (vm_state=ops[vm_state[:program][vm_state[:inst_p]]].(vm_state)) && (vm_state[:inst_p] >= vm_state[:program].length && break) }
|
35
|
-
vm_state
|
36
|
-
end
|
37
|
-
|
38
|
-
def eval(source, vm_state = {})
|
6
|
+
module VM
|
7
|
+
Exec = ->(source, vm_state = {}) {
|
39
8
|
vm_state = { memory: Hash.new { 0 }, inst_p: 0, data_p: 0, stack: [], program: (vm_state[:program] || []) + source.strip.chars }
|
40
|
-
|
41
|
-
|
42
|
-
loop do
|
43
|
-
vm_state = ops[vm_state[:program][vm_state[:inst_p]]].(vm_state)
|
44
|
-
|
45
|
-
break if vm_state[:inst_p] >= vm_state[:program].length
|
46
|
-
end
|
47
|
-
|
9
|
+
loop { (vm_state=Brainfunc::Functions[vm_state[:program][vm_state[:inst_p]]].(vm_state)) && (vm_state[:inst_p] >= vm_state[:program].length && break) }
|
48
10
|
vm_state
|
49
|
-
|
11
|
+
}
|
50
12
|
|
51
|
-
|
13
|
+
Debug = ->(machine) {
|
52
14
|
{ m: machine.slice(:memory, :data_p, :inst_p), in: machine[:program][machine[:inst_p]], stack: machine[:stack] }
|
53
|
-
|
15
|
+
}
|
54
16
|
end
|
55
17
|
end
|