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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a02ecd6a1f5c72ccb430be68372025befe1cd7357b2880ac27a03f1e812b5cfd
4
- data.tar.gz: 50517d11de40c380f7ec27a0612100d45bae5d2d5cfb86d520fd3c4308c4808b
3
+ metadata.gz: 127e1b439735aa67d950f37b652572444bd5a18adcbbf39c83eba03fc0e4c5c8
4
+ data.tar.gz: 3c8dd2547e787247408167d8b6d8b44d21504fa473b6859a4b3873d45a9bc2c6
5
5
  SHA512:
6
- metadata.gz: 7aeb71711fc32a6a953f29aaf2ba0f24d900f8c8a3a70e36a2357d90cabfab5cb2d4fa9ea914ff42e6a4d7638e595000357a06b549a0abcab3f338dd55ea86d4
7
- data.tar.gz: 5cf92fed3d93e4f59d4de7c8d8be43b2133890f5cc1419d9c432b45cd7b39376dc5aed018681e28424c3ed1967b5f3a3408a8bce609969c58a39ca3a3fa3b367
6
+ metadata.gz: 4da859f5322c499d2240230f6172e24139e6d07308a4ee114ee7ce1c1318689bff84727d8010f863532b949f54d52df58ecf6c780f01ade45ea2435c61b4d333
7
+ data.tar.gz: fef4a3ab248952d1dee8f05726dfca6b014aa0bb48012fc38b7f62da65305c8c727971fb2a0f6ff29c06fdc510031f27cb1ce2f2f7d973515c6e48abb052bba1
data/bin/brainfunc CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  require_relative "../lib/brainfunc.rb"
4
- Brainfunc::VM.new.exec(ARGF.read)
4
+ Brainfunc::VM::Exec.call(ARGF.read)
data/bin/ibrainfunc CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  require_relative "../lib/brainfunc.rb"
4
- Brainfunc::Repl.new.run
4
+ Brainfunc::Repl::Run.()
@@ -1,26 +1,19 @@
1
1
  module Brainfunc
2
- class Repl
3
- attr_accessor :vm, :state
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
- exit(0)
9
+ break
17
10
  when "exit"
18
- exit(0)
11
+ break
19
12
  else
20
- @state = @machine.eval(instruction, @state)
21
- pp @state
13
+ state = Brainfunc::VM::Exec.call(instruction, (state || {}))
22
14
  end
23
15
  end
24
- end
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
- class VM
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
- def exec(source)
33
- vm_state = { memory: Hash.new { 0 }, inst_p: 0, data_p: 0, stack: [], program: source.strip.chars }
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
- .merge(vm_state) { |k, defval, setval| k == :program ? defval : setval }
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
- end
11
+ }
50
12
 
51
- def debug(machine)
13
+ Debug = ->(machine) {
52
14
  { m: machine.slice(:memory, :data_p, :inst_p), in: machine[:program][machine[:inst_p]], stack: machine[:stack] }
53
- end
15
+ }
54
16
  end
55
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainfunc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sreedev Kodichath