olavo_calculator 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/lib/olavo_calculator.rb +1 -1
- data/lib/olavo_calculator/cli.rb +1 -1
- data/lib/olavo_calculator/parser.rb +35 -33
- data/lib/olavo_calculator/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fb77dc3076d776b6cc55c77e0d8745a95cc6a3f
|
4
|
+
data.tar.gz: d08dd0d9685d831816f5a849bfa9a4b504146e17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c72c091c88690c077d5c0943c05a4faaafd5d2a2ed227f65ba6bce17ba4ef1843e3631a731736e39f45fe05bab7d9e7d885c18006102408701ff76faa711b79c
|
7
|
+
data.tar.gz: 1c29c7c667886857fc401ce2aee837513da77390b302871f370f0090ba12c0dd5e19ce351727b911a82a1f3e8f2d054cf43882744e32b8d5b3c0e155e8aff6a1
|
data/lib/olavo_calculator.rb
CHANGED
data/lib/olavo_calculator/cli.rb
CHANGED
@@ -6,69 +6,71 @@ class OlavoCalculator::Parser
|
|
6
6
|
:open_key => :close_key
|
7
7
|
}
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def parse token_array
|
10
|
+
@token_array = token_array
|
11
|
+
detect_expression
|
11
12
|
end
|
12
13
|
|
13
14
|
# expressões podem ser da forma: priority (PLUS|MINUS expr)*, term
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
private
|
16
|
+
def detect_expression
|
17
|
+
raise OlavoException, "Unexpected Ending" if @token_array.empty?
|
18
|
+
value = detect_priority
|
17
19
|
|
18
|
-
return value if token_array.empty?
|
20
|
+
return value if @token_array.empty?
|
19
21
|
|
20
|
-
next_token = token_array.shift
|
22
|
+
next_token = @token_array.shift
|
21
23
|
|
22
24
|
while next_token && [:plus, :minus].include?(next_token.type) do
|
23
|
-
value += detect_term
|
24
|
-
value -= detect_term
|
25
|
-
next_token = token_array.shift
|
25
|
+
value += detect_term if next_token.type == :plus
|
26
|
+
value -= detect_term if next_token.type == :minus
|
27
|
+
next_token = @token_array.shift
|
26
28
|
end
|
27
29
|
|
28
|
-
token_array.unshift next_token
|
29
|
-
token_array.unshift OlavoCalculator::Token.new(:number, value)
|
30
|
+
@token_array.unshift next_token
|
31
|
+
@token_array.unshift OlavoCalculator::Token.new(:number, value)
|
30
32
|
|
31
|
-
detect_term
|
33
|
+
detect_term
|
32
34
|
end
|
33
35
|
|
34
36
|
# prioridades podem ser da forma: (expr), [expr], {expr}, term
|
35
|
-
def
|
36
|
-
raise "Unexpected Ending" if token_array.empty?
|
37
|
+
def detect_priority
|
38
|
+
raise OlavoException, "Unexpected Ending" if @token_array.empty?
|
37
39
|
|
38
|
-
if PRIORITY_SEMANTICS.key? token_array[0].type
|
39
|
-
priority = token_array.shift
|
40
|
-
value = detect_expression
|
41
|
-
raise "Semantic Error" unless token_array.shift.type == PRIORITY_SEMANTICS[priority.type]
|
40
|
+
if PRIORITY_SEMANTICS.key? @token_array[0].type
|
41
|
+
priority = @token_array.shift
|
42
|
+
value = detect_expression
|
43
|
+
raise OlavoException, "Semantic Error" unless @token_array.shift.type == PRIORITY_SEMANTICS[priority.type]
|
42
44
|
return value
|
43
45
|
end
|
44
46
|
|
45
|
-
detect_term
|
47
|
+
detect_term
|
46
48
|
end
|
47
49
|
|
48
50
|
# termos podem ser da forma: factor ((MULT|DIV) factor)*
|
49
|
-
def
|
50
|
-
raise "Unexpected Ending" if token_array.empty?
|
51
|
-
value = detect_factor
|
51
|
+
def detect_term
|
52
|
+
raise OlavoException, "Unexpected Ending" if @token_array.empty?
|
53
|
+
value = detect_factor
|
52
54
|
|
53
|
-
return value if token_array.empty?
|
55
|
+
return value if @token_array.empty?
|
54
56
|
|
55
|
-
next_token = token_array.shift
|
57
|
+
next_token = @token_array.shift
|
56
58
|
|
57
59
|
while next_token && [:mult, :div].include?(next_token.type) do
|
58
|
-
value *= detect_factor
|
59
|
-
value /= detect_factor
|
60
|
-
next_token = token_array.shift
|
60
|
+
value *= detect_factor if next_token.type == :mult
|
61
|
+
value /= detect_factor if next_token.type == :div
|
62
|
+
next_token = @token_array.shift
|
61
63
|
end
|
62
64
|
|
63
|
-
token_array.unshift next_token unless next_token.nil?
|
65
|
+
@token_array.unshift next_token unless next_token.nil?
|
64
66
|
|
65
67
|
value
|
66
68
|
end
|
67
69
|
|
68
70
|
# fatores podem ser da forma: NUMBER, priority
|
69
|
-
def
|
70
|
-
raise "Unexpected Ending" if token_array.empty?
|
71
|
-
return token_array.shift.value if token_array[0].type == :number
|
72
|
-
detect_priority
|
71
|
+
def detect_factor
|
72
|
+
raise OlavoException, "Unexpected Ending" if @token_array.empty?
|
73
|
+
return @token_array.shift.value if @token_array[0].type == :number
|
74
|
+
detect_priority
|
73
75
|
end
|
74
76
|
end
|