From 330b92a79feb5eee6350ec68e135611dd9618137 Mon Sep 17 00:00:00 2001 From: i22035 Date: Sat, 15 Jun 2024 14:05:03 +0200 Subject: [PATCH] Deleted old grammar --- src/main/java/ast/ASTNode.java | 9 +- src/main/java/ast/ClassNode.java | 16 +- src/main/java/ast/ProgramNode.java | 2 - src/main/java/ast/member/ConstructorNode.java | 21 ++ .../{oldAst => ast}/member/FieldNode.java | 8 +- .../{oldAst => ast}/member/MemberNode.java | 8 +- .../{oldAst => ast}/member/MethodNode.java | 15 +- .../java/ast/type/AccessModifierNode.java | 13 +- .../java/ast/type/EnumAccessModifierNode.java | 5 - src/main/java/ast/type/TypeNode.java | 5 + src/main/java/bytecode/ByteCodeGenerator.java | 4 +- src/main/java/bytecode/ClassCodeGen.java | 10 +- src/main/java/bytecode/Mapper.java | 18 +- src/main/java/bytecode/MethodCodeGen.java | 8 +- .../java/bytecode/visitor/MethodVisitor.java | 4 +- src/main/java/oldAst/ASTNode.java | 12 -- src/main/java/oldAst/BlockNode.java | 4 - src/main/java/oldAst/ClassNode.java | 63 ------ src/main/java/oldAst/ProgramNode.java | 27 --- .../expression/BinaryExpressionNode.java | 22 -- .../oldAst/expression/ExpressionNode.java | 22 -- .../expression/ExpresssionOperator.java | 14 -- .../expression/IdentifierExpressionNode.java | 22 -- src/main/java/oldAst/expression/InstVar.java | 24 --- .../java/oldAst/expression/LiteralNode.java | 31 --- src/main/java/oldAst/expression/This.java | 26 --- .../expression/UnaryExpressionNode.java | 22 -- .../java/oldAst/member/ConstructorNode.java | 16 -- .../oldAst/parameter/ParameterListNode.java | 16 -- .../java/oldAst/parameter/ParameterNode.java | 16 -- .../statement/AssignmentStatementNode.java | 24 --- .../oldAst/statement/IfStatementNode.java | 22 -- .../oldAst/statement/ReturnStatementNode.java | 18 -- .../java/oldAst/statement/StatementNode.java | 20 -- .../VariableDeclarationStatementNode.java | 25 --- .../oldAst/statement/WhileStatementNode.java | 20 -- src/main/java/oldAst/type/AccessTypeNode.java | 13 -- src/main/java/oldAst/type/BaseTypeNode.java | 28 --- .../java/oldAst/type/EnumAccessTypeNode.java | 5 - src/main/java/oldAst/type/EnumTypeNode.java | 5 - .../java/oldAst/type/ReferenceTypeNode.java | 36 ---- src/main/java/oldAst/type/TypeNode.java | 18 -- .../java/parser/astBuilder/ASTBuilder.java | 195 +----------------- 43 files changed, 75 insertions(+), 837 deletions(-) create mode 100644 src/main/java/ast/member/ConstructorNode.java rename src/main/java/{oldAst => ast}/member/FieldNode.java (74%) rename src/main/java/{oldAst => ast}/member/MemberNode.java (82%) rename src/main/java/{oldAst => ast}/member/MethodNode.java (89%) delete mode 100644 src/main/java/ast/type/EnumAccessModifierNode.java create mode 100644 src/main/java/ast/type/TypeNode.java delete mode 100644 src/main/java/oldAst/ASTNode.java delete mode 100644 src/main/java/oldAst/BlockNode.java delete mode 100644 src/main/java/oldAst/ClassNode.java delete mode 100644 src/main/java/oldAst/ProgramNode.java delete mode 100644 src/main/java/oldAst/expression/BinaryExpressionNode.java delete mode 100644 src/main/java/oldAst/expression/ExpressionNode.java delete mode 100644 src/main/java/oldAst/expression/ExpresssionOperator.java delete mode 100644 src/main/java/oldAst/expression/IdentifierExpressionNode.java delete mode 100644 src/main/java/oldAst/expression/InstVar.java delete mode 100644 src/main/java/oldAst/expression/LiteralNode.java delete mode 100644 src/main/java/oldAst/expression/This.java delete mode 100644 src/main/java/oldAst/expression/UnaryExpressionNode.java delete mode 100644 src/main/java/oldAst/member/ConstructorNode.java delete mode 100644 src/main/java/oldAst/parameter/ParameterListNode.java delete mode 100644 src/main/java/oldAst/parameter/ParameterNode.java delete mode 100644 src/main/java/oldAst/statement/AssignmentStatementNode.java delete mode 100644 src/main/java/oldAst/statement/IfStatementNode.java delete mode 100644 src/main/java/oldAst/statement/ReturnStatementNode.java delete mode 100644 src/main/java/oldAst/statement/StatementNode.java delete mode 100644 src/main/java/oldAst/statement/VariableDeclarationStatementNode.java delete mode 100644 src/main/java/oldAst/statement/WhileStatementNode.java delete mode 100644 src/main/java/oldAst/type/AccessTypeNode.java delete mode 100644 src/main/java/oldAst/type/BaseTypeNode.java delete mode 100644 src/main/java/oldAst/type/EnumAccessTypeNode.java delete mode 100644 src/main/java/oldAst/type/EnumTypeNode.java delete mode 100644 src/main/java/oldAst/type/ReferenceTypeNode.java delete mode 100644 src/main/java/oldAst/type/TypeNode.java diff --git a/src/main/java/ast/ASTNode.java b/src/main/java/ast/ASTNode.java index 3ef48fa..b899ea2 100644 --- a/src/main/java/ast/ASTNode.java +++ b/src/main/java/ast/ASTNode.java @@ -1,12 +1,5 @@ package ast; -//import java.util.List; - -public interface ASTNode { - /** - * Please implement this method to return a list of children of each node. - */ - // public List getChildren(); -} +public interface ASTNode { } diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java index 9690600..231d4c7 100644 --- a/src/main/java/ast/ClassNode.java +++ b/src/main/java/ast/ClassNode.java @@ -1,12 +1,10 @@ package ast; +import ast.type.AccessModifierNode; import bytecode.visitor.ClassVisitor; -import oldAst.ASTNode; -import oldAst.member.ConstructorNode; -import oldAst.member.MemberNode; -import oldAst.member.MethodNode; -import oldAst.type.AccessTypeNode; -import oldAst.type.EnumAccessTypeNode; +import ast.member.ConstructorNode; +import ast.member.MemberNode; +import ast.member.MethodNode; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; @@ -16,13 +14,13 @@ import java.util.List; public class ClassNode implements ASTNode, Visitable { public String identifier; - public AccessTypeNode accessType; + public AccessModifierNode accessType; public List members = new ArrayList<>(); public boolean hasConstructor = false; public ClassNode() {} - public ClassNode(AccessTypeNode accessType, String identifier){ + public ClassNode(AccessModifierNode accessType, String identifier){ this.accessType = accessType; this.identifier = identifier; } @@ -36,7 +34,7 @@ public class ClassNode implements ASTNode, Visitable { public void ensureConstructor(){ if(!hasConstructor) { - ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), identifier); + ConstructorNode constructor = new ConstructorNode(new AccessModifierNode(AccessModifierNode.PUBLIC), identifier); members.add(0,constructor); } } diff --git a/src/main/java/ast/ProgramNode.java b/src/main/java/ast/ProgramNode.java index afed589..f8ad19a 100644 --- a/src/main/java/ast/ProgramNode.java +++ b/src/main/java/ast/ProgramNode.java @@ -1,7 +1,5 @@ package ast; -import ast.ASTNode; -import ast.ClassNode; import bytecode.visitor.ProgramVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; diff --git a/src/main/java/ast/member/ConstructorNode.java b/src/main/java/ast/member/ConstructorNode.java new file mode 100644 index 0000000..380e9ba --- /dev/null +++ b/src/main/java/ast/member/ConstructorNode.java @@ -0,0 +1,21 @@ +package ast.member; + +import bytecode.visitor.MethodVisitor; +import visitor.Visitable; + +public class ConstructorNode extends MethodNode implements Visitable { + public AccessModifierNode accessType; + public String name; + public List parameters = new ArrayList<>(); + public BlockNode body; + + public ConstructorNode(AccessModifierNode accessType, String name) { + this.accessType = accessType; + this.name = name; + } + + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } +} diff --git a/src/main/java/oldAst/member/FieldNode.java b/src/main/java/ast/member/FieldNode.java similarity index 74% rename from src/main/java/oldAst/member/FieldNode.java rename to src/main/java/ast/member/FieldNode.java index 3f34f1d..e942bd3 100644 --- a/src/main/java/oldAst/member/FieldNode.java +++ b/src/main/java/ast/member/FieldNode.java @@ -1,20 +1,18 @@ -package oldAst.member; +package ast.member; -import oldAst.type.AccessTypeNode; -import oldAst.type.TypeNode; import bytecode.visitor.ClassVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; public class FieldNode implements MemberNode, Visitable { - public AccessTypeNode accessTypeNode; + public AccessModifierNode accessTypeNode; public TypeNode type; public String identifier; public FieldNode(){} - public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){ + public FieldNode(AccessModifierNode accessTypeNode, TypeNode type, String name){ this.accessTypeNode = accessTypeNode; this.type = type; this.identifier = name; diff --git a/src/main/java/oldAst/member/MemberNode.java b/src/main/java/ast/member/MemberNode.java similarity index 82% rename from src/main/java/oldAst/member/MemberNode.java rename to src/main/java/ast/member/MemberNode.java index 56e01e3..dd02551 100644 --- a/src/main/java/oldAst/member/MemberNode.java +++ b/src/main/java/ast/member/MemberNode.java @@ -1,9 +1,9 @@ -package oldAst.member; +package ast.member; -import oldAst.ASTNode; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import ast.ASTNode; @JsonIgnoreProperties(ignoreUnknown = true) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) @@ -13,6 +13,4 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; @JsonSubTypes.Type(value = FieldNode.class, name = "Field") } ) -public interface MemberNode extends ASTNode { - -} +public interface MemberNode extends ASTNode {} diff --git a/src/main/java/oldAst/member/MethodNode.java b/src/main/java/ast/member/MethodNode.java similarity index 89% rename from src/main/java/oldAst/member/MethodNode.java rename to src/main/java/ast/member/MethodNode.java index 34d14e1..df50365 100644 --- a/src/main/java/oldAst/member/MethodNode.java +++ b/src/main/java/ast/member/MethodNode.java @@ -1,20 +1,15 @@ -package oldAst.member; +package ast.member; -import oldAst.parameter.ParameterListNode; -import oldAst.statement.StatementNode; -import oldAst.type.AccessTypeNode; - -import java.util.ArrayList; -import java.util.List; - -import oldAst.type.TypeNode; import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; +import java.util.ArrayList; +import java.util.List; + public class MethodNode implements MemberNode, Visitable { - public AccessTypeNode visibility; + public AccessModifierNode accessModifier; public TypeNode type; public String identifier; diff --git a/src/main/java/ast/type/AccessModifierNode.java b/src/main/java/ast/type/AccessModifierNode.java index 5bf3292..0f7596e 100644 --- a/src/main/java/ast/type/AccessModifierNode.java +++ b/src/main/java/ast/type/AccessModifierNode.java @@ -1,14 +1,5 @@ package ast.type; -import oldAst.ASTNode; -import oldAst.type.EnumAccessTypeNode; - -public class AccessModifierNode implements ASTNode { - public EnumAccessModifierNode enumAccessModifierNode; - - public AccessModifierNode(){} - - public AccessModifierNode(EnumAccessModifierNode enumAccessTypeNode) { - this.enumAccessModifierNode = enumAccessTypeNode; - } +public enum AccessModifierNode { + PUBLIC, PRIVATE, PUBLIC_STATIC, PRIVATE_STATIC } diff --git a/src/main/java/ast/type/EnumAccessModifierNode.java b/src/main/java/ast/type/EnumAccessModifierNode.java deleted file mode 100644 index a59e904..0000000 --- a/src/main/java/ast/type/EnumAccessModifierNode.java +++ /dev/null @@ -1,5 +0,0 @@ -package ast.type; - -public enum EnumAccessModifierNode { - PUBLIC, PRIVATE, PUBLIC_STATIC, PRIVATE_STATIC -} diff --git a/src/main/java/ast/type/TypeNode.java b/src/main/java/ast/type/TypeNode.java new file mode 100644 index 0000000..b24628d --- /dev/null +++ b/src/main/java/ast/type/TypeNode.java @@ -0,0 +1,5 @@ +package ast.type; + +public enum TypeNode { + INT, BOOLEAN, CHAR, IDENTIFIER +} diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index eeda3ae..5494255 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -1,7 +1,7 @@ package bytecode; -import oldAst.ProgramNode; -import oldAst.ClassNode; +import ast.ProgramNode; +import ast.ClassNode; import bytecode.visitor.ProgramVisitor; public class ByteCodeGenerator implements ProgramVisitor { diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java index 266f03c..b0afa69 100644 --- a/src/main/java/bytecode/ClassCodeGen.java +++ b/src/main/java/bytecode/ClassCodeGen.java @@ -1,10 +1,10 @@ package bytecode; -import oldAst.ClassNode; -import oldAst.member.FieldNode; -import oldAst.member.MemberNode; -import oldAst.member.MethodNode; -import oldAst.type.BaseTypeNode; +import ast.ClassNode; +import ast.member.FieldNode; +import ast.member.MemberNode; +import ast.member.MethodNode; +import ast.type.BaseTypeNode; import bytecode.visitor.ClassVisitor; import java.io.File; import org.objectweb.asm.ClassWriter; diff --git a/src/main/java/bytecode/Mapper.java b/src/main/java/bytecode/Mapper.java index 91f4a2d..8116565 100644 --- a/src/main/java/bytecode/Mapper.java +++ b/src/main/java/bytecode/Mapper.java @@ -1,10 +1,10 @@ package bytecode; -import oldAst.parameter.ParameterListNode; -import oldAst.parameter.ParameterNode; -import oldAst.type.*; +import ast.parameter.ParameterListNode; +import ast.parameter.ParameterNode; +import ast.type.*; import org.objectweb.asm.Opcodes; -import oldAst.type.BaseTypeNode; +import ast.type.BaseTypeNode; public class Mapper { public int mapAccessTypeToOpcode(AccessTypeNode type) { @@ -20,23 +20,23 @@ public class Mapper { public String generateMethodDescriptor(BaseTypeNode baseTypeNode, ParameterListNode parameterListNode) { String descriptor = "("; for(ParameterNode parameterNode : parameterListNode.parameters) { - descriptor += getTypeChar(EnumTypeNode.INT); + descriptor += getTypeChar(TypeNode.INT); } descriptor += ")"; descriptor += getTypeChar(baseTypeNode.enumType); return descriptor; } - public String getTypeChar(EnumTypeNode enumTypeNode) { + public String getTypeChar(TypeNode enumTypeNode) { String typeChar = ""; switch (enumTypeNode) { - case EnumTypeNode.INT: + case TypeNode.INT: typeChar = "I"; break; - case EnumTypeNode.CHAR: + case TypeNode.CHAR: typeChar = "C"; break; - case EnumTypeNode.BOOLEAN: + case TypeNode.BOOLEAN: typeChar = "Z"; break; } diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java index 4fed389..1cdf1e2 100644 --- a/src/main/java/bytecode/MethodCodeGen.java +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -1,9 +1,9 @@ package bytecode; -import oldAst.member.ConstructorNode; -import oldAst.member.MethodNode; -import oldAst.parameter.ParameterNode; -import oldAst.type.BaseTypeNode; +import ast.member.ConstructorNode; +import ast.member.MethodNode; +import ast.parameter.ParameterNode; +import ast.type.BaseTypeNode; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; diff --git a/src/main/java/bytecode/visitor/MethodVisitor.java b/src/main/java/bytecode/visitor/MethodVisitor.java index bda46ab..70177ce 100644 --- a/src/main/java/bytecode/visitor/MethodVisitor.java +++ b/src/main/java/bytecode/visitor/MethodVisitor.java @@ -1,7 +1,7 @@ package bytecode.visitor; -import oldAst.member.ConstructorNode; -import oldAst.member.MethodNode; +import ast.member.ConstructorNode; +import ast.member.MethodNode; public interface MethodVisitor { void visit(ConstructorNode constructorNode); diff --git a/src/main/java/oldAst/ASTNode.java b/src/main/java/oldAst/ASTNode.java deleted file mode 100644 index 1d8dcf5..0000000 --- a/src/main/java/oldAst/ASTNode.java +++ /dev/null @@ -1,12 +0,0 @@ -package oldAst; - -//import java.util.List; - -public interface ASTNode { - /** - * Please implement this method to return a list of children of each node. - */ - // public List getChildren(); -} - - diff --git a/src/main/java/oldAst/BlockNode.java b/src/main/java/oldAst/BlockNode.java deleted file mode 100644 index 5c8a932..0000000 --- a/src/main/java/oldAst/BlockNode.java +++ /dev/null @@ -1,4 +0,0 @@ -package oldAst; - -public class BlockNode { -} diff --git a/src/main/java/oldAst/ClassNode.java b/src/main/java/oldAst/ClassNode.java deleted file mode 100644 index 34d08dc..0000000 --- a/src/main/java/oldAst/ClassNode.java +++ /dev/null @@ -1,63 +0,0 @@ -package oldAst; - -import oldAst.member.ConstructorNode; -import oldAst.member.MemberNode; -import oldAst.member.MethodNode; -import oldAst.type.AccessTypeNode; -import oldAst.type.EnumAccessTypeNode; - -import java.util.ArrayList; -import java.util.List; - -import bytecode.visitor.ClassVisitor; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class ClassNode implements ASTNode, Visitable { - public String identifier; - public AccessTypeNode accessType; - public List members = new ArrayList<>(); - public boolean hasConstructor = false; - - public ClassNode() {} - - public ClassNode(AccessTypeNode accessType, String identifier){ - this.accessType = accessType; - this.identifier = identifier; - } - - public void addMember(MemberNode member) { - if (member instanceof ConstructorNode) { - this.hasConstructor = true; - } - members.add(member); - } - - public void ensureConstructor(){ - if(!hasConstructor) { - ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), identifier); - members.add(0,constructor); - } - } - - public List getMethods(){ - List methods = new ArrayList<>(); - for (MemberNode member : members) { - if (member instanceof MethodNode methodNode) { - methods.add(methodNode); - } - } - return methods; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } - - @Override - public void accept(ClassVisitor classVisitor) { - classVisitor.visit(this); - } -} diff --git a/src/main/java/oldAst/ProgramNode.java b/src/main/java/oldAst/ProgramNode.java deleted file mode 100644 index beb97d9..0000000 --- a/src/main/java/oldAst/ProgramNode.java +++ /dev/null @@ -1,27 +0,0 @@ -package oldAst; - -import java.util.ArrayList; -import java.util.List; - -import bytecode.visitor.ProgramVisitor; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class ProgramNode implements ASTNode, Visitable{ - public List classes = new ArrayList<>(); - - public void addClass(ClassNode classNode) { - classes.add(classNode); - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } - - @Override - public void accept(ProgramVisitor programVisitor) { - programVisitor.visit(this); - } -} \ No newline at end of file diff --git a/src/main/java/oldAst/expression/BinaryExpressionNode.java b/src/main/java/oldAst/expression/BinaryExpressionNode.java deleted file mode 100644 index bdb1467..0000000 --- a/src/main/java/oldAst/expression/BinaryExpressionNode.java +++ /dev/null @@ -1,22 +0,0 @@ -package oldAst.expression; - -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class BinaryExpressionNode implements ExpressionNode, Visitable { - public ExpressionNode left; - public ExpressionNode right; - public ExpresssionOperator operator; // Stores the operator as a string (e.g., "+", "-", "&&") - - public BinaryExpressionNode(ExpressionNode left, ExpressionNode right, ExpresssionOperator operator) { - this.left = left; - this.right = right; - this.operator = operator; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} \ No newline at end of file diff --git a/src/main/java/oldAst/expression/ExpressionNode.java b/src/main/java/oldAst/expression/ExpressionNode.java deleted file mode 100644 index 27c43e3..0000000 --- a/src/main/java/oldAst/expression/ExpressionNode.java +++ /dev/null @@ -1,22 +0,0 @@ -package oldAst.expression; - -import oldAst.ASTNode; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonSubTypes; -import com.fasterxml.jackson.annotation.JsonTypeInfo; -import visitor.Visitable; - -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) -@JsonSubTypes({ - @JsonSubTypes.Type(value = BinaryExpressionNode.class, name = "Binary"), - @JsonSubTypes.Type(value = This.class, name = "This"), - @JsonSubTypes.Type(value = InstVar.class, name = "InstVar"), - @JsonSubTypes.Type(value = IdentifierExpressionNode.class, name = "Identifier"), - @JsonSubTypes.Type(value = LiteralNode.class, name = "Literal"), - @JsonSubTypes.Type(value = UnaryExpressionNode.class, name = "Unary")} -) - -public interface ExpressionNode extends ASTNode, Visitable { - -} diff --git a/src/main/java/oldAst/expression/ExpresssionOperator.java b/src/main/java/oldAst/expression/ExpresssionOperator.java deleted file mode 100644 index dfd7ef8..0000000 --- a/src/main/java/oldAst/expression/ExpresssionOperator.java +++ /dev/null @@ -1,14 +0,0 @@ -package oldAst.expression; - -public enum ExpresssionOperator { - DOT, // . NICHT MEHR GEBRAUCHT - PLUS, // + - MINUS, // - - MULTIPLY, // * - DIVIDE, // / - NOT, // ! - ASSIGNMENT, // = (NICHT MEHR GEBRAUCHT ??) - EQUALS, // == - UNEQUALS, // != - ERROR //TODO: Remove This -} diff --git a/src/main/java/oldAst/expression/IdentifierExpressionNode.java b/src/main/java/oldAst/expression/IdentifierExpressionNode.java deleted file mode 100644 index 4552a82..0000000 --- a/src/main/java/oldAst/expression/IdentifierExpressionNode.java +++ /dev/null @@ -1,22 +0,0 @@ -package oldAst.expression; - -import oldAst.type.TypeNode; -import semantic.SemanticVisitor; - -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class IdentifierExpressionNode implements ExpressionNode, Visitable { - public String name; - public TypeNode type; - - - public IdentifierExpressionNode(String name) { - this.name = name; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} diff --git a/src/main/java/oldAst/expression/InstVar.java b/src/main/java/oldAst/expression/InstVar.java deleted file mode 100644 index dc9a204..0000000 --- a/src/main/java/oldAst/expression/InstVar.java +++ /dev/null @@ -1,24 +0,0 @@ -package oldAst.expression; - -import oldAst.type.TypeNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class InstVar implements ExpressionNode, Visitable { - public String identifier; - public ExpressionNode expression; - public TypeNode type; - - public InstVar(){} - - public InstVar(ExpressionNode expression, String identifier) { - this.identifier = identifier; - this.expression = expression; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} diff --git a/src/main/java/oldAst/expression/LiteralNode.java b/src/main/java/oldAst/expression/LiteralNode.java deleted file mode 100644 index c53f04a..0000000 --- a/src/main/java/oldAst/expression/LiteralNode.java +++ /dev/null @@ -1,31 +0,0 @@ -package oldAst.expression; - -import oldAst.type.TypeNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; - -public class LiteralNode implements ExpressionNode { - - int value; - private TypeNode type; - - public LiteralNode(){} - - public LiteralNode(int value) { - this.value = value; - } - - public TypeNode getType() { - return type; - } - - public void setType(TypeNode type) { - this.type = type; - } - - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} diff --git a/src/main/java/oldAst/expression/This.java b/src/main/java/oldAst/expression/This.java deleted file mode 100644 index 1a0fbb3..0000000 --- a/src/main/java/oldAst/expression/This.java +++ /dev/null @@ -1,26 +0,0 @@ -package oldAst.expression; - -import oldAst.ASTNode; -import oldAst.type.ReferenceTypeNode; -import oldAst.type.TypeNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; - -public class This implements ASTNode, ExpressionNode { - private TypeNode type; - - public This(){} - - public This(String className) { - type = new ReferenceTypeNode(className); - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } - - public TypeNode getType() { - return type; - } -} diff --git a/src/main/java/oldAst/expression/UnaryExpressionNode.java b/src/main/java/oldAst/expression/UnaryExpressionNode.java deleted file mode 100644 index 17b0a9b..0000000 --- a/src/main/java/oldAst/expression/UnaryExpressionNode.java +++ /dev/null @@ -1,22 +0,0 @@ -package oldAst.expression; - -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class UnaryExpressionNode implements ExpressionNode, Visitable { - public ExpressionNode expression; - public String operator; // Stores the operator (e.g., "-", "!") - - public UnaryExpressionNode(ExpressionNode expression, String operator) { - this.expression = expression; - this.operator = operator; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} - - diff --git a/src/main/java/oldAst/member/ConstructorNode.java b/src/main/java/oldAst/member/ConstructorNode.java deleted file mode 100644 index 9c3bc08..0000000 --- a/src/main/java/oldAst/member/ConstructorNode.java +++ /dev/null @@ -1,16 +0,0 @@ -package oldAst.member; - -import oldAst.type.AccessTypeNode; -import bytecode.visitor.MethodVisitor; -import visitor.Visitable; - -public class ConstructorNode extends MethodNode implements Visitable { - public ConstructorNode(AccessTypeNode visibility, String name) { - super(visibility, name); - } - - @Override - public void accept(MethodVisitor methodVisitor) { - methodVisitor.visit(this); - } -} diff --git a/src/main/java/oldAst/parameter/ParameterListNode.java b/src/main/java/oldAst/parameter/ParameterListNode.java deleted file mode 100644 index b241791..0000000 --- a/src/main/java/oldAst/parameter/ParameterListNode.java +++ /dev/null @@ -1,16 +0,0 @@ -package oldAst.parameter; - -import oldAst.ASTNode; - -import java.util.ArrayList; -import java.util.List; - -public class ParameterListNode implements ASTNode { - public List parameters = new ArrayList<>(); - - public ParameterListNode() {} - - public ParameterListNode(List parameters){ - this.parameters = parameters; - } -} diff --git a/src/main/java/oldAst/parameter/ParameterNode.java b/src/main/java/oldAst/parameter/ParameterNode.java deleted file mode 100644 index 976375c..0000000 --- a/src/main/java/oldAst/parameter/ParameterNode.java +++ /dev/null @@ -1,16 +0,0 @@ -package oldAst.parameter; - -import oldAst.ASTNode; -import oldAst.type.TypeNode; - -public class ParameterNode implements ASTNode { - public TypeNode type; - public String identifier; - - public ParameterNode(){} - - public ParameterNode(TypeNode type, String identifier) { - this.type = type; - this.identifier = identifier; - } -} \ No newline at end of file diff --git a/src/main/java/oldAst/statement/AssignmentStatementNode.java b/src/main/java/oldAst/statement/AssignmentStatementNode.java deleted file mode 100644 index 556fe5e..0000000 --- a/src/main/java/oldAst/statement/AssignmentStatementNode.java +++ /dev/null @@ -1,24 +0,0 @@ -package oldAst.statement; - -import oldAst.expression.ExpressionNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - -public class AssignmentStatementNode extends StatementNode implements Visitable { - public ExpressionNode expressionLeft; - public ExpressionNode expressionRight; -// public TypeNode type; - - public AssignmentStatementNode(){} - - public AssignmentStatementNode(ExpressionNode expressionLeft, ExpressionNode expressionRight) { - this.expressionLeft = expressionLeft; - this.expressionRight = expressionRight; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} diff --git a/src/main/java/oldAst/statement/IfStatementNode.java b/src/main/java/oldAst/statement/IfStatementNode.java deleted file mode 100644 index 6050e7a..0000000 --- a/src/main/java/oldAst/statement/IfStatementNode.java +++ /dev/null @@ -1,22 +0,0 @@ -package oldAst.statement; - -import oldAst.expression.ExpressionNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; - -public class IfStatementNode extends StatementNode { - public ExpressionNode condition; - public StatementNode thenStatement; - public StatementNode elseStatement; - - public IfStatementNode(ExpressionNode condition, StatementNode thenStatement, StatementNode elseStatement) { - this.condition = condition; - this.thenStatement = thenStatement; - this.elseStatement = elseStatement; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} \ No newline at end of file diff --git a/src/main/java/oldAst/statement/ReturnStatementNode.java b/src/main/java/oldAst/statement/ReturnStatementNode.java deleted file mode 100644 index 21db22d..0000000 --- a/src/main/java/oldAst/statement/ReturnStatementNode.java +++ /dev/null @@ -1,18 +0,0 @@ -package oldAst.statement; - -import oldAst.expression.ExpressionNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; - -public class ReturnStatementNode extends StatementNode { - public ExpressionNode expression; - - public ReturnStatementNode(ExpressionNode expression) { - this.expression = expression; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} \ No newline at end of file diff --git a/src/main/java/oldAst/statement/StatementNode.java b/src/main/java/oldAst/statement/StatementNode.java deleted file mode 100644 index 6ae0192..0000000 --- a/src/main/java/oldAst/statement/StatementNode.java +++ /dev/null @@ -1,20 +0,0 @@ -package oldAst.statement; - -import oldAst.ASTNode; -import visitor.Visitable; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonSubTypes; -import com.fasterxml.jackson.annotation.JsonTypeInfo; - -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) -@JsonSubTypes({ - @JsonSubTypes.Type(value = IfStatementNode.class, name = "IF"), - @JsonSubTypes.Type(value = AssignmentStatementNode.class, name = "Assignment"), - @JsonSubTypes.Type(value = VariableDeclarationStatementNode.class, name = "VariableDeclaration") } -) - -public abstract class StatementNode implements ASTNode, Visitable { -} - diff --git a/src/main/java/oldAst/statement/VariableDeclarationStatementNode.java b/src/main/java/oldAst/statement/VariableDeclarationStatementNode.java deleted file mode 100644 index 854b9e1..0000000 --- a/src/main/java/oldAst/statement/VariableDeclarationStatementNode.java +++ /dev/null @@ -1,25 +0,0 @@ -package oldAst.statement; - -import oldAst.expression.ExpressionNode; -import oldAst.type.TypeNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; - -public class VariableDeclarationStatementNode extends StatementNode { - public TypeNode type; - public String identifier; - public ExpressionNode expression; - - public VariableDeclarationStatementNode(){} - - public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) { - this.type = type; - this.identifier = identifier; - this.expression = expression; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} \ No newline at end of file diff --git a/src/main/java/oldAst/statement/WhileStatementNode.java b/src/main/java/oldAst/statement/WhileStatementNode.java deleted file mode 100644 index b3fcee5..0000000 --- a/src/main/java/oldAst/statement/WhileStatementNode.java +++ /dev/null @@ -1,20 +0,0 @@ -package oldAst.statement; - -import oldAst.expression.ExpressionNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; - -public class WhileStatementNode extends StatementNode { - public ExpressionNode condition; - public StatementNode body; - - public WhileStatementNode(ExpressionNode condition, StatementNode body) { - this.condition = condition; - this.body = body; - } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } -} diff --git a/src/main/java/oldAst/type/AccessTypeNode.java b/src/main/java/oldAst/type/AccessTypeNode.java deleted file mode 100644 index e1f884f..0000000 --- a/src/main/java/oldAst/type/AccessTypeNode.java +++ /dev/null @@ -1,13 +0,0 @@ -package oldAst.type; - -import oldAst.ASTNode; - -public class AccessTypeNode implements ASTNode { - public EnumAccessTypeNode enumAccessTypeNode; - - public AccessTypeNode(){} - - public AccessTypeNode(EnumAccessTypeNode enumAccessTypeNode) { - this.enumAccessTypeNode = enumAccessTypeNode; - } -} diff --git a/src/main/java/oldAst/type/BaseTypeNode.java b/src/main/java/oldAst/type/BaseTypeNode.java deleted file mode 100644 index 2a1b23d..0000000 --- a/src/main/java/oldAst/type/BaseTypeNode.java +++ /dev/null @@ -1,28 +0,0 @@ -package oldAst.type; - -import oldAst.ASTNode; - -public class BaseTypeNode implements ASTNode, TypeNode { - - public EnumTypeNode enumType; - - public BaseTypeNode(){} - - public BaseTypeNode(EnumTypeNode enumType) { - this.enumType = enumType; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - BaseTypeNode other = (BaseTypeNode) obj; - if (enumType != other.enumType) - return false; - return true; - } -} diff --git a/src/main/java/oldAst/type/EnumAccessTypeNode.java b/src/main/java/oldAst/type/EnumAccessTypeNode.java deleted file mode 100644 index c8625e5..0000000 --- a/src/main/java/oldAst/type/EnumAccessTypeNode.java +++ /dev/null @@ -1,5 +0,0 @@ -package oldAst.type; - -public enum EnumAccessTypeNode { - PUBLIC, PRIVATE -} diff --git a/src/main/java/oldAst/type/EnumTypeNode.java b/src/main/java/oldAst/type/EnumTypeNode.java deleted file mode 100644 index 1e84e81..0000000 --- a/src/main/java/oldAst/type/EnumTypeNode.java +++ /dev/null @@ -1,5 +0,0 @@ -package oldAst.type; - -public enum EnumTypeNode { - INT, BOOLEAN, CHAR -} diff --git a/src/main/java/oldAst/type/ReferenceTypeNode.java b/src/main/java/oldAst/type/ReferenceTypeNode.java deleted file mode 100644 index b73f4dd..0000000 --- a/src/main/java/oldAst/type/ReferenceTypeNode.java +++ /dev/null @@ -1,36 +0,0 @@ -package oldAst.type; - -import oldAst.ASTNode; - -public class ReferenceTypeNode implements ASTNode, TypeNode { - - private String identifier; - - public ReferenceTypeNode() {} - - public ReferenceTypeNode(String identifier) { - this.identifier = identifier; - } - - public String getIdentifier() { - return identifier; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - ReferenceTypeNode other = (ReferenceTypeNode) obj; - if (identifier == null) { - if (other.identifier != null) - return false; - } else if (!identifier.equals(other.identifier)) - return false; - return true; - } - -} diff --git a/src/main/java/oldAst/type/TypeNode.java b/src/main/java/oldAst/type/TypeNode.java deleted file mode 100644 index 7248755..0000000 --- a/src/main/java/oldAst/type/TypeNode.java +++ /dev/null @@ -1,18 +0,0 @@ -package oldAst.type; - -import oldAst.ASTNode; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonSubTypes; -import com.fasterxml.jackson.annotation.JsonTypeInfo; - -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) -@JsonSubTypes({ - @JsonSubTypes.Type(value = BaseTypeNode.class, name = "Base"), - - @JsonSubTypes.Type(value = ReferenceTypeNode.class, name = "Reference") } -) - -public interface TypeNode extends ASTNode { -} \ No newline at end of file diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 69cad70..5ac218f 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -1,21 +1,14 @@ package parser.astBuilder; -import oldAst.*; -import oldAst.expression.*; -import oldAst.member.FieldNode; -import oldAst.member.MemberNode; -import oldAst.member.MethodNode; -import oldAst.parameter.ParameterListNode; -import oldAst.parameter.ParameterNode; -import oldAst.statement.*; -import oldAst.type.*; +import ast.*; +import ast.member.FieldNode; +import ast.member.MemberNode; +import ast.member.MethodNode; +import ast.type.*; import org.antlr.v4.runtime.tree.TerminalNode; import java.util.ArrayList; -import java.util.List; import parser.generated.*; -import parser.generated.SimpleJavaParser.LiteralContext; -import oldAst.type.BaseTypeNode; public class ASTBuilder extends SimpleJavaBaseVisitor { @Override @@ -29,8 +22,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { - ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText()); - classNode.identifier = ctx.IDENTIFIER().getText(); + ClassNode classNode = new ClassNode((AccessModifierNode) visit(ctx.AccessModifier()),ctx.Identifier().getText()); for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { classNode.addMember((MemberNode) visit(member)); } @@ -38,179 +30,4 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return classNode; } - @Override - public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { - AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType()); - TypeNode type = (TypeNode) visit(ctx.type()); - String identifier = ctx.IDENTIFIER().getText(); - return new FieldNode(accessType, type, identifier); - } - - @Override - public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { - AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType()); - TypeNode returnType = (TypeNode) visit(ctx.type()); - String methodName = ctx.IDENTIFIER().getText(); - - ParameterListNode parameterListNode = null; - if(ctx.parameterList() != null) { - parameterListNode = (ParameterListNode) visit(ctx.parameterList()); - } - List statements = new ArrayList<>(); - for (SimpleJavaParser.StatementContext stmtCtx : ctx.statement()) { - statements.add((StatementNode) visit(stmtCtx)); - } - - MethodNode method = new MethodNode(accessType,returnType, methodName, parameterListNode, statements); - - return method; - } - - @Override - public ASTNode visitParameterList(SimpleJavaParser.ParameterListContext ctx) { - List parameters = new ArrayList<>(); - for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) { - parameters.add((ParameterNode) visitParameter(paramCtx)); - } - return new ParameterListNode(parameters); - } - - @Override - public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { - TypeNode typeNode = (TypeNode) visit(ctx.type()); - String identifier = ctx.IDENTIFIER().getText(); - return new ParameterNode(typeNode, identifier); - } - - @Override - public ASTNode visitType(SimpleJavaParser.TypeContext ctx) { - String typeStr = ctx.getText(); - switch (typeStr) { - case "int": - return new BaseTypeNode(EnumTypeNode.INT); - case "boolean": - return new BaseTypeNode(EnumTypeNode.BOOLEAN); - case "char": - return new BaseTypeNode(EnumTypeNode.CHAR); - default: - throw new IllegalArgumentException("Unsupported type: " + typeStr); - } - } - - @Override - public ASTNode visitAccessType(SimpleJavaParser.AccessTypeContext ctx) { - String typeStr = ctx.getText(); - switch (typeStr) { - case "public": - return new AccessTypeNode(EnumAccessTypeNode.PUBLIC); - case "private": - return new AccessTypeNode(EnumAccessTypeNode.PRIVATE); - default: - throw new IllegalArgumentException("Unsupported type: " + typeStr); - } - } - - @Override - public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) { - if (ctx.variableDeclarationStatement() != null) { - return visitVariableDeclarationStatement(ctx.variableDeclarationStatement()); - } else if (ctx.assignmentStatement() != null) { - return visitAssignmentStatement(ctx.assignmentStatement()); - } - - return null; - } - - @Override - public ASTNode visitVariableDeclarationStatement(SimpleJavaParser.VariableDeclarationStatementContext ctx) { - TypeNode type = (TypeNode) visit(ctx.type()); - String identifier = ctx.IDENTIFIER().getText(); - ExpressionNode expression = null; - if (ctx.expression() != null) { - expression = (ExpressionNode) visit(ctx.expression()); - } - return new VariableDeclarationStatementNode(type, identifier, expression); - } - - @Override - public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { - ExpressionNode expression = (ExpressionNode) visit(ctx.expression()); - return null; - } - - @Override - public ASTNode visitVar(SimpleJavaParser.VarContext ctx) { - return null; - } - - @Override - public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) { - ExpressionNode condition = (ExpressionNode) visit(ctx.expression()); - StatementNode thenStatement = (StatementNode) visit(ctx.statement(0)); // The 'then' branch - StatementNode elseStatement = null; - if (ctx.statement().size() > 1) { - elseStatement = (StatementNode) visit(ctx.statement(1)); // The 'else' branch, if present - } - return new IfStatementNode(condition, thenStatement, elseStatement); - } - - @Override - public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) { - ExpressionNode condition = (ExpressionNode) visit(ctx.expression()); // Visit the condition part of the while statement - StatementNode body = (StatementNode) visit(ctx.statement()); // Visit the body part of the while statement - return new WhileStatementNode(condition, body); - } - - @Override - public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) { - ExpressionNode expression = null; - if (ctx.expression() != null) { - expression = (ExpressionNode) visit(ctx.expression()); // Visit the expression part of the return statement, if it exists - } - return new ReturnStatementNode(expression); - } - - @Override - public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) { - // Handle binary operations - if (ctx.getChildCount() == 3 && ctx.getChild(1) instanceof TerminalNode) { - ExpressionNode left = (ExpressionNode) visit(ctx.expression(0)); - ExpressionNode right = (ExpressionNode) visit(ctx.expression(1)); - return new BinaryExpressionNode(left, right, ExpresssionOperator.ERROR); - } - // Handle unary operations - else if (ctx.getChildCount() == 2) { - String operator = ctx.getChild(0).getText(); - ExpressionNode expression = (ExpressionNode) visit(ctx.expression(0)); - return new UnaryExpressionNode(expression, operator); - } - // Handle parentheses - else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) { - return visit(ctx.expression(0)); // Simply return the inner expression - } - // Handle literals and identifiers - else if (ctx.literal() != null) { - return visit(ctx.literal()); - } - else if (ctx.IDENTIFIER() != null) { - return new IdentifierExpressionNode(ctx.IDENTIFIER().getText()); - } - - return null; // Return null or throw an exception if no valid expression found - } - - @Override - public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) { - LiteralContext literalContext = (LiteralContext) ctx; - try { - int intValue = Integer.parseInt(literalContext.getText()); - LiteralNode literalNode = new LiteralNode(intValue); - - literalNode.setType(new BaseTypeNode(EnumTypeNode.INT)); - return literalNode; - } catch (NumberFormatException ignored) {} - - return null; // Return null or throw an exception if no valid expression found - } - }