Merge pull request 'changed to interfaces' (#6) from johns-branch into main

Reviewed-on: #6
Reviewed-by: i22007 <i22007@hb.dhbw-stuttgart.de>
This commit is contained in:
Johannes Ehlert 2024-05-14 09:21:26 +00:00
commit f77143599b
21 changed files with 52 additions and 43 deletions

2
.idea/misc.xml generated
View File

@ -40,7 +40,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -5,7 +5,9 @@ public class Example {
public static int testMethod(char b){
int a;
a = 3;
boolean b;
char c;
b = 3;
}

View File

@ -1,6 +1,6 @@
package ast;
public class ASTNode {
public interface ASTNode {
}

View File

@ -13,7 +13,7 @@ import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class ClassNode extends ASTNode implements Visitable {
public class ClassNode implements ASTNode, Visitable {
public String identifier;
public AccessTypeNode accessType;
public List<MemberNode> members = new ArrayList<>();
@ -40,7 +40,7 @@ public class ClassNode extends ASTNode implements Visitable {
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
return visitor.analyze(this);
}
@Override

View File

@ -8,7 +8,7 @@ import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class ProgramNode extends ASTNode implements Visitable {
public class ProgramNode implements ASTNode, Visitable{
public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) {
@ -17,7 +17,7 @@ public class ProgramNode extends ASTNode implements Visitable {
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
return visitor.analyze(this);
}
@Override

View File

@ -1,6 +1,6 @@
package ast;
public class VarNode extends ASTNode{
public class VarNode implements ASTNode{
private String identifier;
private String type;

View File

@ -2,7 +2,7 @@ package ast.expression;
import ast.ASTNode;
public class ExpressionNode extends ASTNode {
public class ExpressionNode implements ASTNode {

View File

@ -7,7 +7,7 @@ import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class FieldNode extends MemberNode implements Visitable {
public class FieldNode implements MemberNode, Visitable {
public AccessTypeNode accessTypeNode;
public TypeNode type;
public String identifier;
@ -20,7 +20,7 @@ public class FieldNode extends MemberNode implements Visitable {
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
return visitor.analyze(this);
}
@Override

View File

@ -2,5 +2,5 @@ package ast.member;
import ast.ASTNode;
public class MemberNode extends ASTNode {
public interface MemberNode extends ASTNode {
}

View File

@ -13,7 +13,7 @@ import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class MethodNode extends MemberNode implements Visitable {
public class MethodNode implements MemberNode, Visitable {
public AccessTypeNode visibility;
public TypeNode type;
public String identifier;
@ -38,7 +38,7 @@ public class MethodNode extends MemberNode implements Visitable {
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
return visitor.analyze(this);
}
@Override

View File

@ -5,7 +5,7 @@ import ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class ParameterListNode extends ASTNode {
public class ParameterListNode implements ASTNode {
List<ParameterNode> parameters = new ArrayList<>();
public ParameterListNode(List<ParameterNode> parameters){

View File

@ -3,7 +3,7 @@ package ast.parameter;
import ast.ASTNode;
import ast.type.TypeNode;
public class ParameterNode extends ASTNode {
public class ParameterNode implements ASTNode {
public TypeNode type;
public String identifier;

View File

@ -17,6 +17,6 @@ public class AssignmentStatementNode extends StatementNode implements Visitable
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
return visitor.analyze(this);
}
}

View File

@ -2,6 +2,6 @@ package ast.statement;
import ast.ASTNode;
public abstract class StatementNode extends ASTNode {
public abstract class StatementNode implements ASTNode {
}

View File

@ -2,7 +2,7 @@ package ast.type;
import ast.ASTNode;
public class AccessTypeNode extends ASTNode {
public class AccessTypeNode implements ASTNode {
public EnumAccessTypeNode enumAccessTypeNode;
public AccessTypeNode(EnumAccessTypeNode enumAccessTypeNode) {

View File

@ -0,0 +1,11 @@
package ast.type;
public class BaseTypeNode implements TypeNode{
public EnumTypeNode enumType;
public BaseTypeNode(EnumTypeNode enumType) {
this.enumType = enumType;
}
}

View File

@ -0,0 +1,4 @@
package ast.type;
public class ReferenceTypeNode implements TypeNode{
}

View File

@ -2,10 +2,5 @@ package ast.type;
import ast.ASTNode;
public class TypeNode extends ASTNode {
public EnumTypeNode enumTypeNode;
public TypeNode(EnumTypeNode enumTypeNode) {
this.enumTypeNode = enumTypeNode;
}
public interface TypeNode extends ASTNode {
}

View File

@ -11,10 +11,7 @@ import ast.member.MethodNode;
import ast.parameter.ParameterListNode;
import ast.parameter.ParameterNode;
import ast.statement.*;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import ast.type.EnumTypeNode;
import ast.type.TypeNode;
import ast.type.*;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.ArrayList;
import java.util.List;
@ -87,11 +84,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
String typeStr = ctx.getText();
switch (typeStr) {
case "int":
return new TypeNode(EnumTypeNode.INT);
return new BaseTypeNode(EnumTypeNode.INT);
case "boolean":
return new TypeNode(EnumTypeNode.BOOLEAN);
return new BaseTypeNode(EnumTypeNode.BOOLEAN);
case "char":
return new TypeNode(EnumTypeNode.CHAR);
return new BaseTypeNode(EnumTypeNode.CHAR);
default:
throw new IllegalArgumentException("Unsupported type: " + typeStr);
}

View File

@ -30,7 +30,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
@Override
public TypeCheckResult typeCheck(ProgramNode node) {
public TypeCheckResult analyze(ProgramNode node) {
var valid = true;
@ -42,7 +42,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
@Override
public TypeCheckResult typeCheck(ClassNode classNode) {
public TypeCheckResult analyze(ClassNode classNode) {
List<MemberNode> members = classNode.members;
for (MemberNode memberNode : members) {
if (memberNode instanceof FieldNode fieldNode) {
@ -56,7 +56,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
@Override
public TypeCheckResult typeCheck(MethodNode methodNode) {
public TypeCheckResult analyze(MethodNode methodNode) {
List<StatementNode> statements = methodNode.statements;
for (StatementNode statement : statements) {
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
@ -67,7 +67,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
@Override
public TypeCheckResult typeCheck(FieldNode toCheck) {
public TypeCheckResult analyze(FieldNode toCheck) {
if(currentFields.contains(toCheck.identifier)){
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
}else {
@ -77,13 +77,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
@Override
public TypeCheckResult typeCheck(AssignmentStatementNode assignmentStatementNode) {
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
VarNode varNode = assignmentStatementNode.varNode;
if(varNode.getType().equals(literalNode.getType())) {
System.out.println("Type is same");
} else {
System.out.println("Type Mismatch");
throw new RuntimeException("Type mismatch");
}
}
return null;

View File

@ -11,15 +11,15 @@ import typechecker.TypeCheckResult;
public interface SemanticVisitor {
// TypeCheckResult typeCheck(ASTNode toCheck);
TypeCheckResult typeCheck(ProgramNode toCheck);
TypeCheckResult analyze(ProgramNode toCheck);
TypeCheckResult typeCheck(ClassNode toCheck);
TypeCheckResult analyze(ClassNode toCheck);
TypeCheckResult typeCheck(MethodNode toCheck);
TypeCheckResult analyze(MethodNode toCheck);
TypeCheckResult typeCheck(FieldNode toCheck);
TypeCheckResult analyze(FieldNode toCheck);
TypeCheckResult typeCheck(AssignmentStatementNode toCheck);
TypeCheckResult analyze(AssignmentStatementNode toCheck);
//
// TypeCheckResult typeCheck(MethodParameter toCheck);
//