brainfunc 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '08151903e89f79497698dbbc770c16dd1914a855004bcd1062864573f586df02'
4
+ data.tar.gz: 24e27debd03d41327e03ddca31923c1810d9f8137d4913dc4b0fb37c38ffba88
5
+ SHA512:
6
+ metadata.gz: c352e0c9014adb1c60bff8312fcb1d8d9cfc5dd79b21329482844bf9d3189f1e4870348553bdd4ac4214ff11a513e991e9e513246425abf4b1e5c97a8163bcc3
7
+ data.tar.gz: 739c515d4a24a4db0e379af6f7bf3152f93f5316fa6d52b2d8a884d0fb5dabb3e1fa0682fd6435464d60a70c6f35956362ac8eda2a66c6f73b2af23a31f4a4de
data/bin/brainfunc ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require_relative "../lib/brainfunc.rb"
4
+ Brainfunc::VM.new.exec(ARGF.read)
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
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
31
+
32
+ def exec(source)
33
+ run({ memory: Hash.new { 0 }, inst_p: 0, data_p: 0, stack: [], program: source.strip.chars })
34
+ end
35
+
36
+ def run(machine)
37
+ loop { (machine=ops[machine[:program][machine[:inst_p]]].(machine)) && (machine[:inst_p] >= machine[:program].length && break) }
38
+ end
39
+
40
+ def debug(machine)
41
+ { m: machine.slice(:memory, :data_p, :inst_p), in: machine[:program][machine[:inst_p]], stack: machine[:stack] }
42
+ end
43
+ end
44
+ end
data/lib/brainfunc.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brainfunc
4
+ require_relative "brainfunc/vm"
5
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brainfunc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sreedev Kodichath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tiny, Performant, Functional Brainfuck interpreter
14
+ email: sreedevpadmakumar@gmail.com
15
+ executables:
16
+ - brainfunc
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/brainfunc
21
+ - lib/brainfunc.rb
22
+ - lib/brainfunc/vm.rb
23
+ homepage: https://www.sree.dev
24
+ licenses:
25
+ - MIT
26
+ metadata:
27
+ source_code_uri: https://github.com/sreedevk/brainfunc
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: 3.0.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.2.22
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Tiny, Performant Brainfuck interpreter implemented in the functional paradigm.
47
+ test_files: []