kalc 0.5.9 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kalc (0.5.9)
5
+ parslet (~> 1.3)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ blankslate (2.1.2.4)
11
+ diff-lcs (1.1.3)
12
+ parslet (1.4.0)
13
+ blankslate (~> 2.0)
14
+ rake (0.9.2.2)
15
+ rspec (2.10.0)
16
+ rspec-core (~> 2.10.0)
17
+ rspec-expectations (~> 2.10.0)
18
+ rspec-mocks (~> 2.10.0)
19
+ rspec-core (2.10.1)
20
+ rspec-expectations (2.10.0)
21
+ diff-lcs (~> 1.1.3)
22
+ rspec-mocks (2.10.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ kalc!
29
+ rake
30
+ rspec
data/README.textile CHANGED
@@ -14,6 +14,16 @@ h2. Syntax
14
14
  kalc is a tiny language, and it has very little syntax. It does support
15
15
  functions, variable assignment, and arithmetic.
16
16
 
17
+ h3. Numbers
18
+
19
+ All numbers are floating point.
20
+
21
+ <pre>
22
+ 1 => 1.0
23
+ 2.020 => 2.02
24
+ 1.23E => 12300000000.0
25
+ </pre>
26
+
17
27
  h3. Arithmetic
18
28
 
19
29
  <pre>
data/lib/kalc/grammar.rb CHANGED
@@ -109,8 +109,13 @@ class Kalc::Grammar < Parslet::Parser
109
109
  str('"')
110
110
  }
111
111
 
112
+ rule(:exponent) {
113
+ match('[eE]') >> match('[-+]').maybe >> digits
114
+ }
115
+
112
116
  rule(:number) {
113
- (match('[+-]').maybe >> digits >> (str('.') >> digits).maybe).as(:number) >> spaces?
117
+ (match('[+-]').maybe >>
118
+ digits >> (str('.') >> digits).maybe >> exponent.maybe).as(:number) >> spaces?
114
119
  }
115
120
 
116
121
  rule(:identifier) {
@@ -5,7 +5,7 @@ require 'pp'
5
5
  module Kalc
6
6
  class Interpreter
7
7
 
8
- attr_reader :env
8
+ attr_accessor :env
9
9
 
10
10
  def initialize
11
11
  @env = Environment.new do |env|
@@ -40,6 +40,32 @@ module Kalc
40
40
  retval
41
41
  })
42
42
 
43
+ env.add_function(:RAND, lambda { |cxt, val|
44
+ rand(val.eval(cxt))
45
+ })
46
+
47
+ env.add_function(:SYSTEM, lambda { |cxt, val|
48
+ throw "Nope. I don't think so!"
49
+ })
50
+
51
+ # IS?
52
+ env.add_function(:ISLOGICAL, lambda { |cxt, val|
53
+ newval = val.eval(cxt)
54
+ newval == true || newval == false
55
+ })
56
+
57
+ env.add_function(:ISNONTEXT, lambda { |cxt, val|
58
+ !val.eval(cxt).is_a? String
59
+ })
60
+
61
+ env.add_function(:ISNUMBER, lambda { |cxt, val|
62
+ val.eval(cxt).is_a? Numeric
63
+ })
64
+
65
+ env.add_function(:ISTEXT, lambda { |cxt, val|
66
+ val.eval(cxt).is_a? String
67
+ })
68
+
43
69
  # Math
44
70
  env.add_function(:ABS, lambda { |cxt, val|
45
71
  val.eval(cxt).abs
@@ -88,6 +114,16 @@ module Kalc
88
114
  })
89
115
  end
90
116
 
117
+ # Change case of text
118
+ env.add_function(:UPPER, lambda { |cxt, val|
119
+ val.eval(cxt).upcase
120
+ })
121
+
122
+ env.add_function(:LOWER, lambda { |cxt, val|
123
+ val.eval(cxt).downcase
124
+ })
125
+
126
+ # Debug
91
127
  env.add_function(:P, lambda { |cxt, *output|
92
128
  p output
93
129
  })
@@ -101,7 +137,6 @@ module Kalc
101
137
  })
102
138
 
103
139
  end
104
-
105
140
  end
106
141
 
107
142
  def load_stdlib(grammar, transform)
data/lib/kalc/repl.rb CHANGED
@@ -6,12 +6,16 @@ require 'parslet/convenience'
6
6
  module Kalc
7
7
  class Repl
8
8
 
9
+ def load_env
10
+ @kalc = Kalc::Runner.new(true)
11
+ end
12
+
9
13
  def run
10
14
 
11
15
  puts heading
12
16
 
13
17
  # Load Kalc with debug
14
- @kalc = Kalc::Runner.new(true)
18
+ load_env
15
19
 
16
20
  puts "You are ready to go. Have fun!"
17
21
  puts ""
@@ -36,6 +40,8 @@ module Kalc
36
40
  puts @kalc.interpreter.env.functions.map { |f| f.first }.join(", ")
37
41
  when input == 'variables'
38
42
  puts @kalc.interpreter.env.variables.map { |v| "#{v[0]} = #{v[1]}" }.join("\n\r")
43
+ when input == 'reload'
44
+ load_env
39
45
  when input == 'ast'
40
46
  pp @kalc.ast
41
47
  when input != ""
data/lib/kalc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kalc
2
- VERSION = "0.5.9"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -73,6 +73,11 @@ describe Kalc::Interpreter do
73
73
  it { evaluate("1.01 = 1.02").should == false }
74
74
  end
75
75
 
76
+ context "Exponents" do
77
+ it { evaluate("1.23e+10").should == 12300000000.0 }
78
+ it { evaluate("1.23e-10").should == 1.23e-10 }
79
+ end
80
+
76
81
  private
77
82
  def evaluate(expression)
78
83
  g = @grammar.parse(expression)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kalc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -69,6 +69,7 @@ extensions: []
69
69
  extra_rdoc_files: []
70
70
  files:
71
71
  - Gemfile
72
+ - Gemfile.lock
72
73
  - LICENSE
73
74
  - README.textile
74
75
  - bin/ikalc
@@ -119,3 +120,4 @@ test_files:
119
120
  - spec/interpreter_spec.rb
120
121
  - spec/spec_helper.rb
121
122
  - spec/stdlib_spec.rb
123
+ has_rdoc: