2527d15467
Also included some TODOs in areas where parameters and some connections are missing
66 lines
1.7 KiB
Java
66 lines
1.7 KiB
Java
package abstractSyntaxTree.Expression;
|
|
|
|
import TypeCheck.AbstractType;
|
|
import TypeCheck.TypeCheckHelper;
|
|
import TypeCheck.TypeCheckResult;
|
|
import abstractSyntaxTree.Datatype.IDatatype;
|
|
import org.objectweb.asm.MethodVisitor;
|
|
import org.objectweb.asm.Opcodes;
|
|
|
|
import java.util.Objects;
|
|
|
|
public class UnaryExpression extends AbstractType implements IExpression{
|
|
public String operator;
|
|
public IDatatype operand;
|
|
@Override
|
|
public TypeCheckResult typeCheck() throws Exception {
|
|
TypeCheckResult result = new TypeCheckResult();
|
|
|
|
TypeCheckResult operandTypeCheckResult = operand.typeCheck();
|
|
String operandType = operandTypeCheckResult.type;
|
|
|
|
switch (operator) {
|
|
|
|
case "!" :{
|
|
if (Objects.equals(operandType, "bool")){
|
|
result.type = "bool";
|
|
}
|
|
break;
|
|
}
|
|
|
|
case "-":
|
|
case "+":
|
|
if (Objects.equals(operandType, "int")){
|
|
result.type = "int";
|
|
}
|
|
break;
|
|
|
|
}
|
|
|
|
setTypeCheckResult(result);
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public void CodeGen(MethodVisitor mv) throws Exception {
|
|
|
|
operand.CodeGen(mv);
|
|
|
|
switch (operator) {
|
|
case "!":
|
|
//XOR with 1 to get the negation
|
|
mv.visitInsn(Opcodes.ICONST_1);
|
|
mv.visitInsn(Opcodes.IXOR);
|
|
break;
|
|
case "-":
|
|
mv.visitInsn(Opcodes.INEG);
|
|
break;
|
|
case "+":
|
|
break;
|
|
|
|
default: throw new Exception("Unknown operator :" + operator);
|
|
}
|
|
|
|
}
|
|
}
|