rrpn 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2dd193f6ca23427ec0dbdd608d5ca69f92275361
4
+ data.tar.gz: ef26048dd880fe03516eafe274b5cff16880bfdf
5
+ SHA512:
6
+ metadata.gz: f8a4f40475a9ab08c55eb29d6440405beeb80ce4cb45dc45d7d5c491f7d5e815476c37ae34f9a9cc5d1d2c1b6e1d553967361fcc419410fa8e4053edf7f3b237
7
+ data.tar.gz: 70fc6d5c5ec1c44cc11a8d8817bed7262cb419b2fdd3eec764fa196d04d5c035024e498efda3a30d27f4175e742af80fe7cd76e26b55bfdd07bace727816443d
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /vendor/bundle/
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ # RubyMine
16
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --require spec_helper
3
+ --color
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.0
7
+ - 2.4.0
8
+ before_install: gem install bundler -v 1.14.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at k128585@ie.u-ryukyu.ac.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rrpn.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 siman-man
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,84 @@
1
+ # RRPN
2
+
3
+ [![Build Status](https://travis-ci.org/siman-man/rrpn.svg?branch=master)](https://travis-ci.org/siman-man/rrpn)
4
+
5
+ RRPN is Reverse Polish Notation calculator and converter, written in Ruby.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rrpn'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rrpn
22
+
23
+ ## Usage
24
+
25
+ Integer
26
+
27
+ ```ruby
28
+ rpn = '1 + 2 + 3'.to_rpn
29
+ p rpn.queue #=> [1, 2, :+, 3, :+]
30
+ p rpn.calc #=> 6
31
+ puts rpn #=> 1 2 + 3 +
32
+ ```
33
+
34
+ Float
35
+
36
+ ```ruby
37
+ rpn = '(1.0 + 2.0) * 3'.to_rpn
38
+ p rpn.queue #=> [1.0, 2.0, :+, 3, :*]
39
+ p rpn.calc #=> 9.0
40
+ puts rpn #=> 1.0 2.0 + 3 *
41
+ ```
42
+
43
+ Rational
44
+
45
+ ```ruby
46
+ rpn = '1/2r + 1/3r'.to_rpn
47
+ p rpn.queue #=> [1, "2r", :/, 1, "3r", :/, :+]
48
+ p rpn.calc #=> (5/6)
49
+ puts rpn #=> 1 2r / 1 3r / +
50
+ ```
51
+
52
+ Complex
53
+
54
+ ```ruby
55
+ rpn = '(1+2i) + (3+3i)'.to_rpn
56
+ p rpn.queue #=> [1, (0+2i), :+, 3, (0+3i), :+, :+]
57
+ p rpn.calc #=> (4+5i)
58
+ puts rpn #=> 1 0+2i + 3 0+3i + +
59
+ ```
60
+
61
+ Other
62
+
63
+ ```ruby
64
+ rpn = '(1 << 3) + (16 >> 2)'.to_rpn
65
+ p rpn.queue #=> [1, 3, :<<, 16, 2, :>>, :+]
66
+ p rpn.calc #=> 12
67
+ puts rpn #=> 1 3 << 16 2 >> +
68
+ ```
69
+
70
+ Calculation
71
+
72
+ ```ruby
73
+ p RRPN.calc([3, 4, :*]) #=> 12
74
+ ```
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/siman-man/rrpn. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
79
+
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
84
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rrpn"
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,28 @@
1
+ require 'rrpn'
2
+
3
+ rpn = '1 + 2 + 3'.to_rpn
4
+ p rpn.queue #=> [1, 2, :+, 3, :+]
5
+ p rpn.calc #=> 6
6
+ puts rpn
7
+
8
+ rpn = '(1.0 + 2.0) * 3'.to_rpn
9
+ p rpn.queue #=> [1.0, 2.0, :+, 3, :*]
10
+ p rpn.calc #=> 9.0
11
+ puts rpn
12
+
13
+ rpn = '1/2r + 1/3r'.to_rpn
14
+ p rpn.queue #=> [1, "2r", :/, 1, "3r", :/, :+]
15
+ p rpn.calc #=> (5/6)
16
+ puts rpn
17
+
18
+ rpn = '(1+2i) + (3+3i)'.to_rpn
19
+ p rpn.queue #=> [1, 3, :<<, 16, 2, :>>, :+]
20
+ p rpn.calc #=> (4+5i)
21
+ puts rpn
22
+
23
+ rpn = '(1 << 3) + (16 >> 2)'.to_rpn
24
+ p rpn.queue #=> [1, (0+2i), :+, 3, (0+3i), :+, :+]
25
+ p rpn.calc #=> 12
26
+ puts rpn
27
+
28
+ p RRPN.calc([3, 4, :*]) #=> 12
@@ -0,0 +1,73 @@
1
+ require 'ripper'
2
+
3
+ class String
4
+ def to_rpn
5
+ RRPN.new(self)
6
+ end
7
+ end
8
+
9
+ class RRPN
10
+ attr_reader :queue
11
+
12
+ def self.calc(queue)
13
+ stack = []
14
+
15
+ until queue.empty?
16
+ v = queue.shift
17
+ stack << v
18
+
19
+ if %i(+ - * / ** << >> % & ^ |).include?(stack.last)
20
+ receiver, arg, op = stack.pop(3).map { |e| e.instance_of?(String) ? eval(e) : e }
21
+ stack << receiver.send(op, arg)
22
+ elsif %i(~ -@ +@).include?(stack.last)
23
+ receiver, op = stack.pop(2).map { |e| e.instance_of?(String) ? eval(e) : e }
24
+ stack << receiver.send(op)
25
+ end
26
+ end
27
+
28
+ stack.pop
29
+ end
30
+
31
+ def initialize(formula)
32
+ @formula = formula
33
+ stree = Ripper.sexp(@formula)
34
+ @queue = parse(stree[1..-1])
35
+ end
36
+
37
+ def parse(tree)
38
+ queue = []
39
+
40
+ case tree[0]
41
+ when :paren
42
+ queue << parse(tree[1..-1])
43
+ when :binary
44
+ return [parse(tree[1]), parse(tree[3]), tree[2]]
45
+ when :unary
46
+ return [parse(tree[2]), tree[1]]
47
+ when :@int
48
+ return tree[1].to_i
49
+ when :@float
50
+ return tree[1].to_f
51
+ when :@rational
52
+ return tree[1].to_s
53
+ when :@imaginary
54
+ return tree[1].to_c
55
+ else
56
+ if tree[0].instance_of?(Array)
57
+ queue << parse(tree[0])
58
+ else
59
+ raise "Parse failed. '#{@formula}' - #{tree.inspect}"
60
+ end
61
+ end
62
+
63
+ queue.flatten
64
+ end
65
+
66
+ def calc
67
+ RRPN.calc(queue.dup)
68
+ end
69
+
70
+ def to_s
71
+ @queue.join(' ')
72
+ end
73
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rrpn"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["siman-man"]
9
+ spec.email = ["k128585@ie.u-ryukyu.ac.jp"]
10
+
11
+ spec.summary = %q{Reverse Polish Notation calculator and converter.}
12
+ spec.description = %q{Reverse Polish Notation calculator and converter.}
13
+ spec.homepage = "https://github.com/siman-man/rrpn"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = '>= 2.1.0'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rrpn
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - siman-man
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-21 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Reverse Polish Notation calculator and converter.
56
+ email:
57
+ - k128585@ie.u-ryukyu.ac.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - examples/sample.rb
73
+ - lib/rrpn.rb
74
+ - rrpn.gemspec
75
+ homepage: https://github.com/siman-man/rrpn
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.1.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.10
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Reverse Polish Notation calculator and converter.
99
+ test_files: []