skeem 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6261d01a57850b9accad9ba2136b59625c37ea58
4
- data.tar.gz: 7918e865cadb393e285bfdea1e722970b918549b
3
+ metadata.gz: a55997a668dd1095c0228abea2090acdadedf98d
4
+ data.tar.gz: 33938d730ff4696cb52ad1f520fb0a1cc2e6df96
5
5
  SHA512:
6
- metadata.gz: 1a4efb9e1b96a298448c908e11c1df6a03b810586f7008e10eab9c7336705d3df8e4648155f42737f4fd4453e62ad258af031efc3a884019e42f81babc63b722
7
- data.tar.gz: abd048ab5117289e788090947e142805183283b49f05bdd5f16ac491888c8a5f1c6857bc631d51b6db7dc5c9229ccd5b6f65962e7950cdd24bb30c2f761ebb98
6
+ metadata.gz: 293117e7f86f050aaf6617d2e8d5ccdc838c8b37a5d6a57a3e455e2236640b6063e3cb18215e059d5de76976e1d9f2a2c915c19cceccffce02a8eded1aa04663
7
+ data.tar.gz: d71c34ed8c32fc148d161f35c236f38320d629770094dff17b32f6428f5b0d03019644b6adec00f2d8d08699a080fe1745bc74a9e279216a1484a70f1dad5d49
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## [0.0.10] - 2018-09-15
2
+ Added primitive procedures: 'boolean?', 'string?', 'symbol?', and 'not'
3
+
4
+ ### Added
5
+ - Class `PrimitiveBuilder`. Added methods to implement the predicates 'boolean?', 'string?', 'symbol?', and 'not'
6
+ - File `interpreter_spec.rb` methods to implement the predicates 'boolean?', 'string?', 'symbol?'
7
+
8
+ ### Changed
9
+ - Class hierarchy `SExprElement`. Prefix `SExpr` in class names changed into 'Skm'
10
+
11
+ ### Fixed
12
+ - Method `PrimitiveBuilder#create_minus` When '-' has only one argument then it means sign change.
13
+
1
14
  ## [0.0.9] - 2018-09-15
2
15
  Added primitive procedures: 'number?', 'real?', 'integer?'.
3
16
 
@@ -6,23 +6,39 @@ module Skeem
6
6
  def add_primitives(aRuntime)
7
7
  add_arithmetic(aRuntime)
8
8
  add_number_predicates(aRuntime)
9
+ add_boolean_procedures(aRuntime)
10
+ add_string_procedures(aRuntime)
11
+ add_symbol_procedures(aRuntime)
9
12
  end
10
-
13
+
11
14
  private
12
-
15
+
13
16
  def add_arithmetic(aRuntime)
14
17
  def_procedure(aRuntime, create_plus)
15
18
  def_procedure(aRuntime, create_minus)
16
19
  def_procedure(aRuntime, create_multiply)
17
20
  def_procedure(aRuntime, create_divide)
18
21
  end
19
-
22
+
20
23
  def add_number_predicates(aRuntime)
21
24
  def_procedure(aRuntime, create_number?)
22
25
  def_procedure(aRuntime, create_real?)
23
26
  def_procedure(aRuntime, create_integer?)
24
27
  end
25
-
28
+
29
+ def add_boolean_procedures(aRuntime)
30
+ def_procedure(aRuntime, create_not)
31
+ def_procedure(aRuntime, create_boolean?)
32
+ end
33
+
34
+ def add_string_procedures(aRuntime)
35
+ def_procedure(aRuntime, create_string?)
36
+ end
37
+
38
+ def add_symbol_procedures(aRuntime)
39
+ def_procedure(aRuntime, create_symbol?)
40
+ end
41
+
26
42
  def create_plus()
27
43
  plus_code = ->(runtime, arglist) do
28
44
  first_one = arglist.head.evaluate(runtime)
@@ -34,13 +50,17 @@ module Skeem
34
50
 
35
51
  ['+', plus_code]
36
52
  end
37
-
53
+
38
54
  def create_minus()
