Fixes of Constructor

This commit is contained in:
i22035 2024-07-04 22:24:50 +02:00
parent 195440e9d9
commit 0cf4715782
4 changed files with 11 additions and 13 deletions

View File

@ -11,12 +11,8 @@ import java.util.List;
import java.util.Objects;
public class ConstructorNode extends MethodNode implements Visitable {
public AccessModifierNode accessType;
public String identifier;
public List<ParameterNode> parameters = new ArrayList<>();
public ConstructorNode(String accessType, String identifier, BlockNode block) {
this.accessType = new AccessModifierNode(accessType);
this.accesModifier = new AccessModifierNode(accessType);
this.identifier = identifier;
this.block = block;
}

View File

@ -53,7 +53,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(ConstructorNode constructorNode) {
methodVisitor =
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType),
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accesModifier),
"<init>",
mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters),
null,

View File

@ -1,6 +1,5 @@
package semantic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -25,15 +24,11 @@ import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNod
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*;
import ast.type.AccessModifierNode;
import ast.type.EnumAccessModifierNode;
import ast.type.ValueNode;
import ast.type.type.*;
import com.sun.jdi.IntegerType;
import semantic.context.ClassContext;
import semantic.context.Context;
import semantic.exceptions.*;
import semantic.TypeCheckResult;
public class SemanticAnalyzer implements SemanticVisitor {
@ -311,7 +306,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
var resultIf = toCheck.ifStatement.accept(this);
var validElseIf = true;
if(toCheck.elseIfStatements.size() != 0) {
if(!toCheck.elseIfStatements.isEmpty()) {
for(IfNode ifNode : toCheck.elseIfStatements) {
var resultIfFor = ifNode.accept(this);
validElseIf = validElseIf && resultIfFor.isValid();
@ -365,6 +360,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
if (toCheck.target.thisTar) {
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
if (type != null) {
toCheck.type = type;
return new TypeCheckResult(true, type);
}
}
@ -376,7 +372,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
}
} else {
ReferenceType reference = new ReferenceType(currentClass.identifier);
if (!toCheck.chainedMethods.isEmpty()) {
for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {

View File

@ -8,4 +8,11 @@ public class Person {
public int getAge() {
return this.age;
}
}
public class Person2 {
public void greet() {
Person person = new Person(10);
person.getAge();
}
}