rockstar-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 27a99ba83138f3457b4521440148e8bc20eacef2da0d14f043ab1a09589a7d78
4
+ data.tar.gz: 3e254df9b3907098c8b9242f8a5ba749d5c5b380d66ccf9ebc9c36e392bd67c4
5
+ SHA512:
6
+ metadata.gz: 57fbaf1d47a76b50ef12e398f76e4b34c8e954c02d31aa6fdd441fe001b2e2ee6040b875d8394d08906dcb45d2e7d32c288be590700062d2e619ed47f470d0ff
7
+ data.tar.gz: 980ad60ca170d5dd714fda239b157f772d7821a959668864c1a2711f87f0be60ada3c05083cdcdf3bc6dcd25559d145b0de7cee554eccc91ab55641ba21cc2eb
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ *.iml
@@ -0,0 +1 @@
1
+ 2.5.1
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rockstar.gemspec
6
+ gemspec
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rockstar-ruby (0.1.0)
5
+ parslet
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ minitest (5.11.3)
11
+ parslet (1.8.2)
12
+ rake (10.5.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.16)
19
+ minitest (~> 5.0)
20
+ rake (~> 10.0)
21
+ rockstar-ruby!
22
+
23
+ BUNDLED WITH
24
+ 1.16.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alexander Teplyashin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Rockstar
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rockstar`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rockstar'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rockstar
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rockstar.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rockstar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rockstar"
4
+
5
+ ruby_translated = Rockstar.transpile(ARGV[0])
6
+ ruby_translated && File.write("rockstar.rb", ruby_translated)
@@ -0,0 +1,20 @@
1
+ require 'rockstar/version'
2
+ require 'parslet'
3
+ require 'rockstar/rockstar_parser'
4
+ require 'rockstar/rockstar_transformer'
5
+
6
+ module Rockstar
7
+ def self.parse(input)
8
+ RockstarParser.new.parse(input)
9
+ rescue Parslet::ParseFailed => failure
10
+ puts failure.parse_failure_cause.ascii_tree
11
+ end
12
+
13
+ def self.transform(tree)
14
+ RockstarTransformer.new.apply(tree)
15
+ end
16
+
17
+ def self.transpile(input)
18
+ transform(parse(input))
19
+ end
20
+ end
@@ -0,0 +1,77 @@
1
+ class RockstarParser < Parslet::Parser
2
+
3
+ root(:blocks)
4
+
5
+ # general
6
+ rule(:blocks) {(conditional_block | block).repeat(1).as(:blocks)}
7
+ rule(:eol) {match['\n']}
8
+ rule(:eof) {any.absent?}
9
+ rule(:space) {match('[ \t]').repeat(1)}
10
+ rule(:block) {(block_part >> (eol >> block_part).repeat >> (eol.repeat(2) | eof)).as(:block)}
11
+ rule(:block_part) {loop_block | increment | decrement | loop_continue | loop_break | poetic_literal}
12
+
13
+ # types
14
+ rule(:poetic_literal) {(poetic_string_literal | poetic_type_literal | poetic_number_literal).as(:var)}
15
+ rule(:poetic_string_literal) {proper_variable.as(:var_name) >> space >> str('says') >> space >> string_literal.as(:var_value)}
16
+ rule(:poetic_type_literal) {proper_variable.as(:var_name) >> space >> to_be >> type_literal.as(:var_value)}
17
+ rule(:poetic_number_literal) {proper_variable.as(:var_name) >> space >> to_be >> (math_expression | number_literal).as(:var_value)}
18
+
19
+ rule(:type_literal) {null_type_literal.as(:nil) | true_type_literal.as(:true) | false_type_literal.as(:false)}
20
+ rule(:true_type_literal) {str('true') | str('right') | str('yes') | str('ok')}
21
+ rule(:false_type_literal) {str('false') | str('wrong') | str('no') | str('lies')}
22
+ rule(:null_type_literal) {str('null') | str('nothing') | str('nowhere') | str('nobody')}
23
+
24
+ rule(:string_literal) {match("[^\n]").repeat(1).as(:str)}
25
+ rule(:number_literal) {(unique_variable_name >> (space >> unique_variable_name).repeat).as(:number_str)}
26
+
27
+ rule(:proper_variable) {match('[A-Z]') >> match('[a-z]').repeat >> (space >> proper_variable).repeat}
28
+ rule(:common_variable) {str('a') | str('an') | str('the') | str('my') | str('your')}
29
+ rule(:unique_variable_name) {(keyword.absent? >> match('[a-z]')).repeat(1) >> (space >> unique_variable_name).repeat}
30
+
31
+ rule(:to_be) {(str('is') | str('was') | str('were')) >> space}
32
+ rule(:keyword) {math_operation | comparison_operation | str('up') | str('down') | str('and')}
33
+
34
+ # math
35
+ rule(:plus) {(str('plus') | str('with')).as(:+)}
36
+ rule(:minus) {(str('minus') | str('without')).as(:-)}
37
+ rule(:multiply) {(str('times') | str('of')).as(:*)}
38
+ rule(:divide) {(str('over') | str('by')).as(:/)}
39
+ rule(:math_operation) {(minus | plus | multiply | divide).as(:op)}
40
+ rule(:math_expression) {unique_variable_name.as(:l_op) >> space >> math_operation >> space >> unique_variable_name.as(:r_op)}
41
+
42
+ # comparison
43
+ rule(:comparison_operand) {math_expression | unique_variable_name}
44
+ rule(:comparison) {(comparison_operand.as(:l_op) >> space >> comparison_operation >> space >> comparison_operand.as(:r_op)).as(:comparison)}
45
+ rule(:comparison_operation) {(greater.as(:>) | less.as(:<) | greater_or_equal.as(:>=) | less_or_equal.as(:<=) | not_equal.as(:!=) | equal.as(:==)).as(:comparison_op)}
46
+ rule(:greater) {equal >> space >> (str('higher') | str('greater') | str('bigger') | str('stronger')) >> space >> str('than')}
47
+ rule(:less) {equal >> space >> (str('lower') | str('less') | str('smaller') | str('weaker')) >> space >> str('than')}
48
+ rule(:greater_or_equal) {equal >> space >> str('as') >> space >> (str('high') | str('great') | str('big') | str('strong')) >> space >> str('as')}
49
+ rule(:less_or_equal) {equal >> space >> str('as') >> space >> (str('low') | str('little') | str('small') | str('weak')) >> space >> str('as')}
50
+ rule(:equal) {str('is')}
51
+ rule(:not_equal) {str("ain't") | (equal >> space >> str('not'))}
52
+
53
+ # conditionals
54
+ rule(:conditional_block) {(if_else_block | if_block.as(:if_block))}
55
+ rule(:if_block) {str('If') >> space >> comparison >> eol >> block}
56
+ rule(:else_block) {str('Else') >> space.maybe >> eol >> block}
57
+ rule(:if_else_block) {if_block.as(:if_block) >> else_block.as(:else_block)}
58
+
59
+ # loops
60
+ rule(:loop_block) {until_block.as(:until_block) | while_block.as(:while_block)}
61
+ rule(:until_block) {str('Until') >> space >> comparison >> eol >> block}
62
+ rule(:while_block) {str('While') >> space >> comparison >> eol >> block}
63
+ rule(:loop_break) {str('Break') | str('Break it down')}
64
+ rule(:loop_continue) {str('Continue') | str('Take it to the top')}
65
+ rule(:increment) {str('Build') >> space >> unique_variable_name.as(:increment) >> space >> str('up')}
66
+ rule(:decrement) {str('Knock') >> space >> unique_variable_name.as(:decrement) >> space >> str('down')}
67
+
68
+ # functions
69
+ rule(:function) do
70
+ proper_variable.as(:function_name) >> space >> str('takes') >> space >>
71
+ function_arg >> (space >> str('and') >> space >> function_arg).repeat >>
72
+ function_body.as(:function_body) >> function_return.as(:function_return)
73
+ end
74
+ rule(:function_arg) {unique_variable_name.as(:function_arg)}
75
+ rule(:function_body) {(conditional_block | loop_block | (block_part >> eol)).repeat(1)}
76
+ rule(:function_return) {str('Give back') >> space >> unique_variable_name >> (eol.repeat(2) | eof)}
77
+ end
@@ -0,0 +1,60 @@
1
+ class RockstarTransformer < Parslet::Transform
2
+ # types
3
+ rule(:true => simple(:_)) {'true'}
4
+ rule(:false => simple(:_)) {'false'}
5
+ rule(:nil => simple(:_)) {'nil'}
6
+ rule(:str => simple(:str)) {"'#{str.to_s.gsub("'", "\\'")}'"}
7
+ rule(:number_str => simple(:str)) {str.to_s.split(/\s+/).map {|e| e.length % 10}.join.to_i}
8
+
9
+ # math
10
+ rule(:- => simple(:_)) {'-'}
11
+ rule(:+ => simple(:_)) {'+'}
12
+ rule(:* => simple(:_)) {'*'}
13
+ rule(:/ => simple(:_)) {'/'}
14
+ rule(:l_op => simple(:left), :op => simple(:operation), :r_op => simple(:right)) do |context|
15
+ "#{to_snake_case(context[:left])}.to_f#{context[:operation]}#{to_snake_case(context[:right])}.to_f"
16
+ end
17
+
18
+ # assignment
19
+ rule(:var => {:var_name => simple(:name), :var_value => simple(:value)}) do |context|
20
+ "#{to_snake_case(context[:name])}=#{context[:value]};"
21
+ end
22
+
23
+ # comparison
24
+ rule(:> => simple(:_)) {'>'}
25
+ rule(:< => simple(:_)) {'<'}
26
+ rule(:>= => simple(:_)) {'>='}
27
+ rule(:<= => simple(:_)) {'<='}
28
+ rule(:== => simple(:_)) {'=='}
29
+ rule(:!= => simple(:_)) {'!='}
30
+ rule(:l_op => simple(:left), :comparison_op => simple(:operation), :r_op => simple(:right)) do |context|
31
+ "((#{to_snake_case(context[:left])}).to_f#{context[:operation]}(#{to_snake_case(context[:right])}).to_f)"
32
+ end
33
+ rule(:comparison => simple(:comparison), :block => simple(:block)) {"#{comparison};#{block}"}
34
+
35
+ # conditionals
36
+ rule(:if_block => simple(:if_blk), :else_block => simple(:else_blk)) {"if#{if_blk}else;#{else_blk}end;"}
37
+ rule(:if_block => simple(:if_blk)) {"if#{if_blk}end;"}
38
+
39
+ # loops
40
+ rule(:increment => simple(:increment)) do |context|
41
+ var_name = to_snake_case(context[:increment])
42
+ "#{var_name}=#{var_name}.to_f+1;"
43
+ end
44
+ rule(:decrement => simple(:decrement)) do |context|
45
+ var_name = to_snake_case(context[:decrement])
46
+ "#{var_name}=#{var_name}.to_f-1;"
47
+ end
48
+ rule(:comparison => simple(:comparison)) {"#{comparison};"}
49
+ rule(:until_block => simple(:block)) {"until#{block}end;"}
50
+ rule(:while_block => simple(:block)) {"while#{block}end;"}
51
+
52
+ # general
53
+ rule(:block => simple(:blk)) {blk}
54
+ rule(:block => sequence(:lines)) {lines.join}
55
+ rule(:blocks => sequence(:blocks)) {blocks.join}
56
+
57
+ def self.to_snake_case(str)
58
+ str.to_s.downcase.gsub(/\s+/, '_')
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Rockstar
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rockstar/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rockstar-ruby"
8
+ spec.version = Rockstar::VERSION
9
+ spec.authors = ["Alexander Teplyashin"]
10
+ spec.email = ["scizor46@gmail.com"]
11
+
12
+ spec.summary = %q{Rockstar-Ruby transpiler gem}
13
+ spec.homepage = "https://github.com/scizor666/rockstar-ruby"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "minitest", "~> 5.0"
37
+ spec.add_dependency "parslet"
38
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rockstar-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Teplyashin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: parslet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - scizor46@gmail.com
72
+ executables:
73
+ - rockstar-ruby
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/rockstar-ruby
88
+ - lib/rockstar.rb
89
+ - lib/rockstar/rockstar_parser.rb
90
+ - lib/rockstar/rockstar_transformer.rb
91
+ - lib/rockstar/version.rb
92
+ - rockstar.gemspec
93
+ homepage: https://github.com/scizor666/rockstar-ruby
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Rockstar-Ruby transpiler gem
117
+ test_files: []