39
55
  minus_code = ->(runtime, arglist) do
40
56
  first_one = arglist.head.evaluate(runtime)
41
- operands = arglist.tail.to_eval_enum(runtime)
42
57
  raw_result = first_one.value
43
- operands.each { |elem| raw_result -= elem.value }
58
+ if arglist.length > 1
59
+ operands = arglist.tail.to_eval_enum(runtime)
60
+ operands.each { |elem| raw_result -= elem.value }
61
+ else
62
+ raw_result = -raw_result
63
+ end
44
64
  to_skm(raw_result)
45
65
  end
46
66
 
@@ -52,7 +72,7 @@ module Skeem
52
72
  first_one = arglist.head.evaluate(runtime)
53
73
  operands = arglist.tail.to_eval_enum(runtime)
54
74
  raw_result = first_one.value
55
- operands.each { |elem| raw_result *= elem.value }
75
+ operands.each { |elem| raw_result *= elem.value }
56
76
  to_skm(raw_result)
57
77
  end
58
78
 
@@ -96,8 +116,47 @@ module Skeem
96
116
  end
97
117
 
98
118
  ['integer?', pred_code]
99
- end
100
-
119
+ end
120
+
121
+ def create_not()
122
+ logical = ->(runtime, arg) do
123
+ arg_evaluated = arg.evaluate(runtime)
124
+ if arg_evaluated.boolean? && arg_evaluated.value == false
125
+ to_skm(true)
126
+ else
127
+ to_skm(false)
128
+ end
129
+ end
130
+ ['not', logical]
131
+ end
132
+
133
+ def create_boolean?()
134
+ pred_code = ->(runtime, arg) do
135
+ arg_evaluated = arg.evaluate(runtime)
136
+ to_skm(arg_evaluated.boolean?)
137
+ end
138
+
139
+ ['boolean?', pred_code]
140
+ end
141
+
142
+ def create_string?()
143
+ pred_code = ->(runtime, arg) do
144
+ arg_evaluated = arg.evaluate(runtime)
145
+ to_skm(arg_evaluated.string?)
146
+ end
147
+
148
+ ['string?', pred_code]
149
+ end
150
+
151
+ def create_symbol?
152
+ pred_code = ->(runtime, arg) do
153
+ arg_evaluated = arg.evaluate(runtime)
154
+ to_skm(arg_evaluated.symbol?)
155
+ end
156
+
157
+ ['symbol?', pred_code]
158
+ end
159
+
101
160
  def def_procedure(aRuntime, aPair)
102
161
  func = PrimitiveProcedure.new(aPair.first, aPair.last)
103
162
  define(aRuntime, func.identifier, func)
@@ -106,16 +165,16 @@ module Skeem
106
165
  def define(aRuntime, aKey, anEntry)
107
166
  aRuntime.define(aKey, anEntry)
108
167
  end
109
-
168
+
110
169
  # Convert Ruby object into its Skeem counterpart
111
170
  def to_skm(native_obj)
112
171
  case native_obj
113
172
  when TrueClass, FalseClass
114
173
  SkmBoolean.create(native_obj)
115
- when Float
174
+ when Float
116
175
  SkmReal.create(native_obj)
117
176
  when Integer
118
- SkmInteger.create(native_obj)
177
+ SkmInteger.create(native_obj)
119
178
  end
120
179
  end
121
180
  end # module
@@ -29,6 +29,18 @@ module Skeem
29
29
  def integer?
30
30
  false
31
31
  end
32
+
33
+ def boolean?
34
+ false
35
+ end
36
+
37
+ def string?
38
+ false
39
+ end
40
+
41
+ def symbol?
42
+ false
43
+ end
32
44
 
33
45
  # Abstract method.
34
46
  # Part of the 'visitee' role in Visitor design pattern.
@@ -81,6 +93,9 @@ module Skeem
81
93
  end # class
82
94
 
83
95
  class SkmBoolean < SkmTerminal
96
+ def boolean?
97
+ true
98
+ end
84
99
  end # class
