51 lines
1.8 KiB
Java
51 lines
1.8 KiB
Java
package abstractSyntaxTree.StatementExpression;
|
|
|
|
import TypeCheck.AbstractType;
|
|
import TypeCheck.TypeCheckHelper;
|
|
import TypeCheck.TypeCheckResult;
|
|
import abstractSyntaxTree.Expression.IExpression;
|
|
import abstractSyntaxTree.Expression.InstVarExpression;
|
|
import abstractSyntaxTree.Expression.VarRefExpression;
|
|
import abstractSyntaxTree.Statement.IStatement;
|
|
import org.objectweb.asm.*;
|
|
|
|
import java.util.Objects;
|
|
|
|
public class AssignStatementExpression extends AbstractType implements IExpression, IStatement {
|
|
public String operator;
|
|
public IExpression left;
|
|
public IExpression right;
|
|
|
|
@Override
|
|
public TypeCheckResult typeCheck() throws Exception {
|
|
TypeCheckHelper helper = new TypeCheckHelper();
|
|
TypeCheckResult result = new TypeCheckResult();
|
|
|
|
TypeCheckResult leftType = left.typeCheck();
|
|
TypeCheckResult rightType = right.typeCheck();
|
|
|
|
String upperbound = helper.upperBound(leftType.type, rightType.type);
|
|
if (Objects.equals(upperbound, leftType.type)) {
|
|
result.type = leftType.type;
|
|
}
|
|
setTypeCheckResult(result);
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public void CodeGen(MethodVisitor mv) throws Exception {
|
|
left.CodeGen(mv);
|
|
right.CodeGen(mv);
|
|
|
|
if (left instanceof VarRefExpression varRef) {
|
|
//TODO: Implement the handling of a variable reference --> I need a list of local variables
|
|
// for that to determine if the variable is a local or field variable
|
|
} else if (left instanceof InstVarExpression instVar) {
|
|
mv.visitInsn(Opcodes.DUP_X1);
|
|
|
|
// We now again need the owner (class reference), name (of the Field in the owner) and type of the field
|
|
//mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type);
|
|
}
|
|
}
|
|
}
|