IntegerLiteral: Provide “of” factory method rather than new-ing everywhere.

Including an overload for booleans.
29 files changed, 44 insertions(+), 35 deletions(-)

M src/main/java/nl/grauw/glass/Parser.java
M src/main/java/nl/grauw/glass/expressions/Add.java
M src/main/java/nl/grauw/glass/expressions/And.java
M src/main/java/nl/grauw/glass/expressions/CharacterLiteral.java
M src/main/java/nl/grauw/glass/expressions/Complement.java
M src/main/java/nl/grauw/glass/expressions/ContextLiteral.java
M src/main/java/nl/grauw/glass/expressions/Divide.java
M src/main/java/nl/grauw/glass/expressions/Equals.java
M src/main/java/nl/grauw/glass/expressions/GreaterOrEquals.java
M src/main/java/nl/grauw/glass/expressions/GreaterThan.java
M src/main/java/nl/grauw/glass/expressions/IntegerLiteral.java
M src/main/java/nl/grauw/glass/expressions/LessOrEquals.java
M src/main/java/nl/grauw/glass/expressions/LessThan.java
M src/main/java/nl/grauw/glass/expressions/LogicalAnd.java
M src/main/java/nl/grauw/glass/expressions/LogicalOr.java
M src/main/java/nl/grauw/glass/expressions/Modulo.java
M src/main/java/nl/grauw/glass/expressions/Multiply.java
M src/main/java/nl/grauw/glass/expressions/Negative.java
M src/main/java/nl/grauw/glass/expressions/Not.java
M src/main/java/nl/grauw/glass/expressions/NotEquals.java
M src/main/java/nl/grauw/glass/expressions/Or.java
M src/main/java/nl/grauw/glass/expressions/Positive.java
M src/main/java/nl/grauw/glass/expressions/ShiftLeft.java
M src/main/java/nl/grauw/glass/expressions/ShiftRight.java
M src/main/java/nl/grauw/glass/expressions/StringLiteral.java
M src/main/java/nl/grauw/glass/expressions/Subtract.java
M src/main/java/nl/grauw/glass/expressions/Xor.java
M src/main/java/nl/grauw/glass/instructions/Rept.java
M src/test/java/nl/grauw/glass/expressions/ExpressionTest.java
M src/main/java/nl/grauw/glass/Parser.java +6 -6
@@ 368,22 368,22 @@ public class Parser {
 				String string = accumulator.toString();
 				if (character == 'H' || character == 'h') {
 					int value = parseInt(string, 16);
-					expressionBuilder.addValueToken(new IntegerLiteral(value));
+					expressionBuilder.addValueToken(IntegerLiteral.of(value));
 					accumulator.setLength(0);
 					return argumentOperatorState;
 				} else if (character == 'O' || character == 'o') {
 					int value = parseInt(string, 8);
-					expressionBuilder.addValueToken(new IntegerLiteral(value));
+					expressionBuilder.addValueToken(IntegerLiteral.of(value));
 					accumulator.setLength(0);
 					return argumentOperatorState;
 				} else {
 					if (string.endsWith("B") || string.endsWith("b")) {
 						int value = parseInt(string.substring(0, string.length() - 1), 2);
-						expressionBuilder.addValueToken(new IntegerLiteral(value));
+						expressionBuilder.addValueToken(IntegerLiteral.of(value));
 						accumulator.setLength(0);
 					} else {
 						int value = parseInt(string, 10);
-						expressionBuilder.addValueToken(new IntegerLiteral(value));
+						expressionBuilder.addValueToken(IntegerLiteral.of(value));
 						accumulator.setLength(0);
 					}
 					return argumentOperatorState.parse(character);

          
@@ 416,7 416,7 @@ public class Parser {
 				return argumentHexadecimalState;
 			} else {
 				int value = parseInt(accumulator.toString(), 16);
-				expressionBuilder.addValueToken(new IntegerLiteral(value));
+				expressionBuilder.addValueToken(IntegerLiteral.of(value));
 				accumulator.setLength(0);
 				return argumentOperatorState.parse(character);
 			}

          
@@ 431,7 431,7 @@ public class Parser {
 				return argumentBinaryState;
 			} else {
 				int value = parseInt(accumulator.toString(), 2);
-				expressionBuilder.addValueToken(new IntegerLiteral(value));
+				expressionBuilder.addValueToken(IntegerLiteral.of(value));
 				accumulator.setLength(0);
 				return argumentOperatorState.parse(character);
 			}

          
M src/main/java/nl/grauw/glass/expressions/Add.java +1 -1
@@ 34,7 34,7 @@ public class Add extends BinaryOperator 
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() + term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() + term2.getInteger());
 		if (type == Type.REGISTER) {
 			if (term1.is(Type.REGISTER)) {
 				Register register = term1.getRegister();

          
M src/main/java/nl/grauw/glass/expressions/And.java +1 -1
@@ 14,7 14,7 @@ public class And extends BinaryOperator 
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() & term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() & term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/CharacterLiteral.java +1 -1
@@ 25,7 25,7 @@ public class CharacterLiteral extends Li
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(character);
+			return IntegerLiteral.of(character);
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Complement.java +1 -1
@@ 14,7 14,7 @@ public class Complement extends UnaryOpe
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(~term.getInteger());
+			return IntegerLiteral.of(~term.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/ContextLiteral.java +1 -1
@@ 28,7 28,7 @@ public class ContextLiteral extends Lite
 		if (type == Type.CONTEXT)
 			return this;
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(context.getAddress());
+			return IntegerLiteral.of(context.getAddress());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Divide.java +1 -1
@@ 25,7 25,7 @@ public class Divide extends BinaryOperat
 			int divisor = term2.getInteger();
 			if (divisor == 0)
 				throw new EvaluationException("Division by zero.");
-			return new IntegerLiteral(term1.getInteger() / divisor);
+			return IntegerLiteral.of(term1.getInteger() / divisor);
 		}
 		return super.get(type);
 	}

          
M src/main/java/nl/grauw/glass/expressions/Equals.java +1 -1
@@ 14,7 14,7 @@ public class Equals extends BinaryOperat
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() == term2.getInteger() ? -1 : 0);
+			return IntegerLiteral.of(term1.getInteger() == term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/GreaterOrEquals.java +1 -1
@@ 14,7 14,7 @@ public class GreaterOrEquals extends Bin
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() >= term2.getInteger() ? -1 : 0);
+			return IntegerLiteral.of(term1.getInteger() >= term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/GreaterThan.java +1 -1
@@ 14,7 14,7 @@ public class GreaterThan extends BinaryO
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() > term2.getInteger() ? -1 : 0);
+			return IntegerLiteral.of(term1.getInteger() > term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/IntegerLiteral.java +9 -0
@@ 4,6 4,7 @@ public class IntegerLiteral extends Lite
 
 	public static final IntegerLiteral ZERO = new IntegerLiteral(0);
 	public static final IntegerLiteral ONE = new IntegerLiteral(1);
+	public static final IntegerLiteral MINUSONE = new IntegerLiteral(-1);
 
 	private final int value;
 

          
@@ 11,6 12,14 @@ public class IntegerLiteral extends Lite
 		this.value = value;
 	}
 
+	public static IntegerLiteral of(int value) {
+		return new IntegerLiteral(value);
+	}
+
+	public static IntegerLiteral of(boolean value) {
+		return value ? MINUSONE : ZERO;
+	}
+
 	@Override
 	public IntegerLiteral copy(Context context) {
 		return this;

          
M src/main/java/nl/grauw/glass/expressions/LessOrEquals.java +1 -1
@@ 14,7 14,7 @@ public class LessOrEquals extends Binary
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() <= term2.getInteger() ? -1 : 0);
+			return IntegerLiteral.of(term1.getInteger() <= term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/LessThan.java +1 -1
@@ 14,7 14,7 @@ public class LessThan extends BinaryOper
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() < term2.getInteger() ? -1 : 0);
+			return IntegerLiteral.of(term1.getInteger() < term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/LogicalAnd.java +1 -1
@@ 15,7 15,7 @@ public class LogicalAnd extends BinaryOp
 	public Expression get(Type type) {
 		if (type == Type.INTEGER) {
 			int value1 = term1.getInteger();
-			return new IntegerLiteral(value1 == 0 ? value1 : term2.getInteger());
+			return IntegerLiteral.of(value1 == 0 ? value1 : term2.getInteger());
 		}
 		return super.get(type);
 	}

          
M src/main/java/nl/grauw/glass/expressions/LogicalOr.java +1 -1
@@ 15,7 15,7 @@ public class LogicalOr extends BinaryOpe
 	public Expression get(Type type) {
 		if (type == Type.INTEGER) {
 			int value1 = term1.getInteger();
-			return new IntegerLiteral(value1 != 0 ? value1 : term2.getInteger());
+			return IntegerLiteral.of(value1 != 0 ? value1 : term2.getInteger());
 		}
 		return super.get(type);
 	}

          
M src/main/java/nl/grauw/glass/expressions/Modulo.java +1 -1
@@ 25,7 25,7 @@ public class Modulo extends BinaryOperat
 			int divisor = term2.getInteger();
 			if (divisor == 0)
 				throw new EvaluationException("Division by zero.");
-			return new IntegerLiteral(term1.getInteger() % divisor);
+			return IntegerLiteral.of(term1.getInteger() % divisor);
 		}
 		return super.get(type);
 	}

          
