parser can deal with int i = 5

This commit is contained in:
laurenz 2024-05-16 18:42:30 +02:00
parent 31290c7c2c
commit 4b69908ed7
9 changed files with 490 additions and 443 deletions

View File

@ -4,7 +4,6 @@ grammar Decaf;
class : PUBLIC 'class' id '{' mainmeth? (field | meth | constructor)* '}';
field : PUBLIC? type id ';';
localVar : type id ';';
assignSign : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN;
returntype : type | VOID;
type : INT | BOOL | CHAR | id;
@ -16,6 +15,8 @@ params : param (',' param)*;
param : type id;
block : '{' (localVar | stmt)* return? '}';
localVar : type id ';'
| type id '=' expr';';
return: 'return' expr ';'
| 'return' ';'
;

View File

@ -7,15 +7,16 @@ import de.maishai.ast.records.Return;
import de.maishai.ast.records.Statement;
import de.maishai.ast.records.Block;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class BlockGenerator extends DecafBaseVisitor<Block> {
@Override
public Block visitBlock(DecafParser.BlockContext ctx) {
List<Declaration> vars = ctx.localVar().stream().map(var -> new VariableGenerator().visit(var)).toList();
List<Statement> statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).collect(Collectors.toCollection(ArrayList<Statement> :: new));
List<Statement> statements = VariableGenerator.generateInstanciations(ctx.localVar());
for (DecafParser.StmtContext stmtContext : ctx.stmt()) {
statements.add(new StatementGenerator().visit(stmtContext));
}
if (ctx.return_() != null){
statements.add(ctx.return_().isEmpty() ? new Return(null) : new Return(new ExpressionGenerator().visit(ctx.return_())));
}

View File

@ -3,11 +3,27 @@ package de.maishai;
import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.records.Assignment;
import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.FieldVarAccess;
import de.maishai.ast.records.Statement;
import de.maishai.typedast.Type;
import java.util.ArrayList;
import java.util.List;
public class VariableGenerator extends DecafBaseVisitor<Declaration> {
public static List<Statement> generateInstanciations(List<DecafParser.LocalVarContext> localVarContexts) {
List<Statement> assignements = new ArrayList<>(localVarContexts.size());
for (DecafParser.LocalVarContext localVarContext : localVarContexts) {
if (!localVarContext.expr().isEmpty()) {
assignements.add(new Assignment(new FieldVarAccess(false, null, localVarContext.id().IDENTIFIER().getText()), new ExpressionGenerator().visit(localVarContext.expr())));
}
}
return assignements;
}
@Override
public Declaration visitLocalVar(DecafParser.LocalVarContext ctx) {
Type type = ASTGenerator.getType(ctx.type());

File diff suppressed because one or more lines are too long

View File

@ -36,18 +36,6 @@ public class DecafBaseListener implements DecafListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitField(DecafParser.FieldContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterLocalVar(DecafParser.LocalVarContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitLocalVar(DecafParser.LocalVarContext ctx) { }
/**
* {@inheritDoc}
*
@ -156,6 +144,18 @@ public class DecafBaseListener implements DecafListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitBlock(DecafParser.BlockContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterLocalVar(DecafParser.LocalVarContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitLocalVar(DecafParser.LocalVarContext ctx) { }
/**
* {@inheritDoc}
*

View File

@ -26,13 +26,6 @@ public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitField(DecafParser.FieldContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitLocalVar(DecafParser.LocalVarContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
@ -96,6 +89,13 @@ public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitBlock(DecafParser.BlockContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitLocalVar(DecafParser.LocalVarContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*

View File

@ -27,16 +27,6 @@ public interface DecafListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitField(DecafParser.FieldContext ctx);
/**
* Enter a parse tree produced by {@link DecafParser#localVar}.
* @param ctx the parse tree
*/
void enterLocalVar(DecafParser.LocalVarContext ctx);
/**
* Exit a parse tree produced by {@link DecafParser#localVar}.
* @param ctx the parse tree
*/
void exitLocalVar(DecafParser.LocalVarContext ctx);
/**
* Enter a parse tree produced by {@link DecafParser#assignSign}.
* @param ctx the parse tree
@ -127,6 +117,16 @@ public interface DecafListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitBlock(DecafParser.BlockContext ctx);
/**
* Enter a parse tree produced by {@link DecafParser#localVar}.
* @param ctx the parse tree
*/
void enterLocalVar(DecafParser.LocalVarContext ctx);
/**
* Exit a parse tree produced by {@link DecafParser#localVar}.
* @param ctx the parse tree
*/
void exitLocalVar(DecafParser.LocalVarContext ctx);
/**
* Enter a parse tree produced by {@link DecafParser#return}.
* @param ctx the parse tree

File diff suppressed because it is too large Load Diff

View File

@ -22,12 +22,6 @@ public interface DecafVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitField(DecafParser.FieldContext ctx);
/**
* Visit a parse tree produced by {@link DecafParser#localVar}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitLocalVar(DecafParser.LocalVarContext ctx);
/**
* Visit a parse tree produced by {@link DecafParser#assignSign}.
* @param ctx the parse tree
@ -82,6 +76,12 @@ public interface DecafVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitBlock(DecafParser.BlockContext ctx);
/**
* Visit a parse tree produced by {@link DecafParser#localVar}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitLocalVar(DecafParser.LocalVarContext ctx);
/**
* Visit a parse tree produced by {@link DecafParser#return}.
* @param ctx the parse tree