left part of an assignement can be a FieldId

This commit is contained in:
laurenz 2024-05-08 11:44:15 +02:00
parent 7d1b7bc5b0
commit f816e5c391
5 changed files with 20 additions and 14 deletions

View File

@ -46,7 +46,7 @@ unaryOp : SUB | NOT;
fieldId : (THIS '.')? (recipient '.')* id;
assign : id assignSign expr ;
assign : fieldId assignSign expr ;
methCall : (THIS '.')? (recipient '.')* methName;
recipient : methName | id;
methName : id '(' args? ')';

View File

@ -67,9 +67,15 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
}
private Assignment generateAssign(DecafParser.AssignContext ctx) {
Id id = new Id(ctx.id().getText());
Expression expr = resolveFancyAssign(ctx.assignSign(), id, new ExpressionGenerator().visit(ctx.expr()));
return new Assignment(id, expr);
FieldId fieldId = generateField(ctx.fieldId());
Expression expr = resolveFancyAssign(ctx.assignSign(), fieldId, new ExpressionGenerator().visit(ctx.expr()));
return new Assignment(fieldId, expr);
}
private FieldId generateField(DecafParser.FieldIdContext fieldIdContext) {
return new FieldId(fieldIdContext.THIS() != null,
ExpressionGenerator.generateRecipient(fieldIdContext.recipient(),
null), new Id(fieldIdContext.id().getText()));
}
//StatementExpression
@ -101,15 +107,15 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
return new New(type, args);
}
public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, Id id, Expression expression) {
public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, FieldId fieldId, Expression expression) {
if (ctx.ASSIGN() != null)
return expression;
if (ctx.ADD_ASSIGN() != null)
return new Binary(id, Operator.ADD, expression);
return new Binary(fieldId, Operator.ADD, expression);
if (ctx.SUB_ASSIGN() != null)
return new Binary(id, Operator.SUB, expression);
return new Binary(fieldId, Operator.SUB, expression);
if (ctx.MUL_ASSIGN() != null)
return new Binary(id, Operator.MUL, expression);
return new Binary(fieldId, Operator.MUL, expression);
throw new RuntimeException();
}
}

File diff suppressed because one or more lines are too long

View File

@ -1904,8 +1904,8 @@ public class DecafParser extends Parser {
@SuppressWarnings("CheckReturnValue")
public static class AssignContext extends ParserRuleContext {
public IdContext id() {
return getRuleContext(IdContext.class,0);
public FieldIdContext fieldId() {
return getRuleContext(FieldIdContext.class,0);
}
public AssignSignContext assignSign() {
return getRuleContext(AssignSignContext.class,0);
@ -1939,7 +1939,7 @@ public class DecafParser extends Parser {
enterOuterAlt(_localctx, 1);
{
setState(238);
id();
fieldId();
setState(239);
assignSign();
setState(240);
@ -2494,7 +2494,7 @@ public class DecafParser extends Parser {
"\u00eb\u0001\u0000\u0000\u0000\u00e9\u00e7\u0001\u0000\u0000\u0000\u00e9"+
"\u00ea\u0001\u0000\u0000\u0000\u00ea\u00ec\u0001\u0000\u0000\u0000\u00eb"+
"\u00e9\u0001\u0000\u0000\u0000\u00ec\u00ed\u00030\u0018\u0000\u00ed#\u0001"+
"\u0000\u0000\u0000\u00ee\u00ef\u00030\u0018\u0000\u00ef\u00f0\u0003\u0006"+
"\u0000\u0000\u0000\u00ee\u00ef\u0003\"\u0011\u0000\u00ef\u00f0\u0003\u0006"+
"\u0003\u0000\u00f0\u00f1\u0003\u001c\u000e\u0000\u00f1%\u0001\u0000\u0000"+
"\u0000\u00f2\u00f3\u0005\u0016\u0000\u0000\u00f3\u00f5\u0005\u0012\u0000"+
"\u0000\u00f4\u00f2\u0001\u0000\u0000\u0000\u00f4\u00f5\u0001\u0000\u0000"+

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
public record Assignment(Id loc, Expression value) implements Statement {
public record Assignment(FieldId loc, Expression value) implements Statement {
}