ShyRubyJS 0.0.0
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.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +33 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +54 -0
- data/ShyRubyJS.gemspec +63 -0
- data/VERSION +1 -0
- data/lib/ShyRubyJS.rb +192 -0
- data/test/examples.rb +142 -0
- data/test/helper.rb +18 -0
- data/test/test_ShyRubyJS.rb +48 -0
- metadata +121 -0
    
        data/.document
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    | @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            source "http://rubygems.org"
         | 
| 2 | 
            +
            # Add dependencies required to use your gem here.
         | 
| 3 | 
            +
            # Example:
         | 
| 4 | 
            +
            #   gem "activesupport", ">= 2.3.5"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # Add dependencies to develop your gem here.
         | 
| 7 | 
            +
            # Include everything needed to run rake, tests, features, etc.
         | 
| 8 | 
            +
            group :development do
         | 
| 9 | 
            +
              gem "bundler", "~> 1.0.0"
         | 
| 10 | 
            +
              gem "jeweler", "~> 1.6.4"
         | 
| 11 | 
            +
              gem "rcov", ">= 0"
         | 
| 12 | 
            +
              gem "sourcify", "~> 0.5.0"
         | 
| 13 | 
            +
            end
         | 
    
        data/Gemfile.lock
    ADDED
    
    | @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            GEM
         | 
| 2 | 
            +
              remote: http://rubygems.org/
         | 
| 3 | 
            +
              specs:
         | 
| 4 | 
            +
                file-tail (1.0.6)
         | 
| 5 | 
            +
                  spruz (~> 0.2)
         | 
| 6 | 
            +
                git (1.2.5)
         | 
| 7 | 
            +
                jeweler (1.6.4)
         | 
| 8 | 
            +
                  bundler (~> 1.0)
         | 
| 9 | 
            +
                  git (>= 1.2.5)
         | 
| 10 | 
            +
                  rake
         | 
| 11 | 
            +
                rake (0.8.7)
         | 
| 12 | 
            +
                rcov (0.9.10)
         | 
| 13 | 
            +
                ruby2ruby (1.2.5)
         | 
| 14 | 
            +
                  ruby_parser (~> 2.0)
         | 
| 15 | 
            +
                  sexp_processor (~> 3.0)
         | 
| 16 | 
            +
                ruby_parser (2.0.6)
         | 
| 17 | 
            +
                  sexp_processor (~> 3.0)
         | 
| 18 | 
            +
                sexp_processor (3.0.5)
         | 
| 19 | 
            +
                sourcify (0.5.0)
         | 
| 20 | 
            +
                  file-tail (>= 1.0.5)
         | 
| 21 | 
            +
                  ruby2ruby (>= 1.2.5)
         | 
| 22 | 
            +
                  ruby_parser (>= 2.0.5)
         | 
| 23 | 
            +
                  sexp_processor (>= 3.0.5)
         | 
| 24 | 
            +
                spruz (0.2.12)
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            PLATFORMS
         | 
| 27 | 
            +
              ruby
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            DEPENDENCIES
         | 
| 30 | 
            +
              bundler (~> 1.0.0)
         | 
| 31 | 
            +
              jeweler (~> 1.6.4)
         | 
| 32 | 
            +
              rcov
         | 
| 33 | 
            +
              sourcify (~> 0.5.0)
         | 
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            Copyright (c) 2011 Shy Inc. (DANIEL BRYAN and PETER BENTLEY)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 4 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 5 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 6 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 7 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 8 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 9 | 
            +
            the following conditions:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 12 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 15 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 16 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 17 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 18 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 19 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 20 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.rdoc
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            = ShyRubyJS
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Description goes here.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            == Contributing to ShyRubyJS
         | 
| 6 | 
            +
             
         | 
| 7 | 
            +
            * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
         | 
| 8 | 
            +
            * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
         | 
| 9 | 
            +
            * Fork the project
         | 
| 10 | 
            +
            * Start a feature/bugfix branch
         | 
| 11 | 
            +
            * Commit and push until you are happy with your contribution
         | 
| 12 | 
            +
            * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
         | 
| 13 | 
            +
            * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            == Copyright
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            Copyright (c) 2011 Cerales. See LICENSE.txt for
         | 
| 18 | 
            +
            further details.
         | 
| 19 | 
            +
             | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,54 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'rubygems'
         | 
| 4 | 
            +
            require 'bundler'
         | 
| 5 | 
            +
            begin
         | 
| 6 | 
            +
              Bundler.setup(:default, :development)
         | 
| 7 | 
            +
            rescue Bundler::BundlerError => e
         | 
| 8 | 
            +
              $stderr.puts e.message
         | 
| 9 | 
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         | 
| 10 | 
            +
              exit e.status_code
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
            require 'rake'
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            require 'jeweler'
         | 
