Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java # src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java # src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java # src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java # src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java # src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java # src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java # src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java # src/main/java/abstractSyntaxTree/Statement/BlockStatement.java # src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java # src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java # src/main/java/abstractSyntaxTree/Statement/IfStatement.java # src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java # src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java # src/main/java/abstractSyntaxTree/Statement/WhileStatement.java
This commit is contained in:
commit
0d827c5f32
@ -2,11 +2,21 @@ package abstractSyntaxTree.Datatype;
|
||||
|
||||
import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class BoolDatatype extends AbstractType implements IDatatype{
|
||||
boolean value;
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BoolDatatype boolDatatype = (BoolDatatype) o;
|
||||
return (Objects.equals(value, boolDatatype.value));
|
||||
}
|
||||
@Override
|
||||
public TypeCheckResult typeCheck() throws Exception {
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
|
||||
|
@ -4,8 +4,11 @@ import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CharDatatype extends AbstractType implements IDatatype{
|
||||
char value;
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck() throws Exception {
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
@ -24,6 +27,13 @@ public class CharDatatype extends AbstractType implements IDatatype{
|
||||
|
||||
mv.visitLdcInsn((int)value);
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
CharDatatype charDatatype = (CharDatatype) o;
|
||||
return (Objects.equals(value, charDatatype.value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
|
@ -5,6 +5,8 @@ import TypeCheck.TypeCheckResult;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntDatatype extends AbstractType implements IDatatype{
|
||||
int value;
|
||||
@Override
|
||||
@ -32,6 +34,14 @@ public class IntDatatype extends AbstractType implements IDatatype{
|
||||
mv.visitLdcInsn(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IntDatatype intDatatype = (IntDatatype) o;
|
||||
return (Objects.equals(value, intDatatype.value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -176,6 +176,17 @@ public class BinaryExpression extends AbstractType implements IExpression{
|
||||
mv.visitLabel(expressionEnd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BinaryExpression binaryExpression = (BinaryExpression) o;
|
||||
return (Objects.equals(operator, binaryExpression.operator)
|
||||
&& Objects.equals(left, binaryExpression.left)
|
||||
&& Objects.equals(right, binaryExpression.right)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -64,6 +64,16 @@ public class InstVarExpression implements IExpression{
|
||||
mv.visitFieldInsn(Opcodes.GETFIELD, classRef.name, fieldName, fieldDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
InstVarExpression instVarExpression = (InstVarExpression) o;
|
||||
return (Objects.equals(classRef, instVarExpression.classRef)
|
||||
&& Objects.equals(fieldName, instVarExpression.fieldName)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -8,6 +8,8 @@ import org.objectweb.asm.MethodVisitor;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntConstantExpression extends AbstractType implements IExpression{
|
||||
public int value;
|
||||
|
||||
@ -25,6 +27,15 @@ public class IntConstantExpression extends AbstractType implements IExpression{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IntConstantExpression intConstantExpression = (IntConstantExpression) o;
|
||||
return (Objects.equals(value, intConstantExpression.value)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -10,6 +10,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
public class LocalVarIdentifier implements IExpression{
|
||||
|
||||
@ -72,6 +73,15 @@ public class LocalVarIdentifier implements IExpression{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) o;
|
||||
return (Objects.equals(identifier, localVarIdentifier.identifier)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -71,6 +71,16 @@ public class UnaryExpression extends AbstractType implements IExpression{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
UnaryExpression unaryExpression = (UnaryExpression) o;
|
||||
return (Objects.equals(operator, unaryExpression.operator)
|
||||
&& Objects.equals(operand, unaryExpression.operand)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -1,7 +1,10 @@
|
||||
package abstractSyntaxTree.Parameter;
|
||||
|
||||
import abstractSyntaxTree.Expression.UnaryExpression;
|
||||
import abstractSyntaxTree.Node;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Parameter implements Node {
|
||||
public String type;
|
||||
public String identifier;
|
||||
@ -10,4 +13,14 @@ public class Parameter implements Node {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Parameter parameter = (Parameter) o;
|
||||
return (Objects.equals(type, parameter.type)
|
||||
&& Objects.equals(identifier, parameter.identifier)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package abstractSyntaxTree.Parameter;
|
||||
import abstractSyntaxTree.Node;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ParameterList implements Node {
|
||||
public List<Parameter> parameterList;
|
||||
@ -10,4 +11,13 @@ public class ParameterList implements Node {
|
||||
public ParameterList(List<Parameter> parameterList) {
|
||||
this.parameterList = parameterList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ParameterList parameterListObj = (ParameterList) o;
|
||||
return (Objects.equals(parameterList, parameterListObj.parameterList)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,14 @@ package abstractSyntaxTree.Statement;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import TypeCheck.AbstractType;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Parameter.Parameter;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class BlockStatement extends AbstractType implements IStatement {
|
||||
|
||||
@ -87,6 +89,17 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BlockStatement blockStatement = (BlockStatement) o;
|
||||
return (Objects.equals(localVars, blockStatement.localVars)
|
||||
&& Objects.equals(returnType, blockStatement.returnType)
|
||||
&& Objects.equals(statements, blockStatement.statements)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -8,6 +8,7 @@ import org.objectweb.asm.MethodVisitor;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class EmptyStatement extends AbstractType implements IStatement{
|
||||
|
||||
@ -23,6 +24,11 @@ public class EmptyStatement extends AbstractType implements IStatement{
|
||||
//An empty statement does not generate any code
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -71,6 +71,17 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IfElseStatement ifElseStatement = (IfElseStatement) o;
|
||||
return (Objects.equals(condition, ifElseStatement.condition)
|
||||
&& Objects.equals(ifStatement, ifElseStatement.ifStatement)
|
||||
&& Objects.equals(elseStatement, ifElseStatement.elseStatement)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -9,6 +9,7 @@ import org.objectweb.asm.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class IfStatement extends AbstractType implements IStatement{
|
||||
IExpression condition;
|
||||
@ -51,6 +52,16 @@ public class IfStatement extends AbstractType implements IStatement{
|
||||
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IfStatement ifStatementObj = (IfStatement) o;
|
||||
return (Objects.equals(condition, ifStatementObj.condition)
|
||||
&& Objects.equals(ifStatement, ifStatementObj.ifStatement)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -17,7 +17,7 @@ public class LocalVarDecl implements IStatement{
|
||||
}
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||
TypeCheckHelper.typeExists(this.type, new ArrayList<>(typeContext.keySet()));
|
||||
TypeCheckHelper.typeExists(this.type, new ArrayList<>(methodContext.keySet()));
|
||||
|
||||
localVars.put(this.identifier, this.type);
|
||||
|
||||
@ -50,6 +50,16 @@ public class LocalVarDecl implements IStatement{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LocalVarDecl localVarDecl = (LocalVarDecl) o;
|
||||
return (Objects.equals(type, localVarDecl.type)
|
||||
&& Objects.equals(identifier, localVarDecl.identifier)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -9,6 +9,7 @@ import org.objectweb.asm.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ReturnStatement extends AbstractType implements IStatement{
|
||||
IExpression expression;
|
||||
@ -54,6 +55,15 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ReturnStatement returnStatement = (ReturnStatement) o;
|
||||
return (Objects.equals(expression, returnStatement.expression)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -9,6 +9,7 @@ import org.objectweb.asm.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class WhileStatement extends AbstractType implements IStatement {
|
||||
IExpression condition;
|
||||
@ -60,6 +61,16 @@ public class WhileStatement extends AbstractType implements IStatement {
|
||||
mv.visitLabel(conditionFalse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
WhileStatement whileStatement = (WhileStatement) o;
|
||||
return (Objects.equals(condition, whileStatement.condition)
|
||||
&& Objects.equals(statement, whileStatement.statement)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
|
@ -8,6 +8,7 @@ import abstractSyntaxTree.Expression.InstVarExpression;
|
||||
import abstractSyntaxTree.Expression.LocalVarIdentifier;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import abstractSyntaxTree.Statement.IStatement;
|
||||
import abstractSyntaxTree.Statement.WhileStatement;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -112,4 +113,15 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
AssignStatementExpression assignStatementExpression = (AssignStatementExpression) o;
|
||||
return (Objects.equals(operator, assignStatementExpression.operator)
|
||||
&& Objects.equals(left, assignStatementExpression.left)
|
||||
&& Objects.equals(right, assignStatementExpression.right)
|
||||
);
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ import org.objectweb.asm.Opcodes;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MethodCallStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||
String methodName;
|
||||
@ -91,4 +92,14 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
MethodCallStatementExpression methodCallStatementExpression = (MethodCallStatementExpression) o;
|
||||
return (Objects.equals(methodName, methodCallStatementExpression.methodName)
|
||||
&& Objects.equals(arguments, methodCallStatementExpression.arguments)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import abstractSyntaxTree.Program;
|
||||
import abstractSyntaxTree.Statement.*;
|
||||
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
|
||||
import abstractSyntaxTree.StatementExpression.NewStatementExpression;
|
||||
import gen.DecafBaseVisitor;
|
||||
import gen.DecafParser;
|
||||
|
||||
@ -39,7 +40,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
hasMain = false;
|
||||
}
|
||||
for (DecafParser.LocalVarDeclContext fieldDecl: ctx.localVarDecl()) {
|
||||
fieldDecls.add((FieldDecl) visit(fieldDecl));
|
||||
fieldDecls.add((FieldDecl) generateFieldDecl(fieldDecl));
|
||||
}
|
||||
for (DecafParser.ConstuctorDeclContext constDecl: ctx.constuctorDecl()) {
|
||||
MethodDecl constructor = ((MethodDecl) visit(constDecl));
|
||||
@ -54,9 +55,13 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return new RefType(name,fieldDecls, methodDecls, hasMain);
|
||||
}
|
||||
|
||||
public Node generateFieldDecl(DecafParser.LocalVarDeclContext ctx) {
|
||||
return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitLocalVarDecl(DecafParser.LocalVarDeclContext ctx) {
|
||||
return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText());
|
||||
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -108,6 +113,8 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return visitStmtExpr(ctx.stmtExpr());
|
||||
} else if (ctx.emptyStatement() != null) {
|
||||
return visitEmptyStatement(ctx.emptyStatement());
|
||||
} else if (ctx.localVarDecl() != null) {
|
||||
return visitLocalVarDecl(ctx.localVarDecl());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -178,15 +185,18 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
@Override
|
||||
public Node visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||
return super.visitMethodCall(ctx);
|
||||
}
|
||||
|
||||
//add the expression list later to the constructor
|
||||
@Override
|
||||
public Node visitNewDecl(DecafParser.NewDeclContext ctx) {
|
||||
return super.visitNewDecl(ctx);
|
||||
String name = ctx.Identifier().getText();
|
||||
List<IExpression> expressions = generateExpressions(ctx.argumentList());
|
||||
return new NewStatementExpression(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -290,14 +300,11 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return super.visitInstVar(ctx);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public Node visitArgumentList(DecafParser.ArgumentListContext ctx) {
|
||||
// if (ctx.expression().size() == 1) {
|
||||
// return visitExpression(ctx.expression(0));
|
||||
// } else if (ctx.expression().size() >= 2) {
|
||||
// for(DecafParser.ExpressionContext expressionContext: ctx.expression()) {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public List<IExpression> generateExpressions(DecafParser.ArgumentListContext ctx) {
|
||||
List<IExpression> expressions = new ArrayList<>();
|
||||
for (DecafParser.ExpressionContext expressionContext : ctx.expression()) {
|
||||
expressions.add((IExpression) visitExpression(expressionContext));
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import org.junit.Test;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import abstractSyntaxTree.ASTGenerator;
|
||||
import astGenerator.ASTGenerator;
|
||||
|
||||
public class AstComparer {
|
||||
|
||||
|
@ -1,16 +0,0 @@
|
||||
package ByteCode;
|
||||
|
||||
import java.security.SecureClassLoader;
|
||||
public class ByteCodeLoader extends SecureClassLoader {
|
||||
private final byte[] bytecode;
|
||||
|
||||
public ByteCodeLoader(byte[] bytecode) {
|
||||
this.bytecode = bytecode;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
return defineClass(name, bytecode, 0, bytecode.length);
|
||||
}
|
||||
}
|
||||
|
110
src/test/java/ByteCode/CompareByteCodeBehaviour.java
Normal file
110
src/test/java/ByteCode/CompareByteCodeBehaviour.java
Normal file
@ -0,0 +1,110 @@
|
||||
package ByteCode;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import static ByteCode.CompareByteCodeSyntax.compareMethod;
|
||||
|
||||
public class CompareByteCodeBehaviour {
|
||||
ArrayList<Method> methodArray1;
|
||||
ArrayList<Method> methodArray2;
|
||||
|
||||
public CompareByteCodeBehaviour(){
|
||||
methodArray1 = new ArrayList<Method>();
|
||||
methodArray2 = new ArrayList<Method>();
|
||||
}
|
||||
|
||||
private void clearMethods(){
|
||||
methodArray1.clear();
|
||||
methodArray2.clear();
|
||||
}
|
||||
|
||||
public boolean functionAlreadyAdded(Method method1, Method method2){
|
||||
int lengthOfArray = methodArray1.size();
|
||||
for (int i = 0; i < lengthOfArray; i++) {
|
||||
if (method1.getName().equals(this.methodArray1.get(i).getName()) ||
|
||||
method2.getName().equals(this.methodArray1.get(i).getName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void sortMethods(Class<?> class1, Class<?> class2) {
|
||||
this.clearMethods();
|
||||
Method[] methods1 = class1.getDeclaredMethods();
|
||||
Method[] methods2 = class2.getDeclaredMethods();
|
||||
|
||||
for (Method method1 : methods1) {
|
||||
for (Method method2 : methods2) {
|
||||
if (compareMethod(method1, method2)) {
|
||||
if (!functionAlreadyAdded(method1, method2)) {
|
||||
this.methodArray1.add(method1);
|
||||
this.methodArray2.add(method2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void compareMethodBehaviour(Class<?> class1, Class<?> class2){
|
||||
try {
|
||||
this.sortMethods(class1, class2);
|
||||
// Create instances
|
||||
Constructor<?> constructor1 = class1.getDeclaredConstructor();
|
||||
Constructor<?> constructor2 = class2.getDeclaredConstructor();
|
||||
Object obj1 = constructor1.newInstance();
|
||||
Object obj2 = constructor2.newInstance();
|
||||
|
||||
// Get methods
|
||||
Method[] methods1 = new Method[this.methodArray1.size()];
|
||||
methods1 = this.methodArray1.toArray(methods1);
|
||||
Method[] methods2 = new Method[this.methodArray2.size()];
|
||||
methods2 = this.methodArray2.toArray(methods2);
|
||||
|
||||
|
||||
// Compare methods
|
||||
for (int i = 0; i < methods1.length; i++) {
|
||||
Method method1 = methods1[i];
|
||||
Method method2 = methods2[i];
|
||||
|
||||
|
||||
// Test with some sample inputs
|
||||
Object[] testInputs = getTestInputs(method1.getParameterTypes());
|
||||
|
||||
Object result1 = method1.invoke(obj1, testInputs);
|
||||
Object result2 = method2.invoke(obj2, testInputs);
|
||||
|
||||
/*
|
||||
if (!result1.equals(result2)) {
|
||||
System.out.println("Methods " + method1.getName() + " do not produce the same result");
|
||||
} else {
|
||||
System.out.println("Methods " + method1.getName() + " produce the same result");
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
} catch (InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Object[] getTestInputs(Class<?>[] parameterTypes) {
|
||||
// Create test inputs based on parameter types
|
||||
Object[] inputs = new Object[parameterTypes.length];
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
if (parameterTypes[i] == int.class) {
|
||||
inputs[i] = 1; // example value
|
||||
} else if (parameterTypes[i] == String.class) {
|
||||
inputs[i] = "test"; // example value
|
||||
}
|
||||
// Add more cases as needed for different parameter types
|
||||
}
|
||||
return inputs;
|
||||
}
|
||||
|
||||
}
|
122
src/test/java/ByteCode/CompareByteCodeSyntax.java
Normal file
122
src/test/java/ByteCode/CompareByteCodeSyntax.java
Normal file
@ -0,0 +1,122 @@
|
||||
package ByteCode;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
public class CompareByteCodeSyntax {
|
||||
|
||||
public static boolean haveSameBehavior(Class<?> class1, Class<?> class2) {
|
||||
if (!compareClassSignatures(class1, class2)) {
|
||||
return false;
|
||||
}
|
||||
if (!compareMethods(class1, class2)) {
|
||||
return false;
|
||||
}
|
||||
if (!compareFields(class1, class2)) {
|
||||
return false;
|
||||
}
|
||||
if (!compareAnnotations(class1.getAnnotations(), class2.getAnnotations())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean compareClassSignatures(Class<?> class1, Class<?> class2) {
|
||||
if (!Arrays.equals(class1.getInterfaces(), class2.getInterfaces())) {
|
||||
return false;
|
||||
}
|
||||
if (class1.getSuperclass() != class2.getSuperclass()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean compareMethods(Class<?> class1, Class<?> class2) {
|
||||
Method[] methods1 = class1.getDeclaredMethods();
|
||||
Method[] methods2 = class2.getDeclaredMethods();
|
||||
if (methods1.length != methods2.length) {
|
||||
return false;
|
||||
}
|
||||
for (Method method1 : methods1) {
|
||||
boolean found = false;
|
||||
for (Method method2 : methods2) {
|
||||
if (compareMethod(method1, method2)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean compareMethod(Method method1, Method method2) {
|
||||
if (!method1.getReturnType().equals(method2.getReturnType())) {
|
||||
return false;
|
||||
}
|
||||
if (!Arrays.equals(method1.getParameterTypes(), method2.getParameterTypes())) {
|
||||
return false;
|
||||
}
|
||||
if (!Arrays.equals(method1.getExceptionTypes(), method2.getExceptionTypes())) {
|
||||
return false;
|
||||
}
|
||||
if (!compareAnnotations(method1.getAnnotations(), method2.getAnnotations())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean compareFields(Class<?> class1, Class<?> class2) {
|
||||
Field[] fields1 = class1.getDeclaredFields();
|
||||
Field[] fields2 = class2.getDeclaredFields();
|
||||
if (fields1.length != fields2.length) {
|
||||
return false;
|
||||
}
|
||||
for (Field field1 : fields1) {
|
||||
boolean found = false;
|
||||
for (Field field2 : fields2) {
|
||||
if (compareField(field1, field2)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean compareField(Field field1, Field field2) {
|
||||
if (!field1.getType().equals(field2.getType())) {
|
||||
return false;
|
||||
}
|
||||
if (!compareAnnotations(field1.getAnnotations(), field2.getAnnotations())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean compareAnnotations(Annotation[] annotations1, Annotation[] annotations2) {
|
||||
if (annotations1.length != annotations2.length) {
|
||||
return false;
|
||||
}
|
||||
for (Annotation annotation1 : annotations1) {
|
||||
boolean found = false;
|
||||
for (Annotation annotation2 : annotations2) {
|
||||
if (annotation1.annotationType().equals(annotation2.annotationType())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
4
src/test/java/ByteCode/TestAll.java
Normal file
4
src/test/java/ByteCode/TestAll.java
Normal file
@ -0,0 +1,4 @@
|
||||
package ByteCode;
|
||||
|
||||
public class TestAll {
|
||||
}
|
5
src/test/java/Typecheck/TestAll.java
Normal file
5
src/test/java/Typecheck/TestAll.java
Normal file
@ -0,0 +1,5 @@
|
||||
package Typecheck;
|
||||
|
||||
public class TestAll {
|
||||
|
||||
}
|
30
src/test/java/Typecheck/TypeChecker.java
Normal file
30
src/test/java/Typecheck/TypeChecker.java
Normal file
@ -0,0 +1,30 @@
|
||||
package Typecheck;
|
||||
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Program;
|
||||
import gen.DecafLexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TypeChecker {
|
||||
// Method to test if the TypeCheck returns the expected Result
|
||||
public void assertTypeCheckResult(Program programmToBeTested, boolean expectedResult) throws Exception {
|
||||
boolean actualResult;
|
||||
try {
|
||||
TypeCheckResult typeCheckResult = programmToBeTested.typeCheck();
|
||||
actualResult = true;
|
||||
} catch (Exception e) {
|
||||
actualResult = false;
|
||||
}
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user