Deleted old grammar
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
8094a93582
commit
330b92a79f
@ -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<ASTNode> getChildren();
|
||||
}
|
||||
public interface ASTNode { }
|
||||
|
||||
|
||||
|
@ -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<MemberNode> 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);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package ast;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.ClassNode;
|
||||
import bytecode.visitor.ProgramVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
21
src/main/java/ast/member/ConstructorNode.java
Normal file
21
src/main/java/ast/member/ConstructorNode.java
Normal file
@ -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<ParameterNode> 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);
|
||||
}
|
||||
}
|
@ -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;
|
@ -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 {}
|
@ -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;
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package ast.type;
|
||||
|
||||
public enum EnumAccessModifierNode {
|
||||
PUBLIC, PRIVATE, PUBLIC_STATIC, PRIVATE_STATIC
|
||||
}
|
5
src/main/java/ast/type/TypeNode.java
Normal file
5
src/main/java/ast/type/TypeNode.java
Normal file
@ -0,0 +1,5 @@
|
||||
package ast.type;
|
||||
|
||||
public enum TypeNode {
|
||||
INT, BOOLEAN, CHAR, IDENTIFIER
|
||||
}
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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<ASTNode> getChildren();
|
||||
}
|
||||
|
||||
|
@ -1,4 +0,0 @@
|
||||
package oldAst;
|
||||
|
||||
public class BlockNode {
|
||||
}
|
@ -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<MemberNode> 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<MethodNode> getMethods(){
|
||||
List<MethodNode> 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);
|
||||
}
|
||||
}
|
@ -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<ClassNode> 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
||||
}
|
@ -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
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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<ParameterNode> parameters = new ArrayList<>();
|
||||
|
||||
public ParameterListNode() {}
|
||||
|
||||
public ParameterListNode(List<ParameterNode> parameters){
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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 {
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package oldAst.type;
|
||||
|
||||
public enum EnumAccessTypeNode {
|
||||
PUBLIC, PRIVATE
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package oldAst.type;
|
||||
|
||||
public enum EnumTypeNode {
|
||||
INT, BOOLEAN, CHAR
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
}
|
@ -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<ASTNode> {
|
||||
@Override
|
||||
@ -29,8 +22,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
@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<ASTNode> {
|
||||
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<StatementNode> 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<ParameterNode> 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
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user