Merge branch 'NewParser' into code-generator
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
i22007 2024-06-25 17:48:47 -04:00
commit 70a3788030
10 changed files with 64 additions and 24 deletions

View File

@ -13,17 +13,17 @@ public class ConstructorNode extends MethodNode implements Visitable {
public AccessModifierNode accessType; public AccessModifierNode accessType;
public String identifier; public String identifier;
public List<ParameterNode> parameters = new ArrayList<>(); public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode body; public BlockNode block;
public ConstructorNode(AccessModifierNode accessType, String identifier) { public ConstructorNode(AccessModifierNode accessType, String identifier) {
this.accessType = accessType; this.accessType = accessType;
this.identifier = identifier; this.identifier = identifier;
} }
public ConstructorNode(String accessType, String identifier, BlockNode body) { public ConstructorNode(String accessType, String identifier, BlockNode block) {
this.accessType = new AccessModifierNode(accessType); this.accessType = new AccessModifierNode(accessType);
this.identifier = identifier; this.identifier = identifier;
this.body = body; this.block = block;
} }
public void addParameter(ParameterNode parameterNode) { public void addParameter(ParameterNode parameterNode) {

View File

@ -12,6 +12,7 @@ public class DecrementNode implements IStatementExpressionNode {
public AssignableNode assignableExpression; public AssignableNode assignableExpression;
public DecrementNode(CrementType crementType, AssignableNode assignableExpression) { public DecrementNode(CrementType crementType, AssignableNode assignableExpression) {
this.crementType = crementType;
this.assignableExpression = assignableExpression; this.assignableExpression = assignableExpression;
} }

View File

@ -11,6 +11,7 @@ public class IncrementNode implements IStatementExpressionNode {
public AssignableNode assignableExpression; public AssignableNode assignableExpression;
public IncrementNode(CrementType crementType, AssignableNode assignableExpression) { public IncrementNode(CrementType crementType, AssignableNode assignableExpression) {
this.crementType = crementType;
this.assignableExpression = assignableExpression; this.assignableExpression = assignableExpression;
} }

View File

@ -13,7 +13,7 @@ public class MethodCallNode implements IStatementNode {
public TargetNode target; public TargetNode target;
public List<ChainedMethodNode> chainedMethods = new ArrayList<>(); public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
public String identifier; public String identifier;
public List<IExpressionNode> expressions = new ArrayList<>(); public List<IExpressionNode> parameters = new ArrayList<>();
public MethodCallNode(TargetNode target, String identifier) { public MethodCallNode(TargetNode target, String identifier) {
this.target = target; this.target = target;
@ -24,8 +24,8 @@ public class MethodCallNode implements IStatementNode {
chainedMethods.add(chainedMethode); chainedMethods.add(chainedMethode);
} }
public void addExpression(IExpressionNode expression) { public void addExpression(IExpressionNode parameter) {
expressions.add(expression); parameters.add(parameter);
} }
@Override @Override

View File

@ -16,6 +16,9 @@ public class BlockNode implements ASTNode, Visitable {
public void addStatement(IStatementNode statement) { public void addStatement(IStatementNode statement) {
statements.add(statement); statements.add(statement);
if(statement instanceof ReturnNode) {
hasReturnStatement = true;
}
} }
@Override @Override

View File

@ -10,8 +10,7 @@ import ast.expressions.binaryexpressions.NonCalculationNode;
import ast.expressions.unaryexpressions.MemberAccessNode; import ast.expressions.unaryexpressions.MemberAccessNode;
import ast.expressions.unaryexpressions.NotNode; import ast.expressions.unaryexpressions.NotNode;
import ast.expressions.unaryexpressions.UnaryNode; import ast.expressions.unaryexpressions.UnaryNode;
import ast.members.ConstructorNode; import ast.members.*;
import ast.members.MemberNode;
import ast.parameters.ParameterNode; import ast.parameters.ParameterNode;
import ast.statementexpressions.AssignNode; import ast.statementexpressions.AssignNode;
import ast.statementexpressions.AssignableNode; import ast.statementexpressions.AssignableNode;
@ -23,6 +22,7 @@ import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNod
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*; import ast.statements.*;
import ast.type.AccessModifierNode;
import ast.type.type.*; import ast.type.type.*;
import ast.type.EnumValueNode; import ast.type.EnumValueNode;
import ast.type.ValueNode; import ast.type.ValueNode;
@ -55,15 +55,47 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { if(ctx.parameterList() != null) {
constructorNode.addParameter((ParameterNode) visit(parameter)); for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
constructorNode.addParameter((ParameterNode) visit(parameter));
}
} }
return constructorNode; return constructorNode;
} }
@Override
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
if(ctx.MainMethodDeclaration() != null) {
return new MainMethodNode((BlockNode) visit(ctx.blockStatement()));
} else {
if(ctx.type() != null) {
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
methodNode.addParameter((ParameterNode) visit(parameter));
}
}
return methodNode;
} else {
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
methodNode.addParameter((ParameterNode) visit(parameter));
}
}
return methodNode;
}
}
}
@Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
}
@Override @Override
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
return new ParameterNode(new TypeNode(ctx.type().getText()), ctx.Identifier().getText()); return new ParameterNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} }
@Override @Override
@ -93,7 +125,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) { public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) {
return new LocalVariableDeclarationNode(new TypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (IExpressionNode) visit(ctx.expression())); return new LocalVariableDeclarationNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (IExpressionNode) visit(ctx.expression()));
} }
@Override @Override
@ -102,6 +134,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
for(SimpleJavaParser.StatementContext statement : ctx.statement()) { for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
blockNode.addStatement((IStatementNode) visit(statement)); blockNode.addStatement((IStatementNode) visit(statement));
} }
if(!blockNode.hasReturnStatement) {
blockNode.addStatement(new ReturnNode(null));
}
return blockNode; return blockNode;
} }

