expressions: Replace get*-methods with a get(type) method.

This cleans up the code a lot and allows us to more easily reason over types.
36 files changed, 246 insertions(+), 206 deletions(-)

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/Annotation.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/ErrorLiteral.java
M src/main/java/nl/grauw/glass/expressions/Expression.java
M src/main/java/nl/grauw/glass/expressions/Flag.java
M src/main/java/nl/grauw/glass/expressions/FlagOrRegister.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/Instruction.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/Passthrough.java
M src/main/java/nl/grauw/glass/expressions/Positive.java
M src/main/java/nl/grauw/glass/expressions/Register.java
M src/main/java/nl/grauw/glass/expressions/SectionContextLiteral.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/Type.java
M src/main/java/nl/grauw/glass/expressions/Xor.java
M src/main/java/nl/grauw/glass/expressions/Add.java +11 -11
@@ 32,18 32,18 @@ public class Add extends BinaryOperator 
 	}
 
 	@Override
-	public int getInteger() {
-		return term1.getInteger() + term2.getInteger();
-	}
-
-	@Override
-	public Register getRegister() {
-		if (term1.is(Type.REGISTER)) {
-			Register register = term1.getRegister();
-			if (register.isIndex() && register.isPair())
-				return new Register(register, new Add(register.getIndexOffset(), term2));
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(term1.getInteger() + term2.getInteger());
+		if (type == Type.REGISTER) {
+			if (term1.is(Type.REGISTER)) {
+				Register register = term1.getRegister();
+				if (register.isIndex() && register.isPair()) {
+					return new Register(register, new Add(register.getIndexOffset(), term2));
+				}
+			}
 		}
-		throw new EvaluationException("Not a register.");
+		return super.get(type);
 	}
 
 	@Override

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

          
M src/main/java/nl/grauw/glass/expressions/Annotation.java +7 -0
@@ 28,6 28,13 @@ public class Annotation extends Expressi
 		return type == Type.ANNOTATION;
 	}
 
+	@Override
+	public Expression get(Type type) {
+		if (type == Type.ANNOTATION)
+			return this;
+		return super.get(type);
+	}
+
 	public String toString() {
 		return "" + annotation + " " + annotee;
 	}

          
M src/main/java/nl/grauw/glass/expressions/CharacterLiteral.java +4 -2
@@ 23,8 23,10 @@ public class CharacterLiteral extends Li
 	}
 
 	@Override
-	public int getInteger() {
-		return character;
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(character);
+		return super.get(type);
 	}
 
 	public String toString() {

          
M src/main/java/nl/grauw/glass/expressions/Complement.java +4 -2
@@ 12,8 12,10 @@ public class Complement extends UnaryOpe
 	}
 
 	@Override
-	public int getInteger() {
-		return ~term.getInteger();
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(~term.getInteger());
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/ContextLiteral.java +6 -2
@@ 24,8 24,12 @@ public class ContextLiteral extends Lite
 	}
 
 	@Override
