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.List;
public class BlockNode implements ASTNode, Visitable {
public class BlockNode implements IStatementNode, Visitable {
public List<IStatementNode> statements = new ArrayList<>();
public Boolean hasReturnStatement = false;
public BlockNode() {}
public void addStatement(IStatementNode statement) {
statements.add(statement);
if(statement instanceof ReturnNode) {
hasReturnStatement = true;
}
}
@Override

View File

@ -1,8 +1,10 @@
package ast.statements;
import ast.ASTNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class ElseNode implements ASTNode {
public class ElseNode implements IStatementNode {
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;
import ast.ASTNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import java.util.ArrayList;
import java.util.List;
public class IfElseNode implements ASTNode {
public class IfElseNode implements IStatementNode {
IfNode ifStatement;
List<IfNode> elseIfStatements = new ArrayList<>();
ElseNode elseStatement;
@ -19,4 +21,8 @@ public class IfElseNode implements ASTNode {
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.expressions.IExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class IfNode implements ASTNode {
public class IfNode implements IStatementNode {
IExpressionNode expression;
BlockNode block;
@ -11,4 +13,9 @@ public class IfNode implements ASTNode {
this.expression = expression;
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.expressions.IExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class WhileNode extends StatementNode implements ASTNode {
public class WhileNode implements IStatementNode {
IExpressionNode expression;
BlockNode block;
@ -11,4 +13,14 @@ public class WhileNode extends StatementNode implements ASTNode {
this.expression = expression;
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());
} else if(ctx.whileStatement() != null) {
return visitWhileStatement(ctx.whileStatement());
} else if(ctx.doWhileStatement() != null) {
return visitDoWhileStatement(ctx.doWhileStatement());
} else if(ctx.forStatement() != null) {
return visitForStatement(ctx.forStatement());
} else if(ctx.ifElseStatement() != null) {
@ -131,10 +133,15 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) {
BlockNode blockNode = new BlockNode();
boolean hasReturnStatement = false;
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
blockNode.addStatement((IStatementNode) visit(statement));
IStatementNode statementNode = (IStatementNode) visit(statement);
if(statementNode instanceof ReturnNode) {
hasReturnStatement = true;
}
blockNode.addStatement(statementNode);
}
if(!blockNode.hasReturnStatement) {
if(!hasReturnStatement) {
blockNode.addStatement(new ReturnNode(null));
}
return blockNode;
@ -161,28 +168,30 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
List<StatementNode> statements = new ArrayList<>();
List<IStatementNode> statements = new ArrayList<>();
//init
if(ctx.statementExpression(0) != null){
statements.add((StatementNode) visit(ctx.statementExpression(0)));
} else if (ctx.localVariableDeclaration() != null) {
statements.add((StatementNode) visit(ctx.localVariableDeclaration()));
int i = 0;
if(ctx.localVariableDeclaration() != null) {
statements.add((IStatementNode) visit(ctx.localVariableDeclaration()));
} else if(ctx.statementExpression(i) != null){
statements.add((IStatementNode) visit(ctx.statementExpression(i)));
i++;
}
//condition
IExpressionNode condition = (IExpressionNode) visit(ctx.expression());
//ink
IStatementNode increment = null;
if(ctx.statementExpression(1) != null){
increment = (IStatementNode) visit(ctx.statementExpression(1));
IStatementNode crement = null;
if(ctx.statementExpression(i) != null){
crement = (IStatementNode) visit(ctx.statementExpression(i));
}
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
if(increment != null){
forBlock.addStatement((increment));
if(crement != null){
forBlock.addStatement((crement));
}
WhileNode While = new WhileNode(condition, forBlock);
@ -190,7 +199,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
statements.add(While);
BlockNode resultBlock = new BlockNode();
for(StatementNode statement : statements) {
for(IStatementNode statement : statements) {
resultBlock.addStatement((IStatementNode) statement);
}

View File

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