Compare commits
No commits in common. "21da31dee304c26e3064e6adbcdaa473b0484be4" and "294df16e89451520fdc229dc45400423e4241b63" have entirely different histories.
21da31dee3
...
294df16e89
@ -15,20 +15,30 @@ public class ClassNode implements ASTNode, Visitable {
|
|||||||
public AccessModifierNode accessType;
|
public AccessModifierNode accessType;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
|
public boolean hasConstructor;
|
||||||
|
|
||||||
public ClassNode(){
|
public ClassNode() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public ClassNode(String accessType, String identifier){
|
public ClassNode(String accessType, String identifier){
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accessType = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
|
hasConstructor = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMember(MemberNode member) {
|
public void addMember(MemberNode member) {
|
||||||
|
if (member instanceof ConstructorNode) {
|
||||||
|
this.hasConstructor = true;
|
||||||
|
}
|
||||||
members.add(member);
|
members.add(member);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ensureConstructor(){
|
||||||
|
if(!hasConstructor) {
|
||||||
|
ConstructorNode constructor = new ConstructorNode(new AccessModifierNode("public"), identifier);
|
||||||
|
members.addFirst(constructor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<MethodNode> getMethods(){
|
public List<MethodNode> getMethods(){
|
||||||
List<MethodNode> methods = new ArrayList<>();
|
List<MethodNode> methods = new ArrayList<>();
|
||||||
for (MemberNode member : members) {
|
for (MemberNode member : members) {
|
||||||
|
@ -8,6 +8,4 @@ public interface IExpressionNode extends ASTNode, Visitable {
|
|||||||
|
|
||||||
ITypeNode getType();
|
ITypeNode getType();
|
||||||
|
|
||||||
void setType(ITypeNode type);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@ import typechecker.TypeCheckResult;
|
|||||||
|
|
||||||
public class BinaryNode implements IExpressionNode {
|
public class BinaryNode implements IExpressionNode {
|
||||||
|
|
||||||
private ITypeNode typeNode;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
@ -16,11 +14,6 @@ public class BinaryNode implements IExpressionNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ITypeNode getType() {
|
public ITypeNode getType() {
|
||||||
return typeNode;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setType(ITypeNode type) {
|
|
||||||
this.typeNode = type;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ public class CalculationNode extends BinaryNode {
|
|||||||
public CalculationNode calculationExpression;
|
public CalculationNode calculationExpression;
|
||||||
public EnumLineOperator operator;
|
public EnumLineOperator operator;
|
||||||
public DotNode dotExpression;
|
public DotNode dotExpression;
|
||||||
private ITypeNode typeNode;
|
|
||||||
|
|
||||||
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
|
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
|
||||||
this.calculationExpression = calculationExpression;
|
this.calculationExpression = calculationExpression;
|
||||||
@ -33,4 +32,9 @@ public class CalculationNode extends BinaryNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ITypeNode getType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,4 +34,9 @@ public class DotNode extends BinaryNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ITypeNode getType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -36,4 +36,9 @@ public class DotSubstractionNode extends BinaryNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ITypeNode getType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,4 +42,9 @@ public class NonCalculationNode extends BinaryNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ITypeNode getType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,13 @@
|
|||||||
package ast.expressions.unaryexpressions;
|
package ast.expressions.unaryexpressions;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.type.type.ITypeNode;
|
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import visitor.Visitable;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MemberAccessNode implements ASTNode, Visitable {
|
public class MemberAccessNode implements ASTNode {
|
||||||
public Boolean thisExpr;
|
public Boolean thisExpr;
|
||||||
public List<String> identifiers = new ArrayList<>();
|
public List<String> identifiers = new ArrayList<>();
|
||||||
private ITypeNode typeNode;
|
|
||||||
|
|
||||||
public MemberAccessNode(Boolean thisExpr) {
|
public MemberAccessNode(Boolean thisExpr) {
|
||||||
this.thisExpr = thisExpr;
|
this.thisExpr = thisExpr;
|
||||||
@ -22,17 +17,4 @@ public class MemberAccessNode implements ASTNode, Visitable {
|
|||||||
identifiers.add(identifier);
|
identifiers.add(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return visitor.analyze(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ITypeNode getTypeNode() {
|
|
||||||
return typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTypeNode(ITypeNode typeNode) {
|
|
||||||
this.typeNode = typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import ast.type.AccessModifierNode;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class ConstructorNode extends MethodNode {
|
public class ConstructorNode extends MethodNode {
|
||||||
public AccessModifierNode accessType;
|
public AccessModifierNode accessType;
|
||||||
@ -14,6 +13,11 @@ public class ConstructorNode extends MethodNode {
|
|||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
public List<ParameterNode> parameters = new ArrayList<>();
|
||||||
public BlockNode block;
|
public BlockNode block;
|
||||||
|
|
||||||
|
public ConstructorNode(AccessModifierNode accessType, String identifier) {
|
||||||
|
this.accessType = accessType;
|
||||||
|
this.identifier = identifier;
|
||||||
|
}
|
||||||
|
|
||||||
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accessType = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
@ -24,18 +28,4 @@ public class ConstructorNode extends MethodNode {
|
|||||||
parameters.add(parameterNode);
|
parameters.add(parameterNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isSame(MethodNode methodNode) {
|
|
||||||
if (!(Objects.equals(this.identifier, methodNode.getIdentifier()))
|
|
||||||
|| getParameters().size() != methodNode.getParameters().size()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < this.getParameters().size(); i++) {
|
|
||||||
if (!this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package ast.members;
|
|||||||
import ast.statements.BlockNode;
|
import ast.statements.BlockNode;
|
||||||
|
|
||||||
public class MainMethodNode extends MethodNode {
|
public class MainMethodNode extends MethodNode {
|
||||||
|
public BlockNode block;
|
||||||
|
|
||||||
public MainMethodNode(BlockNode block) {
|
public MainMethodNode(BlockNode block) {
|
||||||
this.block = block;
|
this.block = block;
|
||||||
|
@ -46,7 +46,7 @@ public class MethodNode implements MemberNode, Visitable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < this.getParameters().size(); i++) {
|
for (int i = 0; i < this.getParameters().size(); i++) {
|
||||||
if (!this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
|
if (this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package ast.statementexpressions;
|
package ast.statementexpressions;
|
||||||
|
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.type.type.ITypeNode;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class AssignableNode implements IStatementExpressionNode {
|
public class AssignableNode implements IStatementExpressionNode {
|
||||||
public String identifier;
|
public String identifier;
|
||||||
private ITypeNode typeNode;
|
|
||||||
|
|
||||||
public MemberAccessNode memberAccess;
|
public MemberAccessNode memberAccess;
|
||||||
|
|
||||||
@ -24,12 +22,4 @@ public class AssignableNode implements IStatementExpressionNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITypeNode getTypeNode() {
|
|
||||||
return typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTypeNode(ITypeNode typeNode) {
|
|
||||||
this.typeNode = typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,8 @@ package ast.statementexpressions.methodcallstatementnexpressions;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import visitor.Visitable;
|
|
||||||
|
|
||||||
public class TargetNode implements ASTNode, Visitable {
|
public class TargetNode implements ASTNode {
|
||||||
public Boolean thisTar;
|
public Boolean thisTar;
|
||||||
public MemberAccessNode memberAccess;
|
public MemberAccessNode memberAccess;
|
||||||
public NewDeclarationNode newDeclaration;
|
public NewDeclarationNode newDeclaration;
|
||||||
@ -28,11 +25,4 @@ public class TargetNode implements ASTNode, Visitable {
|
|||||||
public TargetNode(String identifier) {
|
public TargetNode(String identifier) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return visitor.analyze(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -5,7 +5,7 @@ import semantic.SemanticVisitor;
|
|||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class ElseNode implements IStatementNode {
|
public class ElseNode implements IStatementNode {
|
||||||
public BlockNode block;
|
BlockNode block;
|
||||||
|
|
||||||
public ElseNode(BlockNode block) {
|
public ElseNode(BlockNode block) {
|
||||||
this.block = block;
|
this.block = block;
|
||||||
@ -14,6 +14,6 @@ public class ElseNode implements IStatementNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class IfElseNode implements IStatementNode {
|
public class IfElseNode implements IStatementNode {
|
||||||
public IfNode ifStatement;
|
IfNode ifStatement;
|
||||||
public List<IfNode> elseIfStatements = new ArrayList<>();
|
List<IfNode> elseIfStatements = new ArrayList<>();
|
||||||
public ElseNode elseStatement;
|
ElseNode elseStatement;
|
||||||
|
|
||||||
public IfElseNode(IfNode ifStatement, ElseNode elseNode) {
|
public IfElseNode(IfNode ifStatement, ElseNode elseNode) {
|
||||||
this.ifStatement = ifStatement;
|
this.ifStatement = ifStatement;
|
||||||
@ -23,6 +23,6 @@ public class IfElseNode implements IStatementNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import semantic.SemanticVisitor;
|
|||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class IfNode implements IStatementNode {
|
public class IfNode implements IStatementNode {
|
||||||
public IExpressionNode expression;
|
IExpressionNode expression;
|
||||||
public BlockNode block;
|
BlockNode block;
|
||||||
|
|
||||||
public IfNode(IExpressionNode expression, BlockNode block) {
|
public IfNode(IExpressionNode expression, BlockNode block) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
@ -16,6 +16,6 @@ public class IfNode implements IStatementNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import semantic.SemanticVisitor;
|
|||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class WhileNode implements IStatementNode {
|
public class WhileNode implements IStatementNode {
|
||||||
public IExpressionNode expression;
|
IExpressionNode expression;
|
||||||
public BlockNode block;
|
BlockNode block;
|
||||||
|
|
||||||
public WhileNode(IExpressionNode expression, BlockNode block) {
|
public WhileNode(IExpressionNode expression, BlockNode block) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
@ -21,6 +21,6 @@ public class WhileNode implements IStatementNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
package ast.type;
|
package ast.type;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import visitor.Visitable;
|
|
||||||
|
|
||||||
public class ValueNode implements ASTNode, Visitable {
|
public class ValueNode implements ASTNode {
|
||||||
public EnumValueNode valueType;
|
public EnumValueNode valueType;
|
||||||
public String value;
|
public String value;
|
||||||
|
|
||||||
@ -13,10 +10,4 @@ public class ValueNode implements ASTNode, Visitable {
|
|||||||
this.valueType = valueType;
|
this.valueType = valueType;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return visitor.analyze(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -44,32 +44,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
||||||
ClassNode classNode;
|
ClassNode classNode = new ClassNode(ctx.AccessModifier().getText(), ctx.Identifier().getText());
|
||||||
if(ctx.AccessModifier() != null){
|
|
||||||
classNode = new ClassNode(ctx.AccessModifier().getText(), ctx.Identifier().getText());
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
classNode = new ClassNode("public", ctx.Identifier().getText());
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean hasConstructor = false;
|
|
||||||
|
|
||||||
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
||||||
MemberNode memberNode = (MemberNode) visit(member);
|
classNode.addMember((MemberNode) visit(member));
|
||||||
if(memberNode != null) {
|
|
||||||
if(memberNode instanceof ConstructorNode){
|
|
||||||
hasConstructor = true;
|
|
||||||
}
|
|
||||||
classNode.addMember(memberNode);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
classNode.ensureConstructor();
|
||||||
if(!hasConstructor){
|
|
||||||
BlockNode blockNode = new BlockNode();
|
|
||||||
blockNode.addStatement(new ReturnNode(null));
|
|
||||||
classNode.members.addFirst(new ConstructorNode("public", ctx.Identifier().getText(), blockNode));
|
|
||||||
}
|
|
||||||
|
|
||||||
return classNode;
|
return classNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,54 +72,29 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
return mainMethod;
|
return mainMethod;
|
||||||
} else {
|
} else {
|
||||||
if(ctx.type() != null) {
|
if(ctx.type() != null) {
|
||||||
if(ctx.AccessModifier() != null) {
|
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
||||||
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
if(ctx.parameterList() != null) {
|
||||||
if(ctx.parameterList() != null) {
|
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
||||||
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
methodNode.addParameter((ParameterNode) visit(parameter));
|
||||||
methodNode.addParameter((ParameterNode) visit(parameter));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return methodNode;
|
|
||||||
} else {
|
|
||||||
MethodNode methodNode = new MethodNode("public", createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
|
||||||
if(ctx.parameterList() != null) {
|
|
||||||
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
|
||||||
methodNode.addParameter((ParameterNode) visit(parameter));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return methodNode;
|
|
||||||
}
|
}
|
||||||
|
return methodNode;
|
||||||
} else {
|
} else {
|
||||||
if(ctx.AccessModifier() != null) {
|
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
||||||
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
if(ctx.parameterList() != null) {
|
||||||
if(ctx.parameterList() != null) {
|
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
||||||
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
methodNode.addParameter((ParameterNode) visit(parameter));
|
||||||
methodNode.addParameter((ParameterNode) visit(parameter));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
methodNode.block.addStatement(new ReturnNode(null));
|
|
||||||
return methodNode;
|
|
||||||
} else {
|
|
||||||
MethodNode methodNode = new MethodNode("public", null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
|
||||||
if(ctx.parameterList() != null) {
|
|
||||||
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
|
||||||
methodNode.addParameter((ParameterNode) visit(parameter));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
methodNode.block.addStatement(new ReturnNode(null));
|
|
||||||
return methodNode;
|
|
||||||
}
|
}
|
||||||
|
methodNode.block.addStatement(new ReturnNode(null));
|
||||||
|
return methodNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
|
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
|
||||||
if(ctx.AccessModifier() != null) {
|
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
|
||||||
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
|
|
||||||
} else {
|
|
||||||
return new FieldNode(new AccessModifierNode("public"), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -230,17 +184,10 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
crement = (IStatementNode) visit(ctx.statementExpression(i));
|
crement = (IStatementNode) visit(ctx.statementExpression(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockNode forBlock = new BlockNode();
|
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
|
||||||
|
|
||||||
BlockNode forStatements = (BlockNode) visit(ctx.blockStatement());
|
|
||||||
if(forStatements != null) {
|
|
||||||
forBlock.addStatement((IStatementNode) forStatements);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(crement != null){
|
if(crement != null){
|
||||||
BlockNode forCrement = new BlockNode();
|
forBlock.addStatement((crement));
|
||||||
forCrement.addStatement((crement));
|
|
||||||
forBlock.addStatement(forCrement);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WhileNode While = new WhileNode(condition, forBlock);
|
WhileNode While = new WhileNode(condition, forBlock);
|
||||||
@ -312,12 +259,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) {
|
public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) {
|
||||||
MethodCallNode methodCallStatementExpressionNode;
|
MethodCallNode methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText());
|
||||||
if(ctx.target() != null) {
|
|
||||||
methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText());
|
|
||||||
} else {
|
|
||||||
methodCallStatementExpressionNode = new MethodCallNode(null, ctx.Identifier().getText());
|
|
||||||
}
|
|
||||||
for(SimpleJavaParser.ChainedMethodContext chainedMethod : ctx.chainedMethod()) {
|
for(SimpleJavaParser.ChainedMethodContext chainedMethod : ctx.chainedMethod()) {
|
||||||
methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod));
|
methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod));
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||||
package parser.generated;
|
package parser.generated;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
import org.antlr.v4.runtime.ParserRuleContext;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||||
package parser.generated;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||||
package parser.generated;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||||
package parser.generated;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||||
package parser.generated;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.atn.*;
|
import org.antlr.v4.runtime.atn.*;
|
||||||
import org.antlr.v4.runtime.dfa.DFA;
|
import org.antlr.v4.runtime.dfa.DFA;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||||
package parser.generated;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ import java.util.Objects;
|
|||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import ast.expressions.binaryexpressions.*;
|
import ast.expressions.binaryexpressions.*;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
import ast.members.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.members.FieldNode;
|
import ast.members.FieldNode;
|
||||||
@ -22,13 +21,8 @@ import ast.statementexpressions.NewDeclarationNode;
|
|||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import ast.type.AccessModifierNode;
|
|
||||||
import ast.type.EnumAccessModifierNode;
|
|
||||||
import ast.type.ValueNode;
|
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import com.sun.jdi.IntegerType;
|
|
||||||
import semantic.context.Context;
|
import semantic.context.Context;
|
||||||
import semantic.exceptions.*;
|
import semantic.exceptions.*;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
@ -140,20 +134,26 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// Check if this method is already declared
|
||||||
|
|
||||||
currentMethodReturnType = methodNode.getType();
|
currentMethodReturnType = methodNode.getType();
|
||||||
currentNullType = currentMethodReturnType;
|
currentNullType = currentMethodReturnType; // Solange nicht in einem Assign oder Methoden-Aufruf dieser Typ
|
||||||
|
|
||||||
|
ITypeNode resultType = new BaseType(TypeEnum.VOID);
|
||||||
|
|
||||||
|
// gesetzt ist, ist dieser der Rückgabewert der Methode
|
||||||
var result = methodNode.block.accept(this);
|
var result = methodNode.block.accept(this);
|
||||||
valid = valid && result.isValid();
|
valid = valid && result.isValid();
|
||||||
currentScope.popScope();
|
currentScope.popScope();
|
||||||
ITypeNode resultType = result.getType();
|
resultType = result.getType();
|
||||||
|
|
||||||
if (resultType == null) {
|
if (resultType == null) {
|
||||||
resultType = new BaseType(TypeEnum.VOID);
|
resultType = new BaseType(TypeEnum.VOID);
|
||||||
}
|
}
|
||||||
if (methodNode.getType() == null) {
|
if (!resultType.equals(methodNode.getType())) {
|
||||||
methodNode.setType(new BaseType(TypeEnum.VOID));
|
errors.add(new TypeMismatchException("Method-Declaration " + methodNode.getIdentifier() + " with type "
|
||||||
|
+ methodNode.getType() + " has at least one Mismatching return Type:"));
|
||||||
|
valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TypeCheckResult(valid, resultType);
|
return new TypeCheckResult(valid, resultType);
|
||||||
@ -174,65 +174,43 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(IfNode toCheck) {
|
public TypeCheckResult analyze(IfNode toCheck) {
|
||||||
toCheck.block.accept(this);
|
|
||||||
|
|
||||||
var resultExpression = toCheck.expression.accept(this);
|
|
||||||
if (resultExpression.isValid()) {
|
|
||||||
if (!resultExpression.getType().equals(new BaseType(TypeEnum.BOOL))) {
|
|
||||||
errors.add(new TypeMismatchException("Expression must be Boolean"));
|
|
||||||
return new TypeCheckResult(false, new BaseType(TypeEnum.VOID));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TypeCheckResult(true, null);
|
return new TypeCheckResult(true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(ReturnNode toCheck) {
|
public TypeCheckResult analyze(ReturnNode toCheck) {
|
||||||
if (toCheck.expression != null) {
|
|
||||||
|
if(toCheck.expression != null){
|
||||||
var result = toCheck.expression.accept(this);
|
var result = toCheck.expression.accept(this);
|
||||||
if (result.isValid()) {
|
|
||||||
if (!result.getType().equals(currentMethodReturnType)) {
|
|
||||||
errors.add(new TypeMismatchException("Mismatched return Type from method"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new TypeCheckResult(true, result.getType());
|
return new TypeCheckResult(true, result.getType());
|
||||||
} else if (toCheck.voidReturn) {
|
} else {
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID));
|
return new TypeCheckResult(false, null);
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(WhileNode toCheck) {
|
public TypeCheckResult analyze(WhileNode toCheck) {
|
||||||
var expResult = toCheck.expression.accept(this);
|
return null;
|
||||||
var blockRes = toCheck.block.accept(this);
|
|
||||||
return new TypeCheckResult(expResult.isValid() && blockRes.isValid(), blockRes.getType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(ParameterNode toCheck) {
|
public TypeCheckResult analyze(ParameterNode toCheck) {
|
||||||
return new TypeCheckResult(true, toCheck.type);
|
|
||||||
|
|
||||||
|
return new TypeCheckResult(true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(BlockNode blockNode) {
|
public TypeCheckResult analyze(BlockNode blockNode) {
|
||||||
ITypeNode blockReturnType = null;
|
ITypeNode blockReturnType = null;
|
||||||
if (blockNode.statements.isEmpty()) {
|
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
}
|
|
||||||
for (IStatementNode statementNode : blockNode.statements) {
|
for (IStatementNode statementNode : blockNode.statements) {
|
||||||
var result = statementNode.accept(this);
|
var result = statementNode.accept(this);
|
||||||
if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
|
if(result.getType() != null){
|
||||||
if (result.getType() != null) {
|
if(blockReturnType == null){
|
||||||
if (blockReturnType == null) {
|
blockReturnType = result.getType();
|
||||||
blockReturnType = result.getType();
|
} else {
|
||||||
} else {
|
errors.add(new MultipleReturnTypes("There are multiple Return types"));
|
||||||
if (!blockReturnType.equals(result.getType())) {
|
|
||||||
errors.add(new MultipleReturnTypes("There are multiple Return types"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -241,31 +219,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignableNode toCheck) {
|
public TypeCheckResult analyze(AssignableNode toCheck) {
|
||||||
|
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
|
||||||
if (toCheck.memberAccess != null) {
|
|
||||||
var result = toCheck.memberAccess.accept(this);
|
|
||||||
toCheck.setTypeNode(result.getType());
|
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
if (currentFields.get(toCheck.identifier) != null) {
|
|
||||||
var type = currentFields.get(toCheck.identifier);
|
|
||||||
toCheck.setTypeNode(type);
|
|
||||||
return new TypeCheckResult(true, type);
|
|
||||||
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
|
|
||||||
var type = currentScope.getLocalVar(toCheck.identifier);
|
|
||||||
return new TypeCheckResult(true, type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(ElseNode toCheck) {
|
public TypeCheckResult analyze(ElseNode toCheck) {
|
||||||
return toCheck.block.accept(this);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*@Override
|
||||||
|
public TypeCheckResult analyze(ForNode toCheck) {
|
||||||
|
return null;
|
||||||
|
}*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignNode toCheck) {
|
public TypeCheckResult analyze(AssignNode toCheck) {
|
||||||
AssignableNode assignable = toCheck.assignable;
|
AssignableNode assignable = toCheck.assignable;
|
||||||
@ -291,61 +258,33 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentNullType = lResult.getType();
|
currentNullType = lResult.getType();
|
||||||
var rResult = rExpression.accept(this);
|
var rResult = rExpression.accept(this);
|
||||||
|
|
||||||
if (!Objects.equals(lResult.getType(), rResult.getType())) {
|
if (!Objects.equals(currentScope.getLocalVar(toCheck.assignable.identifier), rExpression.getType())) {
|
||||||
errors.add(new TypeMismatchException(
|
errors.add(new TypeMismatchException(
|
||||||
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
|
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
|
||||||
+ rResult.getType() + "\""));
|
+ rResult.getType() + "\""));
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
|
// else {
|
||||||
|
// toCheck.setType(assignable.getType());
|
||||||
|
// }
|
||||||
valid = valid && lResult.isValid() && rResult.isValid();
|
valid = valid && lResult.isValid() && rResult.isValid();
|
||||||
currentNullType = null;
|
currentNullType = null;
|
||||||
return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently
|
return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(DecrementNode decrementNode) {
|
public TypeCheckResult analyze(DecrementNode toCheck) {
|
||||||
return decrementNode.assignableExpression.accept(this);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(IfElseNode toCheck) {
|
public TypeCheckResult analyze(IfElseNode toCheck) {
|
||||||
var resultIf = toCheck.ifStatement.accept(this);
|
return new TypeCheckResult(true, null);
|
||||||
var resultElse = toCheck.elseStatement.accept(this);
|
|
||||||
|
|
||||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
||||||
|
return null;
|
||||||
if (toCheck.target.identifier != null) {
|
|
||||||
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
|
|
||||||
if (targetType == null) {
|
|
||||||
targetType = currentFields.get(toCheck.target.identifier);
|
|
||||||
}
|
|
||||||
if (targetType instanceof ReferenceType reference) {
|
|
||||||
var type = getTypeFromMethod(toCheck, reference);
|
|
||||||
if (type != null) {
|
|
||||||
return new TypeCheckResult(true, type);
|
|
||||||
} else {
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (toCheck.target.thisTar) {
|
|
||||||
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
|
|
||||||
if (type != null) {
|
|
||||||
return new TypeCheckResult(true, type);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var result = toCheck.target.accept(this);
|
|
||||||
if (result.getType() instanceof ReferenceType reference) {
|
|
||||||
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -356,9 +295,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
TypeCheckResult result = localVarDecl.expression.accept(this);
|
TypeCheckResult result = localVarDecl.expression.accept(this);
|
||||||
|
|
||||||
var resultType = localVarDecl.expression.getType();
|
var resultType = localVarDecl.expression.getType();
|
||||||
if (result.getType() != null) {
|
|
||||||
resultType = result.getType();
|
|
||||||
}
|
|
||||||
valid = result.isValid() && valid;
|
valid = result.isValid() && valid;
|
||||||
|
|
||||||
if (!Objects.equals(resultType, localVarDecl.type)) {
|
if (!Objects.equals(resultType, localVarDecl.type)) {
|
||||||
@ -380,17 +316,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(NewDeclarationNode toCheck) {
|
public TypeCheckResult analyze(NewDeclarationNode toCheck) {
|
||||||
|
|
||||||
if (context.containsClass(toCheck.identifier)) {
|
|
||||||
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(IncrementNode toCheck) {
|
public TypeCheckResult analyze(IncrementNode toCheck) {
|
||||||
return toCheck.assignableExpression.accept(this);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -401,223 +332,38 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(CalculationNode calcNode) {
|
public TypeCheckResult analyze(CalculationNode calcNode) {
|
||||||
if (calcNode.calculationExpression != null) {
|
if (calcNode.calculationExpression != null) {
|
||||||
var calcRes = calcNode.calculationExpression.accept(this);
|
calcNode.calculationExpression.accept(this);
|
||||||
if (calcNode.dotExpression != null) {
|
|
||||||
var dotRes = calcNode.dotExpression.accept(this);
|
|
||||||
|
|
||||||
switch (calcNode.operator) {
|
|
||||||
case PLUS, MINUS:
|
|
||||||
if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType &&
|
|
||||||
calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return new TypeCheckResult(calcRes.isValid(), calcRes.getType());
|
|
||||||
}
|
|
||||||
} else if (calcNode.dotExpression != null) {
|
|
||||||
var dotRes = calcNode.dotExpression.accept(this);
|
|
||||||
return new TypeCheckResult(dotRes.isValid(), dotRes.getType());
|
|
||||||
}
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(DotNode toCheck) {
|
|
||||||
if (toCheck.dotSubstractionExpression != null) {
|
|
||||||
return toCheck.dotSubstractionExpression.accept(this);
|
|
||||||
}
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(DotSubstractionNode toCheck) {
|
|
||||||
if (toCheck.value != null) {
|
|
||||||
return toCheck.value.accept(this);
|
|
||||||
} else if (toCheck.memberAccess != null) {
|
|
||||||
return toCheck.memberAccess.accept(this);
|
|
||||||
} else if (toCheck.methodCall != null) {
|
|
||||||
return toCheck.methodCall.accept(this);
|
|
||||||
} else if (toCheck.identifier != null) {
|
|
||||||
if (currentScope.contains(toCheck.identifier)) {
|
|
||||||
return new TypeCheckResult(true, currentScope.getLocalVar(toCheck.identifier));
|
|
||||||
} else if (currentFields.get(toCheck.identifier) != null) {
|
|
||||||
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
|
|
||||||
}
|
|
||||||
} else if (toCheck.calculationExpression != null) {
|
|
||||||
return toCheck.calculationExpression.accept(this);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(NonCalculationNode nonCalculationNode) {
|
public TypeCheckResult analyze(DotNode toCheck) {
|
||||||
var expResult = nonCalculationNode.expression.accept(this);
|
return null;
|
||||||
var unaryResult = nonCalculationNode.unaryExpression.accept(this);
|
}
|
||||||
|
|
||||||
switch (nonCalculationNode.operator) {
|
@Override
|
||||||
case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL:
|
public TypeCheckResult analyze(DotSubstractionNode toCheck) {
|
||||||
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
return null;
|
||||||
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
}
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
|
||||||
} else {
|
|
||||||
errors.add(new TypeMismatchException("Both types must be Integer"));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OR, AND:
|
|
||||||
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
|
||||||
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
|
||||||
} else {
|
|
||||||
errors.add(new TypeMismatchException("Both types must be Boolean"));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case EQUAL, NOT_EQUAL:
|
|
||||||
if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType
|
|
||||||
&& Objects.equals(expResultType, unaryResultType)) {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
|
||||||
} else {
|
|
||||||
errors.add(new TypeMismatchException("Both types must be the same"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
@Override
|
||||||
return new TypeCheckResult(false, null);
|
public TypeCheckResult analyze(NonCalculationNode toCheck) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(UnaryNode unary) {
|
public TypeCheckResult analyze(UnaryNode unary) {
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
if (unary.identifier != null) {
|
if (currentScope.contains(unary.identifier)) {
|
||||||
if (currentScope.contains(unary.identifier)) {
|
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
|
||||||
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
|
} else if(currentFields.get(unary.identifier) != null) {
|
||||||
} else if (currentFields.get(unary.identifier) != null) {
|
return new TypeCheckResult(valid, currentFields.get(unary.identifier));
|
||||||
return new TypeCheckResult(valid, currentFields.get(unary.identifier));
|
|
||||||
} else if (unary.statement != null) {
|
|
||||||
var result = unary.statement.accept(this);
|
|
||||||
unary.setType(result.getType());
|
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
errors.add(new NotDeclaredException("Var is not Declared"));
|
|
||||||
}
|
|
||||||
} else if (unary.statement != null) {
|
|
||||||
var result = unary.statement.accept(this);
|
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
|
||||||
} else if (unary.value != null) {
|
|
||||||
var result = unary.value.accept(this);
|
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
|
||||||
} else if (unary.memberAccess != null) {
|
|
||||||
var result = unary.memberAccess.accept(this);
|
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
|
||||||
} else if (unary.expression != null) {
|
|
||||||
var result = unary.expression.accept(this);
|
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(MemberAccessNode memberAccessNode) {
|
|
||||||
|
|
||||||
ITypeNode currentType = null;
|
|
||||||
|
|
||||||
for (String s : memberAccessNode.identifiers) {
|
|
||||||
if (currentType == null) {
|
|
||||||
if (currentScope.getLocalVar(s) != null) {
|
|
||||||
currentType = currentScope.getLocalVar(s);
|
|
||||||
|
|
||||||
} else if (currentFields.get(s) != null) {
|
|
||||||
currentType = currentFields.get(s);
|
|
||||||
} else {
|
|
||||||
errors.add(new NotDeclaredException(s + "Not Declared"));
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if (currentType instanceof ReferenceType reference) {
|
|
||||||
var currentTypeClass = context.getClass(reference.getIdentifier());
|
|
||||||
|
|
||||||
var currentField = currentTypeClass.getField(s);
|
|
||||||
if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) {
|
|
||||||
currentType = currentField.getType();
|
|
||||||
} else {
|
|
||||||
errors.add(new NotVisibleException("This field is not visible"));
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TypeCheckResult(true, currentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(TargetNode targetNode) {
|
|
||||||
|
|
||||||
if (targetNode.memberAccess != null) {
|
|
||||||
return targetNode.memberAccess.accept(this);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(ValueNode valueNode) {
|
|
||||||
|
|
||||||
switch (valueNode.valueType) {
|
|
||||||
case INT_VALUE -> {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
|
||||||
}
|
|
||||||
case CHAR_VALUE -> {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
|
|
||||||
}
|
|
||||||
case BOOLEAN_VALUE -> {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
|
||||||
}
|
|
||||||
default -> {
|
|
||||||
return new TypeCheckResult(false, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ITypeNode getTypeFromMethod(MethodCallNode toCheck, ReferenceType reference) {
|
|
||||||
var classContext = context.getClass(reference.getIdentifier());
|
|
||||||
|
|
||||||
if (classContext == null) {
|
|
||||||
errors.add(new NotDeclaredException(toCheck.target.identifier + "is not Defined"));
|
|
||||||
} else {
|
} else {
|
||||||
var methods = classContext.getMethods();
|
errors.add(new NotDeclaredException("Var is not Declared"));
|
||||||
for (var method : methods) {
|
|
||||||
if (toCheck.identifier.equals(method.getIdentifier())) {
|
|
||||||
if (method.getParameters().size() == toCheck.parameters.size() && !(method instanceof ConstructorNode)) {
|
|
||||||
boolean same = true;
|
|
||||||
for (int i = 0; i < method.getParameters().size(); i++) {
|
|
||||||
var result1 = method.getParameters().get(i).accept(this);
|
|
||||||
var result2 = toCheck.parameters.get(i).accept(this);
|
|
||||||
if (!Objects.equals(result1.getType(), result2.getType())) {
|
|
||||||
same = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (same) {
|
|
||||||
if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) {
|
|
||||||
if (method.getType() == null) {
|
|
||||||
return new BaseType(TypeEnum.VOID);
|
|
||||||
}
|
|
||||||
return method.getType();
|
|
||||||
} else {
|
|
||||||
errors.add(new NotVisibleException("This Method is not Visible"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
errors.add(new WrongOverloading("No Method found with this parameters"));
|
|
||||||
}
|
}
|
||||||
return null;
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,7 +2,6 @@ package semantic;
|
|||||||
|
|
||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.expressions.binaryexpressions.*;
|
import ast.expressions.binaryexpressions.*;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
import ast.members.*;
|
import ast.members.*;
|
||||||
import ast.parameters.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
@ -12,9 +11,7 @@ import ast.statementexpressions.NewDeclarationNode;
|
|||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import ast.type.ValueNode;
|
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public interface SemanticVisitor {
|
public interface SemanticVisitor {
|
||||||
@ -41,6 +38,8 @@ public interface SemanticVisitor {
|
|||||||
|
|
||||||
TypeCheckResult analyze(ElseNode toCheck);
|
TypeCheckResult analyze(ElseNode toCheck);
|
||||||
|
|
||||||
|
//TypeCheckResult analyze(ForNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(AssignNode toCheck);
|
TypeCheckResult analyze(AssignNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(DecrementNode toCheck);
|
TypeCheckResult analyze(DecrementNode toCheck);
|
||||||
@ -67,10 +66,4 @@ public interface SemanticVisitor {
|
|||||||
|
|
||||||
TypeCheckResult analyze(UnaryNode toCheck);
|
TypeCheckResult analyze(UnaryNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(MemberAccessNode toCheck);
|
|
||||||
|
|
||||||
TypeCheckResult analyze(TargetNode toCheck);
|
|
||||||
|
|
||||||
TypeCheckResult analyze(ValueNode toCheck);
|
|
||||||
|
|
||||||
}
|
}
|
@ -2,15 +2,11 @@ package semantic.context;
|
|||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.members.FieldNode;
|
import ast.members.FieldNode;
|
||||||
import ast.members.MethodNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class ClassContext {
|
public class ClassContext {
|
||||||
|
|
||||||
private HashMap<String, FieldContext> fields;
|
private HashMap<String, FieldContext> fields;
|
||||||
private ArrayList<MethodNode> methods = new ArrayList<>();
|
|
||||||
|
|
||||||
public ClassContext(ClassNode classNode) {
|
public ClassContext(ClassNode classNode) {
|
||||||
|
|
||||||
@ -19,8 +15,6 @@ public class ClassContext {
|
|||||||
classNode.members.forEach(member -> {
|
classNode.members.forEach(member -> {
|
||||||
if(member instanceof FieldNode fieldNode) {
|
if(member instanceof FieldNode fieldNode) {
|
||||||
fields.put(fieldNode.identifier, new FieldContext(fieldNode));
|
fields.put(fieldNode.identifier, new FieldContext(fieldNode));
|
||||||
}else if(member instanceof MethodNode methodNode) {
|
|
||||||
methods.add(methodNode);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -30,8 +24,4 @@ public class ClassContext {
|
|||||||
return fields.get(name);
|
return fields.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<MethodNode> getMethods() {
|
|
||||||
return methods;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,4 @@ public class Context {
|
|||||||
return classes.get(identifier);
|
return classes.get(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsClass(String identifier) {
|
|
||||||
return classes.containsKey(identifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,4 @@ public class FieldContext {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AccessModifierNode getAccessModifier() {
|
|
||||||
return accessModifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package semantic.exceptions;
|
|
||||||
|
|
||||||
public class NotVisibleException extends RuntimeException {
|
|
||||||
|
|
||||||
public NotVisibleException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package semantic.exceptions;
|
|
||||||
|
|
||||||
public class WrongOverloading extends RuntimeException {
|
|
||||||
|
|
||||||
public WrongOverloading(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,405 +1,57 @@
|
|||||||
package parser;
|
package parser;
|
||||||
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.expressions.IExpressionNode;
|
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
|
||||||
import ast.members.ConstructorNode;
|
|
||||||
import ast.members.FieldNode;
|
|
||||||
import ast.members.MemberNode;
|
|
||||||
import ast.members.MethodNode;
|
|
||||||
import ast.parameters.ParameterNode;
|
|
||||||
import ast.statementexpressions.AssignNode;
|
|
||||||
import ast.statementexpressions.AssignableNode;
|
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
|
||||||
import ast.statements.BlockNode;
|
|
||||||
import ast.statements.IStatementNode;
|
|
||||||
import ast.statements.ReturnNode;
|
|
||||||
import ast.type.AccessModifierNode;
|
|
||||||
import ast.type.EnumValueNode;
|
|
||||||
import ast.type.ValueNode;
|
|
||||||
import ast.type.type.BaseType;
|
|
||||||
import ast.type.type.ITypeNode;
|
|
||||||
import ast.type.type.TypeEnum;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import parser.astBuilder.ASTBuilder;
|
import parser.astBuilder.ASTBuilder;
|
||||||
import parser.generated.SimpleJavaLexer;
|
import parser.generated.SimpleJavaLexer;
|
||||||
import parser.generated.SimpleJavaParser;
|
import parser.generated.SimpleJavaParser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import java.lang.reflect.Member;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
public class AstBuilderTest {
|
||||||
|
|
||||||
@DisplayName("Untyped Abstract Syntax Tree")
|
|
||||||
class AstBuilderTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Empty Class Test")
|
public void astBuilderTest() {
|
||||||
public void emptyClassTest(){
|
// ---------------- Leere Klasse nachgebaut ----------------
|
||||||
ClassNode emptyClass = Helper.generateEmptyClass("TestClass");
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(emptyClass);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClass.java");
|
ProgramNode expectedASTEmptyClass = new ProgramNode();
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
// public class Name {}
|
||||||
@DisplayName("Multiple Empty Classes Test")
|
ClassNode nameClass = new ClassNode("public", "Name");
|
||||||
public void multipleEmptyClassesTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass1");
|
|
||||||
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
expected.addClass(class2);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java");
|
expectedASTEmptyClass.addClass(nameClass);
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Empty Class Test with Constructor")
|
|
||||||
public void emptyClassWithConstructorTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java");
|
// ---------------- Leere Klasse erzeugt ----------------
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
// init
|
||||||
@DisplayName("Field Test")
|
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
||||||
public void fieldTest() {
|
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
class1.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
|
tokenStream.fill();
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
/* Parser -> Parsetree */
|
||||||
expected.addClass(class1);
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
|
ParseTree parseTreeEmptyClass = parser.program(); // parse the input
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java");
|
/* AST builder -> AST */
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
}
|
ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass);
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Field Test with Accessmodifier")
|
|
||||||
public void fieldTestWithModifier() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
// ---------------- Vergleichen ----------------
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java");
|
String expectedASTasString = expectedASTEmptyClass.toString();
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString();
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
// Wie vergleiche ich das?
|
||||||
@DisplayName("Commments Ignore Test")
|
assertEquals(expectedASTasString, actualASTasString);
|
||||||
public void commmentsIgnoreTest(){
|
assertEquals(expectedASTEmptyClass, actualASTEmptyClass);
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Comments.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor Paramerter Test")
|
|
||||||
public void constructorParameterTest(){
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorParameter.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("This Dot Test")
|
|
||||||
public void thisDotTest(){
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
ValueNode value = new ValueNode(EnumValueNode.INT_VALUE, "1");
|
|
||||||
IExpressionNode expression = new UnaryNode(value);
|
|
||||||
|
|
||||||
block.addStatement(new AssignNode(assignable, expression));
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ThisDot.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor This Dot Test")
|
|
||||||
public void constructorThisDotTest(){
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
IExpressionNode expression = new UnaryNode("a");
|
|
||||||
|
|
||||||
block.addStatement(new AssignNode(assignable, expression));
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorThisDot.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Void Methoden Test")
|
|
||||||
public void voidMethodenTest(){
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
class1.addMember(new MethodNode("public", null, true, "test", block));
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/VoidMethod.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor Method call Test")
|
|
||||||
public void constructorMethodCallTest(){
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
IExpressionNode expression = new UnaryNode(new MethodCallNode(null, "testMethod"));
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
|
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
class1.addMember(method);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCall.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor Method call Parameters Test")
|
|
||||||
public void constructorMethodCallParametersTest(){
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
|
|
||||||
methodCall.addExpression(new UnaryNode("a"));
|
|
||||||
IExpressionNode expression = new UnaryNode(methodCall);
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
|
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
|
||||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
class1.addMember(method);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCallParameters.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Char Test")
|
|
||||||
public void charTest(){
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
|
|
||||||
methodCall.addExpression(new UnaryNode("a"));
|
|
||||||
IExpressionNode expression = new UnaryNode(methodCall);
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
|
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
|
|
||||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
class1.addMember(method);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Char.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Null Test")
|
|
||||||
public void nullTest(){
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Null.java");
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Self Reference Test")
|
|
||||||
public void selfReferneceTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Variable Compare Test")
|
|
||||||
public void variableCompareTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Variable Calculation Test")
|
|
||||||
public void variableCalculationTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Main Method Test")
|
|
||||||
public void mainMethodTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("While Test")
|
|
||||||
public void whileTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Do While Test")
|
|
||||||
public void doWhileTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("For Test")
|
|
||||||
public void forTest(){
|
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Noch nicht speziell Increment nur zum Development Testen per Debug
|
|
||||||
@Test
|
|
||||||
@DisplayName("Increment Test")
|
|
||||||
public void incrementTest(){
|
|
||||||
ClassNode classNode = Helper.generateEmptyClass("TestClass");
|
|
||||||
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
|
||||||
expected.addClass(classNode);
|
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Increment.java");
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -413,4 +65,39 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ---------------- Alter CompilerInput nachgebaut ----------------
|
||||||
|
// ProgramNode startNode = new ProgramNode();
|
||||||
|
// public class CompilerInput {}
|
||||||
|
// ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
||||||
|
// public int a;
|
||||||
|
// compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
||||||
|
// public static int testMethod(char x) { return 0; }
|
||||||
|
/* compilerInputClass.addMember(
|
||||||
|
new MethodNode(
|
||||||
|
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||||
|
new BaseTypeNode(EnumTypeNode.INT),
|
||||||
|
"testMethod",
|
||||||
|
new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"))),
|
||||||
|
List.of(new ReturnStatementNode(new LiteralNode(0)))
|
||||||
|
));
|
||||||
|
|
||||||
|
ClassNode testClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "Test");
|
||||||
|
testClass.addMember(
|
||||||
|
new MethodNode(
|
||||||
|
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||||
|
new BaseTypeNode(EnumTypeNode.INT),
|
||||||
|
"testMethod",
|
||||||
|
new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"), new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "a"))),
|
||||||
|
List.of(new ReturnStatementNode(new LiteralNode(0)))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//compilerInputClass.addClass(testClass);
|
||||||
|
|
||||||
|
// startNode.addClass(compilerInputClass);
|
||||||
|
// startNode.addClass(testClass);
|
||||||
|
}
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
package parser;
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.ClassNode;
|
|
||||||
import ast.ProgramNode;
|
|
||||||
import ast.members.ConstructorNode;
|
|
||||||
import ast.members.MemberNode;
|
|
||||||
import ast.statements.BlockNode;
|
|
||||||
import ast.statements.ReturnNode;
|
|
||||||
import ast.type.AccessModifierNode;
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
|
||||||
import parser.astBuilder.ASTBuilder;
|
|
||||||
import parser.generated.SimpleJavaLexer;
|
|
||||||
import parser.generated.SimpleJavaParser;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Helper {
|
|
||||||
public static ASTNode generateAST(String filePath) {
|
|
||||||
CharStream testFile = null;
|
|
||||||
try {
|
|
||||||
testFile = CharStreams.fromFileName(filePath);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(testFile);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
||||||
tokenStream.fill();
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
|
||||||
ParseTree parseTree = parser.program();
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
|
||||||
|
|
||||||
return astBuilder.visit(parseTree);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ClassNode generateEmptyClass(String className) {
|
|
||||||
BlockNode blockNode = new BlockNode();
|
|
||||||
blockNode.addStatement(new ReturnNode(null));
|
|
||||||
MemberNode constructor = new ConstructorNode("public",className, blockNode);
|
|
||||||
ClassNode classNode = new ClassNode("public", className);
|
|
||||||
classNode.addMember(constructor);
|
|
||||||
return classNode;
|
|
||||||
}
|
|
||||||
}
|
|
@ -26,37 +26,11 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class EndToTypedAstTest {
|
public class EndToTypedAstTest {
|
||||||
private static final Map<String, Class<?>> exceptionMap = new HashMap<>();
|
private static final Map<String, Class<?>> exceptionMap = new HashMap<>();
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOnlyOneFile() {
|
|
||||||
SemanticAnalyzer.clearAnalyzer();
|
|
||||||
|
|
||||||
CharStream codeCharStream = null;
|
|
||||||
try {
|
|
||||||
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/typedAstFeaturesTests/CorrectTest.java"));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
||||||
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
|
||||||
ParseTree parseTree = parser.program(); // parse the input
|
|
||||||
|
|
||||||
/* ------------------------- AST builder -> AST ------------------------- */
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
|
||||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
|
||||||
|
|
||||||
var result = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exceptionsTest() {
|
public void exceptionsTest() {
|
||||||
String directoryPath = "src/test/resources/input/typedAstExceptionsTests";
|
String directoryPath = "src/test/resources/input/typedAstExceptionsTests";
|
||||||
@ -114,7 +88,7 @@ public class EndToTypedAstTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void featureTest() {
|
public void featureTest(){
|
||||||
String directoryPath = "src/test/resources/input/typedAstFeaturesTests";
|
String directoryPath = "src/test/resources/input/typedAstFeaturesTests";
|
||||||
File folder = new File(directoryPath);
|
File folder = new File(directoryPath);
|
||||||
if (folder.isDirectory()) {
|
if (folder.isDirectory()) {
|
||||||
@ -141,9 +115,6 @@ public class EndToTypedAstTest {
|
|||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
|
|
||||||
System.out.println("Testing the file: " + file.getName());
|
System.out.println("Testing the file: " + file.getName());
|
||||||
for(Exception runtimeException : SemanticAnalyzer.errors){
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||||
assertNotNull(typedAst);
|
assertNotNull(typedAst);
|
||||||
}
|
}
|
||||||
@ -156,8 +127,9 @@ public class EndToTypedAstTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------ Helpers ------------------
|
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------ Helpers ------------------
|
||||||
/**
|
/**
|
||||||
* This method is used to extract the expected exception from a given file.
|
* This method is used to extract the expected exception from a given file.
|
||||||
* It reads the file line by line and uses a regular expression to match the expected exception annotation.
|
* It reads the file line by line and uses a regular expression to match the expected exception annotation.
|
||||||
@ -186,25 +158,25 @@ public class EndToTypedAstTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is used to retrieve the Class object associated with a given exception name.
|
* This method is used to retrieve the Class object associated with a given exception name.
|
||||||
* It first prints the original exception name, then appends the package name to the exception name and prints it.
|
* It first prints the original exception name, then appends the package name to the exception name and prints it.
|
||||||
* It then retrieves the Class object from the exceptionMap using the fully qualified exception name.
|
* It then retrieves the Class object from the exceptionMap using the fully qualified exception name.
|
||||||
* If the Class object is not found in the exceptionMap, it throws a RuntimeException.
|
* If the Class object is not found in the exceptionMap, it throws a RuntimeException.
|
||||||
*
|
*
|
||||||
* @param exceptionName The name of the exception for which the Class object is to be retrieved.
|
* @param exceptionName The name of the exception for which the Class object is to be retrieved.
|
||||||
* @return The Class object associated with the given exception name.
|
* @return The Class object associated with the given exception name.
|
||||||
* @throws RuntimeException If the Class object for the given exception name is not found in the exceptionMap.
|
* @throws RuntimeException If the Class object for the given exception name is not found in the exceptionMap.
|
||||||
*/
|
*/
|
||||||
private Class<?> getExceptionClass(String exceptionName) {
|
private Class<?> getExceptionClass(String exceptionName) {
|
||||||
System.out.println(exceptionName);
|
System.out.println(exceptionName);
|
||||||
exceptionName = "semantic.exceptions." + exceptionName;
|
exceptionName = "semantic.exceptions." + exceptionName;
|
||||||
System.out.println(exceptionName);
|
System.out.println(exceptionName);
|
||||||
Class<?> exceptionClass = exceptionMap.get(exceptionName);
|
Class<?> exceptionClass = exceptionMap.get(exceptionName);
|
||||||
if (exceptionClass == null) {
|
if (exceptionClass == null) {
|
||||||
throw new RuntimeException("Exception class not found: " + exceptionName);
|
throw new RuntimeException("Exception class not found: " + exceptionName);
|
||||||
}
|
|
||||||
return exceptionClass;
|
|
||||||
}
|
}
|
||||||
|
return exceptionClass;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is used to load custom exceptions from a specified package.
|
* This method is used to load custom exceptions from a specified package.
|
||||||
|
102
src/test/java/semantic/Mocker.java
Normal file
102
src/test/java/semantic/Mocker.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package semantic;
|
||||||
|
|
||||||
|
import ast.*;
|
||||||
|
import ast.members.FieldNode;
|
||||||
|
import ast.members.MethodNode;
|
||||||
|
import ast.parameters.ParameterNode;
|
||||||
|
import ast.type.AccessModifierNode;
|
||||||
|
import ast.type.type.*;
|
||||||
|
|
||||||
|
public class Mocker {
|
||||||
|
|
||||||
|
public static ASTNode mockTwoSameFields(){
|
||||||
|
ProgramNode p = new ProgramNode();
|
||||||
|
|
||||||
|
ClassNode c = new ClassNode();
|
||||||
|
c.identifier = "testClass";
|
||||||
|
|
||||||
|
FieldNode f1 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
|
||||||
|
|
||||||
|
c.members.add(f1);
|
||||||
|
|
||||||
|
FieldNode f2 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
|
||||||
|
|
||||||
|
c.members.add(f2);
|
||||||
|
|
||||||
|
p.classes.add(c);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ASTNode mockSimpleMethod(){
|
||||||
|
ProgramNode p = new ProgramNode();
|
||||||
|
|
||||||
|
ClassNode c = new ClassNode();
|
||||||
|
|
||||||
|
MethodNode methodNode = new MethodNode();
|
||||||
|
|
||||||
|
//Parameter
|
||||||
|
ParameterNode parameterNode = new ParameterNode(new BaseType(TypeEnum.INT), "a");
|
||||||
|
|
||||||
|
methodNode.addParameter(parameterNode);
|
||||||
|
|
||||||
|
//Statements
|
||||||
|
|
||||||
|
//Block
|
||||||
|
methodNode.block = new ast.statements.BlockNode();
|
||||||
|
|
||||||
|
c.members.add(methodNode);
|
||||||
|
|
||||||
|
p.classes.add(c);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ASTNode mockTwoSameMethods(){
|
||||||
|
ProgramNode p = new ProgramNode();
|
||||||
|
|
||||||
|
ClassNode c = new ClassNode();
|
||||||
|
|
||||||
|
MethodNode methodNode = new MethodNode();
|
||||||
|
methodNode.block = new ast.statements.BlockNode();
|
||||||
|
methodNode.setType(new BaseType(TypeEnum.INT));
|
||||||
|
|
||||||
|
methodNode.setIdentifier("testMethod");
|
||||||
|
|
||||||
|
c.members.add(methodNode);
|
||||||
|
|
||||||
|
MethodNode methodNode1 = new MethodNode();
|
||||||
|
methodNode1.block = new ast.statements.BlockNode();
|
||||||
|
methodNode1.setType(new BaseType(TypeEnum.INT));
|
||||||
|
|
||||||
|
methodNode1.setIdentifier("testMethod");
|
||||||
|
|
||||||
|
c.members.add(methodNode1);
|
||||||
|
|
||||||
|
p.classes.add(c);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ASTNode mockTwoDifferentMethods(){
|
||||||
|
ProgramNode p = new ProgramNode();
|
||||||
|
|
||||||
|
ClassNode c = new ClassNode();
|
||||||
|
|
||||||
|
MethodNode methodNode = new MethodNode();
|
||||||
|
methodNode.block = new ast.statements.BlockNode();
|
||||||
|
methodNode.setIdentifier("testMethod");
|
||||||
|
|
||||||
|
c.members.add(methodNode);
|
||||||
|
|
||||||
|
MethodNode methodNode1 = new MethodNode();
|
||||||
|
methodNode1.block = new ast.statements.BlockNode();
|
||||||
|
methodNode1.setIdentifier("testMethod1");
|
||||||
|
|
||||||
|
c.members.add(methodNode1);
|
||||||
|
|
||||||
|
p.classes.add(c);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,113 +0,0 @@
|
|||||||
package semantic;
|
|
||||||
|
|
||||||
public class SemanticTest {
|
|
||||||
|
|
||||||
|
|
||||||
public void test(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test(int a, boolean b){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test(boolean b, int a){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// public void alreadyDeclaredLocalFieldVar() {
|
|
||||||
// ProgramNode programNode = new ProgramNode();
|
|
||||||
// List<ClassNode> classList = new ArrayList<>();
|
|
||||||
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
|
||||||
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
|
||||||
//
|
|
||||||
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
|
||||||
// ASTNode tast = semanticAnalyzer.generateTast(ast);
|
|
||||||
//
|
|
||||||
// MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
|
|
||||||
// classNode.members.add(memberNode2);
|
|
||||||
//
|
|
||||||
// classList.add(classNode);
|
|
||||||
// programNode.classes = classList;
|
|
||||||
//
|
|
||||||
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
|
||||||
//
|
|
||||||
// assertEquals(1, SemanticAnalyzer.errors.size());
|
|
||||||
// assertInstanceOf(AlreadyDeclaredException.class, SemanticAnalyzer.errors.getFirst());
|
|
||||||
// assertNull(typedAst);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Test
|
|
||||||
// public void shouldWorkWithNoError() {
|
|
||||||
// ProgramNode programNode = new ProgramNode();
|
|
||||||
// List<ClassNode> classList = new ArrayList<>();
|
|
||||||
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
|
||||||
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
|
||||||
//
|
|
||||||
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
|
||||||
// ASTNode tast = semanticAnalyzer.generateTast(ast);
|
|
||||||
//
|
|
||||||
// assertEquals(semanticAnalyzer.errors.size(), 0);
|
|
||||||
// assertNotNull(tast);
|
|
||||||
//
|
|
||||||
// MemberNode memberNode3 = getMemberNode(accessTypeNode);
|
|
||||||
// classNode.members.add(memberNode3);
|
|
||||||
//
|
|
||||||
// classList.add(classNode);
|
|
||||||
// programNode.classes = classList;
|
|
||||||
//
|
|
||||||
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
|
||||||
//
|
|
||||||
// assertEquals(0, SemanticAnalyzer.errors.size());
|
|
||||||
// assertEquals(programNode, typedAst);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * This method is used to create a MemberNode representing a method.
|
|
||||||
// * It first creates a list of ParameterNodes and adds a ParameterNode to it.
|
|
||||||
// * Then, it creates a ParameterListNode using the list of ParameterNodes.
|
|
||||||
// * After that, it creates a list of StatementNodes and adds a StatementNode to it by calling the getStatementNode method.
|
|
||||||
// * Finally, it creates a MethodNode using the provided AccessTypeNode, a BaseTypeNode representing the return type of the method,
|
|
||||||
// * the method name, the ParameterListNode, and the list of StatementNodes, and returns this MethodNode.
|
|
||||||
// *
|
|
||||||
// * @param accessTypeNode The AccessTypeNode representing the access type of the method.
|
|
||||||
// * @return The created MemberNode representing the method.
|
|
||||||
// */
|
|
||||||
//private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
|
|
||||||
// List<ParameterNode> parameterNodeList = new ArrayList<>();
|
|
||||||
// ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
|
|
||||||
// parameterNodeList.add(parameterNode1);
|
|
||||||
// ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
|
|
||||||
//
|
|
||||||
// List<StatementNode> statementNodeList = new ArrayList<>();
|
|
||||||
//
|
|
||||||
// StatementNode statementNode1 = getStatementNode();
|
|
||||||
// statementNodeList.add(statementNode1);
|
|
||||||
//
|
|
||||||
// return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * This method is used to create a StatementNode for an assignment operation.
|
|
||||||
// * It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
|
|
||||||
// * Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
|
|
||||||
// * After that, it creates a LiteralNode to represent the integer value 1.
|
|
||||||
// * Finally, it creates another BinaryExpressionNode to represent the assignment operation 'this.objectVar = 1',
|
|
||||||
// * and wraps this expression in an AssignmentStatementNode.
|
|
||||||
// *
|
|
||||||
// * @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
|
|
||||||
// */
|
|
||||||
//private static StatementNode getStatementNode() {
|
|
||||||
// ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
|
|
||||||
// ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
|
|
||||||
//
|
|
||||||
// ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
|
|
||||||
//
|
|
||||||
// ExpressionNode expressionNodeRight = new LiteralNode(1);
|
|
||||||
//
|
|
||||||
// BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
|
|
||||||
//
|
|
||||||
// return new AssignmentStatementNode(expressionNode);
|
|
||||||
//}
|
|
||||||
}
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,12 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
char a;
|
|
||||||
|
|
||||||
public TestClass(char a){
|
|
||||||
this.a = testMethod(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
char testMethod(char a){
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
Mutliple Line Comment. Ignore
|
|
||||||
|
|
||||||
*/
|
|
||||||
class TestClass{
|
|
||||||
private int a; // Ignore
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
class TestClass {
|
|
||||||
|
|
||||||
int a;
|
|
||||||
|
|
||||||
public TestClass(){
|
|
||||||
this.a = testMethod();
|
|
||||||
}
|
|
||||||
|
|
||||||
int testMethod(){
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
class TestClass {
|
|
||||||
|
|
||||||
int a;
|
|
||||||
|
|
||||||
public TestClass(int a){
|
|
||||||
this.a = testMethod(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
int testMethod(int a){
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
class TestClass {
|
|
||||||
public TestClass(int a, int b){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
private int a;
|
|
||||||
|
|
||||||
public TestClass(int a){
|
|
||||||
this.a = a;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
public TestClass(){
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
do{
|
|
||||||
i++
|
|
||||||
}while(i < 10);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
class TestClass {}
|
|
@ -1,5 +0,0 @@
|
|||||||
public class TestClass {
|
|
||||||
public TestClass() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
public class TestClass {
|
|
||||||
int a;
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
public class TestClass {
|
|
||||||
public int a;
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
public TestClass(){
|
|
||||||
for(int i = 0; i < 10; i++){
|
|
||||||
int a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
public class Increment {
|
|
||||||
|
|
||||||
public int test;
|
|
||||||
|
|
||||||
public void increment(int p) {
|
|
||||||
test = p++;
|
|
||||||
|
|
||||||
for(int i = 1; i<=10, i++) {
|
|
||||||
int a = 5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
class TestClass1 {}
|
|
||||||
|
|
||||||
class TestClass2{}
|
|
@ -1,8 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
int a;
|
|
||||||
|
|
||||||
public TestClass(){
|
|
||||||
this.a = null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
TestClass testClass;
|
|
||||||
|
|
||||||
int testMethod1() {
|
|
||||||
return this.testMethod2()
|
|
||||||
}
|
|
||||||
|
|
||||||
int testMethod2() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int testMehtod3(){
|
|
||||||
TestClass testClass1 = new TestClass();
|
|
||||||
return testClass1.testClass.testMethod1();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
public int a;
|
|
||||||
|
|
||||||
public TestClass() {
|
|
||||||
this.a = 1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
void test(){}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
public TestClass(){
|
|
||||||
int i = 10;
|
|
||||||
|
|
||||||
while ( i > 0){
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
int aPlusB(int a, int b){
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int aMinusB(int a, int b){
|
|
||||||
return a - b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int aTimeB(int a, int b){
|
|
||||||
return a * b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int aDivB(int a, int b){
|
|
||||||
return a / b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int colmplexCalc (int a, int b){
|
|
||||||
return a * (b / 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean aSmallerB (int a, int b){
|
|
||||||
return a < b;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean aGreaterB (int a, int b){
|
|
||||||
return a > b;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean aEqualsB (int a, int b){
|
|
||||||
return a == b;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
boolean true(){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean false(){
|
|
||||||
return false();
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean trueAndTrue(){
|
|
||||||
return true && true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean trueAndFalse(){
|
|
||||||
return true && true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean falseAndFalse(){
|
|
||||||
return false && false;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean trueOrFalse(){
|
|
||||||
return true || false;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean falseOrFalse(){
|
|
||||||
return false || false;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
// @expected: TypeMismatchException
|
|
||||||
public class AllFeaturesClassExample {
|
|
||||||
|
|
||||||
public void controlStructures(int a, boolean bool) {
|
|
||||||
while (a > bool) {
|
|
||||||
a--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
// @expected: TypeMismatchException
|
|
||||||
public class Test{
|
|
||||||
|
|
||||||
public void test(boolean b){
|
|
||||||
if(b == 2){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
// @expected: NotVisibleException
|
|
||||||
public class Test{
|
|
||||||
|
|
||||||
public Car c;
|
|
||||||
|
|
||||||
public int test(){
|
|
||||||
return c.speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public int getSpeed(){
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
// @expected: TypeMismatchException
|
|
||||||
public class Test{
|
|
||||||
|
|
||||||
public void test(int x){
|
|
||||||
|
|
||||||
if(x){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
// @expected: NotVisibleException
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
public int firstInt;
|
|
||||||
public Car ca;
|
|
||||||
|
|
||||||
public int speed(){
|
|
||||||
return ca.getSpeed();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
private int getSpeed(){
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
// @expected: WrongOverloading
|
|
||||||
public class Test{
|
|
||||||
|
|
||||||
public void test(int x){
|
|
||||||
|
|
||||||
if(this.get()){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public boolean b;
|
|
||||||
|
|
||||||
public boolean get(int c){
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
// @expected: TypeMismatchException
|
// @expected: MultipleReturnTypes
|
||||||
public class Example {
|
public class Example {
|
||||||
|
|
||||||
public static int testMethod(int x, char c){
|
public static int testMethod(int x, char c){
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
// @expected: TypeMismatchException
|
|
||||||
public class Test{
|
|
||||||
|
|
||||||
public int i;
|
|
||||||
public boolean b;
|
|
||||||
|
|
||||||
public int test(){
|
|
||||||
|
|
||||||
return this.test(i);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test(int a){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public int test(boolean bool){
|
|
||||||
int ret = 1;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,8 @@
|
|||||||
|
// @expected: TypeMismatchException
|
||||||
|
public class Example {
|
||||||
|
|
||||||
|
public static void testMethod(int x){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,20 +0,0 @@
|
|||||||
public class Test {
|
|
||||||
|
|
||||||
public int firstInt;
|
|
||||||
public Car ca;
|
|
||||||
|
|
||||||
public int speed(){
|
|
||||||
return ca.getSpeed();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public int getSpeed(){
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
public class Test{
|
|
||||||
|
|
||||||
public Car c;
|
|
||||||
|
|
||||||
public int test(){
|
|
||||||
return c.getSpeed();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public int getSpeed(){
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
public class Test{
|
|
||||||
|
|
||||||
public void test(int x){
|
|
||||||
|
|
||||||
if(this.get(x)){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public boolean b;
|
|
||||||
|
|
||||||
public boolean get(int c){
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
public class Test{
|
|
||||||
|
|
||||||
public void test(boolean b){
|
|
||||||
if(b == true){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,38 +1,18 @@
|
|||||||
public class AllFeaturesClassExample {
|
public class Example {
|
||||||
int a;
|
|
||||||
boolean b;
|
public int a;
|
||||||
char c;
|
|
||||||
|
public static int testMethod(int b, boolean bo){
|
||||||
|
a = b;
|
||||||
|
if(bo){
|
||||||
|
|
||||||
public void controlStructures(int adf, boolean bool) {
|
|
||||||
if (a > (10 + 8)) {
|
|
||||||
} else {
|
|
||||||
}
|
}
|
||||||
|
return a;
|
||||||
|
|
||||||
while (a > adf) {
|
|
||||||
a--;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// void logicalOperations() {
|
public static void testMethod(int b){
|
||||||
// Logische UND-Operation
|
|
||||||
// if (b && a > 5) {
|
|
||||||
// System.out.println("a ist größer als 5 und b ist wahr");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Logische ODER-Operation
|
}
|
||||||
// if (b || a < 5) {
|
|
||||||
// System.out.println("b ist wahr oder a ist kleiner als 5");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
}
|
||||||
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
|
|
||||||
// obj.controlStructures();
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
|
|
||||||
public class AllFeaturesClassExample {
|
|
||||||
int a;
|
|
||||||
boolean b;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
public void controlStructures(int adf, boolean bool) {
|
|
||||||
if (a > (10 + 8)) {
|
|
||||||
} else {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
while (a > adf) {
|
|
||||||
a--;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// void logicalOperations() {
|
|
||||||
// // Logische UND-Operation
|
|
||||||
// if (b && a > 5) {
|
|
||||||
//// System.out.println("a ist größer als 5 und b ist wahr");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Logische ODER-Operation
|
|
||||||
// if (b || a < 5) {
|
|
||||||
//// System.out.println("b ist wahr oder a ist kleiner als 5");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
|
||||||
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
|
|
||||||
// obj.controlStructures();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
public Car c;
|
|
||||||
|
|
||||||
public int test(boolean b, int x) {
|
|
||||||
if (b == true) {
|
|
||||||
return c.getSpeed();
|
|
||||||
} else {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car {
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public int getSpeed() {
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
|||||||
public class Car{
|
|
||||||
|
|
||||||
public void test(boolean boo){
|
|
||||||
|
|
||||||
if(boo){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
public class Car{
|
|
||||||
|
|
||||||
public int getSpeed(boolean bool, int a, int b){
|
|
||||||
|
|
||||||
if(bool){
|
|
||||||
return a;
|
|
||||||
} else {
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
public class Test{
|
|
||||||
|
|
||||||
public int i;
|
|
||||||
public boolean b;
|
|
||||||
|
|
||||||
public int test(){
|
|
||||||
|
|
||||||
return this.test(b);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test(int a){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public int test(boolean bool){
|
|
||||||
int ret = 1;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
public class Car{
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public int getSpeed(){
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int test(){
|
|
||||||
return this.getSpeed();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
public class Car{
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public void getSpeed(boolean boo){
|
|
||||||
if(boo){
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user