M src/main/java/nl/grauw/glass/expressions/Multiply.java +1 -1
@@ 22,7 22,7 @@ public class Multiply extends BinaryOper
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() * term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() * term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Negative.java +1 -1
@@ 14,7 14,7 @@ public class Negative extends UnaryOpera
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(-term.getInteger());
+			return IntegerLiteral.of(-term.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Not.java +1 -1
@@ 21,7 21,7 @@ public class Not extends UnaryOperator {
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term.getInteger() == 0 ? -1 : 0);
+			return IntegerLiteral.of(term.getInteger() == 0);
 		if (type == Type.FLAG) {
 			Flag flag = term.getFlag();
 			if (flag == Flag.NZ)

          
M src/main/java/nl/grauw/glass/expressions/NotEquals.java +1 -1
@@ 14,7 14,7 @@ public class NotEquals extends BinaryOpe
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() != term2.getInteger() ? -1 : 0);
+			return IntegerLiteral.of(term1.getInteger() != term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Or.java +1 -1
@@ 14,7 14,7 @@ public class Or extends BinaryOperator {
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() | term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() | term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Positive.java +1 -1
@@ 14,7 14,7 @@ public class Positive extends UnaryOpera
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(+term.getInteger());
+			return IntegerLiteral.of(+term.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/ShiftLeft.java +1 -1
@@ 14,7 14,7 @@ public class ShiftLeft extends BinaryOpe
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() << term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() << term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/ShiftRight.java +1 -1
@@ 14,7 14,7 @@ public class ShiftRight extends BinaryOp
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() >> term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() >> term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/StringLiteral.java +1 -1
@@ 29,7 29,7 @@ public class StringLiteral extends Liter
 		if (type == Type.STRING)
 			return this;
 		if (type == Type.INTEGER && string.length() == 1)
-			return new IntegerLiteral(string.codePointAt(0));
+			return IntegerLiteral.of(string.codePointAt(0));
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Subtract.java +1 -1
@@ 34,7 34,7 @@ public class Subtract extends BinaryOper
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() - term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() - term2.getInteger());
 		if (type == Type.REGISTER) {
 			if (term1.is(Type.REGISTER) && term2.is(Type.INTEGER)) {
 				Register register = term1.getRegister();

          
M src/main/java/nl/grauw/glass/expressions/Xor.java +1 -1
@@ 14,7 14,7 @@ public class Xor extends BinaryOperator 
 	@Override
 	public Expression get(Type type) {
 		if (type == Type.INTEGER)
-			return new IntegerLiteral(term1.getInteger() ^ term2.getInteger());
+			return IntegerLiteral.of(term1.getInteger() ^ term2.getInteger());
 		return super.get(type);
 	}
 

          
M src/main/java/nl/grauw/glass/instructions/Rept.java +1 -1
@@ 46,7 46,7 @@ public class Rept extends InstructionFac
 		super.expand(line, lines);
 		for (int i = 0, counter = start; i < count; i++, counter += step) {
 			Scope parameterScope = new ParameterScope(line.getScope(), parameter,
-					parameter != null ? new IntegerLiteral(counter) : null);
+					parameter != null ? IntegerLiteral.of(counter) : null);
 
 			// set up the number symbol
 			line.getScope().addSymbol(Integer.toString(i), parameterScope);

          
M src/test/java/nl/grauw/glass/expressions/ExpressionTest.java +3 -3
@@ 220,7 220,7 @@ public class ExpressionTest extends Test
 	@Test
 	public void testIdentifier() {
 		Scope scope = new Scope();
-		scope.addSymbol("symbol", new IntegerLiteral(11));
+		scope.addSymbol("symbol", IntegerLiteral.of(11));
 		assertEquals(11, parse("symbol", scope).getInteger());
 	}
 

          
@@ 228,7 228,7 @@ public class ExpressionTest extends Test
 	public void testMember() {
 		Scope scope = new Scope();
 		Scope objectScope = new Scope(scope);
-		objectScope.addSymbol("symbol", new IntegerLiteral(11));
+		objectScope.addSymbol("symbol", IntegerLiteral.of(11));
 		scope.addSymbol("object", new ContextLiteral(objectScope));
 		assertEquals(11, parse("object.symbol", scope).getInteger());
 		assertEquals(11, parse("(object).symbol", scope).getInteger());

          
@@ 238,7 238,7 @@ public class ExpressionTest extends Test
 	public void testMemberInParent() {
 		Scope scope = new Scope();
 		scope.addSymbol("object", new ContextLiteral(new Scope(scope)));
-		scope.addSymbol("symbol", new IntegerLiteral(11));
+		scope.addSymbol("symbol", IntegerLiteral.of(11));
 		assertThrows(SymbolNotFoundException.class, () -> {
 			parse("object.symbol", scope).getInteger();
 		});