-	public int getInteger() {
-		return context.getAddress();
+	public Expression get(Type type) {
+		if (type == Type.CONTEXT)
+			return this;
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(context.getAddress());
+		return super.get(type);
 	}
 
 	public String toString() {

          
M src/main/java/nl/grauw/glass/expressions/Divide.java +8 -5
@@ 20,11 20,14 @@ public class Divide extends BinaryOperat
 	}
 
 	@Override
-	public int getInteger() {
-		int divisor = term2.getInteger();
-		if (divisor == 0)
-			throw new EvaluationException("Division by zero.");
-		return term1.getInteger() / divisor;
+	public Expression get(Type type) {
+		if (type == Type.INTEGER) {
+			int divisor = term2.getInteger();
+			if (divisor == 0)
+				throw new EvaluationException("Division by zero.");
+			return new IntegerLiteral(term1.getInteger() / divisor);
+		}
+		return super.get(type);
 	}
 
 	@Override

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

          
M src/main/java/nl/grauw/glass/expressions/ErrorLiteral.java +1 -43
@@ 1,7 1,5 @@ 
 package nl.grauw.glass.expressions;
 
-import nl.grauw.glass.instructions.InstructionFactory;
-
 public class ErrorLiteral extends Literal {
 
 	private final EvaluationException exception;

          
@@ 26,47 24,7 @@ public class ErrorLiteral extends Litera
 	}
 
 	@Override
-	public int getInteger() {
-		throw exception;
-	}
-
-	@Override
-	public String getString() {
-		throw exception;
-	}
-
-	@Override
-	public Register getRegister() {
-		throw exception;
-	}
-
-	@Override
-	public Flag getFlag() {
-		throw exception;
-	}
-
-	@Override
-	public Identifier getAnnotation() {
-		throw exception;
-	}
-
-	@Override
-	public Expression getAnnotee() {
-		throw exception;
-	}
-
-	@Override
-	public InstructionFactory getInstruction() {
-		throw exception;
-	}
-
-	@Override
-	public Context getContext() {
-		throw exception;
-	}
-
-	@Override
-	public SectionContext getSectionContext() {
+	public Expression get(Type type) {
 		throw exception;
 	}
 

          
M src/main/java/nl/grauw/glass/expressions/Expression.java +29 -1
@@ 13,44 13,72 @@ public abstract class Expression {
 
 	public abstract boolean is(Type type);
 
+	public Expression get(Type type) {
+		return new ErrorLiteral(new EvaluationException("Not of type " + type)).get(type);
+	}
+
 	public Expression resolve() {
 		return this;
 	}
 
 	public int getInteger() {
+		if (is(Type.INTEGER))
+			return get(Type.INTEGER).getInteger();
 		throw new EvaluationException("Not an integer.");
 	}
 
 	public String getString() {
+		if (is(Type.STRING))
+			return get(Type.STRING).getString();
 		throw new EvaluationException("Not a string.");
 	}
 
 	public Register getRegister() {
+		if (is(Type.REGISTER)) {
+			Expression register = get(Type.REGISTER);
+			if (register instanceof Register)
+				return (Register)register;
+		}
 		throw new EvaluationException("Not a register.");
 	}
 
 	public Flag getFlag() {
+		if (is(Type.FLAG)) {
+			Expression flag = get(Type.FLAG);
+			if (flag instanceof Flag)
+				return (Flag)flag;
+		}
 		throw new EvaluationException("Not a flag.");
 	}
 
 	public Identifier getAnnotation() {
+		if (is(Type.ANNOTATION))
+			return get(Type.ANNOTATION).getAnnotation();
 		throw new EvaluationException("Not an annotation.");
 	}
 
 	public Expression getAnnotee() {
+		if (is(Type.ANNOTATION))
+			return get(Type.ANNOTATION).getAnnotee();
 		throw new EvaluationException("Not an annotation.");
 	}
 
 	public InstructionFactory getInstruction() {
+		if (is(Type.INSTRUCTION))
+			return get(Type.INSTRUCTION).getInstruction();
 		throw new EvaluationException("Not an instruction.");
 	}
 
 	public Context getContext() {
+		if (is(Type.CONTEXT))
+			return get(Type.CONTEXT).getContext();
 		throw new EvaluationException("Not a context.");
 	}
 
 	public SectionContext getSectionContext() {
-		throw new EvaluationException("Not a context.");
+		if (is(Type.SECTIONCONTEXT))
+			return get(Type.SECTIONCONTEXT).getSectionContext();
+		throw new EvaluationException("Not a section context.");
 	}
 
 	public List<Expression> getList() {

          
M src/main/java/nl/grauw/glass/expressions/Flag.java +4 -2
@@ 34,8 34,10 @@ public class Flag extends Literal {
 	}
 
 	@Override
-	public Flag getFlag() {
-		return this;
+	public Expression get(Type type) {
+		if (type == Type.FLAG)
+			return this;
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/FlagOrRegister.java +9 -0
@@ 33,6 33,15 @@ public class FlagOrRegister extends Lite
 	}
 
 	@Override
+	public Expression get(Type type) {
+		if (type == Type.FLAG)
+			return flag;
+		if (type == Type.REGISTER)
+			return register;
+		return super.get(type);
+	}
+
+	@Override
 	public String toString() {
 		return flag.toString();
 	}

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

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

          
M src/main/java/nl/grauw/glass/expressions/Instruction.java +8 -4
@@ 23,13 23,17 @@ public class Instruction extends Express
 	}
 
 	@Override
-	public Context getContext() {
-		return context;
+	public boolean is(Type type) {
+		return type == Type.INSTRUCTION || type == Type.CONTEXT;
 	}
 
 	@Override
-	public boolean is(Type type) {
-		return type == Type.INSTRUCTION || type == Type.CONTEXT;
+	public Expression get(Type type) {
+		if (type == Type.INSTRUCTION)
+			return this;
+		if (type == Type.CONTEXT)
+			return new ContextLiteral(context);
+		return super.get(type);
 	}
 
 	public String toString() {

          
M src/main/java/nl/grauw/glass/expressions/IntegerLiteral.java +7 -0
@@ 26,6 26,13 @@ public class IntegerLiteral extends Lite
 		return type == Type.INTEGER;
 	}
 
+	@Override
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return this;
+		return super.get(type);
+	}
+
 	public String toString() {
 		String string = Integer.toHexString(value).toUpperCase();
 		return (string.charAt(0) >= 'A' && string.charAt(0) <= 'F' ? "0" : "") + string + "H";

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

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

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

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

          
M src/main/java/nl/grauw/glass/expressions/Modulo.java +8 -5
@@ 20,11 20,14 @@ public class Modulo extends BinaryOperat
 	}
 
 	@Override
-	public int getInteger() {
-		int divisor = term2.getInteger();
-		if (divisor == 0)
-			throw new EvaluationException("Division by zero.");
-		return term1.getInteger() % divisor;
+	public Expression get(Type type) {
+		if (type == Type.INTEGER) {
+			int divisor = term2.getInteger();
+			if (divisor == 0)
+				throw new EvaluationException("Division by zero.");
+			return new IntegerLiteral(term1.getInteger() % divisor);
+		}
+		return super.get(type);
 	}
 
 	@Override

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

          
M src/main/java/nl/grauw/glass/expressions/Negative.java +4 -2
@@ 12,8 12,10 @@ public class Negative extends UnaryOpera
 	}
 
 	@Override
-	public int getInteger() {
-		return -term.getInteger();
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(-term.getInteger());
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/Not.java +24 -24
@@ 19,30 19,30 @@ public class Not extends UnaryOperator {
 	}
 
 	@Override
-	public int getInteger() {
-		return term.getInteger() == 0 ? -1 : 0;
-	}
-
-	@Override
-	public Flag getFlag() {
-		Flag flag = term.getFlag();
-		if (flag == Flag.NZ)
-			return Flag.Z;
-		if (flag == Flag.Z)
-			return Flag.NZ;
-		if (flag == Flag.NC)
-			return Flag.C;
-		if (flag == Flag.C)
-			return Flag.NC;
-		if (flag == Flag.PO)
-			return Flag.PE;
-		if (flag == Flag.PE)
-			return Flag.PO;
-		if (flag == Flag.P)
-			return Flag.M;
-		if (flag == Flag.M)
-			return Flag.P;
-		throw new AssemblyException("Unrecognised flag.");
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(term.getInteger() == 0 ? -1 : 0);
+		if (type == Type.FLAG) {
+			Flag flag = term.getFlag();
+			if (flag == Flag.NZ)
+				return Flag.Z;
+			if (flag == Flag.Z)
+				return Flag.NZ;
+			if (flag == Flag.NC)
+				return Flag.C;
+			if (flag == Flag.C)
+				return Flag.NC;
+			if (flag == Flag.PO)
+				return Flag.PE;
+			if (flag == Flag.PE)
+				return Flag.PO;
+			if (flag == Flag.P)
+				return Flag.M;
+			if (flag == Flag.M)
+				return Flag.P;
+			throw new AssemblyException("Unrecognised flag.");
+		}
+		return super.get(type);
 	}
 
 	@Override

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

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

          
M src/main/java/nl/grauw/glass/expressions/Passthrough.java +2 -44
@@ 1,7 1,5 @@ 
 package nl.grauw.glass.expressions;
 
-import nl.grauw.glass.instructions.InstructionFactory;
-
 public abstract class Passthrough extends Expression {
 
 	@Override

          
@@ 10,48 8,8 @@ public abstract class Passthrough extend
 	}
 
 	@Override
-	public int getInteger() {
-		return resolve().getInteger();
-	}
-
-	@Override
-	public String getString() {
-		return resolve().getString();
-	}
-
-	@Override
-	public Register getRegister() {
-		return resolve().getRegister();
-	}
-
-	@Override
-	public Flag getFlag() {
-		return resolve().getFlag();
-	}
-
-	@Override
-	public Identifier getAnnotation() {
-		return resolve().getAnnotation();
-	}
-
-	@Override
-	public Expression getAnnotee() {
-		return resolve().getAnnotee();
-	}
-
-	@Override
-	public InstructionFactory getInstruction() {
-		return resolve().getInstruction();
-	}
-
-	@Override
-	public Context getContext() {
-		return resolve().getContext();
-	}
-
-	@Override
-	public SectionContext getSectionContext() {
-		return resolve().getSectionContext();
+	public Expression get(Type type) {
+		return resolve().get(type);
 	}
 
 }

          
M src/main/java/nl/grauw/glass/expressions/Positive.java +4 -2
@@ 12,8 12,10 @@ public class Positive extends UnaryOpera
 	}
 
 	@Override
-	public int getInteger() {
-		return +term.getInteger();
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(+term.getInteger());
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/Register.java +4 -2
@@ 100,8 100,10 @@ public class Register extends Literal {
 	}
 
 	@Override
-	public Register getRegister() {
-		return this;
+	public Expression get(Type type) {
+		if (type == Type.REGISTER)
+			return this;
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/SectionContextLiteral.java +7 -0
@@ 19,4 19,11 @@ public class SectionContextLiteral exten
 		return type == Type.SECTIONCONTEXT || super.is(type);
 	}
 
+	@Override
+	public Expression get(Type type) {
+		if (type == Type.SECTIONCONTEXT)
+			return this;
+		return super.get(type);
+	}
+
 }

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

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

          
M src/main/java/nl/grauw/glass/expressions/StringLiteral.java +6 -4
@@ 25,10 25,12 @@ public class StringLiteral extends Liter
 	}
 
 	@Override
-	public int getInteger() {
-		if (string.length() != 1)
-			throw new EvaluationException("Can not evaluate strings of more than 1 character to integer.");
-		return string.codePointAt(0);
+	public Expression get(Type type) {
+		if (type == Type.STRING)
+			return this;
+		if (type == Type.INTEGER && string.length() == 1)
+			return new IntegerLiteral(string.codePointAt(0));
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/Subtract.java +11 -11
@@ 32,18 32,18 @@ public class Subtract extends BinaryOper
 	}
 
 	@Override
-	public int getInteger() {
-		return term1.getInteger() - term2.getInteger();
-	}
-
-	@Override
-	public Register getRegister() {
-		if (term1.is(Type.REGISTER) && term2.is(Type.INTEGER)) {
-			Register register = term1.getRegister();
-			if (register.isIndex() && register.isPair())
-				return new Register(register, new Subtract(register.getIndexOffset(), term2));
+	public Expression get(Type type) {
+		if (type == Type.INTEGER)
+			return new IntegerLiteral(term1.getInteger() - term2.getInteger());
+		if (type == Type.REGISTER) {
+			if (term1.is(Type.REGISTER) && term2.is(Type.INTEGER)) {
+				Register register = term1.getRegister();
+				if (register.isIndex() && register.isPair()) {
+					return new Register(register, new Subtract(register.getIndexOffset(), term2));
+				}
+			}
 		}
-		throw new EvaluationException("Not a register.");
+		return super.get(type);
 	}
 
 	@Override

          
M src/main/java/nl/grauw/glass/expressions/Type.java +18 -10
@@ 2,17 2,25 @@ package nl.grauw.glass.expressions;
 
 public class Type {
 
-	public static final Type INTEGER = new Type();
-	public static final Type STRING = new Type();
-	public static final Type REGISTER = new Type();
-	public static final Type FLAG = new Type();
-	public static final Type GROUP = new Type();
-	public static final Type ANNOTATION = new Type();
-	public static final Type INSTRUCTION = new Type();
-	public static final Type CONTEXT = new Type();
-	public static final Type SECTIONCONTEXT = new Type();
+	public static final Type INTEGER = new Type("Integer");
+	public static final Type STRING = new Type("String");
+	public static final Type REGISTER = new Type("Register");
+	public static final Type FLAG = new Type("Flag");
+	public static final Type GROUP = new Type("Group");
+	public static final Type ANNOTATION = new Type("Annotation");
+	public static final Type INSTRUCTION = new Type("Instruction");
+	public static final Type CONTEXT = new Type("Context");
+	public static final Type SECTIONCONTEXT = new Type("SectionContext");
 
-	Type() {
+	private String name;
+
+	Type(String name) {
+		this.name = name;
+	}
+
+	@Override
+	public String toString() {
+		return name;
 	}
 
 }

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