2024-04-25 11:27:38 +00:00
|
|
|
package abstractSyntaxTree.Expression;
|
|
|
|
|
2024-05-02 12:31:37 +00:00
|
|
|
import TypeCheck.AbstractType;
|
|
|
|
import TypeCheck.TypeCheckHelper;
|
|
|
|
import TypeCheck.TypeCheckResult;
|
|
|
|
import abstractSyntaxTree.Datatype.IDatatype;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2024-04-25 11:27:38 +00:00
|
|
|
}
|