View File

@ -228,10 +228,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
return null; return null;
} }
@Override /*@Override
public TypeCheckResult analyze(ForNode toCheck) { public TypeCheckResult analyze(ForNode toCheck) {
return null; return null;
} }*/
@Override @Override
public TypeCheckResult analyze(AssignNode toCheck) { public TypeCheckResult analyze(AssignNode toCheck) {

View File

@ -38,7 +38,7 @@ public interface SemanticVisitor {
TypeCheckResult analyze(ElseNode toCheck); TypeCheckResult analyze(ElseNode toCheck);
TypeCheckResult analyze(ForNode toCheck); //TypeCheckResult analyze(ForNode toCheck);
TypeCheckResult analyze(AssignNode toCheck); TypeCheckResult analyze(AssignNode toCheck);

View File

@ -42,7 +42,7 @@ public class Mocker {
//Statements //Statements
//Block //Block
methodNode.block = new ast.block.BlockStatementNode(); methodNode.block = new ast.statements.BlockNode();
c.members.add(methodNode); c.members.add(methodNode);
@ -57,7 +57,7 @@ public class Mocker {
ClassNode c = new ClassNode(); ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode(); MethodNode methodNode = new MethodNode();
methodNode.block = new ast.block.BlockStatementNode(); methodNode.block = new ast.statements.BlockNode();
methodNode.setType(new BaseType(TypeEnum.INT)); methodNode.setType(new BaseType(TypeEnum.INT));
methodNode.setIdentifier("testMethod"); methodNode.setIdentifier("testMethod");
@ -65,7 +65,7 @@ public class Mocker {
c.members.add(methodNode); c.members.add(methodNode);
MethodNode methodNode1 = new MethodNode(); MethodNode methodNode1 = new MethodNode();
methodNode1.block = new ast.block.BlockStatementNode(); methodNode1.block = new ast.statements.BlockNode();
methodNode1.setType(new BaseType(TypeEnum.INT)); methodNode1.setType(new BaseType(TypeEnum.INT));
methodNode1.setIdentifier("testMethod"); methodNode1.setIdentifier("testMethod");
@ -83,13 +83,13 @@ public class Mocker {
ClassNode c = new ClassNode(); ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode(); MethodNode methodNode = new MethodNode();
methodNode.block = new ast.block.BlockStatementNode(); methodNode.block = new ast.statements.BlockNode();
methodNode.setIdentifier("testMethod"); methodNode.setIdentifier("testMethod");
c.members.add(methodNode); c.members.add(methodNode);
MethodNode methodNode1 = new MethodNode(); MethodNode methodNode1 = new MethodNode();
methodNode1.block = new ast.block.BlockStatementNode(); methodNode1.block = new ast.statements.BlockNode();
methodNode1.setIdentifier("testMethod1"); methodNode1.setIdentifier("testMethod1");
c.members.add(methodNode1); c.members.add(methodNode1);

View File

@ -1,10 +1,10 @@
package semantic; package semantic;
import ast.*; import ast.*;
import ast.member.FieldNode; import ast.members.FieldNode;
import ast.member.MemberNode; import ast.members.MemberNode;
import ast.member.MethodNode; import ast.members.MethodNode;
import ast.parameter.ParameterNode; import ast.parameters.ParameterNode;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import semantic.exeptions.AlreadyDeclearedException; import semantic.exeptions.AlreadyDeclearedException;