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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BlockNode implements IStatementNode, Visitable {
|
public class BlockNode implements ASTNode, 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
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package ast.statements;
|
package ast.statements;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
|
|
||||||
public class ElseNode implements IStatementNode {
|
public class ElseNode implements ASTNode {
|
||||||
BlockNode block;
|
BlockNode block;
|
||||||
|
|
||||||
public ElseNode(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;
|
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 IStatementNode {
|
public class IfElseNode implements ASTNode {
|
||||||
IfNode ifStatement;
|
IfNode ifStatement;
|
||||||
List<IfNode> elseIfStatements = new ArrayList<>();
|
List<IfNode> elseIfStatements = new ArrayList<>();
|
||||||
ElseNode elseStatement;
|
ElseNode elseStatement;
|
||||||
@ -21,8 +19,4 @@ public class IfElseNode implements IStatementNode {
|
|||||||
elseIfStatements.add(elseIfStament);
|
elseIfStatements.add(elseIfStament);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,8 @@ 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 IStatementNode {
|
public class IfNode implements ASTNode {
|
||||||
IExpressionNode expression;
|
IExpressionNode expression;
|
||||||
BlockNode block;
|
BlockNode block;
|
||||||
|
|
||||||
@ -13,9 +11,4 @@ public class IfNode implements IStatementNode {
|
|||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.block = block;
|
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.ASTNode;
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
|
|
||||||
public class WhileNode implements IStatementNode {
|
public class WhileNode extends StatementNode implements ASTNode {
|
||||||
IExpressionNode expression;
|
IExpressionNode expression;
|
||||||
BlockNode block;
|
BlockNode block;
|
||||||
|
|
||||||
@ -13,14 +11,4 @@ public class WhileNode implements IStatementNode {
|
|||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.block = block;
|
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());
|
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) {
|
||||||
@ -133,15 +131,10 @@ 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()) {
|
||||||
IStatementNode statementNode = (IStatementNode) visit(statement);
|
blockNode.addStatement((IStatementNode) visit(statement));
|
||||||
if(statementNode instanceof ReturnNode) {
|
|
||||||
hasReturnStatement = true;
|
|
||||||
}
|
}
|
||||||
blockNode.addStatement(statementNode);
|
if(!blockNode.hasReturnStatement) {
|
||||||
}
|
|
||||||
if(!hasReturnStatement) {
|
|
||||||
blockNode.addStatement(new ReturnNode(null));
|
blockNode.addStatement(new ReturnNode(null));
|
||||||
}
|
}
|
||||||
return blockNode;
|
return blockNode;
|
||||||
@ -168,30 +161,28 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
@Override
|
@Override
|
||||||
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
|
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
|
||||||
|
|
||||||
List<IStatementNode> statements = new ArrayList<>();
|
List<StatementNode> statements = new ArrayList<>();
|
||||||
|
|
||||||
//init
|
//init
|
||||||
int i = 0;
|
if(ctx.statementExpression(0) != null){
|
||||||
if(ctx.localVariableDeclaration() != null) {
|
statements.add((StatementNode) visit(ctx.statementExpression(0)));
|
||||||
statements.add((IStatementNode) visit(ctx.localVariableDeclaration()));
|
} else if (ctx.localVariableDeclaration() != null) {
|
||||||
} else if(ctx.statementExpression(i) != null){
|
statements.add((StatementNode) visit(ctx.localVariableDeclaration()));
|
||||||
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 crement = null;
|
IStatementNode increment = null;
|
||||||
if(ctx.statementExpression(i) != null){
|
if(ctx.statementExpression(1) != null){
|
||||||
crement = (IStatementNode) visit(ctx.statementExpression(i));
|
increment = (IStatementNode) visit(ctx.statementExpression(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
|
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
|
||||||
|
|
||||||
if(crement != null){
|
if(increment != null){
|
||||||
forBlock.addStatement((crement));
|
forBlock.addStatement((increment));
|
||||||
}
|
}
|
||||||
|
|
||||||
WhileNode While = new WhileNode(condition, forBlock);
|
WhileNode While = new WhileNode(condition, forBlock);
|
||||||
@ -199,7 +190,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
statements.add(While);
|
statements.add(While);
|
||||||
|
|
||||||
BlockNode resultBlock = new BlockNode();
|
BlockNode resultBlock = new BlockNode();
|
||||||
for(IStatementNode statement : statements) {
|
for(StatementNode statement : statements) {
|
||||||
resultBlock.addStatement((IStatementNode) statement);
|
resultBlock.addStatement((IStatementNode) statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
public class Compiler {
|
public class CompilerInput {
|
||||||
public int add(int i, int j) {
|
|
||||||
return i+j;
|
public int a;
|
||||||
}
|
|
||||||
}
|
public static int testMethod(char x){
|
||||||
|
return 0;
|
||||||
public class Node {
|
}
|
||||||
public void main() {
|
|
||||||
Compiler compiler = new Compiler();
|
public class Test {
|
||||||
int i = compiler.add(5, 8);
|
|
||||||
return i;
|
public static int testMethod(char x, int a){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user