| 15 | 
            +
            Jeweler::Tasks.new do |gem|
         | 
| 16 | 
            +
              # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
         | 
| 17 | 
            +
              gem.name = "ShyRubyJS"
         | 
| 18 | 
            +
              gem.homepage = "http://github.com/Cerales/ShyRubyJS"
         | 
| 19 | 
            +
              gem.license = "MIT"
         | 
| 20 | 
            +
              gem.summary = %Q{Little library to convert Ruby blocks into JavaScript code, via S Expressions. Work in progress.}
         | 
| 21 | 
            +
              gem.description = %Q{Little library to convert Ruby blocks into JavaScript code, via S Expressions. Work in progress.}
         | 
| 22 | 
            +
              gem.email = "danbryan@gmail.com"
         | 
| 23 | 
            +
              gem.authors = ["Shy Inc.", "Daniel Bryan", "Cerales"]
         | 
| 24 | 
            +
              gem.add_dependency "sourcify", "~> 0"
         | 
| 25 | 
            +
              # dependencies defined in Gemfile
         | 
| 26 | 
            +
            end
         | 
| 27 | 
            +
            Jeweler::RubygemsDotOrgTasks.new
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            require 'rake/testtask'
         | 
| 30 | 
            +
            Rake::TestTask.new(:test) do |test|
         | 
| 31 | 
            +
              test.libs << 'lib' << 'test'
         | 
| 32 | 
            +
              test.pattern = 'test/**/test_*.rb'
         | 
| 33 | 
            +
              test.verbose = true
         | 
| 34 | 
            +
            end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            require 'rcov/rcovtask'
         | 
| 37 | 
            +
            Rcov::RcovTask.new do |test|
         | 
| 38 | 
            +
              test.libs << 'test'
         | 
| 39 | 
            +
              test.pattern = 'test/**/test_*.rb'
         | 
| 40 | 
            +
              test.verbose = true
         | 
| 41 | 
            +
              test.rcov_opts << '--exclude "gems/*"'
         | 
| 42 | 
            +
            end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            task :default => :test
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            require 'rake/rdoctask'
         | 
| 47 | 
            +
            Rake::RDocTask.new do |rdoc|
         | 
| 48 | 
            +
              version = File.exist?('VERSION') ? File.read('VERSION') : ""
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              rdoc.rdoc_dir = 'rdoc'
         | 
| 51 | 
            +
              rdoc.title = "ShyRubyJS #{version}"
         | 
| 52 | 
            +
              rdoc.rdoc_files.include('README*')
         | 
| 53 | 
            +
              rdoc.rdoc_files.include('lib/**/*.rb')
         | 
| 54 | 
            +
            end
         | 
    
        data/ShyRubyJS.gemspec
    ADDED
    
    | @@ -0,0 +1,63 @@ | |
| 1 | 
            +
            # Generated by jeweler
         | 
| 2 | 
            +
            # DO NOT EDIT THIS FILE DIRECTLY
         | 
| 3 | 
            +
            # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
         | 
| 4 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |s|
         | 
| 7 | 
            +
              s.name = %q{ShyRubyJS}
         | 
| 8 | 
            +
              s.version = "0.0.0"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 | 
            +
              s.authors = [%q{Shy Inc.}, %q{Daniel Bryan}, %q{Cerales}]
         | 
| 12 | 
            +
              s.date = %q{2011-08-14}
         | 
| 13 | 
            +
              s.description = %q{Little library to convert Ruby blocks into JavaScript code, via S Expressions. Work in progress.}
         | 
| 14 | 
            +
              s.email = %q{danbryan@gmail.com}
         | 
| 15 | 
            +
              s.extra_rdoc_files = [
         | 
| 16 | 
            +
                "LICENSE.txt",
         | 
| 17 | 
            +
                "README.rdoc"
         | 
| 18 | 
            +
              ]
         | 
| 19 | 
            +
              s.files = [
         | 
| 20 | 
            +
                ".document",
         | 
| 21 | 
            +
                "Gemfile",
         | 
| 22 | 
            +
                "Gemfile.lock",
         | 
| 23 | 
            +
                "LICENSE.txt",
         | 
| 24 | 
            +
                "README.rdoc",
         | 
| 25 | 
            +
                "Rakefile",
         | 
| 26 | 
            +
                "ShyRubyJS.gemspec",
         | 
| 27 | 
            +
                "VERSION",
         | 
| 28 | 
            +
                "lib/ShyRubyJS.rb",
         | 
| 29 | 
            +
                "test/examples.rb",
         | 
| 30 | 
            +
                "test/helper.rb",
         | 
| 31 | 
            +
                "test/test_ShyRubyJS.rb"
         | 
| 32 | 
            +
              ]
         | 
