logic 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,13 @@ require 'array'
2
2
  require 'test_case_set'
3
3
  require 'truth_table'
4
4
 
5
- module LogicStatement
5
+ class Treetop::Runtime::SyntaxNode
6
+ def condition_identifiers
7
+ []
8
+ end
9
+ end
6
10
 
11
+ module Decision
7
12
  def test_cases
8
13
  @test_cases ||= TestCaseSet.new(condition_identifiers, self)
9
14
  end
@@ -15,12 +20,10 @@ module LogicStatement
15
20
  def mcdc_pairs
16
21
  test_cases.mcdc_pairs
17
22
  end
18
-
19
23
  end
20
24
 
21
25
  module Condition
22
-
23
- include LogicStatement
26
+ include Decision
24
27
 
25
28
  def condition_identifiers
26
29
  [text_value]
@@ -29,12 +32,10 @@ module Condition
29
32
  def evaluate(conditions)
30
33
  conditions.shift
31
34
  end
32
-
33
35
  end
34
36
 
35
- module Decision
36
-
37
- include LogicStatement
37
+ module BinaryDecision
38
+ include Decision
38
39
 
39
40
  def condition_identifiers
40
41
  elements.map(&:condition_identifiers).flatten
@@ -45,27 +46,44 @@ module Decision
45
46
  right = operand_2.evaluate(conditions)
46
47
  operator.apply(left, right)
47
48
  end
49
+ end
50
+
51
+ module Negation
52
+ include Decision
53
+
54
+ def condition_identifiers
55
+ operand.condition_identifiers
56
+ end
48
57
 
58
+ def evaluate(conditions)
59
+ negate(operand.evaluate(conditions))
60
+ end
61
+
62
+ def negate(value)
63
+ 1 ^ value
64
+ end
49
65
  end
50
66
 
51
67
  module Parenthesized
52
-
53
- include LogicStatement
68
+ include Decision
54
69
 
55
70
  def condition_identifiers
56
- binary_condition.condition_identifiers
71
+ contents.condition_identifiers
57
72
  end
58
73
 
59
74
  def evaluate(conditions)
60
- binary_condition.evaluate(conditions)
75
+ contents.evaluate(conditions)
61
76
  end
62
-
63
77
  end
64
78
 
65
- module NonCondition
66
-
67
- def condition_identifiers
68
- []
79
+ module And
80
+ def apply(a, b)
81
+ a & b
69
82
  end
83
+ end
70
84
 
85
+ module Or
86
+ def apply(a, b)
87
+ a | b
88
+ end
71
89
  end
@@ -1,51 +1,39 @@
1
1
  grammar Logic
2
2
 
3
3
  rule decision
4
- binary_condition / condition
4
+ binary_decision / unary_decision
5
5
  end
6
6
 
7
- rule binary_condition
8
- operand_1:condition S operator S operand_2:decision <Decision>
7
+ rule binary_decision
8
+ operand_1:unary_decision space
9
+ operator space
10
+ operand_2:decision <BinaryDecision>
9
11
  end
10
12
 
11
- rule condition
12
- parenthesized / variable
13
+ rule unary_decision
14
+ negation / condition / parenthesized
15
+ end
16
+
17
+ rule negation
18
+ 'not' space operand:unary_decision <Negation>
13
19
  end
14
20
 
15
- rule variable
21
+ rule condition
16
22
  [A-Za-z]+ <Condition>
17
23
  end
18
24
 
19
25
  rule parenthesized
20
- '(' binary_condition ')' <Parenthesized>
26
+ '(' contents:decision ')' <Parenthesized>
21
27
  end
22
28
 
23
29
  rule operator
24
- ( and / or ) <NonCondition>
25
- end
26
-
27
- rule and
28
- 'and' {
29
- def apply(a, b)
30
- a & b
31
- end
32
- }
33
- end
34
-
35
- rule or
36
- 'or' {
37
- def apply(a, b)
38
- a | b
39
- end
40
- }
30
+ 'and' <And>
31
+ /
32
+ 'or' <Or>
41
33
  end
42
34
 
43
- rule s # Optional space
44
- S?
45
- end
46
-
47
- rule S # Mandatory space
48
- [ \t\n\r]+ <NonCondition>
35
+ rule space
36
+ [ \t\n\r]+
49
37
  end
50
38
 
51
39
  end
@@ -56,6 +56,14 @@ describe LogicParser, :parsing do
56
56
  { [0] => 0,
57
57
  [1] => 1
58
58
  },
59
+ '(Hello)' =>
60
+ { [0] => 0,
61
+ [1] => 1
62
+ },
63
+ 'not Hello' =>
64
+ { [0] => 1,
65
+ [1] => 0
66
+ },
59
67
  'A or B' =>
60
68
  { [0, 0] => 0,
61
69
  [0, 1] => 1,
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bryan Ash
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-28 00:00:00 -04:00
17
+ date: 2010-05-27 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency