add new package for astGenerator
This commit is contained in:
parent
7ff84e5ad7
commit
99b979d77f
@ -1,5 +1,6 @@
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import abstractSyntaxTree.Program;
|
||||
import astGenerator.ASTGenerator;
|
||||
import gen.DecafLexer;
|
||||
import gen.DecafParser;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
|
@ -1,5 +1,6 @@
|
||||
package astGenerator;
|
||||
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.IClass;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import abstractSyntaxTree.Expression.BinaryExpression;
|
||||
@ -8,10 +9,10 @@ import abstractSyntaxTree.Parameter.Parameter;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import abstractSyntaxTree.Program;
|
||||
import abstractSyntaxTree.Statement.*;
|
||||
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
|
||||
import gen.DecafBaseVisitor;
|
||||
import gen.DecafParser;
|
||||
|
||||
import java.beans.Expression;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -33,6 +34,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
boolean hasMain;
|
||||
if(ctx.MainMethodDecl() != null) {
|
||||
hasMain = true;
|
||||
MethodDecl mainMethod = new MethodDecl(name, "void", "main", new ParameterList(new ArrayList<>()), new BlockStatement(new ArrayList<>(), "void"));
|
||||
} else {
|
||||
hasMain = false;
|
||||
}
|
||||
@ -68,13 +70,14 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
public Node visitMethodDecl(DecafParser.MethodDeclContext ctx) {
|
||||
String type;
|
||||
String name = ctx.Identifier().getText();
|
||||
BlockStatement block = (BlockStatement) visitBlock(ctx.block());
|
||||
ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
|
||||
if (ctx.Void() != null) {
|
||||
type = "void";
|
||||
} else {
|
||||
type = ctx.type().getText();
|
||||
}
|
||||
return new MethodDecl("", type , name, parameterList, new BlockStatement(new ArrayList<>(), "void"));
|
||||
return new MethodDecl("", type , name, parameterList, block);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -103,6 +106,8 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return visitBlock(ctx.block());
|
||||
} else if (ctx.stmtExpr() != null) {
|
||||
return visitStmtExpr(ctx.stmtExpr());
|
||||
} else if (ctx.emptyStatement() != null) {
|
||||
return visitEmptyStatement(ctx.emptyStatement());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -112,6 +117,16 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return new ReturnStatement(new BinaryExpression());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitBlock(DecafParser.BlockContext ctx) {
|
||||
List<IStatement> stmts = new ArrayList<>();
|
||||
for (DecafParser.StatementContext stmt: ctx.statement()) {
|
||||
Node statement = visitStatement(stmt);
|
||||
stmts.add((IStatement) statement);
|
||||
}
|
||||
return new BlockStatement(stmts, "void");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitIfElseStmt(DecafParser.IfElseStmtContext ctx) {
|
||||
if (ctx.elseStmt() != null) {
|
||||
@ -134,4 +149,37 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
Node statement = visitStatement(ctx.statement());
|
||||
return new WhileStatement(new BinaryExpression(), (IStatement) statement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitEmptyStatement(DecafParser.EmptyStatementContext ctx) {
|
||||
return new EmptyStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitStmtExpr(DecafParser.StmtExprContext ctx) {
|
||||
if (ctx.assign() != null) {
|
||||
return visitAssign(ctx.assign());
|
||||
} else if (ctx.methodCall() != null) {
|
||||
return visitMethodCall(ctx.methodCall());
|
||||
} else if (ctx.newDecl() != null) {
|
||||
return visitNewDecl(ctx.newDecl());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Node visitAssign(DecafParser.AssignContext ctx) {
|
||||
return new AssignStatementExpression();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||
return super.visitMethodCall(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitNewDecl(DecafParser.NewDeclContext ctx) {
|
||||
return super.visitNewDecl(ctx);
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -396,6 +396,18 @@ public class DecafBaseListener implements DecafListener {
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitReceivingMethod(DecafParser.ReceivingMethodContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterEmptyStatement(DecafParser.EmptyStatementContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitEmptyStatement(DecafParser.EmptyStatementContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -236,6 +236,13 @@ public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitReceivingMethod(DecafParser.ReceivingMethodContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitEmptyStatement(DecafParser.EmptyStatementContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -327,6 +327,16 @@ public interface DecafListener extends ParseTreeListener {
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitReceivingMethod(DecafParser.ReceivingMethodContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link DecafParser#emptyStatement}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterEmptyStatement(DecafParser.EmptyStatementContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link DecafParser#emptyStatement}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitEmptyStatement(DecafParser.EmptyStatementContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link DecafParser#type}.
|
||||
* @param ctx the parse tree
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -202,6 +202,12 @@ public interface DecafVisitor<T> extends ParseTreeVisitor<T> {
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitReceivingMethod(DecafParser.ReceivingMethodContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DecafParser#emptyStatement}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitEmptyStatement(DecafParser.EmptyStatementContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link DecafParser#type}.
|
||||
* @param ctx the parse tree
|
||||
|
Loading…
Reference in New Issue
Block a user