Compare commits
No commits in common. "bf365194be31f6ffda971a1e9a38368c10d62c8b" and "35b3e9ee462641ef7d2e2458587add0fffc43218" have entirely different histories.
bf365194be
...
35b3e9ee46
@ -8,13 +8,17 @@ import visitor.Visitable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockNode implements IStatementNode, Visitable {
|
||||
public class BlockNode implements ASTNode, 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
|
||||
|
@ -1,10 +1,8 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.ASTNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class ElseNode implements IStatementNode {
|
||||
public class ElseNode implements ASTNode {
|
||||
BlockNode block;
|
||||
|
||||
public ElseNode(BlockNode block) {
|
||||
@ -12,8 +10,4 @@ public class ElseNode implements IStatementNode {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
package ast.statements;
|
||||
|
||||
import ast.ASTNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IfElseNode implements IStatementNode {
|
||||
public class IfElseNode implements ASTNode {
|
||||
IfNode ifStatement;
|
||||
List<IfNode> elseIfStatements = new ArrayList<>();
|
||||
ElseNode elseStatement;
|
||||
@ -21,8 +19,4 @@ public class IfElseNode implements IStatementNode {
|
||||
elseIfStatements.add(elseIfStament);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,8 @@ package ast.statements;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expressions.IExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class IfNode implements IStatementNode {
|
||||
public class IfNode implements ASTNode {
|
||||
IExpressionNode expression;
|
||||
BlockNode block;
|
||||
|
||||
@ -13,9 +11,4 @@ public class IfNode implements IStatementNode {
|
||||
this.expression = expression;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
2
src/main/java/ast/statements/StatementNode.java
Normal file
2
src/main/java/ast/statements/StatementNode.java
Normal file
@ -0,0 +1,2 @@
|
||||
package ast.statements;public class StatementNode {
|
||||
}
|
@ -2,10 +2,8 @@ package ast.statements;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.expressions.IExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class WhileNode implements IStatementNode {
|
||||
public class WhileNode extends StatementNode implements ASTNode {
|
||||
IExpressionNode expression;
|
||||
BlockNode block;
|
||||
|
||||
@ -13,14 +11,4 @@ public class WhileNode implements IStatementNode {
|
||||
this.expression = expression;
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -108,8 +108,6 @@ 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) {
|
||||
@ -133,15 +131,10 @@ 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()) {
|
||||
IStatementNode statementNode = (IStatementNode) visit(statement);
|
||||
if(statementNode instanceof ReturnNode) {
|
||||
hasReturnStatement = true;
|
||||
}
|
||||
blockNode.addStatement(statementNode);
|
||||
blockNode.addStatement((IStatementNode) visit(statement));
|
||||
}
|
||||
if(!hasReturnStatement) {
|
||||
if(!blockNode.hasReturnStatement) {
|
||||
blockNode.addStatement(new ReturnNode(null));
|
||||
}
|
||||
return blockNode;
|
||||
@ -168,30 +161,28 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@Override
|
||||
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
|
||||
|
||||
List<IStatementNode> statements = new ArrayList<>();
|
||||
List<StatementNode> statements = new ArrayList<>();
|
||||
|
||||
//init
|
||||
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++;
|
||||
if(ctx.statementExpression(0) != null){
|
||||
statements.add((StatementNode) visit(ctx.statementExpression(0)));
|
||||
} else if (ctx.localVariableDeclaration() != null) {
|
||||
statements.add((StatementNode) visit(ctx.localVariableDeclaration()));
|
||||
}
|
||||
|
||||
//condition
|
||||
IExpressionNode condition = (IExpressionNode) visit(ctx.expression());
|
||||
|
||||
//ink
|
||||
IStatementNode crement = null;
|
||||
if(ctx.statementExpression(i) != null){
|
||||
crement = (IStatementNode) visit(ctx.statementExpression(i));
|
||||
IStatementNode increment = null;
|
||||
if(ctx.statementExpression(1) != null){
|
||||
increment = (IStatementNode) visit(ctx.statementExpression(1));
|
||||
}
|
||||
|
||||
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
|
||||
|
||||
if(crement != null){
|
||||
forBlock.addStatement((crement));
|
||||
if(increment != null){
|
||||
forBlock.addStatement((increment));
|
||||
}
|
||||
|
||||
WhileNode While = new WhileNode(condition, forBlock);
|
||||
@ -199,7 +190,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
statements.add(While);
|
||||
|
||||
BlockNode resultBlock = new BlockNode();
|
||||
for(IStatementNode statement : statements) {
|
||||
for(StatementNode statement : statements) {
|
||||
resultBlock.addStatement((IStatementNode) statement);
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,16 @@
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user