rubyfuu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 46c420fbf375a8c940ab1320624261406b4e4c4b
4
+ data.tar.gz: ab32f7c015e7b6734da321785776309be268cc41
5
+ SHA512:
6
+ metadata.gz: 51dbd5bd556434445004cad960c6c93fb091e6a9382257ea0498733250f445c80813c3a9e6a801467fd4a74757afd43967f673e8afdc72206c33502057a70a59
7
+ data.tar.gz: c862853ae7e61277fb6b6b78a7ccccc8ab598bc365d9046870063e0c397d79beac64c5a075d749d6de912e8d29b67f98bd1f200c9b0c47f7422b8a51f1ab14cd
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubyfuu.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jan Votava
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Rubyfuu
2
+
3
+ Ruby compiler for Brainfuck programming language. Currently implemented only to
4
+ generate 32bit OSX binaries.
5
+
6
+ ## Installation
7
+
8
+ $ gem install rubyfuu
9
+
10
+ ## Usage
11
+
12
+ $ rubyfuu compile hello_world.b
13
+ $ ./hello_world
14
+
15
+ ...or...
16
+
17
+ $ rubyfuu execute hello_world.b
18
+
19
+ ## Todo
20
+
21
+ - Support for other platforms
22
+ - More options
23
+ - Cleanup
24
+
25
+ ## FAQ
26
+
27
+ #### Why?!
28
+
29
+ Why not?
30
+
31
+ ## Authors
32
+
33
+ Jan Votava (http://deployment.cz) from fine folks at http://sensible.io
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/cyner/rubyfuu/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/rubyfuu ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubyfuu/cli'
4
+
5
+ Rubyfuu::CLI.start
@@ -0,0 +1,65 @@
1
+ module Rubyfuu
2
+ class Assembly < Compiler
3
+ def initialize(*params)
4
+ super
5
+
6
+ @loop_counter = 0
7
+ @stack = []
8
+ end
9
+
10
+ def >
11
+ write 'inc %edi'
12
+ end
13
+
14
+ def <
15
+ write 'dec %edi'
16
+ end
17
+
18
+ def +
19
+ write 'incb (%edi)'
20
+ end
21
+
22
+ def -
23
+ write 'decb (%edi)'
24
+ end
25
+
26
+ def comma
27
+ write 'push $0x1'
28
+ write 'push %edi'
29
+ write 'push $0x1'
30
+
31
+ write 'mov $0x3, %eax'
32
+ write 'subl $0x4, %esp'
33
+ write 'int $0x80'
34
+ write 'addl $16, %esp'
35
+ end
36
+
37
+ def period
38
+ write 'push $0x1'
39
+ write 'push %edi'
40
+ write 'push $0x1'
41
+
42
+ write 'mov $0x4, %eax'
43
+ write 'subl $0x4, %esp'
44
+ write 'int $0x80'
45
+ write 'addl $16, %esp'
46
+ end
47
+
48
+ def left_bracket
49
+ loop = @loop_counter+=1
50
+ @stack << loop
51
+
52
+ write 'cmpb $0, (%edi)'
53
+ write "jz .LE#{loop}"
54
+ write ".LS#{loop}:", false
55
+ end
56
+
57
+ def right_bracket
58
+ loop = @stack.pop
59
+
60
+ write 'cmpb $0, (%edi)'
61
+ write "jnz .LS#{loop}"
62
+ write ".LE#{loop}:", false
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubyfuu'
2
+ require 'thor'
3
+
4
+ module Rubyfuu
5
+ class CLI < Thor
6
+ desc "compile PATH", "Compile file & output it to the current directory"
7
+ def compile(path)
8
+ builder = Rubyfuu::Builder.new
9
+ with_rescue { builder.build(path) }
10
+ end
11
+
12
+ desc "execute PATH", "Compile file and run it from temporary location"
13
+ def execute(path)
14
+ builder = Rubyfuu::Builder.new
15
+ with_rescue { builder.run(path) }
16
+ end
17
+
18
+ private
19
+ def with_rescue(&block)
20
+ yield
21
+ rescue Errno::ENOENT
22
+ puts "File does not exist."
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ module Rubyfuu
2
+ class Compiler
3
+ OPERATORS = {
4
+ '+' => nil,
5
+ '-' => nil,
6
+ '>' => nil,
7
+ '<' => nil,
8
+ ',' => 'comma',
9
+ '.' => 'period',
10
+ '[' => 'left_bracket',
11
+ ']' => 'right_bracket'
12
+ }
13
+
14
+ def initialize(readable, writable)
15
+ @readable = readable
16
+ @writable = writable
17
+ end
18
+
19
+ def write(*params)
20
+ @writable.write(*params)
21
+ end
22
+
23
+ def build(outfile)
24
+ link(compile, outfile)
25
+ end
26
+
27
+ def link
28
+ raise Error::NotImplemented
29
+ end
30
+
31
+ def compile
32
+ header
33
+ while c = @readable.getc
34
+ if OPERATORS.has_key?(c)
35
+ parse(c)
36
+ end
37
+ end
38
+ footer
39
+ @writable.close
40
+ @writable.path
41
+ end
42
+
43
+ def parse(char)
44
+ public_send method_name(char)
45
+ end
46
+
47
+ def method_name(char)
48
+ OPERATORS.each do |from,to|
49
+ char.gsub!(from, to) if to
50
+ end
51
+ char
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,31 @@
1
+ require 'tempfile'
2
+
3
+ module Rubyfuu
4
+ class FileWriter
5
+ def initialize(target = :temp)
6
+ if target == :temp
7
+ @file = Tempfile.new("out.S")
8
+ else
9
+ @file = File.open(target, "w")
10
+ end
11
+ end
12
+
13
+ def write(string, tab = true)
14
+ @file.write "\t" if tab
15
+ @file.write string
16
+ @file.write "\n"
17
+ end
18
+
19
+ def path
20
+ @file.path
21
+ end
22
+
23
+ def close
24
+ @file.close
25
+ end
26
+
27
+ def unlink
28
+ @file.unlink
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module Rubyfuu
2
+ class OSXAssembly < Assembly
3
+ end
4
+ end
@@ -0,0 +1,30 @@
1
+ module Rubyfuu
2
+ class OSXAssembly32 < Assembly
3
+ def header
4
+ write <<EOF
5
+ .lcomm buffer1, 10000
6
+
7
+ .globl _main
8
+ _main:
9
+ \tmov $buffer1, %edi
10
+ EOF
11
+ end
12
+
13
+ def footer
14
+ write <<EOF
15
+ \tpush $0
16
+ \tmov $0x1, %eax
17
+ \tsubl $0x4, %esp
18
+ \tint $0x80
19
+ EOF
20
+ end
21
+
22
+ def link(source_path, outfile)
23
+ tempfile = Tempfile.new(["out", ".o"])
24
+ tempfile.close
25
+
26
+ system "as -arch i386 -o #{tempfile.path} #{source_path} && ld -e _main -macosx_version_min 10.7.0 -o #{outfile} #{tempfile.path}"
27
+ tempfile.unlink
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Rubyfuu
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rubyfuu.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'rubyfuu/version'
2
+
3
+ require 'rubyfuu/file_writer'
4
+
5
+ require 'rubyfuu/compiler'
6
+ require 'rubyfuu/assembly'
7
+ require 'rubyfuu/osx_assembly'
8
+ require 'rubyfuu/osx_assembly32'
9
+
10
+ require 'tempfile'
11
+
12
+ module Rubyfuu
13
+ class Builder
14
+ def initialize(compiler = OSXAssembly32)
15
+ @compiler = compiler
16
+ end
17
+
18
+ def build(source_path, destination = nil)
19
+ base = source_path.chomp(File.extname(source_path) )
20
+
21
+ destination ||= File.basename(base)
22
+
23
+ writer = FileWriter.new
24
+ reader = File.open(source_path, 'r')
25
+
26
+ compiler = @compiler.new(reader, writer)
27
+ compiler.build(destination)
28
+ end
29
+
30
+ def run(source_path)
31
+ tmp = Tempfile.new("out")
32
+ tmp.close
33
+ out_path = tmp.path
34
+ build(source_path, out_path)
35
+ system out_path
36
+ tmp.unlink
37
+ end
38
+ end
39
+ end
data/rubyfuu.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubyfuu/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubyfuu"
8
+ spec.version = Rubyfuu::VERSION
9
+ spec.authors = ["Jan Votava"]
10
+ spec.email = ["votava@deployment.cz"]
11
+ spec.summary = %q{Brainfuck compiler written in Ruby}
12
+ spec.description = %q{Brainfuck compiler written in Ruby - highly experimental and for fun}
13
+ spec.homepage = "http://github.com/cyner/rubyfuu"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'thor', '~> 0.19', '>= 0.19.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyfuu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan Votava
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.19.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.19'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.19.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.6'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.6'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ description: Brainfuck compiler written in Ruby - highly experimental and for fun
62
+ email:
63
+ - votava@deployment.cz
64
+ executables:
65
+ - rubyfuu
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - ".gitignore"
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bin/rubyfuu
75
+ - lib/rubyfuu.rb
76
+ - lib/rubyfuu/assembly.rb
77
+ - lib/rubyfuu/cli.rb
78
+ - lib/rubyfuu/compiler.rb
79
+ - lib/rubyfuu/file_writer.rb
80
+ - lib/rubyfuu/osx_assembly.rb
81
+ - lib/rubyfuu/osx_assembly32.rb
82
+ - lib/rubyfuu/version.rb
83
+ - rubyfuu.gemspec
84
+ homepage: http://github.com/cyner/rubyfuu
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Brainfuck compiler written in Ruby
108
+ test_files: []