hesabu 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1890b75fe839c948d3209040eb520e852e11681babaf9b2261a8bc932cf42230
4
+ data.tar.gz: 51c8bb8eb5ea57c69dbe4abc1549c51568cdca686f249648df1507394281a8c9
5
+ SHA512:
6
+ metadata.gz: 5f6b9395785205e50a586e57ebe9de9814a01bad904699a750a1d5683831a28c49fe0f0691d0c1272cbfff5eb628bedef6bf7e2a7af76a9f5a6de314a7d10769
7
+ data.tar.gz: da50d8020081d571c5ef11819068f9377ebd39b159c5e2b57d6b78b5934a8c4505434ff42730283d85b1adce818f325c479a8526ba8bc817b4a7ae4815c86d25
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
12
+
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,60 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - db/schema.rb
5
+ Rails:
6
+ Enabled: true
7
+ Layout/AlignHash:
8
+ # Alignment of entries using hash rocket as separator. Valid values are:
9
+ #
10
+ # key - left alignment of keys
11
+ # 'a' => 2
12
+ # 'bb' => 3
13
+ # separator - alignment of hash rockets, keys are right aligned
14
+ # 'a' => 2
15
+ # 'bb' => 3
16
+ # table - left alignment of keys, hash rockets, and values
17
+ # 'a' => 2
18
+ # 'bb' => 3
19
+ EnforcedHashRocketStyle: table
20
+ # Alignment of entries using colon as separator. Valid values are:
21
+ #
22
+ # key - left alignment of keys
23
+ # a: 0
24
+ # bb: 1
25
+ # separator - alignment of colons, keys are right aligned
26
+ # a: 0
27
+ # bb: 1
28
+ # table - left alignment of keys and values
29
+ # a: 0
30
+ # bb: 1
31
+ EnforcedColonStyle: table
32
+ LineLength:
33
+ Max: 100
34
+ StringLiterals:
35
+ EnforcedStyle: double_quotes
36
+ Style/StringLiterals:
37
+ EnforcedStyle: double_quotes
38
+ MethodCalledOnDoEndBlock:
39
+ Description: 'Avoid chaining a method call on a do...end block.'
40
+ Enabled: true
41
+ ClassLength:
42
+ Max: 400
43
+ Documentation:
44
+ Enabled: false
45
+ Style/FrozenStringLiteralComment:
46
+ Enabled: false
47
+ # I don't think there's a good way to enforce this sometime commit_sha1 is ok sometimes user_1 is ok
48
+ Naming/VariableNumber:
49
+ Enabled: false
50
+ # this one triggers a lot of false positive as orbf rules are "%{}"-based
51
+ Style/FormatStringToken:
52
+ EnforcedStyle: template
53
+ Metrics/LineLength:
54
+ Exclude:
55
+ - 'spec/**/*.rb'
56
+ Metrics/BlockLength:
57
+ Exclude:
58
+ - 'spec/**/*.rb'
59
+
60
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.4.2
6
+ before_install: gem install bundler -v 1.16.1
7
+ before_script:
8
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
9
+ > ./cc-test-reporter
10
+ - chmod +x ./cc-test-reporter
11
+ - "./cc-test-reporter before-build"
12
+ script:
13
+ - bundle exec rspec
14
+ after_script:
15
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
16
+ env:
17
+ global:
18
+ secure: kbyOp8JIB3pR2zr6h8UmcIIwVhdF51AU2NPknis2hry1SnORUrX3b7SAgy5J0nW54U+fk857Z9FiEDhuRq74LGug+eqPqeSa7Kjip1IaPk3/jk+0nE9HUutia5knzwrWf/LlPwX0fSY4dEzmmlittCvICxlIVXITn5dtCWzkW389K5xmW6oRT3dxzAohbl36vlpshDSgF84M3LGPC0WVl9qUBLVQWep0XU8s6oRFw12WexlpFCNSDXDGDkmJtcYAB6rjEHEZ/9rLpmrVH8XrvuVtUyy/DnOZ+hW05QtdPuwcHG9sVRkbEZxd2X+KoDNGafkMOzBObiA8hAwL2NUGl/g7jxgVU90Sv4YDQpTkodIhrxadP/6pyqGwRCTVx5MLu5qn2FCIbYq0B0W478MUWOa9CmfK7+0mpjrfCWEjqZbHPm4ZRh3ZOm0AhZSOvwelH5Eluam3qcbbd7OBaR/plS2Zhr0sw2VcCkaWsTu2QALlzw7A8E65UbkZqKzTFPcGDs97bD4n18OwlLy1JWQK1Px5IA3GISN5MnC40JvKUmoy2tcLd4C2jV/BYjxESkP6hBDTxGfXbXkHQsdBOJ53egkTpkGk2cQaW70IBHWCR6wJcvIdopTSsS2NqVm6Ca9HPOSFfxHgbnxhoejDelDTdvIMX8QlGdrpL7vXb6c8R5Q=
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hesabu.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,114 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hesabu (0.1.1)
5
+ parslet
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ ast (2.4.0)
13
+ byebug (10.0.2)
14
+ diff-lcs (1.3)
15
+ docile (1.3.0)
16
+ erubis (2.7.0)
17
+ faraday (0.14.0)
18
+ multipart-post (>= 1.2, < 3)
19
+ flay (2.10.0)
20
+ erubis (~> 2.7.0)
21
+ path_expander (~> 1.0)
22
+ ruby_parser (~> 3.0)
23
+ sexp_processor (~> 4.0)
24
+ gitlab (4.3.0)
25
+ httparty
26
+ terminal-table
27
+ httparty (0.16.2)
28
+ multi_xml (>= 0.5.2)
29
+ json (2.1.0)
30
+ multi_xml (0.6.0)
31
+ multipart-post (2.0.0)
32
+ octokit (4.8.0)
33
+ sawyer (~> 0.8.0, >= 0.5.3)
34
+ parallel (1.12.1)
35
+ parser (2.5.1.0)
36
+ ast (~> 2.4.0)
37
+ parslet (1.8.2)
38
+ path_expander (1.0.2)
39
+ powerpack (0.1.1)
40
+ pronto (0.9.5)
41
+ gitlab (~> 4.0, >= 4.0.0)
42
+ httparty (>= 0.13.7)
43
+ octokit (~> 4.7, >= 4.7.0)
44
+ rainbow (~> 2.1)
45
+ rugged (~> 0.24, >= 0.23.0)
46
+ thor (~> 0.19.0)
47
+ pronto-flay (0.9.0)
48
+ flay (~> 2.8)
49
+ pronto (~> 0.9.0)
50
+ pronto-rubocop (0.9.0)
51
+ pronto (~> 0.9.0)
52
+ rubocop (~> 0.38, >= 0.35.0)
53
+ pronto-simplecov (0.1.0)
54
+ pronto (~> 0.9.0)
55
+ simplecov
56
+ public_suffix (3.0.2)
57
+ rainbow (2.2.2)
58
+ rake
59
+ rake (10.5.0)
60
+ rspec (3.7.0)
61
+ rspec-core (~> 3.7.0)
62
+ rspec-expectations (~> 3.7.0)
63
+ rspec-mocks (~> 3.7.0)
64
+ rspec-core (3.7.1)
65
+ rspec-support (~> 3.7.0)
66
+ rspec-expectations (3.7.0)
67
+ diff-lcs (>= 1.2.0, < 2.0)
68
+ rspec-support (~> 3.7.0)
69
+ rspec-mocks (3.7.0)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.7.0)
72
+ rspec-support (3.7.1)
73
+ rubocop (0.55.0)
74
+ parallel (~> 1.10)
75
+ parser (>= 2.5)
76
+ powerpack (~> 0.1)
77
+ rainbow (>= 2.2.2, < 4.0)
78
+ ruby-progressbar (~> 1.7)
79
+ unicode-display_width (~> 1.0, >= 1.0.1)
80
+ ruby-progressbar (1.9.0)
81
+ ruby_parser (3.11.0)
82
+ sexp_processor (~> 4.9)
83
+ rugged (0.27.0)
84
+ sawyer (0.8.1)
85
+ addressable (>= 2.3.5, < 2.6)
86
+ faraday (~> 0.8, < 1.0)
87
+ sexp_processor (4.10.1)
88
+ simplecov (0.16.1)
89
+ docile (~> 1.1)
90
+ json (>= 1.8, < 3)
91
+ simplecov-html (~> 0.10.0)
92
+ simplecov-html (0.10.2)
93
+ terminal-table (1.8.0)
94
+ unicode-display_width (~> 1.1, >= 1.1.1)
95
+ thor (0.19.4)
96
+ unicode-display_width (1.3.2)
97
+
98
+ PLATFORMS
99
+ ruby
100
+
101
+ DEPENDENCIES
102
+ bundler (~> 1.16)
103
+ byebug
104
+ hesabu!
105
+ pronto
106
+ pronto-flay
107
+ pronto-rubocop
108
+ pronto-simplecov
109
+ rake (~> 10.0)
110
+ rspec (~> 3.0)
111
+ simplecov
112
+
113
+ BUNDLED WITH
114
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Stéphan Mestach
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.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ [![Maintainability](https://api.codeclimate.com/v1/badges/f2643a76ea031525ed1f/maintainability)](https://codeclimate.com/github/BLSQ/hesabu/maintainability)
2
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/f2643a76ea031525ed1f/test_coverage)](https://codeclimate.com/github/BLSQ/hesabu/test_coverage)
3
+ [![Build Status](https://travis-ci.org/BLSQ/hesabu.svg?branch=master)](https://travis-ci.org/BLSQ/hesabu)
4
+
5
+ # Hesabu
6
+
7
+ Hesabu : equation solver based on parslet.
8
+
9
+
10
+ ## License
11
+
12
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hesabu"
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__)
data/bin/setup ADDED
@@ -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
data/hesabu.gemspec ADDED
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hesabu/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hesabu"
8
+ spec.version = Hesabu::VERSION
9
+ spec.authors = ["Stéphan Mestach"]
10
+ spec.email = ["smestach@bluesquarehub.com"]
11
+
12
+ spec.summary = %q{arithmetic equation solver.}
13
+ spec.description = %q{arithmetic equation solver.}
14
+ spec.homepage = "https://github.com/BLSQ/hesabu"
15
+ spec.license = "MIT"
16
+
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "parslet"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+
33
+ spec.add_development_dependency "byebug"
34
+ spec.add_development_dependency "pronto"
35
+ spec.add_development_dependency "pronto-flay"
36
+ spec.add_development_dependency "pronto-rubocop"
37
+ spec.add_development_dependency "pronto-simplecov"
38
+ spec.add_development_dependency "simplecov"
39
+ end
@@ -0,0 +1,22 @@
1
+ module Hesabu
2
+ class Interpreter < Parslet::Transform
3
+ rule(left: simple(:left),
4
+ right: simple(:right),
5
+ op: simple(:op)) do
6
+ Hesabu::Types::Operation.new(left, op, right)
7
+ end
8
+ rule(plist: sequence(:arr)) { arr }
9
+ rule(plist: "()") { [] }
10
+ rule(fcall: { name: simple(:name), varlist: sequence(:vars) }) do
11
+ Hesabu::Types::FunCall.new(name, vars)
12
+ end
13
+ rule(identifier: simple(:id)) { id.to_s }
14
+ rule(variable: simple(:variable)) do |d|
15
+ d[:var_identifiers]&.add(d[:variable])
16
+ Hesabu::Types::IdentifierLit.new(d[:variable], d[:doc])
17
+ end
18
+
19
+ rule(integer: simple(:integer)) { Hesabu::Types::IntLit.new(integer) }
20
+ rule(float: simple(:float)) { Hesabu::Types::FloatLit.new(float) }
21
+ end
22
+ end
@@ -0,0 +1,71 @@
1
+ module Hesabu
2
+ class Parser < Parslet::Parser
3
+ def cts(atom_arg)
4
+ atom_arg >> space?
5
+ end
6
+
7
+ # simple things
8
+ rule(:lparen) { str('(') >> space? }
9
+ rule(:rparen) { str(')') >> space? }
10
+ rule(:comma) { str(',') >> space? }
11
+ rule(:space) { match["\s"] | match["\t"] | match["\n"] }
12
+ rule(:spaces) { space.repeat }
13
+ rule(:space?) { spaces.maybe }
14
+
15
+ rule(:identifier) do
16
+ cts((match['a-zA-Z'] >> match['a-zA-Z0-9_'].repeat).as(:identifier))
17
+ end
18
+
19
+ rule(:separator) { str(';') }
20
+
21
+ rule(:digit) { match['0-9'] }
22
+
23
+ rule(:integer) do
24
+ cts((str('-').maybe >> match['1-9'] >> digit.repeat).as(:integer) | str('0').as(:integer))
25
+ end
26
+
27
+ rule(:float) do
28
+ cts((str('-').maybe >> digit.repeat(1) >> str('.') >> digit.repeat(1)).as(:float))
29
+ end
30
+
31
+ # arithmetic
32
+
33
+ rule(:expression) { sum | comparison | variable | pexpression }
34
+ rule(:pexpression) { lparen >> expression >> rparen }
35
+
36
+ rule(:variable) { identifier.as(:variable) } # gets simplified into a value, an "identifier" does not
37
+ rule(:sum_op) { match('[+-]') >> space? }
38
+ rule(:mul_op) { match('[*/]') >> space? }
39
+ rule(:comparison_op) { (str('<=') | str('>=') | str('==') | str('<') | str('=') | str('>')) >> space? }
40
+
41
+ rule(:atom) { pexpression | float | integer | fcall.as(:fcall) | variable }
42
+
43
+ rule(:comparison) do
44
+ atom.as(:left) >> comparison_op.as(:op) >> atom.as(:right)
45
+ end
46
+
47
+ rule(:sum) do
48
+ mul.as(:left) >> sum_op.as(:op) >> sum.as(:right) | mul | comparison
49
+ end
50
+
51
+ rule(:mul) do
52
+ atom.as(:left) >> mul_op.as(:op) >> mul.as(:right) | comparison | atom
53
+ end
54
+
55
+ # lists
56
+ rule(:varlist) { expression >> (comma >> expression).repeat }
57
+ rule(:pvarlist) { (lparen >> varlist.repeat >> rparen).as(:plist) }
58
+
59
+ # functions
60
+ rule(:fdef_keyword) { str('def ') >> space? }
61
+ rule(:fend_keyword) { str('endf') >> space? }
62
+ rule(:fcall) { identifier.as(:name) >> pvarlist.as(:varlist) }
63
+
64
+ # root
65
+ rule(:command) do
66
+ sum
67
+ end
68
+ rule(:commands) { commands.repeat }
69
+ root :command
70
+ end
71
+ end
@@ -0,0 +1,70 @@
1
+ module Hesabu
2
+ class Solver
3
+ include TSort
4
+
5
+ Equation = Struct.new(:name, :evaluable, :dependencies)
6
+ EMPTY_DEPENDENCIES = [].freeze
7
+ FakeEvaluable = Struct.new(:eval)
8
+
9
+ def initialize
10
+ @parser = ::Hesabu::Parser.new
11
+ @interpreter = ::Hesabu::Interpreter.new
12
+ @equations = {}
13
+ @bindings = {}
14
+ end
15
+
16
+ alias solving_order tsort
17
+
18
+ def add(name, raw_expression)
19
+ expression = raw_expression
20
+ raw_expression_as_i = raw_expression.to_i
21
+ raw_expression_as_f = raw_expression.to_f
22
+
23
+ if raw_expression == raw_expression_as_i.to_s
24
+ @equations[name] = Equation.new(
25
+ name,
26
+ FakeEvaluable.new(raw_expression_as_i),
27
+ EMPTY_DEPENDENCIES
28
+ )
29
+ elsif raw_expression == raw_expression_as_f.to_s
30
+ @equations[name] = Equation.new(
31
+ name,
32
+ FakeEvaluable.new(raw_expression_as_f),
33
+ EMPTY_DEPENDENCIES
34
+ )
35
+ else
36
+ expression = raw_expression.gsub(/\r\n?/, "")
37
+ ast_tree = begin
38
+ @parser.parse(expression)
39
+ rescue Parslet::ParseFailed => e
40
+ raise "failed to parse #{name} := #{expression} : #{e.message}"
41
+ end
42
+ var_identifiers = Set.new
43
+ interpretation = @interpreter.apply(
44
+ ast_tree,
45
+ doc: @bindings,
46
+ var_identifiers: var_identifiers
47
+ )
48
+ @equations[name] = Equation.new(name, interpretation, var_identifiers)
49
+ end
50
+ end
51
+
52
+ def solve!
53
+ solving_order.each do |name|
54
+ equation = @equations[name]
55
+ @bindings[equation.name] = equation.evaluable.eval
56
+ end
57
+ solution = @bindings.dup
58
+ @bindings.clear
59
+ solution
60
+ end
61
+
62
+ def tsort_each_node(&block)
63
+ @equations.each_key(&block)
64
+ end
65
+
66
+ def tsort_each_child(node, &block)
67
+ @equations[node].dependencies.each(&block)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ module Hesabu
2
+ module Types
3
+ FloatLit = Struct.new(:float) do
4
+ def eval
5
+ float.to_f
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,90 @@
1
+ module Hesabu
2
+ module Types
3
+ class IfFunction
4
+ def call(args)
5
+ raise "expected args #{name} : #{args}" unless args.size != 2
6
+ condition_expression = args[0]
7
+ condition = condition_expression.eval
8
+ condition ? args[1].eval : args[2].eval
9
+ end
10
+ end
11
+
12
+ class SumFunction
13
+ def call(args)
14
+ values = args.map(&:eval)
15
+ values.reduce(0, :+)
16
+ end
17
+ end
18
+
19
+ class ScoreTableFunction
20
+ def call(args)
21
+ values = args.map(&:eval)
22
+ target = values.shift
23
+ matching_rules = values.each_slice(3).find do |lower, greater, result|
24
+ greater.nil? || result.nil? ? true : lower <= target && target < greater
25
+ end
26
+ matching_rules.last
27
+ end
28
+ end
29
+
30
+ class AvgFunction
31
+ def call(args)
32
+ values = args.map(&:eval)
33
+ values.inject(0.0) { |acc, elem| acc + elem } / values.size
34
+ end
35
+ end
36
+
37
+ class SafeDivFunction
38
+ def call(args)
39
+ eval_denom = args[1].eval
40
+ if eval_denom == 0
41
+ 0
42
+ else
43
+ eval_num = args[0].eval
44
+ eval_num / eval_denom.to_f
45
+ end
46
+ end
47
+ end
48
+
49
+ class MinFunction
50
+ def call(args)
51
+ values = args.map(&:eval)
52
+ values.min
53
+ end
54
+ end
55
+
56
+ class MaxFunction
57
+ def call(args)
58
+ values = args.map(&:eval)
59
+ values.max
60
+ end
61
+ end
62
+
63
+ class RandbetweenFunction
64
+ def call(args)
65
+ values = args.map(&:eval)
66
+ rand(values.first..values.last)
67
+ end
68
+ end
69
+
70
+ FUNCTIONS = {
71
+ "if" => IfFunction.new,
72
+ "sum" => SumFunction.new,
73
+ "avg" => AvgFunction.new,
74
+ "min" => MinFunction.new,
75
+ "max" => MaxFunction.new,
76
+ "safe_div" => SafeDivFunction.new,
77
+ "randbetween" => RandbetweenFunction.new,
78
+ "score_table" => ScoreTableFunction.new
79
+ }.freeze
80
+
81
+ FunCall = Struct.new(:name, :args) do
82
+ def eval
83
+ function_name = name.strip.downcase
84
+ function = FUNCTIONS[function_name]
85
+ raise "unsupported function call : #{function_name} only knows #{FUNCTIONS.keys.join(', ')}" unless function
86
+ function.call(args)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,9 @@
1
+ module Hesabu
2
+ module Types
3
+ IdentifierLit = Struct.new(:var_identifier, :bindings) do
4
+ def eval
5
+ bindings[var_identifier] ||= 0
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Hesabu
2
+ module Types
3
+ IntLit = Struct.new(:int) do
4
+ def eval
5
+ int.to_i
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ module Hesabu
2
+ module Types
3
+ Operation = Struct.new(:left, :operator, :right) do
4
+ def eval
5
+ op = operator.str.strip
6
+
7
+ result = if op == "+"
8
+ left.eval + right.eval
9
+ elsif op == "-"
10
+ left.eval - right.eval
11
+ elsif op == "*"
12
+ left.eval * right.eval
13
+ elsif op == "/"
14
+ left.eval / right.eval.to_f
15
+ elsif op == ">"
16
+ left.eval > right.eval
17
+ elsif op == "<"
18
+ left.eval < right.eval
19
+ elsif op == ">="
20
+ left.eval >= right.eval
21
+ elsif op == "<="
22
+ left.eval <= right.eval
23
+ elsif op == "=" || op == "=="
24
+ left.eval == right.eval
25
+ else
26
+ raise "unsupported operand : #{operator} : #{left} #{operator} #{right}"
27
+ end
28
+ # puts "#{left.eval} #{op} #{right.eval} => #{result}"
29
+ result
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Hesabu
2
+ VERSION = "0.1.1"
3
+ end
data/lib/hesabu.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "hesabu/version"
2
+ require "parslet"
3
+ require_relative "./hesabu/parser"
4
+ require_relative "./hesabu/types/float_lit"
5
+ require_relative "./hesabu/types/fun_call"
6
+ require_relative "./hesabu/types/indentifier_lit"
7
+ require_relative "./hesabu/types/int_lit"
8
+ require_relative "./hesabu/types/operation"
9
+
10
+ require_relative "./hesabu/interpreter"
11
+ require_relative "./hesabu/solver"
12
+
13
+ module Hesabu
14
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hesabu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Stéphan Mestach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parslet
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pronto
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pronto-flay
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pronto-rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pronto-simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: arithmetic equation solver.
154
+ email:
155
+ - smestach@bluesquarehub.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".rubocop.yml"
163
+ - ".ruby-version"
164
+ - ".travis.yml"
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - hesabu.gemspec
173
+ - lib/hesabu.rb
174
+ - lib/hesabu/interpreter.rb
175
+ - lib/hesabu/parser.rb
176
+ - lib/hesabu/solver.rb
177
+ - lib/hesabu/types/float_lit.rb
178
+ - lib/hesabu/types/fun_call.rb
179
+ - lib/hesabu/types/indentifier_lit.rb
180
+ - lib/hesabu/types/int_lit.rb
181
+ - lib/hesabu/types/operation.rb
182
+ - lib/hesabu/version.rb
183
+ homepage: https://github.com/BLSQ/hesabu
184
+ licenses:
185
+ - MIT
186
+ metadata: {}
187
+ post_install_message:
188
+ rdoc_options: []
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ requirements: []
202
+ rubyforge_project:
203
+ rubygems_version: 2.7.6
204
+ signing_key:
205
+ specification_version: 4
206
+ summary: arithmetic equation solver.
207
+ test_files: []