math_expr 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: e909048f6c47274b8c4661b73c856118ee04bb3790ac4f15f8d5d154e5493888
4
+ data.tar.gz: d54004a17b72df3dbe951d396d2d476f6fa6e4be153f8d69298ab16187fbec39
5
+ SHA512:
6
+ metadata.gz: 6952fb7b5010b46c42982bfe305199bc8c1307e2368d995d697e47c46caa2a409c3a06e052cd6f04a2e54c8014dbbcf295462735ab5353e6774681bfef61196e
7
+ data.tar.gz: df65283ffd5cbd3d8d13ecaf58ac25e8026573420bb95952354467d17afc644c83cab59a71175a87b3841ec63f6593ec1cf203c4a8b40fa4cb08364ad08e5308
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1 @@
1
+ 2.7.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in math_expression.gemspec
4
+ gemspec
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ math_expr (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rspec (3.9.0)
11
+ rspec-core (~> 3.9.0)
12
+ rspec-expectations (~> 3.9.0)
13
+ rspec-mocks (~> 3.9.0)
14
+ rspec-core (3.9.1)
15
+ rspec-support (~> 3.9.1)
16
+ rspec-expectations (3.9.1)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.9.0)
19
+ rspec-mocks (3.9.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-support (3.9.2)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ math_expr!
29
+ rspec (~> 3.9.0)
30
+
31
+ BUNDLED WITH
32
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mateusz Kluge
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,40 @@
1
+ # MathExpression
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/math_expression`. 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 'math_expression'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install math_expression
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 respec` 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/inclooder/math_expression.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "math_expression"
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,4 @@
1
+ require "math_expression/version"
2
+
3
+ module MathExpression
4
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MathExpression::Parser
4
+ def initialize(expression)
5
+ @expression = expression
6
+ end
7
+
8
+ def to_tokens
9
+ tokens = []
10
+ reading_number = []
11
+
12
+ form_number = lambda do
13
+ return if reading_number.empty?
14
+ num = reading_number.join.to_i
15
+ tokens << { type: :number, value: num }
16
+ reading_number = []
17
+ end
18
+
19
+ expression.chars.each do |character|
20
+ if operator?(character)
21
+ form_number.call
22
+ tokens << { type: :operator, value: character}
23
+ else
24
+ reading_number << character
25
+ end
26
+ end
27
+ form_number.call
28
+ tokens
29
+ end
30
+
31
+ def to_postfix
32
+ output_queue = []
33
+ operator_stack = []
34
+
35
+ to_tokens.each do |token|
36
+ case token[:type]
37
+ when :number
38
+ output_queue << token[:value]
39
+ when :operator
40
+ loop do
41
+ break if operator_stack.empty?
42
+ operator_on_stack_precedence = operator_precedence(operator_stack.last)
43
+ current_operator_precedence = operator_precedence(token[:value])
44
+ break if operator_on_stack_precedence < current_operator_precedence
45
+
46
+ output_queue << operator_stack.pop
47
+ end
48
+ operator_stack.push(token[:value])
49
+ end
50
+ end
51
+ (output_queue + operator_stack.reverse)
52
+ end
53
+
54
+ def evaluate
55
+ stack = []
56
+ to_postfix.each do |token|
57
+ result = case token
58
+ when '+'
59
+ stack.pop + stack.pop
60
+ when '-'
61
+ first, second = stack.pop, stack.pop
62
+ second - first
63
+ when '*'
64
+ stack.pop * stack.pop
65
+ when '/'
66
+ stack.pop / stack.pop
67
+ else
68
+ token.to_i
69
+ end
70
+ stack.push(result)
71
+ end
72
+ stack.pop
73
+ end
74
+
75
+ private
76
+
77
+ OPERATORS = %w(+ - * /).freeze
78
+
79
+ def operator?(token)
80
+ OPERATORS.include?(token)
81
+ end
82
+
83
+ def operator_precedence(operator)
84
+ {
85
+ '+' => 2,
86
+ '-' => 2,
87
+ '*' => 3,
88
+ '/' => 3,
89
+ }.fetch(operator)
90
+ end
91
+
92
+ attr_reader :expression
93
+ end
@@ -0,0 +1,3 @@
1
+ module MathExpression
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/math_expression/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "math_expr"
5
+ spec.version = MathExpression::VERSION
6
+ spec.authors = ["Inclooder"]
7
+ spec.email = ["inclooder@gmail.com"]
8
+
9
+ spec.summary = %q{Mathematical expression parser and executor}
10
+ spec.description = %q{Mathematical expression parser and executor}
11
+ spec.homepage = "https://github.com/inclooder/math_expression"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/inclooder/math_expression"
17
+ spec.metadata["changelog_uri"] = "https://github.com/inclooder/math_expression"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency 'rspec', '~> 3.9.0'
29
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: math_expr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Inclooder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.9.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.9.0
27
+ description: Mathematical expression parser and executor
28
+ email:
29
+ - inclooder@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".ruby-version"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/math_expression.rb
44
+ - lib/math_expression/parser.rb
45
+ - lib/math_expression/version.rb
46
+ - math_expression.gemspec
47
+ homepage: https://github.com/inclooder/math_expression
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/inclooder/math_expression
52
+ source_code_uri: https://github.com/inclooder/math_expression
53
+ changelog_uri: https://github.com/inclooder/math_expression
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.3.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Mathematical expression parser and executor
73
+ test_files: []