changed to interfaces
This commit is contained in:
parent
e987faae64
commit
dfe96d9a87
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -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>
|
@ -5,7 +5,9 @@ public class Example {
|
||||
public static int testMethod(char b){
|
||||
|
||||
int a;
|
||||
a = 3;
|
||||
boolean b;
|
||||
char c;
|
||||
b = 3;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package ast;
|
||||
|
||||
public class ASTNode {
|
||||
public interface ASTNode {
|
||||
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,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<>();
|
||||
@ -38,6 +38,6 @@ public class ClassNode extends ASTNode implements Visitable {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,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) {
|
||||
@ -15,6 +15,6 @@ public class ProgramNode extends ASTNode implements Visitable{
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package ast;
|
||||
|
||||
public class VarNode extends ASTNode{
|
||||
public class VarNode implements ASTNode{
|
||||
|
||||
private String identifier;
|
||||
private String type;
|
||||
|
@ -2,7 +2,7 @@ package ast.expression;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public class ExpressionNode extends ASTNode {
|
||||
public class ExpressionNode implements ASTNode {
|
||||
|
||||
|
||||
|
||||
|
@ -6,7 +6,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;
|
||||
@ -19,6 +19,6 @@ public class FieldNode extends MemberNode implements Visitable {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,5 @@ package ast.member;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public class MemberNode extends ASTNode {
|
||||
public interface MemberNode extends ASTNode {
|
||||
}
|
||||
|
@ -11,7 +11,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;
|
||||
@ -36,6 +36,6 @@ public class MethodNode extends MemberNode implements Visitable {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -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){
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,6 @@ package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public abstract class StatementNode extends ASTNode {
|
||||
public abstract class StatementNode implements ASTNode {
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
11
src/main/java/ast/type/BaseTypeNode.java
Normal file
11
src/main/java/ast/type/BaseTypeNode.java
Normal file
@ -0,0 +1,11 @@
|
||||
package ast.type;
|
||||
|
||||
public class BaseTypeNode implements TypeNode{
|
||||
|
||||
public EnumTypeNode enumType;
|
||||
|
||||
public BaseTypeNode(EnumTypeNode enumType) {
|
||||
this.enumType = enumType;
|
||||
}
|
||||
|
||||
}
|
4
src/main/java/ast/type/ReferenceTypeNode.java
Normal file
4
src/main/java/ast/type/ReferenceTypeNode.java
Normal file
@ -0,0 +1,4 @@
|
||||
package ast.type;
|
||||
|
||||
public class ReferenceTypeNode implements TypeNode{
|
||||
}
|
@ -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 {
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user