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>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -5,7 +5,9 @@ public class Example {
|
|||||||
public static int testMethod(char b){
|
public static int testMethod(char b){
|
||||||
|
|
||||||
int a;
|
int a;
|
||||||
a = 3;
|
boolean b;
|
||||||
|
char c;
|
||||||
|
b = 3;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public class ASTNode {
|
public interface ASTNode {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import semantic.SemanticVisitor;
|
|||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class ClassNode extends ASTNode implements Visitable {
|
public class ClassNode implements ASTNode, Visitable {
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public AccessTypeNode accessType;
|
public AccessTypeNode accessType;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
@ -38,6 +38,6 @@ public class ClassNode extends ASTNode implements Visitable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
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.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class ProgramNode extends ASTNode implements Visitable{
|
public class ProgramNode implements ASTNode, Visitable{
|
||||||
public List<ClassNode> classes = new ArrayList<>();
|
public List<ClassNode> classes = new ArrayList<>();
|
||||||
|
|
||||||
public void addClass(ClassNode classNode) {
|
public void addClass(ClassNode classNode) {
|
||||||
@ -15,6 +15,6 @@ public class ProgramNode extends ASTNode implements Visitable{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public class VarNode extends ASTNode{
|
public class VarNode implements ASTNode{
|
||||||
|
|
||||||
private String identifier;
|
private String identifier;
|
||||||
private String type;
|
private String type;
|
||||||
|
@ -2,7 +2,7 @@ package ast.expression;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
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.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class FieldNode extends MemberNode implements Visitable {
|
public class FieldNode implements MemberNode, Visitable {
|
||||||
public AccessTypeNode accessTypeNode;
|
public AccessTypeNode accessTypeNode;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
@ -19,6 +19,6 @@ public class FieldNode extends MemberNode implements Visitable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,5 @@ package ast.member;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
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.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class MethodNode extends MemberNode implements Visitable {
|
public class MethodNode implements MemberNode, Visitable {
|
||||||
public AccessTypeNode visibility;
|
public AccessTypeNode visibility;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
@ -36,6 +36,6 @@ public class MethodNode extends MemberNode implements Visitable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ParameterListNode extends ASTNode {
|
public class ParameterListNode implements ASTNode {
|
||||||
List<ParameterNode> parameters = new ArrayList<>();
|
List<ParameterNode> parameters = new ArrayList<>();
|
||||||
|
|
||||||
public ParameterListNode(List<ParameterNode> parameters){
|
public ParameterListNode(List<ParameterNode> parameters){
|
||||||
|
@ -3,7 +3,7 @@ package ast.parameter;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
||||||
|
|
||||||
public class ParameterNode extends ASTNode {
|
public class ParameterNode implements ASTNode {
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
|
|
||||||
|
@ -17,6 +17,6 @@ public class AssignmentStatementNode extends StatementNode implements Visitable
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@ package ast.statement;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
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;
|
import ast.ASTNode;
|
||||||
|
|
||||||
public class AccessTypeNode extends ASTNode {
|
public class AccessTypeNode implements ASTNode {
|
||||||
public EnumAccessTypeNode enumAccessTypeNode;
|
public EnumAccessTypeNode enumAccessTypeNode;
|
||||||
|
|
||||||
public AccessTypeNode(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;
|
import ast.ASTNode;
|
||||||
|
|
||||||
public class TypeNode extends ASTNode {
|
public interface TypeNode extends ASTNode {
|
||||||
public EnumTypeNode enumTypeNode;
|
|
||||||
|
|
||||||
public TypeNode(EnumTypeNode enumTypeNode) {
|
|
||||||
this.enumTypeNode = enumTypeNode;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -11,10 +11,7 @@ import ast.member.MethodNode;
|
|||||||
import ast.parameter.ParameterListNode;
|
import ast.parameter.ParameterListNode;
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameter.ParameterNode;
|
||||||
import ast.statement.*;
|
import ast.statement.*;
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.*;
|
||||||
import ast.type.EnumAccessTypeNode;
|
|
||||||
import ast.type.EnumTypeNode;
|
|
||||||
import ast.type.TypeNode;
|
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -87,11 +84,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
String typeStr = ctx.getText();
|
String typeStr = ctx.getText();
|
||||||
switch (typeStr) {
|
switch (typeStr) {
|
||||||
case "int":
|
case "int":
|
||||||
return new TypeNode(EnumTypeNode.INT);
|
return new BaseTypeNode(EnumTypeNode.INT);
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return new TypeNode(EnumTypeNode.BOOLEAN);
|
return new BaseTypeNode(EnumTypeNode.BOOLEAN);
|
||||||
case "char":
|
case "char":
|
||||||
return new TypeNode(EnumTypeNode.CHAR);
|
return new BaseTypeNode(EnumTypeNode.CHAR);
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unsupported type: " + typeStr);
|
throw new IllegalArgumentException("Unsupported type: " + typeStr);
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult typeCheck(ProgramNode node) {
|
public TypeCheckResult analyze(ProgramNode node) {
|
||||||
|
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult typeCheck(ClassNode classNode) {
|
public TypeCheckResult analyze(ClassNode classNode) {
|
||||||
List<MemberNode> members = classNode.members;
|
List<MemberNode> members = classNode.members;
|
||||||
for (MemberNode memberNode : members) {
|
for (MemberNode memberNode : members) {
|
||||||
if (memberNode instanceof FieldNode fieldNode) {
|
if (memberNode instanceof FieldNode fieldNode) {
|
||||||
@ -56,7 +56,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult typeCheck(MethodNode methodNode) {
|
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||||
List<StatementNode> statements = methodNode.statements;
|
List<StatementNode> statements = methodNode.statements;
|
||||||
for (StatementNode statement : statements) {
|
for (StatementNode statement : statements) {
|
||||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||||
@ -67,7 +67,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult typeCheck(FieldNode toCheck) {
|
public TypeCheckResult analyze(FieldNode toCheck) {
|
||||||
if(currentFields.contains(toCheck.identifier)){
|
if(currentFields.contains(toCheck.identifier)){
|
||||||
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
||||||
}else {
|
}else {
|
||||||
@ -77,13 +77,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult typeCheck(AssignmentStatementNode assignmentStatementNode) {
|
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
||||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||||
VarNode varNode = assignmentStatementNode.varNode;
|
VarNode varNode = assignmentStatementNode.varNode;
|
||||||
if(varNode.getType().equals(literalNode.getType())) {
|
if(varNode.getType().equals(literalNode.getType())) {
|
||||||
System.out.println("Type is same");
|
System.out.println("Type is same");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Type Mismatch");
|
throw new RuntimeException("Type mismatch");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -11,15 +11,15 @@ import typechecker.TypeCheckResult;
|
|||||||
public interface SemanticVisitor {
|
public interface SemanticVisitor {
|
||||||
// TypeCheckResult typeCheck(ASTNode toCheck);
|
// 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);
|
// TypeCheckResult typeCheck(MethodParameter toCheck);
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user