Skip to content

Commit 929402c

Browse files
committed
Added the + (addition) operator
1 parent 2bed387 commit 929402c

2 files changed

Lines changed: 151 additions & 2 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2020 Cufy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jamplate.logic;
17+
18+
import org.jamplate.memory.Memory;
19+
20+
import java.util.Objects;
21+
22+
/**
23+
* A scope that joins two logics.
24+
*
25+
* @author LSafer
26+
* @version 0.0.6
27+
* @since 0.0.6 ~2020.09.22
28+
*/
29+
public class Addition implements Logic {
30+
/**
31+
* The logic at the left.
32+
*
33+
* @since 0.0.6 ~2020.09.22
34+
*/
35+
protected final Logic left;
36+
/**
37+
* The logic at the right.
38+
*
39+
* @since 0.0.6 ~2020.09.22
40+
*/
41+
protected final Logic right;
42+
43+
/**
44+
* Construct a new logic that joins two logics.
45+
*
46+
* @param left the logic at the left.
47+
* @param right the logic at the right.
48+
* @since 0.0.6 ~2020.09.22
49+
*/
50+
public Addition(Logic left, Logic right) {
51+
Objects.requireNonNull(left, "left");
52+
Objects.requireNonNull(right, "right");
53+
this.left = left;
54+
this.right = right;
55+
}
56+
57+
/**
58+
* Get the {@link #left} logic of this.
59+
*
60+
* @return the {@link #left} logic of this.
61+
* @since 0.0.6 ~2020.09.22
62+
*/
63+
public final Logic left() {
64+
return this.left;
65+
}
66+
67+
/**
68+
* Get the {@link #right} logic of this.
69+
*
70+
* @return the {@link #right} logic of this.
71+
* @since 0.0.6 ~2020.09.22
72+
*/
73+
public final Logic right() {
74+
return this.right;
75+
}
76+
77+
@Override
78+
public String evaluate(Memory memory) {
79+
Objects.requireNonNull(memory, "memory");
80+
String left = this.left.evaluate(memory);
81+
String right = this.right.evaluate(memory);
82+
83+
//double join
84+
if (left.matches("^\\d*[.]\\d*$") &&
85+
right.matches("^\\d*[.]\\d*$"))
86+
return String.valueOf(
87+
Double.parseDouble(left) +
88+
Double.parseDouble(right)
89+
);
90+
else if (left.matches("^\\d*$") &&
91+
right.matches("^\\d*$"))
92+
return String.valueOf(
93+
Long.parseLong(left) +
94+
Long.parseLong(right)
95+
);
96+
97+
return left + right;
98+
}
99+
}

src/main/java/org/jamplate/parser/LogicParser.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* The default jamplate parser that parses {@link String}s into {@link Logic}s.
2727
*
2828
* @author LSafer
29-
* @version 0.0.4
29+
* @version 0.0.6
3030
* @since 0.0.1 ~2020.09.19
3131
*/
3232
public class LogicParser implements PollParser<Logic> {
@@ -65,7 +65,7 @@ public class LogicParser implements PollParser<Logic> {
6565
*
6666
* @since 0.0.1 ~2020.09.19
6767
*/
68-
protected final Pattern PATTERN_WHITESPACES = Pattern.compile("(\\s+)|[|]|[&]|(!=)|(==)|(!(?!=))");
68+
protected final Pattern PATTERN_WHITESPACES = Pattern.compile("(\\s+)|[|]|[+]|[&]|(!=)|(==)|(!(?!=))");
6969

7070
@Override
7171
public Logic link(List poll) {
@@ -101,6 +101,7 @@ public void parse(List poll) {
101101

102102
//clear all negations, equations
103103
this.parseNegations(poll);
104+
this.parseAdditions(poll);
104105
this.parseAnds(poll);
105106
this.parseOrs(poll);
106107
this.parseEquations(poll);
@@ -112,6 +113,55 @@ public List poll(String string) {
112113
return new ArrayList(Collections.singleton(string));
113114
}
114115

116+
/**
117+
* Parse any possible {@link Addition}s in the given {@code poll}. After calling this method, no
118+
* addition statement in a {@link String} should remain in the given {@code poll}.
119+
* <p>
120+
* Clear these before calling this method:
121+
* <ul>
122+
* <li>{@link #processParenthesis(List)}</li>
123+
* <li>{@link #parseConstants(List)}</li>
124+
* <li>{@link #parseReferences(List)}</li>
125+
* <li>{@link #processWhitespaces(List)}</li>
126+
* <li>{@link #parseNegations(List)}</li>
127+
* <li>any statement that could be before or next to an addition</li>
128+
* </ul>
129+
*
130+
* @param poll the poll to parse any addition in it.
131+
* @throws NullPointerException if the given {@code poll} is null.
132+
* @throws ParseException if any parse exception occurs; if an addition has no element
133+
* before or after it; if an element before or after an addition
134+
* has not been resolved into a {@link Logic}.
135+
* @since 0.0.6 ~2020.09.22
136+
*/
137+
protected void parseAdditions(List poll) {
138+
Objects.requireNonNull(poll, "poll");
139+
140+
ListIterator iterator = poll.listIterator();
141+
while (iterator.hasNext()) {
142+
Object next = iterator.next();
143+
144+
if (next instanceof String) {
145+
String string = (String) next;
146+
147+
if (string.equals("+"))
148+
try {
149+
iterator.remove();
150+
Logic rightLogic = (Logic) iterator.next();
151+
iterator.remove();
152+
Logic leftLogic = (Logic) iterator.previous();
153+
iterator.remove();
154+
155+
Logic logic = new Addition(leftLogic, rightLogic);
156+
157+
iterator.add(logic);
158+
} catch (NoSuchElementException | ClassCastException e) {
159+
throw new ParseException("Invalid Addition", e);
160+
}
161+
}
162+
}
163+
}
164+
115165
/**
116166
* Parse any possible {@link And}s in the given {@code poll}. After calling this method, no and
117167
* statement in a {@link String} should remain in the given {@code poll}.

0 commit comments

Comments
 (0)