maths 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/maths +38 -0
- data/lib/maths/brain.rb +39 -0
- data/lib/maths/calc.y +67 -0
- data/lib/maths/version.rb +3 -0
- data/lib/maths.rb +8 -0
- data/maths.gemspec +21 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alan Johnson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Maths
|
2
|
+
|
3
|
+
Maths is a command line calculator. I do a lot of number crunching, and always
|
4
|
+
get frustrated with other solutions, like my phone calculator, a physical
|
5
|
+
calculator, or [Alfred](http://www.alfredapp.com/) because they don't keep a good
|
6
|
+
record of what I've done with them when I'm using them for more than one
|
7
|
+
calculation at a time.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Just install the gem:
|
12
|
+
|
13
|
+
$ gem install maths
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To start maths, just run it in your terminal:
|
18
|
+
|
19
|
+
```
|
20
|
+
maths
|
21
|
+
```
|
22
|
+
|
23
|
+
Then do some math:
|
24
|
+
|
25
|
+
```
|
26
|
+
Welcome to maths (version 0.0.1)!
|
27
|
+
Type "exit" to quit.
|
28
|
+
|
29
|
+
maths> 5 + 5
|
30
|
+
= 10
|
31
|
+
maths> 5 / 5
|
32
|
+
= 1
|
33
|
+
maths> 20 * 3
|
34
|
+
= 60
|
35
|
+
maths> 8 - 4
|
36
|
+
= 4
|
37
|
+
maths> 4.4 + 3.2
|
38
|
+
= 7.6000000000000005
|
39
|
+
```
|
40
|
+
|
41
|
+
Maths also supports assigning variables:
|
42
|
+
|
43
|
+
```
|
44
|
+
maths> x = 10
|
45
|
+
= 10
|
46
|
+
maths> x
|
47
|
+
= 10
|
48
|
+
maths> x + 4
|
49
|
+
= 14
|
50
|
+
```
|
51
|
+
|
52
|
+
You can even assign multiple variables at once:
|
53
|
+
|
54
|
+
```
|
55
|
+
maths> x = y = z = 25
|
56
|
+
= 25
|
57
|
+
```
|
58
|
+
|
59
|
+
And of course, sometimes you'll want to close the app:
|
60
|
+
|
61
|
+
```
|
62
|
+
maths> exit
|
63
|
+
```
|
64
|
+
|
65
|
+
Variables persist in maths between runs of the command, so feel free to exit and reopen maths all you want - those variables will stick around between executions.
|
66
|
+
|
67
|
+
Maths is still pretty new - I hacked the original version together on a flight, and it's still maturing. If you'd like to see something added, let me know in the [issue tracker](https://github.com/commondream/maths/issues).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/maths
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "maths")
|
4
|
+
|
5
|
+
# The main script!
|
6
|
+
|
7
|
+
parser = Maths::Calculator.new
|
8
|
+
|
9
|
+
|
10
|
+
puts
|
11
|
+
puts "Welcome to maths (version #{Maths::VERSION})!"
|
12
|
+
puts "Type \"exit\" to quit."
|
13
|
+
puts
|
14
|
+
|
15
|
+
Readline.completion_proc = Proc.new do |str|
|
16
|
+
# TODO: Get this working
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
prompt = "maths> "
|
21
|
+
while command = Readline.readline(prompt, true)
|
22
|
+
break if /exit/i =~ command
|
23
|
+
|
24
|
+
if command == "vars" || command == "variables"
|
25
|
+
puts
|
26
|
+
puts "Defined Variables:"
|
27
|
+
Maths::Brain.read.each do |key, value|
|
28
|
+
puts "#{key}: #{value}"
|
29
|
+
end
|
30
|
+
puts
|
31
|
+
else
|
32
|
+
begin
|
33
|
+
puts "= #{parser.parse(command)}"
|
34
|
+
rescue ParseError
|
35
|
+
puts $!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/maths/brain.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module Maths
|
2
|
+
class Brain
|
3
|
+
|
4
|
+
# Public: Assigns data
|
5
|
+
def self.assign(variable, value)
|
6
|
+
data = read
|
7
|
+
data[variable] = value
|
8
|
+
write(data)
|
9
|
+
|
10
|
+
value
|
11
|
+
end
|
12
|
+
|
13
|
+
# Public: Looks up data
|
14
|
+
def self.lookup(variable)
|
15
|
+
read[variable]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Public: The file we're using to store our memories
|
19
|
+
def self.brain_file
|
20
|
+
File.join(ENV['HOME'], ".maths_brain")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Public: Reads the data out of the brain
|
24
|
+
def self.read
|
25
|
+
if File.exist?(brain_file)
|
26
|
+
JSON.parse(File.read(brain_file))
|
27
|
+
else
|
28
|
+
{}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Public: Reads the
|
33
|
+
def self.write(data)
|
34
|
+
File.open(brain_file, "wb") do |file|
|
35
|
+
file << JSON.dump(data)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/maths/calc.y
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Simple calculator grammar
|
2
|
+
class Calculator
|
3
|
+
prechigh
|
4
|
+
nonassoc UMINUS
|
5
|
+
left '*' '/' '%'
|
6
|
+
left '+' '-'
|
7
|
+
right "="
|
8
|
+
preclow
|
9
|
+
rule
|
10
|
+
target: exp
|
11
|
+
| assign
|
12
|
+
| /* none */ { result = 0 }
|
13
|
+
|
14
|
+
exp: exp '+' exp { result += val[2] }
|
15
|
+
| exp '-' exp { result -= val[2] }
|
16
|
+
| exp '*' exp { result *= val[2] }
|
17
|
+
| exp '/' exp { result /= val[2] }
|
18
|
+
| exp '%' exp { result %= val[2] }
|
19
|
+
| '(' exp ')' { result = val[1] }
|
20
|
+
| '|' exp '|' { result = val[1].abs }
|
21
|
+
| '-' NUMBER =UMINUS { result = -val[1] }
|
22
|
+
| VARIABLE { result = Brain.lookup(val[0])}
|
23
|
+
| NUMBER
|
24
|
+
| FLOAT
|
25
|
+
|
26
|
+
assign: VARIABLE "=" exp { result = Brain.assign(val[0], val[2]) }
|
27
|
+
| VARIABLE "=" assign { result = Brain.assign(val[0], val[2]) }
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
---- header
|
32
|
+
# Generated grammar file.
|
33
|
+
module Maths
|
34
|
+
---- inner
|
35
|
+
|
36
|
+
def parse(str)
|
37
|
+
@q = []
|
38
|
+
until str.empty?
|
39
|
+
case str
|
40
|
+
when /\A\s+/
|
41
|
+
when /\A[A-Za-z]+[0-9]*/
|
42
|
+
@q.push [:VARIABLE, $&]
|
43
|
+
when /\A\d+\.\d+/
|
44
|
+
@q.push [:FLOAT, $&.to_f]
|
45
|
+
when /\A\d+/
|
46
|
+
@q.push [:NUMBER, $&.to_i]
|
47
|
+
when /\A.|\n/o
|
48
|
+
s = $&
|
49
|
+
@q.push [s, s]
|
50
|
+
end
|
51
|
+
str = $'
|
52
|
+
end
|
53
|
+
@q.push [false, '$end']
|
54
|
+
do_parse
|
55
|
+
end
|
56
|
+
|
57
|
+
def next_token
|
58
|
+
@q.shift
|
59
|
+
end
|
60
|
+
|
61
|
+
def assign(val)
|
62
|
+
puts "Assigning: #{val}"
|
63
|
+
val
|
64
|
+
end
|
65
|
+
|
66
|
+
---- footer
|
67
|
+
end
|
data/lib/maths.rb
ADDED
data/maths.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'maths/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "maths"
|
8
|
+
gem.version = Maths::VERSION
|
9
|
+
gem.authors = ["Alan Johnson"]
|
10
|
+
gem.email = ["alan@commondream.net"]
|
11
|
+
gem.description = %q{Your friendly neighborhood console calculator}
|
12
|
+
gem.summary = %q{Your friendly neighborhood console calculator}
|
13
|
+
gem.homepage = "http://github.com/commondream/maths"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency "json"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: maths
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan Johnson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Your friendly neighborhood console calculator
|
31
|
+
email:
|
32
|
+
- alan@commondream.net
|
33
|
+
executables:
|
34
|
+
- maths
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/maths
|
44
|
+
- lib/maths.rb
|
45
|
+
- lib/maths/brain.rb
|
46
|
+
- lib/maths/calc.y
|
47
|
+
- lib/maths/version.rb
|
48
|
+
- maths.gemspec
|
49
|
+
homepage: http://github.com/commondream/maths
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.24
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Your friendly neighborhood console calculator
|
73
|
+
test_files: []
|