kalc 0.9.1 → 1.0.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.
- data/Gemfile.lock +5 -5
- data/README.md +11 -11
- data/kalc.gemspec +1 -1
- data/lib/kalc.rb +1 -0
- data/lib/kalc/ast.rb +3 -3
- data/lib/kalc/interpreter.rb +2 -2
- data/lib/kalc/transform.rb +1 -1
- data/lib/kalc/version.rb +1 -1
- data/spec/grammar_spec.rb +2 -2
- data/spec/interpreter_spec.rb +2 -1
- metadata +23 -15
- checksums.yaml +0 -7
data/Gemfile.lock
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
kalc (0.
|
5
|
-
parslet (~> 1.
|
4
|
+
kalc (1.0.0)
|
5
|
+
parslet (~> 1.6)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
blankslate (
|
10
|
+
blankslate (3.1.3)
|
11
11
|
diff-lcs (1.2.5)
|
12
|
-
parslet (1.
|
13
|
-
blankslate (
|
12
|
+
parslet (1.6.2)
|
13
|
+
blankslate (>= 2.0, <= 4.0)
|
14
14
|
rake (10.1.1)
|
15
15
|
rspec (2.14.1)
|
16
16
|
rspec-core (~> 2.14.0)
|
data/README.md
CHANGED
@@ -20,10 +20,10 @@ functionality.
|
|
20
20
|
Numbers
|
21
21
|
-------
|
22
22
|
|
23
|
-
All numbers are
|
23
|
+
All numbers are BigDecimal ruby class numbers for precision.
|
24
24
|
|
25
|
-
1 => 1.0
|
26
|
-
2.020 => 2.02
|
25
|
+
1 => 1.0
|
26
|
+
2.020 => 2.02
|
27
27
|
1.23E => 12300000000.0
|
28
28
|
|
29
29
|
Arithmetic
|
@@ -37,7 +37,7 @@ Arithmetic is standard infix with nesting via parenthesis.
|
|
37
37
|
Logical operations
|
38
38
|
------------------
|
39
39
|
|
40
|
-
2 < 1 ? 1 : 3 # Ternary
|
40
|
+
2 < 1 ? 1 : 3 # Ternary
|
41
41
|
(1 || 2) # 1.0
|
42
42
|
2 or 3 # 2.0
|
43
43
|
OR(1 > 2, 3 < 2, 8 == 8) # true
|
@@ -47,7 +47,7 @@ Variable assignment
|
|
47
47
|
|
48
48
|
The assignment operator `:=` is borrowed from Pascal. This decision was
|
49
49
|
made for practical reason. Since comparison operators are both `=` and
|
50
|
-
`==` and the language is expression-based, `=` could not be chosen.
|
50
|
+
`==` and the language is expression-based, `=` could not be chosen.
|
51
51
|
|
52
52
|
Variables come in a lot of different flavors.
|
53
53
|
|
@@ -98,7 +98,7 @@ Some of them are:
|
|
98
98
|
# Random number generation
|
99
99
|
RAND
|
100
100
|
|
101
|
-
# System level integration
|
101
|
+
# System level integration
|
102
102
|
SYSTEM
|
103
103
|
|
104
104
|
# Boolean functions
|
@@ -120,7 +120,7 @@ Some of them are:
|
|
120
120
|
REGEXP_MATCH, REGEXP_REPLACE
|
121
121
|
|
122
122
|
# Debugging
|
123
|
-
P, PP, PUTS,
|
123
|
+
P, PP, PUTS,
|
124
124
|
|
125
125
|
# Other
|
126
126
|
PLUS_ONE, MINUS_ONE, SQUARE, CUBE, FIB, FACTORIAL,
|
@@ -129,12 +129,12 @@ Some of them are:
|
|
129
129
|
Loops
|
130
130
|
-----
|
131
131
|
|
132
|
-
There are no looping mechanisms to speak of, but recursion works (pretty) well.
|
132
|
+
There are no looping mechanisms to speak of, but recursion works (pretty) well.
|
133
133
|
**Note:** *go too deep and you might blow the stack!*
|
134
134
|
|
135
|
-
DEFINE SAMPLE_LOOP(a) {
|
136
|
-
PUTS(a)
|
137
|
-
IF(a == 1, 1, SAMPLE_LOOP(a - 1))
|
135
|
+
DEFINE SAMPLE_LOOP(a) {
|
136
|
+
PUTS(a)
|
137
|
+
IF(a == 1, 1, SAMPLE_LOOP(a - 1))
|
138
138
|
}
|
139
139
|
|
140
140
|
There are a few examples of loops via recursion in `lib/stdlib.kalc`
|
data/kalc.gemspec
CHANGED
data/lib/kalc.rb
CHANGED
data/lib/kalc/ast.rb
CHANGED
@@ -77,15 +77,15 @@ module Kalc
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
class
|
80
|
+
class BigDecimalNumber
|
81
81
|
attr_reader :value
|
82
82
|
|
83
83
|
def initialize(value)
|
84
|
-
@value = value
|
84
|
+
@value = BigDecimal.new(value.to_s)
|
85
85
|
end
|
86
86
|
|
87
87
|
def eval(context)
|
88
|
-
|
88
|
+
BigDecimal.new(@value)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
data/lib/kalc/interpreter.rb
CHANGED
@@ -139,7 +139,7 @@ module Kalc
|
|
139
139
|
})
|
140
140
|
|
141
141
|
env.add_function(:DOLLAR, lambda { |cxt, val, decimal_places|
|
142
|
-
"%.#{Integer(decimal_places.eval(cxt))}f" %
|
142
|
+
"%.#{Integer(decimal_places.eval(cxt))}f" % BigDecimal.new(val.eval(cxt))
|
143
143
|
})
|
144
144
|
|
145
145
|
env.add_function(:EXACT, lambda { |cxt, string1, string2|
|
@@ -152,7 +152,7 @@ module Kalc
|
|
152
152
|
})
|
153
153
|
|
154
154
|
env.add_function(:FIXED, lambda { |cxt, val, decimal_places, no_commas|
|
155
|
-
output = "%.#{Integer(decimal_places.eval(cxt))}f" %
|
155
|
+
output = "%.#{Integer(decimal_places.eval(cxt))}f" % BigDecimal.new(val.eval(cxt))
|
156
156
|
output = output.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse if !no_commas.eval(cxt)
|
157
157
|
output
|
158
158
|
})
|
data/lib/kalc/transform.rb
CHANGED
data/lib/kalc/version.rb
CHANGED
data/spec/grammar_spec.rb
CHANGED
@@ -9,7 +9,7 @@ describe Kalc::Grammar do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
context '
|
12
|
+
context 'decimal numbers' do
|
13
13
|
1.upto(10) do |i|
|
14
14
|
1.upto(10) do |n|
|
15
15
|
it { grammar.should parse("#{i}.#{n}") }
|
@@ -27,7 +27,7 @@ describe Kalc::Grammar do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
context 'basic
|
30
|
+
context 'basic decimal math' do
|
31
31
|
1.upto(10) do |i|
|
32
32
|
1.upto(10) do |n|
|
33
33
|
it { grammar.should parse("#{i}.#{n} - #{i}.#{n}") }
|
data/spec/interpreter_spec.rb
CHANGED
@@ -69,11 +69,12 @@ describe Kalc::Interpreter do
|
|
69
69
|
it { evaluate('FALSE && TRUE').should == false }
|
70
70
|
end
|
71
71
|
|
72
|
-
context '
|
72
|
+
context 'Decimal numbers' do
|
73
73
|
it { evaluate('1.01').should == 1.01 }
|
74
74
|
it { evaluate('1.01 + 0.02').should == 1.03 }
|
75
75
|
it { evaluate('1.01 - 0.01').should == 1 }
|
76
76
|
it { evaluate('1.1 + 1.1').should == 2.2 }
|
77
|
+
it { evaluate('1.2 - 1.0').should == 0.2 }
|
77
78
|
it { evaluate('1.01 = 1.01').should == true }
|
78
79
|
it { evaluate('1.01 = 1.02').should == false }
|
79
80
|
end
|
metadata
CHANGED
@@ -1,57 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Chris Parker
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2014-
|
12
|
+
date: 2014-12-12 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rake
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
|
-
- -
|
19
|
+
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: '0'
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
|
-
- -
|
27
|
+
- - ! '>='
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '0'
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: rspec
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
|
-
- -
|
35
|
+
- - ! '>='
|
32
36
|
- !ruby/object:Gem::Version
|
33
37
|
version: '0'
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
|
-
- -
|
43
|
+
- - ! '>='
|
39
44
|
- !ruby/object:Gem::Version
|
40
45
|
version: '0'
|
41
46
|
- !ruby/object:Gem::Dependency
|
42
47
|
name: parslet
|
43
48
|
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
44
50
|
requirements:
|
45
|
-
- -
|
51
|
+
- - ~>
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: '1.
|
53
|
+
version: '1.6'
|
48
54
|
type: :runtime
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
51
58
|
requirements:
|
52
|
-
- -
|
59
|
+
- - ~>
|
53
60
|
- !ruby/object:Gem::Version
|
54
|
-
version: '1.
|
61
|
+
version: '1.6'
|
55
62
|
description: Calculation language slightly based on Excel's formula language.
|
56
63
|
email:
|
57
64
|
- mrcsparker@gmail.com
|
@@ -86,26 +93,27 @@ files:
|
|
86
93
|
- spec/stdlib_spec.rb
|
87
94
|
homepage: https://github.com/mrcsparker/kalc
|
88
95
|
licenses: []
|
89
|
-
metadata: {}
|
90
96
|
post_install_message:
|
91
97
|
rdoc_options: []
|
92
98
|
require_paths:
|
93
99
|
- lib
|
94
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
95
102
|
requirements:
|
96
|
-
- -
|
103
|
+
- - ! '>='
|
97
104
|
- !ruby/object:Gem::Version
|
98
105
|
version: '0'
|
99
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
100
108
|
requirements:
|
101
|
-
- -
|
109
|
+
- - ! '>='
|
102
110
|
- !ruby/object:Gem::Version
|
103
111
|
version: '0'
|
104
112
|
requirements: []
|
105
113
|
rubyforge_project: kalc
|
106
|
-
rubygems_version:
|
114
|
+
rubygems_version: 1.8.23.2
|
107
115
|
signing_key:
|
108
|
-
specification_version:
|
116
|
+
specification_version: 3
|
109
117
|
summary: Small calculation language.
|
110
118
|
test_files:
|
111
119
|
- spec/grammar_spec.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 78824d628e330b0cea023998c7d855eb7ea61059
|
4
|
-
data.tar.gz: 12eceba2e9e1495b692b725902f859373fc6fd10
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 40aead2c8de8ea0094ff49bcc49fdd6655b44de4c12d4f5564685821b4e06061d4480947c1b0937a5f62c6304ccae47d7decd7d4ff1c3f632cb4c2af63b9ba93
|
7
|
-
data.tar.gz: 2c61afc8a347df7dde81435165339ac0e56d221aa79fb2fd755a915b3d9c55582899248361b9ec9549e87b301bb2547053c1b37842d017112b31ed46f5bdbf65
|