diff --git a/.idea/misc.xml b/.idea/misc.xml index bb14756..f26d89f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -40,7 +40,7 @@ - + \ No newline at end of file diff --git a/src/main/java/CompilerInput.txt b/src/main/java/CompilerInput.txt index 49da971..b784cf7 100644 --- a/src/main/java/CompilerInput.txt +++ b/src/main/java/CompilerInput.txt @@ -5,7 +5,9 @@ public class Example { public static int testMethod(char b){ int a; - a = 3; + boolean b; + char c; + b = 3; } diff --git a/src/main/java/ast/ASTNode.java b/src/main/java/ast/ASTNode.java index 9c5880e..9944050 100644 --- a/src/main/java/ast/ASTNode.java +++ b/src/main/java/ast/ASTNode.java @@ -1,6 +1,6 @@ package ast; -public class ASTNode { +public interface ASTNode { } diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java index 654b745..2f09f41 100644 --- a/src/main/java/ast/ClassNode.java +++ b/src/main/java/ast/ClassNode.java @@ -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 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 diff --git a/src/main/java/ast/ProgramNode.java b/src/main/java/ast/ProgramNode.java index cdfdd34..116c5a2 100644 --- a/src/main/java/ast/ProgramNode.java +++ b/src/main/java/ast/ProgramNode.java @@ -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 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 diff --git a/src/main/java/ast/VarNode.java b/src/main/java/ast/VarNode.java index a3ac170..e27f080 100644 --- a/src/main/java/ast/VarNode.java +++ b/src/main/java/ast/VarNode.java @@ -1,6 +1,6 @@ package ast; -public class VarNode extends ASTNode{ +public class VarNode implements ASTNode{ private String identifier; private String type; diff --git a/src/main/java/ast/expression/ExpressionNode.java b/src/main/java/ast/expression/ExpressionNode.java index 8204bed..5dbead0 100644 --- a/src/main/java/ast/expression/ExpressionNode.java +++ b/src/main/java/ast/expression/ExpressionNode.java @@ -2,7 +2,7 @@ package ast.expression; import ast.ASTNode; -public class ExpressionNode extends ASTNode { +public class ExpressionNode implements ASTNode { diff --git a/src/main/java/ast/member/FieldNode.java b/src/main/java/ast/member/FieldNode.java index cbb70d4..70880c5 100644 --- a/src/main/java/ast/member/FieldNode.java +++ b/src/main/java/ast/member/FieldNode.java @@ -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 diff --git a/src/main/java/ast/member/MemberNode.java b/src/main/java/ast/member/MemberNode.java index aa823af..f186835 100644 --- a/src/main/java/ast/member/MemberNode.java +++ b/src/main/java/ast/member/MemberNode.java @@ -2,5 +2,5 @@ package ast.member; import ast.ASTNode; -public class MemberNode extends ASTNode { +public interface MemberNode extends ASTNode { } diff --git a/src/main/java/ast/member/MethodNode.java b/src/main/java/ast/member/MethodNode.java index 3bc7732..88ed182 100644 --- a/src/main/java/ast/member/MethodNode.java +++ b/src/main/java/ast/member/MethodNode.java @@ -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 diff --git a/src/main/java/ast/parameter/ParameterListNode.java b/src/main/java/ast/parameter/ParameterListNode.java index f409a65..69d12f3 100644 --- a/src/main/java/ast/parameter/ParameterListNode.java +++ b/src/main/java/ast/parameter/ParameterListNode.java @@ -5,8 +5,8 @@ import ast.ASTNode; import java.util.ArrayList; import java.util.List; -public class ParameterListNode extends ASTNode { - public List parameters = new ArrayList<>(); +public class ParameterListNode implements ASTNode { + List parameters = new ArrayList<>(); public ParameterListNode(List parameters){ this.parameters = parameters; diff --git a/src/main/java/ast/parameter/ParameterNode.java b/src/main/java/ast/parameter/ParameterNode.java index 739ae21..f3f5193 100644 --- a/src/main/java/ast/parameter/ParameterNode.java +++ b/src/main/java/ast/parameter/ParameterNode.java @@ -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; diff --git a/src/main/java/ast/statement/AssignmentStatementNode.java b/src/main/java/ast/statement/AssignmentStatementNode.java index b9158e7..7efbd73 100644 --- a/src/main/java/ast/statement/AssignmentStatementNode.java +++ b/src/main/java/ast/statement/AssignmentStatementNode.java @@ -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); } } diff --git a/src/main/java/ast/statement/StatementNode.java b/src/main/java/ast/statement/StatementNode.java index 910a134..6c90f8c 100644 --- a/src/main/java/ast/statement/StatementNode.java +++ b/src/main/java/ast/statement/StatementNode.java @@ -2,6 +2,6 @@ package ast.statement; import ast.ASTNode; -public abstract class StatementNode extends ASTNode { +public abstract class StatementNode implements ASTNode { } diff --git a/src/main/java/ast/type/AccessTypeNode.java b/src/main/java/ast/type/AccessTypeNode.java index ddacf68..ba156c1 100644 --- a/src/main/java/ast/type/AccessTypeNode.java +++ b/src/main/java/ast/type/AccessTypeNode.java @@ -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) { diff --git a/src/main/java/ast/type/BaseTypeNode.java b/src/main/java/ast/type/BaseTypeNode.java new file mode 100644 index 0000000..81cdf43 --- /dev/null +++ b/src/main/java/ast/type/BaseTypeNode.java @@ -0,0 +1,11 @@ +package ast.type; + +public class BaseTypeNode implements TypeNode{ + + public EnumTypeNode enumType; + + public BaseTypeNode(EnumTypeNode enumType) { + this.enumType = enumType; + } + +} diff --git a/src/main/java/ast/type/ReferenceTypeNode.java b/src/main/java/ast/type/ReferenceTypeNode.java new file mode 100644 index 0000000..1937054 --- /dev/null +++ b/src/main/java/ast/type/ReferenceTypeNode.java @@ -0,0 +1,4 @@ +package ast.type; + +public class ReferenceTypeNode implements TypeNode{ +} diff --git a/src/main/java/ast/type/TypeNode.java b/src/main/java/ast/type/TypeNode.java index 5b1c2a6..79be9c9 100644 --- a/src/main/java/ast/type/TypeNode.java +++ b/src/main/java/ast/type/TypeNode.java @@ -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 { } \ No newline at end of file diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index 87b0238..1e8cbc3 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -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 { 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); } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 07d0ca2..d499be2 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -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 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 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; diff --git a/src/main/java/semantic/SemanticVisitor.java b/src/main/java/semantic/SemanticVisitor.java index 2fbaf41..f1228bb 100644 --- a/src/main/java/semantic/SemanticVisitor.java +++ b/src/main/java/semantic/SemanticVisitor.java @@ -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); //