goling 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "shoulda", ">= 0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.6.4"
7
+ gem "rcov", ">= 0"
8
+ end
9
+ gem 'sourcify'
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
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.9.2)
12
+ rcov (0.9.10)
13
+ ruby2ruby (1.3.0)
14
+ ruby_parser (~> 2.0)
15
+ sexp_processor (~> 3.0)
16
+ ruby_parser (2.3.0)
17
+ sexp_processor (~> 3.0)
18
+ sexp_processor (3.0.6)
19
+ shoulda (2.11.3)
20
+ sourcify (0.5.0)
21
+ file-tail (>= 1.0.5)
22
+ ruby2ruby (>= 1.2.5)
23
+ ruby_parser (>= 2.0.5)
24
+ sexp_processor (>= 3.0.5)
25
+ spruz (0.2.13)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.0.0)
32
+ jeweler (~> 1.6.4)
33
+ rcov
34
+ shoulda
35
+ sourcify
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Goling
2
+
3
+ Goling is a linguistic compiler allowing you to compile and execute plain english.
4
+ And thus allows you to program in plain english provided you have the reduction rules needed.
5
+
6
+ Since it compiled like all code should be you can execute your code so amazingly fast I am in awe just to be able to write this divine text to you about it.
7
+
8
+ ## Installation
9
+
10
+ gem install goling
11
+
12
+ ## Basic usage
13
+
14
+ reduce /all directories/ => 'directories' do
15
+ Dir.entries('.').select{ |f| f[0] != '.' && File.directory?(f) }
16
+ end
17
+
18
+ reduce /({directories:[^}]*}) recursively/ => 'directories' do |dirs|
19
+ all_dirs = dirs
20
+ Find.find(dirs) do |path|
21
+ if FileTest.directory?(path)
22
+ if File.basename(path)[0] == '.'
23
+ Find.prune # Don't look any further into this directory.
24
+ else
25
+ all_dirs << path
26
+ next
27
+ end
28
+ end
29
+ end
30
+ all_dirs
31
+ end
32
+
33
+ reduce /every largest file inside ({directories:[^}]*})/ => 'files' do |dirs|
34
+ dirs.map{ |f| File.new(f, "r") }
35
+ end
36
+
37
+ reduce /view ({files:[^}]*})/ => '' do |files|
38
+ files.each do |file|
39
+ pp file
40
+ end
41
+ end
42
+
43
+ puts "view every largest file inside all directories recursively".linguify.to_ruby
44
+ # => code = lambda do
45
+ directories_0 = Dir.entries(".").select { |f| ((not (f[0] == ".")) and File.directory?(f)) }
46
+ directories_1 = (all_dirs = dirs
47
+ Find.find(dirs) do |path|
48
+ if FileTest.directory?(path) then
49
+ if (File.basename(path)[0] == ".") then
50
+ Find.prune
51
+ else
52
+ (all_dirs << path)
53
+ next
54
+ end
55
+ end
56
+ end
57
+ all_dirs)
58
+ files_2 = dirs.map { |f| File.new(f, "r") }
59
+ files.each { |file| pp(file) }
60
+ end
61
+ Goling::Linguified.trampoline(code)
62
+
63
+ And if you simply want to execute your magnificent piece of art:
64
+
65
+ "view every largest file inside all directories recursively".linguify.run
66
+
67
+ Or even:
68
+
69
+ # compile once, run plenty
70
+ code = "view every largest file inside all directories recursively".linguify
71
+ loop do
72
+ code.run
73
+ end
74
+
75
+ ## License
76
+
77
+ (The MIT License)
78
+
79
+ Copyright (c) 2010 Patrick Hanevold
80
+
81
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
84
+
85
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
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.name = "goling"
17
+ gem.homepage = "http://github.com/patrickhno/goling"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{Goling, the linguistic compiler.}
20
+ gem.description = %Q{Goling is a linguistic compiler allowing you to compile and execute plain english.}
21
+ gem.email = "patrick.hanevold@gmail.com"
22
+ gem.authors = ["Patrick Hanevold"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |test|
36
+ test.libs << 'test'
37
+ test.pattern = 'test/**/test_*.rb'
38
+ test.verbose = true
39
+ test.rcov_opts << '--exclude "gems/*"'
40
+ end
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "goling #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/goling.rb ADDED
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'goling/linguified'
4
+ require 'goling/reduction'
5
+
6
+ def reduce(regexp,&code)
7
+ Goling::rules << { :match => regexp.keys[0], :result => regexp.values[0], :proc => code }
8
+ end
9
+
10
+ module Goling
11
+
12
+ def self.rules
13
+ @@rules ||= []
14
+ end
15
+
16
+ end
17
+
18
+ class String
19
+ def linguify bind=binding
20
+ return Goling::Linguified::cace[self] if Goling::Linguified::cache[self]
21
+ Goling::Linguified::cache[self] = Goling::Linguified.new(self,bind)
22
+ end
23
+ end
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+
3
+ require 'sourcify'
4
+
5
+ module Goling
6
+
7
+ class Linguified
8
+
9
+ attr_accessor :proc, :sentence
10
+
11
+ def initialize str,bind
12
+
13
+ @sentence = str.dup
14
+ @bind = bind
15
+ loop do
16
+ found = Goling.rules.select do |rule|
17
+ if rule[:match] =~ str
18
+ true
19
+ else
20
+ false
21
+ end
22
+ end
23
+ raise "no step definition for #{str}" if found.size == 0
24
+
25
+ rule = found[0]
26
+ match = rule[:match].match(str)
27
+ reduced = Reduction.new(
28
+ :returns => rule[:result],
29
+ :location => rule[:proc].source_location[0],
30
+ :line => rule[:proc].source_location[1],
31
+ :regexp => rule[:match].inspect,
32
+ :args => match.to_a[1..-1],
33
+ :sexp => rule[:proc].to_sexp,
34
+ )
35
+ str.gsub!(rule[:match],reduced.to_rexp)
36
+ break if /^{.*}$/ =~ str
37
+ end
38
+
39
+ @encoded = str
40
+
41
+ @merged_code = []
42
+ if /^{(?<code>.*)}$/ =~ @encoded
43
+ code = Reduction::parse(code).compile_with_return_to_var(nil)
44
+
45
+ @sexy = Sexp.new(:block,
46
+ Sexp.new(:lasgn,:code, Sexp.new(:iter,
47
+ Sexp.new(:call, nil, :lambda, Sexp.new(:arglist)), nil,
48
+ Sexp.new(:block,
49
+ *code
50
+ )
51
+ )
52
+ ),
53
+ Sexp.new(:call, Sexp.new(:colon2, Sexp.new(:const, :Goling), :Linguified), :trampoline, Sexp.new(:arglist, Sexp.new(:lvar, :code)))
54
+ )
55
+
56
+ @@me = self
57
+ eval to_ruby,bind
58
+ raise "hell" unless @proc
59
+ else
60
+ raise "hell"
61
+ end
62
+ end
63
+
64
+ def to_sexp
65
+ @sexy
66
+ end
67
+
68
+ def to_ruby
69
+ clone = Marshal.load(Marshal.dump(@sexy)) # sexy is not cleanly duplicated
70
+ Ruby2Ruby.new.process(clone)
71
+ end
72
+
73
+ def run
74
+ begin
75
+ @proc.call
76
+ rescue Exception => e
77
+ $stderr.puts e
78
+ end
79
+ end
80
+
81
+ def register_code code
82
+ @proc = code
83
+ end
84
+
85
+ def self.trampoline code
86
+ @@me.register_code code
87
+ end
88
+
89
+ def self.cache
90
+ @@cache ||= {}
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,161 @@
1
+ # encoding: utf-8
2
+
3
+ module Goling
4
+
5
+ class Reduction
6
+
7
+ attr_accessor :returns, :location, :line, :regexp, :args, :sexp, :rule_args, :from, :reduction_id
8
+
9
+ @@reductions = []
10
+
11
+ def initialize params
12
+ @returns = params[:returns]
13
+ @location = params[:location]
14
+ @line = params[:line]
15
+ @regexp = params[:regexp]
16
+ @rule_args= @regexp.split(')').map{ |sub| sub.split('(')[1] }.select{ |v| v }
17
+ @args = params[:args]
18
+ @sexp = params[:sexp]
19
+ @reduction_id = @@reductions.size
20
+ @@reductions << self
21
+ end
22
+
23
+ def self.parse str
24
+ if /^{(?<return>[^:]*):(?<rid>[0-9]+)}$/ =~ str
25
+ @@reductions[rid.to_i]
26
+ elsif /^(?<return>[^:]*):(?<rid>[0-9]+)$/ =~ str
27
+ @@reductions[rid.to_i]
28
+ else
29
+ ap str
30
+ raise "hell"
31
+ end
32
+ end
33
+
34
+ def compile_with_return_to_var(return_variable, replace = {})
35
+ s = Marshal.load(Marshal.dump(self)) # sexp handling is not clean cut
36
+ raise "what is this?" unless s.sexp[0] == :iter && s.sexp[1][0] == :call && s.sexp[1][1] == nil && s.sexp[1][2] == :proc && s.sexp[1][3][0] == :arglist
37
+
38
+ block_args = s.sexp[2]
39
+ #ap "block args: #{block_args}"
40
+ if block_args
41
+ if block_args[0]==:lasgn
42
+ # single argument
43
+ args = [block_args[1]]
44
+ elsif block_args[0]==:masgn
45
+ # multiple arguments
46
+ args = block_args[1]
47
+ raise "unsupported argument type #{args}" unless args[0]==:array
48
+ args = args[1..-1].map{ |arg|
49
+ raise "unsupported argument type #{arg}" unless arg[0]==:lasgn
50
+ arg[1]
51
+ }
52
+ else
53
+ raise "unsupported argument type #{args}"
54
+ end
55
+ unless s.rule_args.size == args.size
56
+ raise "number of arguments in reduction rule (#{s.rule_args}) do not match the number of block arguments (#{args.size}) at #{s.location}:#{s.line}"
57
+ end
58
+ # args[] now has the symbolized argument names of the code block
59
+
60
+ args_code = []
61
+ s.args.each_with_index do |arg,i|
62
+ if /^{(?<ret>[^:]*):(?<n>[0-9]+)}$/ =~ arg
63
+ args_code += Reduction::parse(arg).compile_with_return_to_var("#{ret}_#{n}".to_sym,replace)
64
+ elsif /^[0-9]+$/ =~ arg
65
+ args_code << Sexp.new(:lasgn, args[i], Sexp.new(:lit, arg.to_i))
66
+ else
67
+ raise "hell"
68
+ end
69
+ end
70
+ s.args.each_with_index do |arg,i|
71
+ if /^{(?<ret>[^:]*):(?<n>[0-9]+)}$/ =~ arg
72
+ replace[args[i]] = "#{ret}_#{n}".to_sym
73
+ end
74
+ end
75
+
76
+ if s.sexp[3][0] == :block
77
+ if return_variable
78
+ code = Sexp.new(:lasgn, return_variable, Sexp.new(:block,
79
+ *(s.sexp[3][1..-1].map{ |s| s.dup })
80
+ )
81
+ )
82
+ else
83
+ code = s.sexp[3].dup
84
+ end
85
+ replace.each do |k,v|
86
+ replace_variable_references(code,v,k)
87
+ end
88
+ return *args_code + [code]
89
+ else
90
+ if return_variable
91
+ code = Sexp.new(:lasgn, return_variable, s.sexp[3].dup)
92
+ else
93
+ code = s.sexp[3].dup
94
+ end
95
+ replace.each do |k,v|
96
+ replace_variable_references(code,v,k)
97
+ end
98
+ return *args_code + [code]
99
+ end
100
+ else
101
+ raise "number of arguments in reduction rule do not match the number of block arguments" unless s.rule_args.size == 0
102
+ # code block without arguments
103
+ if s.sexp[3][0] == :block
104
+ if return_variable
105
+ code = Sexp.new(:lasgn, return_variable, Sexp.new(:block,
106
+ *(s.sexp[3][1..-1].map{ |s| s.dup })
107
+ )
108
+ )
109
+ else
110
+ code = s.sexp[3].dup
111
+ end
112
+ replace.each do |k,v|
113
+ replace_variable_references(code,v,k)
114
+ end
115
+ [code]
116
+ else
117
+ if return_variable
118
+ code = Sexp.new(:lasgn, return_variable, s.sexp[3].dup)
119
+ else
120
+ code = s.sexp[3].dup
121
+ end
122
+ replace.each do |k,v|
123
+ replace_variable_references(code,v,k)
124
+ end
125
+ [code]
126
+ end
127
+ end
128
+ end
129
+
130
+ def replace_variable_references(code,replacement,needle)
131
+ case code[0]
132
+ when :lasgn
133
+ if code[1] == needle
134
+ code[1]=replacement
135
+ end
136
+ code[2..-1].each do |h|
137
+ replace_variable_references(h,replacement,needle)
138
+ end
139
+ when :call
140
+ replace_variable_references(code[1],replacement,needle) if code[1]
141
+ if code[2] == needle
142
+ code[2]=replacement
143
+ end
144
+ replace_variable_references(code[3],replacement,needle)
145
+ when :lvar
146
+ if code[1] == needle
147
+ code[1]=replacement
148
+ end
149
+ else
150
+ code[1..-1].each do |h|
151
+ replace_variable_references(h,replacement,needle) if h.kind_of(Sexp)
152
+ end
153
+ end
154
+ end
155
+
156
+ def to_rexp
157
+ "{#{returns}:#{reduction_id}}"
158
+ end
159
+ end
160
+
161
+ end
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 'goling'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+
3
+ class TestGoling < Test::Unit::TestCase
4
+ should "work as intended" do
5
+
6
+ reduce /all directories/ => 'directories' do
7
+ Dir.entries('.').select{ |f| f[0] != '.' && File.directory?(f) }
8
+ end
9
+
10
+ reduce /({directories:[^}]*}) recursively/ => 'directories' do |dirs|
11
+ all_dirs = dirs
12
+ Find.find(dirs) do |path|
13
+ if FileTest.directory?(path)
14
+ if File.basename(path)[0] == '.'
15
+ Find.prune # Don't look any further into this directory.
16
+ else
17
+ all_dirs << path
18
+ next
19
+ end
20
+ end
21
+ end
22
+ all_dirs
23
+ end
24
+
25
+ reduce /every largest file inside ({directories:[^}]*})/ => 'files' do |dirs|
26
+ dirs.map{ |f| File.new(f, "r") }
27
+ end
28
+
29
+ reduce /view ({files:[^}]*})/ => '' do |files|
30
+ files.each do |file|
31
+ pp file
32
+ end
33
+ end
34
+
35
+ "view every largest file inside all directories recursively".linguify.to_ruby.size > 0
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goling
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Patrick Hanevold
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-07 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sourcify
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: shoulda
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.4
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ description: Goling is a linguistic compiler allowing you to compile and execute plain english.
71
+ email: patrick.hanevold@gmail.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - README.md
78
+ files:
79
+ - .document
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - README.md
83
+ - Rakefile
84
+ - VERSION
85
+ - lib/goling.rb
86
+ - lib/goling/linguified.rb
87
+ - lib/goling/reduction.rb
88
+ - test/helper.rb
89
+ - test/test_goling.rb
90
+ homepage: http://github.com/patrickhno/goling
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3437852268998191115
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.8
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Goling, the linguistic compiler.
120
+ test_files: []
121
+