| 33 | 
            +
              s.homepage = %q{http://github.com/Cerales/ShyRubyJS}
         | 
| 34 | 
            +
              s.licenses = [%q{MIT}]
         | 
| 35 | 
            +
              s.require_paths = [%q{lib}]
         | 
| 36 | 
            +
              s.rubygems_version = %q{1.8.6}
         | 
| 37 | 
            +
              s.summary = %q{Little library to convert Ruby blocks into JavaScript code, via S Expressions. Work in progress.}
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              if s.respond_to? :specification_version then
         | 
| 40 | 
            +
                s.specification_version = 3
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
         | 
| 43 | 
            +
                  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 44 | 
            +
                  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
         | 
| 45 | 
            +
                  s.add_development_dependency(%q<rcov>, [">= 0"])
         | 
| 46 | 
            +
                  s.add_development_dependency(%q<sourcify>, ["~> 0.5.0"])
         | 
| 47 | 
            +
                  s.add_runtime_dependency(%q<sourcify>, ["~> 0"])
         | 
| 48 | 
            +
                else
         | 
| 49 | 
            +
                  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 50 | 
            +
                  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
         | 
| 51 | 
            +
                  s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 52 | 
            +
                  s.add_dependency(%q<sourcify>, ["~> 0.5.0"])
         | 
| 53 | 
            +
                  s.add_dependency(%q<sourcify>, ["~> 0"])
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
              else
         | 
| 56 | 
            +
                s.add_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 57 | 
            +
                s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
         | 
| 58 | 
            +
                s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 59 | 
            +
                s.add_dependency(%q<sourcify>, ["~> 0.5.0"])
         | 
| 60 | 
            +
                s.add_dependency(%q<sourcify>, ["~> 0"])
         | 
| 61 | 
            +
              end
         | 
| 62 | 
            +
            end
         | 
| 63 | 
            +
             | 
    
        data/VERSION
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            0.0.0
         | 
    
        data/lib/ShyRubyJS.rb
    ADDED
    
    | @@ -0,0 +1,192 @@ | |
| 1 | 
            +
            # July - August 2011
         | 
| 2 | 
            +
            # Library to convert Ruby blocks into Javascript functions
         | 
| 3 | 
            +
            # Originally designed to help build CouchDB map and reduce functions
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # Sample usage: 
         | 
| 6 | 
            +
             | 
| 7 | 
            +
             | 
| 8 | 
            +
            require 'sourcify'
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            #TODO: 
         | 
| 11 | 
            +
            # Deal with javascript string immutability.
         | 
| 12 | 
            +
            # Implement a stack for indendation
         | 
| 13 | 
            +
            # Maybe also a stack for variable scope?
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            module ShyRubyJS
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              class ShySexpParser
         | 
| 18 | 
            +
              
         | 
| 19 | 
            +
                NL = "\n"
         | 
| 20 | 
            +
                SPACE = " "
         | 
| 21 | 
            +
                INDENT = "  "
         | 
| 22 | 
            +
              
         | 
| 23 | 
            +
                def initialize
         | 
| 24 | 
            +
                  literals
         | 
| 25 | 
            +
                  keywords
         | 
| 26 | 
            +
                  functions
         | 
| 27 | 
            +
                  operators
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
                
         | 
| 30 | 
            +
                def parse_block(&block)
         | 
| 31 | 
            +
                  parse block.to_sexp(:strip_enclosure => true)
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
                
         | 
| 34 | 
            +
                def parse(s_expression)
         | 
| 35 | 
            +
                  return "" unless s_expression
         | 
| 36 | 
            +
                  raise TypeError unless s_expression.class == Sexp
         | 
| 37 | 
            +
                  self.method(s_expression.sexp_type).call(s_expression.sexp_body)
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              
         | 
| 40 | 
            +
                # More methods in the stupid second class definition below
         | 
| 41 | 
            +
                # TODO: move the back together when dev is finished
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
              class ShySexpParser
         | 
| 45 | 
            +
              
         | 
| 46 | 
            +
                # S EXPRESSION METHODS
         | 
| 47 | 
            +
                # Corresponding to different "kinds" of S expression
         | 
| 48 | 
            +
              
         | 
| 49 | 
            +
                [:and, :or].map { |kw|
         | 
| 50 | 
            +
                  define_method kw do |body|
         | 
| 51 | 
            +
                    return body.map {|x| parse(x)}.join(SPACE + @keywords[kw] + SPACE)
         | 
| 52 | 
            +
                  end
         | 
| 53 | 
            +
                }
         | 
| 54 | 
            +
              
         | 
| 55 | 
            +
                [:block, :scope].map { |sexp|
         | 
| 56 | 
            +
                  define_method sexp do |body|
         | 
| 57 | 
            +
                    return body.map { |x| parse(x) }.join(NL) if body
         | 
| 58 | 
            +
                  end
         | 
| 59 | 
            +
                }
         | 
| 60 | 
            +
                [:lvar, :lit].map { |sexp|
         | 
| 61 | 
            +
                  define_method sexp do |body|
         | 
| 62 | 
            +
                    return body[0].to_s if body[0]
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
                }
         | 
| 65 | 
            +
              
         | 
| 66 | 
            +
                def if(body)
         | 
| 67 | 
            +
                  # Processes an if block. Indentation is broken if there are ifs within ifs.
         | 
| 68 | 
            +
                  string = @keywords[:if] + "(" + SPACE + parse(body[0]) + SPACE + ") {" + NL
         | 
| 69 | 
            +
                  if body[1] # if there's actually anything in the if statement
         | 
| 70 | 
            +
                    string += INDENT + parse(body[1]) + NL
         | 
| 71 | 
            +
                  end
         | 
| 72 | 
            +
                  if body[2] # if there's an else block
         | 
| 73 | 
            +
                    string += "}" + @keywords[:else] + "{" + NL
         | 
| 74 | 
            +
                    string += INDENT + parse(body[2])
         | 
| 75 | 
            +
                  end
         | 
| 76 | 
            +
                  string += NL unless string[string.length-1] == "}" or string[string.length-1] == NL
         | 
| 77 | 
            +
                  string += "}"
         | 
| 78 | 
            +
                  return string
         | 
| 79 | 
            +
                end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                def arglist(body)
         | 
| 82 | 
            +
                  # TODO - Sometimes this needs brackets e.g. eggs.find(5), but sometimes not eg 5 + 1 ie 5.+(1)
         | 
| 83 | 
            +
                  # To solve it needs either to receive information about what preceded it, or refactoring elsewhere
         | 
| 84 | 
            +
                  return "(" + SPACE + body.map { |a| parse(a) }.join(", ") + SPACE + ")" if body[0]
         | 
| 85 | 
            +
                end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                 def args(body)
         | 
| 88 | 
            +
                     return body.map { |a| a.to_s}.join(", ") if body
         | 
| 89 | 
            +
                   end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                def array(body)
         | 
| 92 | 
            +
                  string = ["[", map(body, ", "), "]"].join(SPACE)
         | 
| 93 | 
            +
                end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                def call(body)    
         | 
| 96 | 
            +
                  subject, method_name, arglist = body
         | 
| 97 | 
            +
                  standard_operator = @operators[method_name] ? true : false
         | 
| 98 | 
            +
                  string = ""
         | 
| 99 | 
            +
                  string += parse(subject)
         | 
| 100 | 
            +
                  string += "." if subject unless standard_operator
         | 
| 101 | 
            +
                  string += SPACE if standard_operator
         | 
| 102 | 
            +
                  string += method_name.to_s
         | 
| 103 | 
            +
                  if arglist.sexp_body.length > 0
         | 
| 104 | 
            +
                    standard_operator ? string += SPACE : string += "("
         | 
| 105 | 
            +
                    string += arglist.sexp_body.map { |a|
         | 
| 106 | 
            +
                      parse(a)
         | 
| 107 | 
            +
                    }.join(", ")
         | 
| 108 | 
            +
                    string += ")" unless standard_operator
         | 
| 109 | 
            +
                  end
         | 
| 110 | 
            +
                  return string
         | 
| 111 | 
            +
                end
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                def const(body)
         | 
| 114 | 
            +
                  return body[0].to_s
         | 
| 115 | 
            +
                end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                def defn(body)
         | 
| 118 | 
            +
                  ["function", "(", parse(body[1]), ")", "{", NL,
         | 
| 119 | 
            +
                    INDENT, parse(body[2]), NL,
         | 
| 120 | 
            +
                    "}"].join(SPACE)
         | 
| 121 | 
            +
                end
         | 
| 122 | 
            +
             | 
| 123 | 
            +
                def hash(body)
         | 
| 124 | 
            +
                  string = "{" + NL
         | 
| 125 | 
            +
                  body.each_slice(2) { |k,v| string += INDENT + parse(k) + ":" + SPACE + parse(v) + "," + NL }
         | 
| 126 | 
            +
                  string += "}"
         | 
| 127 | 
            +
                  return string
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                # def iter
         | 
| 131 | 
            +
                #   TODO
         | 
| 132 | 
            +
                # end
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                def lasgn(body)
         | 
| 135 | 
            +
                  [body[0].to_s, @literals[:lit], parse(body[1])].join(SPACE) if body[0] and body[1]
         | 
| 136 | 
            +
                end
         | 
| 137 | 
            +
              
         | 
| 138 | 
            +
                def not(body)
         | 
| 139 | 
            +
                  return @keywords[:!] + map(body)
         | 
| 140 | 
            +
                end
         | 
| 141 | 
            +
              
         | 
| 142 | 
            +
                def return(body)
         | 
| 143 | 
            +
                  return @keywords[:return] + SPACE + parse(body[0])
         | 
| 144 | 
            +
                end
         | 
| 145 | 
            +
              
         | 
| 146 | 
            +
                def str(body)
         | 
| 147 | 
            +
                  return ["\"", body[0], "\""].join if body[0]
         | 
| 148 | 
            +
                end
         | 
| 149 | 
            +
             | 
| 150 | 
            +
                # other methods
         | 
| 151 | 
            +
             | 
| 152 | 
            +
                def map(body, join=nil)
         | 
| 153 | 
            +
                  return body.map { |x| parse(x) }.join(join) if body
         | 
| 154 | 
            +
                end
         | 
| 155 | 
            +
             | 
| 156 | 
            +
                def literals
         | 
| 157 | 
            +
                  @literals = {
         | 
| 158 | 
            +
                    :true => "true",
         | 
| 159 | 
            +
                    :false => "false",
         | 
| 160 | 
            +
                    :lit => "=",
         | 
| 161 | 
            +
                  }
         | 
| 162 | 
            +
                end
         | 
| 163 | 
            +
                def keywords
         | 
| 164 | 
            +
                  @keywords = {
         | 
| 165 | 
            +
                    :if => "if",
         | 
| 166 | 
            +
                    :else => "else",
         | 
| 167 | 
            +
                    :return => "return",
         | 
| 168 | 
            +
                    :and => "&&",
         | 
| 169 | 
            +
                    :or => "||",
         | 
| 170 | 
            +
                    :! => "!"
         | 
| 171 | 
            +
                  }
         | 
| 172 | 
            +
                end
         | 
| 173 | 
            +
                def functions
         | 
| 174 | 
            +
                  @functions = {
         | 
| 175 | 
            +
                    :puts => "alert", 
         | 
| 176 | 
            +
                  }
         | 
| 177 | 
            +
                end
         | 
| 178 | 
            +
                def operators
         | 
| 179 | 
            +
                  @operators = {
         | 
| 180 | 
            +
                    :+ => "+",
         | 
| 181 | 
            +
                    :- => "-",
         | 
| 182 | 
            +
                    :>= => ">=",
         | 
| 183 | 
            +
                    :> => ">",
         | 
| 184 | 
            +
                    :<= => "<=",
         | 
| 185 | 
            +
                    :< => "<",
         | 
| 186 | 
            +
                    :* => "*",
         | 
| 187 | 
            +
                    :/ => "/",
         | 
| 188 | 
            +
                  }
         | 
| 189 | 
            +
                end
         | 
| 190 | 
            +
             | 
| 191 | 
            +
              end
         | 
| 192 | 
            +
            end
         | 
    
        data/test/examples.rb
    ADDED
    
    | @@ -0,0 +1,142 @@ | |
| 1 | 
            +
            require 'irb'
         | 
| 2 | 
            +
            require_relative '../lib/ShyRubyJS.rb'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
              $parser = ShyRubyJS::ShySexpParser.new
         | 
| 5 | 
            +
              $sexps = []
         | 
| 6 | 
            +
            def parse_block(&block)
         | 
| 7 | 
            +
              sexp = block.to_sexp(:strip_enclosure => true)
         | 
| 8 | 
            +
              $sexps << {"kind"=>sexp[0], "sexp"=>sexp}
         | 
| 9 | 
            +
              puts $parser.parse($sexps.last["sexp"])
         | 
| 10 | 
            +
              puts "\n"
         | 
| 11 | 
            +
            rescue TypeError, NameError => e
         | 
| 12 | 
            +
              $e = e
         | 
| 13 | 
            +
              puts e.class
         | 
| 14 | 
            +
              puts e.message
         | 
| 15 | 
            +
              puts e.backtrace
         | 
| 16 | 
            +
            rescue NameError => e
         | 
| 17 | 
            +
            end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            def view(&block)
         | 
| 20 | 
            +
              sexp = block.to_sexp(:strip_enclosure => true)
         | 
| 21 | 
            +
              $sexps << {"kind"=>sexp[0], "sexp"=>sexp}
         | 
| 22 | 
            +
              puts $parser.parse($sexps.last["sexp"])
         | 
| 23 | 
            +
              puts "\n"
         | 
| 24 | 
            +
            rescue TypeError, NameError => e
         | 
| 25 | 
            +
              $e = e
         | 
| 26 | 
            +
              puts e.class
         | 
| 27 | 
            +
              puts e.message
         | 
| 28 | 
            +
              puts e.backtrace
         | 
| 29 | 
            +
            rescue NameError => e
         | 
| 30 | 
            +
            end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            def dump
         | 
| 33 | 
            +
              # Spits out all the S expressions
         | 
| 34 | 
            +
              puts $sexps.map { |s| s["sexp"].inspect}.join("\n\n")
         | 
| 35 | 
            +
            end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            # blocks for testing
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            parse_block { 
         | 
| 40 | 
            +
              if x == "yep"
         | 
| 41 | 
            +
                y = "butt"
         | 
| 42 | 
            +
              elsif x == "do"
         | 
| 43 | 
            +
                  x = "boot"
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
            }
         | 
| 46 | 
            +
             
         | 
| 47 | 
            +
            parse_block {
         | 
| 48 | 
            +
              eggs(5)
         | 
| 49 | 
            +
            }
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            parse_block {
         | 
| 52 | 
            +
              eggs(5 + 1)
         | 
| 53 | 
            +
            }
         | 
| 54 | 
            +
             | 
| 55 | 
            +
             # parse_block {
         | 
| 56 | 
            +
             #   begin
         | 
| 57 | 
            +
             #     puts 1
         | 
| 58 | 
            +
             #   end while true
         | 
| 59 | 
            +
             # }
         | 
| 60 | 
            +
             
         | 
| 61 | 
            +
             # parse_block {
         | 
| 62 | 
            +
             #   while true
         | 
| 63 | 
            +
             #     puts 1
         | 
| 64 | 
            +
             #   end
         | 
| 65 | 
            +
             # }
         | 
| 66 | 
            +
             
         | 
| 67 | 
            +
            # parse_block {
         | 
| 68 | 
            +
            #   x = {"1"=>"2", "3"=>"4"}
         | 
| 69 | 
            +
            # }
         | 
| 70 | 
            +
             
         | 
| 71 | 
            +
            parse_block {
         | 
| 72 | 
            +
              x = [1,2,[3,3],4,5,6]
         | 
| 73 | 
            +
            }
         | 
| 74 | 
            +
             | 
| 75 | 
            +
            parse_block {
         | 
| 76 | 
            +
              x = []
         | 
| 77 | 
            +
            }
         | 
| 78 | 
            +
             | 
| 79 | 
            +
            parse_block {
         | 
| 80 | 
            +
              eggs(5 + 1)
         | 
| 81 | 
            +
            }
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            parse_block {
         | 
| 84 | 
            +
              x = 1
         | 
| 85 | 
            +
              x = 2
         | 
| 86 | 
            +
            }
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            parse_block {
         | 
| 89 | 
            +
              def function(doc)
         | 
| 90 | 
            +
                x = 1
         | 
| 91 | 
            +
              end
         | 
| 92 | 
            +
            }
         | 
| 93 | 
            +
             | 
| 94 | 
            +
            parse_block {
         | 
| 95 | 
            +
              return 1 + 8
         | 
| 96 | 
            +
            }
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            parse_block {
         | 
| 99 | 
            +
              x = 1
         | 
| 100 | 
            +
              y = 2
         | 
| 101 | 
            +
              if x and y
         | 
| 102 | 
            +
                puts "1"
         | 
| 103 | 
            +
              end
         | 
| 104 | 
            +
            }
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            parse_block {
         | 
| 107 | 
            +
              if x or y
         | 
| 108 | 
            +
                puts "1"
         | 
| 109 | 
            +
              end
         | 
| 110 | 
            +
            }
         | 
| 111 | 
            +
             | 
| 112 | 
            +
            # # Actual map functions:
         | 
| 113 | 
            +
            parse_block {
         | 
| 114 | 
            +
             def function(doc)
         | 
| 115 | 
            +
               if doc.age and doc.name
         | 
| 116 | 
            +
                 emit(doc.age, doc.name)
         | 
| 117 | 
            +
               end
         | 
| 118 | 
            +
             end
         | 
| 119 | 
            +
            }
         | 
| 120 | 
            +
            parse_block {
         | 
| 121 | 
            +
              if !x
         | 
| 122 | 
            +
                y = 1
         | 
| 123 | 
            +
              end
         | 
| 124 | 
            +
            }
         | 
| 125 | 
            +
             | 
| 126 | 
            +
            parse_block {
         | 
| 127 | 
            +
              hash = {"butt"=>"leg",
         | 
| 128 | 
            +
                "boot"=>[1,2],
         | 
| 129 | 
            +
                "bott"=>apples,
         | 
| 130 | 
            +
                "buggs"=>{"head"=>"leg"}
         | 
| 131 | 
            +
              }
         | 
| 132 | 
            +
            }
         | 
| 133 | 
            +
             | 
| 134 | 
            +
            view {
         | 
| 135 | 
            +
              def function(doc)
         | 
| 136 | 
            +
                if doc.kind == "post" and !doc.hidden
         | 
| 137 | 
            +
                  emit(doc)
         | 
| 138 | 
            +
                end
         | 
| 139 | 
            +
              end
         | 
| 140 | 
            +
            }
         | 
| 141 | 
            +
             | 
| 142 | 
            +
            IRB.start
         | 
    
        data/test/helper.rb
    ADDED
    
    | @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'bundler'
         | 
| 3 | 
            +
            begin
         | 
| 4 | 
            +
              Bundler.setup(:default, :development)
         | 
| 5 | 
            +
            rescue Bundler::BundlerError => e
         | 
| 6 | 
            +
              $stderr.puts e.message
         | 
| 7 | 
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         | 
| 8 | 
            +
              exit e.status_code
         | 
| 9 | 
            +
            end
         | 
| 10 | 
            +
            require 'test/unit'
         | 
| 11 | 
            +
            require 'shoulda'
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
         | 
| 14 | 
            +
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         | 
| 15 | 
            +
            require 'ShyRubyJS'
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            class Test::Unit::TestCase
         | 
| 18 | 
            +
            end
         | 
| @@ -0,0 +1,48 @@ | |
| 1 | 
            +
            #require 'helper'
         | 
| 2 | 
            +
            require 'test/unit'
         | 
| 3 | 
            +
            require_relative '../lib/ShyRubyJS.rb'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class TestShyRubyJS < Test::Unit::TestCase
         | 
| 6 | 
            +
              def setup
         | 
| 7 | 
            +
                @parser = ShyRubyJS::ShySexpParser.new
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
              def teardown
         | 
| 10 | 
            +
                @parser = nil
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
              
         | 
| 13 | 
            +
              def parse(&block)
         | 
| 14 | 
            +
                @parser.parse_block(&block)
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
              
         | 
| 17 | 
            +
              def test_simple_expressions
         | 
| 18 | 
            +
                expressions = []
         | 
| 19 | 
            +
                expressions << [%{5 * 1}, parse { 5 * 1 }]
         | 
| 20 | 
            +
                expressions << [%{7 + 11}, parse { 7 + 11 }]
         | 
| 21 | 
            +
                expressions << [%{9 + 2 + 1}, parse { 9 + 2 + 1 }]
         | 
| 22 | 
            +
                expressions << [%{9 <= 2}, parse { 9 <= 2}]
         | 
| 23 | 
            +
                expressions << [%{a = 2 + 1}, parse { a = 2 + 1}]
         | 
| 24 | 
            +
                expressions.each { |pair|
         | 
| 25 | 
            +
                  assert_equal(pair[0], pair[1])
         | 
| 26 | 
            +
                }
         | 
| 27 | 
            +
                # assert_equal(expected, result)
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
              
         | 
| 31 | 
            +
              def test_function_call
         | 
| 32 | 
            +
                expected = %{eggs}
         | 
| 33 | 
            +
                result = parse { eggs } 
         | 
| 34 | 
            +
                assert_equal(expected, result)
         | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
              
         | 
| 37 | 
            +
              def test_function_call_with_arg
         | 
| 38 | 
            +
                expected = %{eggs(5)}
         | 
| 39 | 
            +
                result = parse { eggs(5) }
         | 
| 40 | 
            +
                assert_equal(expected, result)
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
              
         | 
| 43 | 
            +
              def test_function_call_with_expression_arg
         | 
| 44 | 
            +
                expected = %{eggs(5 + 1)}
         | 
| 45 | 
            +
                result = parse { eggs(5+1) }
         | 
| 46 | 
            +
                assert_equal(expected, result)
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,121 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: ShyRubyJS
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.0
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Shy Inc.
         | 
| 9 | 
            +
            - Daniel Bryan
         | 
| 10 | 
            +
            - Cerales
         | 
| 11 | 
            +
            autorequire: 
         | 
| 12 | 
            +
            bindir: bin
         | 
| 13 | 
            +
            cert_chain: []
         | 
| 14 | 
            +
            date: 2011-08-14 00:00:00.000000000Z
         | 
| 15 | 
            +
            dependencies:
         | 
| 16 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 17 | 
            +
              name: bundler
         | 
| 18 | 
            +
              requirement: &70241116515440 !ruby/object:Gem::Requirement
         | 
| 19 | 
            +
                none: false
         | 
| 20 | 
            +
                requirements:
         | 
| 21 | 
            +
                - - ~>
         | 
| 22 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 23 | 
            +
                    version: 1.0.0
         | 
| 24 | 
            +
              type: :development
         | 
| 25 | 
            +
              prerelease: false
         | 
| 26 | 
            +
              version_requirements: *70241116515440
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: jeweler
         | 
| 29 | 
            +
              requirement: &70241116514220 !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                none: false
         | 
| 31 | 
            +
                requirements:
         | 
| 32 | 
            +
                - - ~>
         | 
| 33 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 34 | 
            +
                    version: 1.6.4
         | 
| 35 | 
            +
              type: :development
         | 
| 36 | 
            +
              prerelease: false
         | 
| 37 | 
            +
              version_requirements: *70241116514220
         | 
| 38 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 39 | 
            +
              name: rcov
         | 
| 40 | 
            +
              requirement: &70241116512680 !ruby/object:Gem::Requirement
         | 
| 41 | 
            +
                none: false
         | 
| 42 | 
            +
                requirements:
         | 
| 43 | 
            +
                - - ! '>='
         | 
| 44 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 45 | 
            +
                    version: '0'
         | 
| 46 | 
            +
              type: :development
         | 
| 47 | 
            +
              prerelease: false
         | 
| 48 | 
            +
              version_requirements: *70241116512680
         | 
| 49 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 50 | 
            +
              name: sourcify
         | 
| 51 | 
            +
              requirement: &70241116511480 !ruby/object:Gem::Requirement
         | 
| 52 | 
            +
                none: false
         | 
| 53 | 
            +
                requirements:
         | 
| 54 | 
            +
                - - ~>
         | 
| 55 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 56 | 
            +
                    version: 0.5.0
         | 
| 57 | 
            +
              type: :development
         | 
| 58 | 
            +
              prerelease: false
         | 
| 59 | 
            +
              version_requirements: *70241116511480
         | 
| 60 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 61 | 
            +
              name: sourcify
         | 
| 62 | 
            +
              requirement: &70241116510200 !ruby/object:Gem::Requirement
         | 
| 63 | 
            +
                none: false
         | 
| 64 | 
            +
                requirements:
         | 
| 65 | 
            +
                - - ~>
         | 
| 66 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 67 | 
            +
                    version: '0'
         | 
| 68 | 
            +
              type: :runtime
         | 
| 69 | 
            +
              prerelease: false
         | 
| 70 | 
            +
              version_requirements: *70241116510200
         | 
| 71 | 
            +
            description: Little library to convert Ruby blocks into JavaScript code, via S Expressions.
         | 
| 72 | 
            +
              Work in progress.
         | 
| 73 | 
            +
            email: danbryan@gmail.com
         | 
| 74 | 
            +
            executables: []
         | 
| 75 | 
            +
            extensions: []
         | 
| 76 | 
            +
            extra_rdoc_files:
         | 
| 77 | 
            +
            - LICENSE.txt
         | 
| 78 | 
            +
            - README.rdoc
         | 
| 79 | 
            +
            files:
         | 
| 80 | 
            +
            - .document
         | 
| 81 | 
            +
            - Gemfile
         | 
| 82 | 
            +
            - Gemfile.lock
         | 
| 83 | 
            +
            - LICENSE.txt
         | 
| 84 | 
            +
            - README.rdoc
         | 
| 85 | 
            +
            - Rakefile
         | 
| 86 | 
            +
            - ShyRubyJS.gemspec
         | 
| 87 | 
            +
            - VERSION
         | 
| 88 | 
            +
            - lib/ShyRubyJS.rb
         | 
| 89 | 
            +
            - test/examples.rb
         | 
| 90 | 
            +
            - test/helper.rb
         | 
| 91 | 
            +
            - test/test_ShyRubyJS.rb
         | 
| 92 | 
            +
            homepage: http://github.com/Cerales/ShyRubyJS
         | 
| 93 | 
            +
            licenses:
         | 
| 94 | 
            +
            - MIT
         | 
| 95 | 
            +
            post_install_message: 
         | 
| 96 | 
            +
            rdoc_options: []
         | 
| 97 | 
            +
            require_paths:
         | 
| 98 | 
            +
            - lib
         | 
| 99 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
              none: false
         | 
| 101 | 
            +
              requirements:
         | 
| 102 | 
            +
              - - ! '>='
         | 
| 103 | 
            +
                - !ruby/object:Gem::Version
         | 
| 104 | 
            +
                  version: '0'
         | 
| 105 | 
            +
                  segments:
         | 
| 106 | 
            +
                  - 0
         | 
| 107 | 
            +
                  hash: -3435630312527472729
         | 
| 108 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 109 | 
            +
              none: false
         | 
| 110 | 
            +
              requirements:
         | 
| 111 | 
            +
              - - ! '>='
         | 
| 112 | 
            +
                - !ruby/object:Gem::Version
         | 
| 113 | 
            +
                  version: '0'
         | 
| 114 | 
            +
            requirements: []
         | 
| 115 | 
            +
            rubyforge_project: 
         | 
| 116 | 
            +
            rubygems_version: 1.8.6
         | 
| 117 | 
            +
            signing_key: 
         | 
| 118 | 
            +
            specification_version: 3
         | 
| 119 | 
            +
            summary: Little library to convert Ruby blocks into JavaScript code, via S Expressions.
         | 
| 120 | 
            +
              Work in progress.
         | 
| 121 | 
            +
            test_files: []
         |