rubulator 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fad14d7230d98f24f583f0bd51e33fe39f6ccf7364d9d01219d7415258004029
4
+ data.tar.gz: 687887bc8a463bf7f8df9e41d1c5387f35061ccbc122886bb0ca1f218ec4bb0e
5
+ SHA512:
6
+ metadata.gz: e59c98aa943efb2bb4598229835dea497f2999fc7fb876be7c5ea726416edac28f1c49041299f244d9da330f7c7c2f59a301d7fdfe981aed15a31bdd29ca343e
7
+ data.tar.gz: 4bcbab989cc6ef9fe879c49f10b0d9a1a5ea326a8bfcf793281d12b7a6e6530aad32d2a768511810f5546f1daf43e287d62bfb65b88a39a457c565d7fcc47662
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.6.6
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in rubulator.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubulator (0.1.0)
5
+ readline (~> 0)
6
+ readline-ext (~> 0.1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.1)
12
+ diff-lcs (1.4.4)
13
+ io-console (0.5.6)
14
+ parallel (1.19.2)
15
+ parser (2.7.1.4)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.0.0)
18
+ rake (12.3.3)
19
+ readline (0.0.2)
20
+ reline
21
+ readline-ext (0.1.0)
22
+ regexp_parser (1.7.1)
23
+ reline (0.1.4)
24
+ io-console (~> 0.5)
25
+ rexml (3.2.4)
26
+ rspec (3.9.0)
27
+ rspec-core (~> 3.9.0)
28
+ rspec-expectations (~> 3.9.0)
29
+ rspec-mocks (~> 3.9.0)
30
+ rspec-core (3.9.2)
31
+ rspec-support (~> 3.9.3)
32
+ rspec-expectations (3.9.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-mocks (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-support (3.9.3)
39
+ rubocop (0.89.0)
40
+ parallel (~> 1.10)
41
+ parser (>= 2.7.1.1)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.7)
44
+ rexml
45
+ rubocop-ast (>= 0.1.0, < 1.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 1.4.0, < 2.0)
48
+ rubocop-ast (0.3.0)
49
+ parser (>= 2.7.1.4)
50
+ ruby-progressbar (1.10.1)
51
+ unicode-display_width (1.7.0)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ rake (~> 12.0)
58
+ rspec (~> 3.0)
59
+ rubocop (~> 0.89.0)
60
+ rubulator!
61
+
62
+ BUNDLED WITH
63
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jeremy Cole
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
+ # Rubulator
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/rubulator`. 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 'rubulator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubulator
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 spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubulator.
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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rubulator'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,37 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'rubulator'
5
+ require 'readline'
6
+
7
+ rubulator = Rubulator.new
8
+
9
+ Readline.completer_word_break_characters = ' '
10
+ Readline.completion_append_character = ' '
11
+ Readline.completion_proc = proc do |partial|
12
+ completions = rubulator.operators + rubulator.functions + rubulator.constants + rubulator.variable_names
13
+ completions.select { |v| v.start_with?(partial) }.sort
14
+ end
15
+
16
+ puts "Rubulator version #{Rubulator::VERSION} on Ruby version #{RUBY_VERSION}."
17
+ puts 'Supported operations:'
18
+ puts " * Operators: #{rubulator.operators.sort.join(', ')}"
19
+ puts " * Functions: #{rubulator.functions.sort.join(', ')}"
20
+ puts " * Constants: #{rubulator.constants.sort.join(', ')}"
21
+ puts
22
+
23
+ while (line = Readline.readline("\u001b[32;1m>>\u001b[0m \u001b[1m", true))
24
+ print "\u001b[0m"
25
+ begin
26
+ result = rubulator.calculate(line)
27
+ if result
28
+ puts format("\u001b[34m$$ <- \u001b[1m%<result>g\u001b[0m", result: result)
29
+ puts
30
+ end
31
+ rescue Rubulator::CalculationError => e
32
+ puts "\u001b[31m!! Error: #{e}\u001b[0m"
33
+ puts
34
+ end
35
+ end
36
+
37
+ print "\u001b[0m\n"
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubulator/version'
4
+
5
+ class Rubulator
6
+ class CalculationError < StandardError; end
7
+
8
+ # Store and retrieve the value of a variable.
9
+ class Variable
10
+ def initialize(name, value = nil)
11
+ @name = name
12
+ @value = value
13
+ end
14
+
15
+ def store(value)
16
+ @value = value.to_f
17
+ end
18
+
19
+ def to_f
20
+ raise CalculationError, "Variable '#{@name}' not set" unless @value
21
+
22
+ @value.to_f
23
+ end
24
+ end
25
+
26
+ attr_accessor :variables
27
+
28
+ def initialize
29
+ @variables = {}
30
+
31
+ result.store(0.0)
32
+ end
33
+
34
+ def self.calculate(expression)
35
+ Rubulator.new.calculate(expression)
36
+ end
37
+
38
+ def result
39
+ @variables[RESULT_VARIABLE] ||= Variable.new(RESULT_VARIABLE)
40
+ end
41
+
42
+ def calculate(expression)
43
+ return unless expression && expression != ''
44
+
45
+ operands = expression.sub(/^\s+/, '').sub(/\s+$/, '').split(/\s+/)
46
+ result.store(consume(nil, 'initial', operands).to_f)
47
+
48
+ unless operands.empty?
49
+ raise CalculationError, "Operands #{operands.inspect} left over in expression '#{expression}'"
50
+ end
51
+
52
+ result.to_f
53
+ end
54
+
55
+ def operators
56
+ ['='] + OPERATORS
57
+ end
58
+
59
+ def functions
60
+ MATH_FUNCTIONS
61
+ end
62
+
63
+ def constants
64
+ MATH_CONSTANTS.keys.map(&:to_s)
65
+ end
66
+
67
+ def variable_names
68
+ variables.keys.map { |v| VARIABLE_PREFIX + v }
69
+ end
70
+
71
+ private
72
+
73
+ VARIABLE_PREFIX = '$'
74
+ VARIABLE_PREFIX_MATCH_RE = Regexp.new("^#{Regexp.escape(VARIABLE_PREFIX)}")
75
+
76
+ RESULT_VARIABLE = '$'
77
+
78
+ OPERATORS = %w[+ - * / % **].freeze
79
+
80
+ MATH_CONSTANTS = {
81
+ pi: Math::PI,
82
+ e: Math::E
83
+ }.freeze
84
+
85
+ MATH_FUNCTIONS = %w[sqrt cbrt sin sinh cos cosh tan tanh atan atanh log log2 log10].freeze
86
+
87
+ def consume(operator, name, operands)
88
+ raise CalculationError, "Missing #{name} operand for '#{operator || 'main expression'}'" if operands.empty?
89
+
90
+ operand = operands.shift
91
+
92
+ case # rubocop:disable Style/EmptyCaseCondition
93
+ when operand == '='
94
+ variable = consume('=', 'assignment variable', operands)
95
+ unless variable.is_a?(Variable)
96
+ raise CalculationError, "Variable expected for 1st operand, found '#{variable}' instead"
97
+ end
98
+
99
+ value = consume('=', 'value', operands).to_f
100
+
101
+ variable.store(value)
102
+ when operand.start_with?(VARIABLE_PREFIX)
103
+ name = operand.sub(VARIABLE_PREFIX_MATCH_RE, '')
104
+ raise CalculationError, "No variable name provided for '$'" if name == ''
105
+
106
+ @variables[name] ||= Variable.new(name)
107
+ when operand == ','
108
+ consume(',', '1st', operands)
109
+ consume(',', '2nd', operands)
110
+ when OPERATORS.include?(operand)
111
+ op1 = consume(operand, '1st', operands).to_f
112
+ op2 = consume(operand, '2nd', operands).to_f
113
+ op1.send(operand.to_sym, op2)
114
+ when MATH_FUNCTIONS.include?(operand)
115
+ op1 = consume(operand, 'input', operands).to_f
116
+ Math.send(operand.to_sym, op1)
117
+ when MATH_CONSTANTS.include?(operand.downcase.to_sym)
118
+ MATH_CONSTANTS.fetch(operand.downcase.to_sym)
119
+ when /[+-]?[0-9][0-9.]*/.match(operand)
120
+ operand
121
+ else
122
+ raise CalculationError, "Unknown argument '#{operand}'"
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Rubulator
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/rubulator/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rubulator'
7
+ spec.version = Rubulator::VERSION
8
+ spec.authors = ['Jeremy Cole']
9
+ spec.email = ['jeremy@jcole.us']
10
+
11
+ spec.summary = 'A stupid Ruby-based calculator'
12
+ spec.description = 'A stupid Ruby-based calculator which uses only prefix operators.'
13
+ spec.homepage = 'https://github.com/jeremycole/rubulator'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/jeremycole/rubulator'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/jeremycole/rubulator'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency('readline', '~> 0')
31
+ spec.add_dependency('readline-ext', '~> 0.1.0')
32
+
33
+ spec.add_development_dependency('rubocop', '~> 0.89.0')
34
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubulator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Cole
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: readline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: readline-ext
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.89.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.89.0
55
+ description: A stupid Ruby-based calculator which uses only prefix operators.
56
+ email:
57
+ - jeremy@jcole.us
58
+ executables:
59
+ - rubulator
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".ruby-version"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - exe/rubulator
75
+ - lib/rubulator.rb
76
+ - lib/rubulator/version.rb
77
+ - rubulator.gemspec
78
+ homepage: https://github.com/jeremycole/rubulator
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ homepage_uri: https://github.com/jeremycole/rubulator
83
+ source_code_uri: https://github.com/jeremycole/rubulator
84
+ changelog_uri: https://github.com/jeremycole/rubulator
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.3.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A stupid Ruby-based calculator
104
+ test_files: []