assign has it's own rule

This commit is contained in:
laurenz 2024-05-07 20:30:03 +02:00
parent 706e6ee872
commit ee0a465c84
9 changed files with 441 additions and 350 deletions

View File

@ -19,14 +19,14 @@ param : type id;
block : '{' (localVar | stmt)* '}'; block : '{' (localVar | stmt)* '}';
stmt : 'if' '(' expr ')' block ('else' block)? #If stmt : 'if' '(' expr ')' block ('else' block)? #If
| 'for' '(' assignement ';' expr ';' assignement ')' block #For | 'for' '(' assign ';' expr ';' assign ')' block #For
| 'while' '(' expr ')' block #While | 'while' '(' expr ')' block #While
| 'do' block 'while' '(' expr ')' #DoWhile | 'do' block 'while' '(' expr ')' #DoWhile
| 'return' expr ';' #Return | 'return' expr ';' #Return
| 'return' ';' #ReturnVoid | 'return' ';' #ReturnVoid
| 'break' ';' #Break | 'break' ';' #Break
| 'continue' ';' #Continue | 'continue' ';' #Continue
| assignement ';' #Assignment | assign ';' #Assignment
| stmtexpr ';' #StatementExpressionstmt | stmtexpr ';' #StatementExpressionstmt
; ;
@ -48,7 +48,7 @@ unaryOp : SUB | NOT;
fieldId : (THIS '.')? (recipient '.')* id; fieldId : (THIS '.')? (recipient '.')* id;
assignement : id assignSign expr ; assign : id assignSign expr ;
methCall : (THIS '.')? (recipient '.')* methName; methCall : (THIS '.')? (recipient '.')* methName;
recipient : methName | id; recipient : methName | id;
methName : id '(' args? ')'; methName : id '(' args? ')';

View File

@ -25,9 +25,9 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
@Override @Override
public For visitFor(DecafParser.ForContext ctx) { public For visitFor(DecafParser.ForContext ctx) {
Expression init = new ExpressionGenerator().visit(ctx.expr(0)); Assignment init = generateAssign(ctx.assign(0));
Expression expr = new ExpressionGenerator().visit(ctx.expr(1)); Expression expr = new ExpressionGenerator().visit(ctx.expr());
Expression update = new ExpressionGenerator().visit(ctx.expr(2)); Assignment update = generateAssign(ctx.assign(1));
Block block = new BlockGenerator().visit(ctx.block()); Block block = new BlockGenerator().visit(ctx.block());
return new For(init, expr, update, block); return new For(init, expr, update, block);
} }
@ -68,6 +68,10 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
@Override @Override
public Statement visitAssignment(DecafParser.AssignmentContext ctx) { public Statement visitAssignment(DecafParser.AssignmentContext ctx) {
return generateAssign(ctx.assign());
}
private Assignment generateAssign(DecafParser.AssignContext ctx) {
Id id = new Id(ctx.id().getText()); Id id = new Id(ctx.id().getText());
AssignSign sign = getAssignSign(ctx.assignSign()); AssignSign sign = getAssignSign(ctx.assignSign());
Expression expr = new ExpressionGenerator().visit(ctx.expr()); Expression expr = new ExpressionGenerator().visit(ctx.expr());

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1 // Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr; package de.maishai.antlr;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.ParserRuleContext;
@ -432,6 +432,18 @@ public class DecafBaseListener implements DecafListener {
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void exitFieldId(DecafParser.FieldIdContext ctx) { } @Override public void exitFieldId(DecafParser.FieldIdContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterAssign(DecafParser.AssignContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitAssign(DecafParser.AssignContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1 // Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr; package de.maishai.antlr;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
@ -257,6 +257,13 @@ public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
* {@link #visitChildren} on {@code ctx}.</p> * {@link #visitChildren} on {@code ctx}.</p>
*/ */
@Override public T visitFieldId(DecafParser.FieldIdContext ctx) { return visitChildren(ctx); } @Override public T visitFieldId(DecafParser.FieldIdContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitAssign(DecafParser.AssignContext ctx) { return visitChildren(ctx); }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1 // Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr; package de.maishai.antlr;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1 // Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr; package de.maishai.antlr;
import org.antlr.v4.runtime.tree.ParseTreeListener; import org.antlr.v4.runtime.tree.ParseTreeListener;
@ -395,6 +395,16 @@ public interface DecafListener extends ParseTreeListener {
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void exitFieldId(DecafParser.FieldIdContext ctx); void exitFieldId(DecafParser.FieldIdContext ctx);
/**
* Enter a parse tree produced by {@link DecafParser#assign}.
* @param ctx the parse tree
*/
void enterAssign(DecafParser.AssignContext ctx);
/**
* Exit a parse tree produced by {@link DecafParser#assign}.
* @param ctx the parse tree
*/
void exitAssign(DecafParser.AssignContext ctx);
/** /**
* Enter a parse tree produced by {@link DecafParser#methCall}. * Enter a parse tree produced by {@link DecafParser#methCall}.
* @param ctx the parse tree * @param ctx the parse tree

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Generated from C:/dev/Compilerbau/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1 // Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr; package de.maishai.antlr;
import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.ParseTreeVisitor;
@ -239,6 +239,12 @@ public interface DecafVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result * @return the visitor result
*/ */
T visitFieldId(DecafParser.FieldIdContext ctx); T visitFieldId(DecafParser.FieldIdContext ctx);
/**
* Visit a parse tree produced by {@link DecafParser#assign}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitAssign(DecafParser.AssignContext ctx);
/** /**
* Visit a parse tree produced by {@link DecafParser#methCall}. * Visit a parse tree produced by {@link DecafParser#methCall}.
* @param ctx the parse tree * @param ctx the parse tree