Merge branch 'johns-branch' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
commit
449b895d20
@ -10,14 +10,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ConstructorNode extends MethodNode implements Visitable {
|
public class ConstructorNode extends MethodNode {
|
||||||
public AccessModifierNode accessType;
|
|
||||||
public String identifier;
|
|
||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
|
||||||
public BlockNode block;
|
|
||||||
|
|
||||||
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accesModifier = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class MethodNode implements MemberNode, Visitable {
|
|||||||
public AccessModifierNode accesModifier;
|
public AccessModifierNode accesModifier;
|
||||||
private ITypeNode type;
|
private ITypeNode type;
|
||||||
public Boolean voidType;
|
public Boolean voidType;
|
||||||
private String identifier;
|
protected String identifier;
|
||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
public List<ParameterNode> parameters = new ArrayList<>();
|
||||||
public BlockNode block;
|
public BlockNode block;
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visit(ConstructorNode constructorNode) {
|
public void visit(ConstructorNode constructorNode) {
|
||||||
methodVisitor =
|
methodVisitor =
|
||||||
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType),
|
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accesModifier),
|
||||||
"<init>",
|
"<init>",
|
||||||
mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters),
|
mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters),
|
||||||
null,
|
null,
|
||||||
|
@ -16,7 +16,7 @@ public class Scope {
|
|||||||
|
|
||||||
public void addLocalVar(String name, ITypeNode type) {
|
public void addLocalVar(String name, ITypeNode type) {
|
||||||
if (this.contains(name)) {
|
if (this.contains(name)) {
|
||||||
throw new AlreadyDeclaredException("Variable " + name + " already exists in this scope");
|
SemanticAnalyzer.errors.add(new AlreadyDeclaredException("Duplicate local variable " + name));
|
||||||
}
|
}
|
||||||
localVars.peek().put(name, type);
|
localVars.peek().put(name, type);
|
||||||
}
|
}
|
||||||
|
@ -112,53 +112,49 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(MethodNode methodNode) {
|
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||||
if (methodNode instanceof ConstructorNode) {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID));
|
|
||||||
} else {
|
|
||||||
|
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
for (var otherMethod : currentClass.getMethods()) {
|
for (var otherMethod : currentClass.getMethods()) {
|
||||||
if (Objects.equals(otherMethod, methodNode))
|
if (Objects.equals(otherMethod, methodNode))
|
||||||
break;
|
break;
|
||||||
if (otherMethod.isSame(methodNode)) {
|
if (otherMethod.isSame(methodNode)) {
|
||||||
errors.add(new AlreadyDeclaredException(
|
errors.add(new AlreadyDeclaredException(
|
||||||
"Method " + methodNode.getIdentifier() + " is already defined in class "
|
"Method " + methodNode.getIdentifier() + " is already defined in class "
|
||||||
+ currentClass.identifier));
|
+ currentClass.identifier));
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
currentScope.pushScope();
|
currentScope.pushScope();
|
||||||
for (var parameter : methodNode.getParameters()) {
|
for (var parameter : methodNode.getParameters()) {
|
||||||
var result = parameter.accept(this);
|
var result = parameter.accept(this);
|
||||||
valid = valid && result.isValid();
|
|
||||||
try {
|
|
||||||
currentScope.addLocalVar(parameter.identifier, parameter.type);
|
|
||||||
} catch (AlreadyDeclaredException e) {
|
|
||||||
errors.add(new AlreadyDeclaredException(parameter.identifier));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
currentMethodReturnType = methodNode.getType();
|
|
||||||
currentNullType = currentMethodReturnType;
|
|
||||||
|
|
||||||
var result = methodNode.block.accept(this);
|
|
||||||
valid = valid && result.isValid();
|
valid = valid && result.isValid();
|
||||||
currentScope.popScope();
|
try {
|
||||||
ITypeNode resultType = result.getType();
|
currentScope.addLocalVar(parameter.identifier, parameter.type);
|
||||||
|
} catch (AlreadyDeclaredException e) {
|
||||||
if (resultType == null) {
|
errors.add(new AlreadyDeclaredException(parameter.identifier));
|
||||||
resultType = new BaseType(TypeEnum.VOID);
|
|
||||||
}
|
}
|
||||||
if (methodNode.getType() == null) {
|
|
||||||
methodNode.setType(new BaseType(TypeEnum.VOID));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TypeCheckResult(valid, resultType);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentMethodReturnType = methodNode.getType();
|
||||||
|
currentNullType = currentMethodReturnType;
|
||||||
|
|
||||||
|
var result = methodNode.block.accept(this);
|
||||||
|
valid = valid && result.isValid();
|
||||||
|
currentScope.popScope();
|
||||||
|
ITypeNode resultType = result.getType();
|
||||||
|
|
||||||
|
if (resultType == null) {
|
||||||
|
resultType = new BaseType(TypeEnum.VOID);
|
||||||
|
}
|
||||||
|
if (methodNode.getType() == null) {
|
||||||
|
methodNode.setType(new BaseType(TypeEnum.VOID));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TypeCheckResult(valid, resultType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -224,7 +220,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
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 (!(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();
|
||||||
@ -250,6 +246,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
if (currentFields.get(toCheck.identifier) != null) {
|
if (currentFields.get(toCheck.identifier) != null) {
|
||||||
var type = currentFields.get(toCheck.identifier);
|
var type = currentFields.get(toCheck.identifier);
|
||||||
toCheck.setTypeNode(type);
|
toCheck.setTypeNode(type);
|
||||||
|
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
|
||||||
|
memberAccessNode.identifiers.add(currentClass.identifier);
|
||||||
|
toCheck.memberAccess = memberAccessNode;
|
||||||
return new TypeCheckResult(true, type);
|
return new TypeCheckResult(true, type);
|
||||||
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
|
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
|
||||||
var type = currentScope.getLocalVar(toCheck.identifier);
|
var type = currentScope.getLocalVar(toCheck.identifier);
|
||||||
@ -409,6 +408,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
case PLUS, MINUS:
|
case PLUS, MINUS:
|
||||||
if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType &&
|
if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType &&
|
||||||
calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) {
|
calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) {
|
||||||
|
calcNode.setType(new BaseType(TypeEnum.INT));
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -416,10 +416,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
calcNode.setType(calcRes.getType());
|
||||||
return new TypeCheckResult(calcRes.isValid(), calcRes.getType());
|
return new TypeCheckResult(calcRes.isValid(), calcRes.getType());
|
||||||
}
|
}
|
||||||
} else if (calcNode.dotExpression != null) {
|
} else if (calcNode.dotExpression != null) {
|
||||||
var dotRes = calcNode.dotExpression.accept(this);
|
var dotRes = calcNode.dotExpression.accept(this);
|
||||||
|
calcNode.setType(dotRes.getType());
|
||||||
return new TypeCheckResult(dotRes.isValid(), dotRes.getType());
|
return new TypeCheckResult(dotRes.isValid(), dotRes.getType());
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(false, null);
|
return new TypeCheckResult(false, null);
|
||||||
@ -462,6 +464,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL:
|
case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL:
|
||||||
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
||||||
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
||||||
|
nonCalculationNode.setType(new BaseType(TypeEnum.BOOL));
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
||||||
} else {
|
} else {
|
||||||
errors.add(new TypeMismatchException("Both types must be Integer"));
|
errors.add(new TypeMismatchException("Both types must be Integer"));
|
||||||
@ -470,6 +473,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
case OR, AND:
|
case OR, AND:
|
||||||
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
||||||
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
||||||
|
nonCalculationNode.setType(new BaseType(TypeEnum.BOOL));
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
||||||
} else {
|
} else {
|
||||||
errors.add(new TypeMismatchException("Both types must be Boolean"));
|
errors.add(new TypeMismatchException("Both types must be Boolean"));
|
||||||
@ -478,6 +482,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
case EQUAL, NOT_EQUAL:
|
case EQUAL, NOT_EQUAL:
|
||||||
if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType
|
if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType
|
||||||
&& Objects.equals(expResultType, unaryResultType)) {
|
&& Objects.equals(expResultType, unaryResultType)) {
|
||||||
|
nonCalculationNode.setType(new BaseType(TypeEnum.BOOL));
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
||||||
} else {
|
} else {
|
||||||
errors.add(new TypeMismatchException("Both types must be the same"));
|
errors.add(new TypeMismatchException("Both types must be the same"));
|
||||||
@ -493,9 +498,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
if (unary.identifier != null) {
|
if (unary.identifier != null) {
|
||||||
if (currentScope.contains(unary.identifier)) {
|
if (currentScope.contains(unary.identifier)) {
|
||||||
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
|
var type = currentScope.getLocalVar(unary.identifier);
|
||||||
|
unary.setType(type);
|
||||||
|
return new TypeCheckResult(valid, type);
|
||||||
} else if (currentFields.get(unary.identifier) != null) {
|
} else if (currentFields.get(unary.identifier) != null) {
|
||||||
return new TypeCheckResult(valid, currentFields.get(unary.identifier));
|
var type = currentFields.get(unary.identifier);
|
||||||
|
unary.setType(type);
|
||||||
|
return new TypeCheckResult(valid, type);
|
||||||
} else if (unary.statement != null) {
|
} else if (unary.statement != null) {
|
||||||
var result = unary.statement.accept(this);
|
var result = unary.statement.accept(this);
|
||||||
unary.setType(result.getType());
|
unary.setType(result.getType());
|
||||||
@ -505,15 +514,19 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
} else if (unary.statement != null) {
|
} else if (unary.statement != null) {
|
||||||
var result = unary.statement.accept(this);
|
var result = unary.statement.accept(this);
|
||||||
|
unary.setType(result.getType());
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
return new TypeCheckResult(result.isValid(), result.getType());
|
||||||
} else if (unary.value != null) {
|
} else if (unary.value != null) {
|
||||||
var result = unary.value.accept(this);
|
var result = unary.value.accept(this);
|
||||||
|
unary.setType(result.getType());
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
return new TypeCheckResult(result.isValid(), result.getType());
|
||||||
} else if (unary.memberAccess != null) {
|
} else if (unary.memberAccess != null) {
|
||||||
var result = unary.memberAccess.accept(this);
|
var result = unary.memberAccess.accept(this);
|
||||||
|
unary.setType(result.getType());
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
return new TypeCheckResult(result.isValid(), result.getType());
|
||||||
} else if (unary.expression != null) {
|
} else if (unary.expression != null) {
|
||||||
var result = unary.expression.accept(this);
|
var result = unary.expression.accept(this);
|
||||||
|
unary.setType(result.getType());
|
||||||
return new TypeCheckResult(result.isValid(), result.getType());
|
return new TypeCheckResult(result.isValid(), result.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -525,6 +538,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
ITypeNode currentType = null;
|
ITypeNode currentType = null;
|
||||||
|
|
||||||
|
if (memberAccessNode.thisExpr) {
|
||||||
|
currentType = new ReferenceType(currentClass.identifier);
|
||||||
|
}
|
||||||
|
|
||||||
for (String s : memberAccessNode.identifiers) {
|
for (String s : memberAccessNode.identifiers) {
|
||||||
if (currentType == null) {
|
if (currentType == null) {
|
||||||
if (currentScope.getLocalVar(s) != null) {
|
if (currentScope.getLocalVar(s) != null) {
|
||||||
@ -542,7 +559,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
var currentTypeClass = context.getClass(reference.getIdentifier());
|
var currentTypeClass = context.getClass(reference.getIdentifier());
|
||||||
|
|
||||||
var currentField = currentTypeClass.getField(s);
|
var currentField = currentTypeClass.getField(s);
|
||||||
if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) {
|
if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC || memberAccessNode.thisExpr) {
|
||||||
currentType = currentField.getType();
|
currentType = currentField.getType();
|
||||||
} else {
|
} else {
|
||||||
errors.add(new NotVisibleException("This field is not visible"));
|
errors.add(new NotVisibleException("This field is not visible"));
|
||||||
|
@ -4,13 +4,15 @@ import ast.members.FieldNode;
|
|||||||
import ast.type.*;
|
import ast.type.*;
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class FieldContext {
|
public class FieldContext {
|
||||||
|
|
||||||
private AccessModifierNode accessModifier;
|
private AccessModifierNode accessModifier;
|
||||||
private ITypeNode type;
|
private ITypeNode type;
|
||||||
|
|
||||||
public FieldContext(FieldNode field) {
|
public FieldContext(FieldNode field) {
|
||||||
accessModifier = field.accessTypeNode;
|
accessModifier = Objects.requireNonNullElseGet(field.accessTypeNode, () -> new AccessModifierNode("private"));
|
||||||
type = field.type;
|
type = field.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
// @expected: AlreadyDeclaredException
|
||||||
|
public class AllFeaturesClassExample {
|
||||||
|
|
||||||
|
public AllFeaturesClassExample(boolean b){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public AllFeaturesClassExample(boolean b){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
// @expected: TypeMismatchException
|
||||||
|
public class AllFeaturesClassExample {
|
||||||
|
int x;
|
||||||
|
|
||||||
|
public boolean test(boolean x){
|
||||||
|
return this.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
public class AllFeaturesClassExample {
|
||||||
|
|
||||||
|
public AllFeaturesClassExample(boolean b){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public AllFeaturesClassExample(boolean b, int a){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,38 +1,10 @@
|
|||||||
|
|
||||||
public class AllFeaturesClassExample {
|
public class AllFeaturesClassExample {
|
||||||
int a;
|
int x;
|
||||||
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++) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
public void test(){
|
||||||
|
x = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user