85
100
 
86
101
  class SkmNumber < SkmTerminal
@@ -106,6 +121,10 @@ module Skeem
106
121
  def init_value(aValue)
107
122
  super(aValue.dup)
108
123
  end
124
+
125
+ def string?
126
+ true
127
+ end
109
128
  end # class
110
129
 
111
130
  class SkmIdentifier < SkmTerminal
@@ -113,6 +132,10 @@ module Skeem
113
132
  def init_value(aValue)
114
133
  super(aValue.dup)
115
134
  end
135
+
136
+ def symbol?
137
+ true
138
+ end
116
139
  end # class
117
140
 
118
141
  class SkmReserved < SkmIdentifier
@@ -123,7 +146,7 @@ module Skeem
123
146
  attr_accessor(:members)
124
147
  extend Forwardable
125
148
 
126
- def_delegator :@members, :first, :empty?
149
+ def_delegators :@members, :first, :length, :empty?
127
150
 
128
151
  def initialize(theMembers)
129
152
  super(nil)
data/lib/skeem/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Skeem
2
- VERSION = '0.0.9'.freeze
2
+ VERSION = '0.0.10'.freeze
3
3
  end
@@ -102,6 +102,22 @@ module Skeem
102
102
  expect(result).to be_kind_of(SkmReal)
103
103
  expect(result.value).to eq(4.34)
104
104
  end
105
+
106
+ it 'should implement the negation of integer' do
107
+ result = subject.run('(- 3)')
108
+ expect(result).to be_kind_of(SkmInteger)
109
+ expect(result.value).to eq(-3)
110
+ end
111
+
112
+ it 'should implement the substraction of integers' do
113
+ result = subject.run('(- 3 4)')
114
+ expect(result).to be_kind_of(SkmInteger)
115
+ expect(result.value).to eq(-1)
116
+
117
+ result = subject.run('(- 3 4 5)')
118
+ expect(result).to be_kind_of(SkmInteger)
119
+ expect(result.value).to eq(-6)
120
+ end
105
121
 
106
122
  it 'should implement the product of numbers' do
107
123
  result = subject.run('(* 2 3 4)')
@@ -159,7 +175,53 @@ module Skeem
159
175
  result = subject.run(skeem_expr)
160
176
  expect(result.value).to eq(expectation)
161
177
  end
162
- end
178
+ end
179
+
180
+ it 'should implement the not procedure' do
181
+ checks = [
182
+ ['(not #t)', false],
183
+ ['(not 3)', false],
184
+ ['(not #f)', true]
185
+ ]
186
+ checks.each do |(skeem_expr, expectation)|
187
+ result = subject.run(skeem_expr)
188
+ expect(result.value).to eq(expectation)
189
+ end
190
+ end
191
+
192
+ it 'should implement the boolean? procedure' do
193
+ checks = [
194
+ ['(boolean? #f)', true],
195
+ ['(boolean? 0)', false]
196
+ ]
197
+ checks.each do |(skeem_expr, expectation)|
198
+ result = subject.run(skeem_expr)
199
+ expect(result.value).to eq(expectation)
200
+ end
201
+ end
202
+
203
+ it 'should implement the string? procedure' do
204
+ checks = [
205
+ ['(string? #f)', false],
206
+ ['(string? 3)', false],
207
+ ['(string? "hi")', true]
208
+ ]
209
+ checks.each do |(skeem_expr, expectation)|
210
+ result = subject.run(skeem_expr)
211
+ expect(result.value).to eq(expectation)
212
+ end
213
+ end
214
+
215
+ it 'should implement the symbol? procedure' do
216
+ checks = [
217
+ ['(symbol? #f)', false],
218
+ ['(symbol? "bar")', false]
219
+ ]
220
+ checks.each do |(skeem_expr, expectation)|
221
+ result = subject.run(skeem_expr)
222
+ expect(result.value).to eq(expectation)
223
+ end
224
+ end
163
225
  end # context
164
226
  end # describe
165
227
  end # module
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skeem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef