Merge branch 'NewParser' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-06-27 08:31:56 +02:00
commit bf365194be
8 changed files with 69 additions and 37 deletions

View File

@ -8,17 +8,13 @@ import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class BlockNode implements ASTNode, Visitable { public class BlockNode implements IStatementNode, Visitable {
public List<IStatementNode> statements = new ArrayList<>(); public List<IStatementNode> statements = new ArrayList<>();
public Boolean hasReturnStatement = false;
public BlockNode() {} public BlockNode() {}
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

@ -1,8 +1,10 @@
package ast.statements; package ast.statements;
import ast.ASTNode; import ast.ASTNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class ElseNode implements ASTNode { public class ElseNode implements IStatementNode {
BlockNode block; BlockNode block;
public ElseNode(BlockNode block) { public ElseNode(BlockNode block) {
@ -10,4 +12,8 @@ public class ElseNode implements ASTNode {
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;
}
} }

View File

@ -1,11 +1,13 @@
package ast.statements; package ast.statements;
import ast.ASTNode; import ast.ASTNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class IfElseNode implements ASTNode { public class IfElseNode implements IStatementNode {
IfNode ifStatement; IfNode ifStatement;
List<IfNode> elseIfStatements = new ArrayList<>(); List<IfNode> elseIfStatements = new ArrayList<>();
ElseNode elseStatement; ElseNode elseStatement;
@ -19,4 +21,8 @@ public class IfElseNode implements ASTNode {
elseIfStatements.add(elseIfStament); elseIfStatements.add(elseIfStament);
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;
}
} }

View File

@ -2,8 +2,10 @@ package ast.statements;
import ast.ASTNode; import ast.ASTNode;
import ast.expressions.IExpressionNode; import ast.expressions.IExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class IfNode implements ASTNode { public class IfNode implements IStatementNode {
IExpressionNode expression; IExpressionNode expression;
BlockNode block; BlockNode block;
@ -11,4 +13,9 @@ public class IfNode implements ASTNode {
this.expression = expression; this.expression = expression;
this.block = block; this.block = block;
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;
}
} }

View File

@ -1,2 +0,0 @@
package ast.statements;public class StatementNode {
}

View File

@ -2,8 +2,10 @@ package ast.statements;
import ast.ASTNode; import ast.ASTNode;
import ast.expressions.IExpressionNode; import ast.expressions.IExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class WhileNode extends StatementNode implements ASTNode { public class WhileNode implements IStatementNode {
IExpressionNode expression; IExpressionNode expression;
BlockNode block; BlockNode block;
@ -11,4 +13,14 @@ public class WhileNode extends StatementNode implements ASTNode {
this.expression = expression; this.expression = expression;
this.block = block; this.block = block;
} }
public void test() {
return;
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;
}
} }

View File

@ -108,6 +108,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return visitBlockStatement(ctx.blockStatement()); return visitBlockStatement(ctx.blockStatement());
} else if(ctx.whileStatement() != null) { } else if(ctx.whileStatement() != null) {
return visitWhileStatement(ctx.whileStatement()); return visitWhileStatement(ctx.whileStatement());
} else if(ctx.doWhileStatement() != null) {
return visitDoWhileStatement(ctx.doWhileStatement());
} else if(ctx.forStatement() != null) { } else if(ctx.forStatement() != null) {
return visitForStatement(ctx.forStatement()); return visitForStatement(ctx.forStatement());
} else if(ctx.ifElseStatement() != null) { } else if(ctx.ifElseStatement() != null) {
@ -131,10 +133,15 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) { public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) {
BlockNode blockNode = new BlockNode(); BlockNode blockNode = new BlockNode();
boolean hasReturnStatement = false;
for(SimpleJavaParser.StatementContext statement : ctx.statement()) { for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
blockNode.addStatement((IStatementNode) visit(statement)); IStatementNode statementNode = (IStatementNode) visit(statement);
if(statementNode instanceof ReturnNode) {
hasReturnStatement = true;
} }
if(!blockNode.hasReturnStatement) { blockNode.addStatement(statementNode);
}
if(!hasReturnStatement) {
blockNode.addStatement(new ReturnNode(null)); blockNode.addStatement(new ReturnNode(null));
} }
return blockNode; return blockNode;
@ -161,28 +168,30 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) { public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
List<StatementNode> statements = new ArrayList<>(); List<IStatementNode> statements = new ArrayList<>();
//init //init
if(ctx.statementExpression(0) != null){ int i = 0;
statements.add((StatementNode) visit(ctx.statementExpression(0))); if(ctx.localVariableDeclaration() != null) {
} else if (ctx.localVariableDeclaration() != null) { statements.add((IStatementNode) visit(ctx.localVariableDeclaration()));
statements.add((StatementNode) visit(ctx.localVariableDeclaration())); } else if(ctx.statementExpression(i) != null){
statements.add((IStatementNode) visit(ctx.statementExpression(i)));
i++;
} }
//condition //condition
IExpressionNode condition = (IExpressionNode) visit(ctx.expression()); IExpressionNode condition = (IExpressionNode) visit(ctx.expression());
//ink //ink
IStatementNode increment = null; IStatementNode crement = null;
if(ctx.statementExpression(1) != null){ if(ctx.statementExpression(i) != null){
increment = (IStatementNode) visit(ctx.statementExpression(1)); crement = (IStatementNode) visit(ctx.statementExpression(i));
} }
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement()); BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
if(increment != null){ if(crement != null){
forBlock.addStatement((increment)); forBlock.addStatement((crement));
} }
WhileNode While = new WhileNode(condition, forBlock); WhileNode While = new WhileNode(condition, forBlock);
@ -190,7 +199,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
statements.add(While); statements.add(While);
BlockNode resultBlock = new BlockNode(); BlockNode resultBlock = new BlockNode();
for(StatementNode statement : statements) { for(IStatementNode statement : statements) {
resultBlock.addStatement((IStatementNode) statement); resultBlock.addStatement((IStatementNode) statement);
} }

View File

@ -1,16 +1,14 @@
public class CompilerInput { public class Compiler {
public int add(int i, int j) {
public int a; return i+j;
public static int testMethod(char x){
return 0;
}
public class Test {
public static int testMethod(char x, int a){
return 0;
} }
} }
public class Node {
public void main() {
Compiler compiler = new Compiler();
int i = compiler.add(5, 8);
return i;
}
} }