update records

This commit is contained in:
Boolean-True 2024-05-02 14:38:07 +02:00
parent 2b10b883a6
commit 73def2733a
17 changed files with 52 additions and 30 deletions

View File

@ -6,7 +6,7 @@ class : PUBLIC? 'class' id '{' (field | meth)* '}';
field : type id ';'; field : type id ';';
localVar : type id ';'; localVar : type id ';';
assignSign : '=' | '+=' | '-=' | '*='; assignSign : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN;
returntype : type | VOID; returntype : type | VOID;
type : INT | BOOL | CHAR; type : INT | BOOL | CHAR;
@ -26,11 +26,11 @@ stmt : 'if' '(' expr ')' block ('else' block)? #If
| 'return' ';' #ReturnVoid | 'return' ';' #ReturnVoid
| 'break' ';' #Break | 'break' ';' #Break
| 'continue' ';' #Continue | 'continue' ';' #Continue
| stmtexpr #StatementExpressionstmt | id assignSign expr ';' #Assignment
| stmtexpr ';' #StatementExpressionstmt
; ;
stmtexpr : id assignSign expr ';' #Assignment stmtexpr : methCall #MethodCall
| methCall ';' #MethodCall
| NEW type '(' args? ')' #New | NEW type '(' args? ')' #New
; ;
@ -38,7 +38,6 @@ expr : expr binaryOp expr #BinaryOperation
| unaryOp expr #UnaryOperation | unaryOp expr #UnaryOperation
| literal #Constant | literal #Constant
| '(' expr ')' #Expression | '(' expr ')' #Expression
| methCall #MethodCallExpression
| fieldId #Identifier | fieldId #Identifier
| stmtexpr #StatementExpressionexpr | stmtexpr #StatementExpressionexpr
| NULL #Null | NULL #Null
@ -75,6 +74,11 @@ NE : '!=';
AND : '&&'; AND : '&&';
OR : '||'; OR : '||';
ASSIGN : '=';
ADD_ASSIGN : '+=';
SUB_ASSIGN : '-=';
MUL_ASSIGN : '*=';
NOT : '!'; NOT : '!';
INT : 'int'; INT : 'int';

View File

@ -46,9 +46,9 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
public static Expression generateConstant(DecafParser.LiteralContext ctx){ public static Expression generateConstant(DecafParser.LiteralContext ctx){
if(ctx.NUMBER() != null) if(ctx.NUMBER() != null)
return new IntConstant(Integer.valueOf(ctx.NUMBER().getText())); return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText()));
if(ctx.boolean_() != null) if(ctx.boolean_() != null)
return new BoolConstant(Boolean.valueOf(ctx.boolean_().getText())); return new BoolLiteral(Boolean.valueOf(ctx.boolean_().getText()));
throw new RuntimeException(); throw new RuntimeException();
} }

View File

@ -0,0 +1,8 @@
package de.maishai.ast;
public enum AssignSign {
ASSIGN,
ADD_ASSIGN,
SUB_ASSIGN,
MUL_ASSIGN,
}

View File

@ -1,5 +1,7 @@
package de.maishai.ast.records; package de.maishai.ast.records;
public record Assignment(Id loc, Expression value) implements Statement, Expression { import de.maishai.ast.AssignSign;
public record Assignment(Id loc, AssignSign assignment, Expression value) implements Statement {
} }

View File

@ -1,5 +0,0 @@
package de.maishai.ast.records;
public record BoolConstant(Boolean value) implements Expression {
}

View File

@ -0,0 +1,5 @@
package de.maishai.ast.records;
public record BoolLiteral(Boolean value) implements Expression {
}

View File

@ -1,5 +0,0 @@
package de.maishai.ast.records;
public record CharConstant(char value) implements Expression {
}

View File

@ -0,0 +1,5 @@
package de.maishai.ast.records;
public record CharLiteral(char value) implements Expression {
}

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
import java.util.List; import java.util.List;
public record Class(Id id , List<LocalVariable> variables, List<Method> methods) implements Node { public record Class(Id id , List<Field> fields, List<Method> methods) implements Node {
} }

View File

@ -0,0 +1,6 @@
package de.maishai.ast.records;
import java.util.List;
public record Constructor(Id id, List<Parameter> params, Block block) {
}

View File

@ -1,5 +1,5 @@
package de.maishai.ast.records; package de.maishai.ast.records;
public sealed interface Expression extends Node permits Assignment, Binary, BoolConstant, CharConstant, Id, IntConstant, MethodCall, New { public sealed interface Expression extends Node permits Binary, BoolLiteral, CharLiteral, Id, IntLiteral, MethodCall, New {
} }

View File

@ -0,0 +1,4 @@
package de.maishai.ast.records;
public record FieldId(Boolean field, Expression recipient, Id id) {
}

View File

@ -1,5 +0,0 @@
package de.maishai.ast.records;
public record IntConstant(Integer value) implements Expression {
}

View File

@ -0,0 +1,5 @@
package de.maishai.ast.records;
public record IntLiteral(Integer value) implements Expression {
}

View File

@ -1,8 +1,6 @@
package de.maishai.ast.records; package de.maishai.ast.records;
import java.util.List; import java.util.List;
public record MethodCall(Id id, List<Expression> args) implements Expression, Statement { public record MethodCall(Boolean field, Expression recipient, Id id, List<Expression> args) implements Expression, Statement {
} }

View File

@ -1,4 +1,4 @@
package de.maishai.ast.records; package de.maishai.ast.records;
public sealed interface Node permits Expression, Statement, Block, Class, MainMethod, Method, Parameter, Program, Variable { public sealed interface Node permits Block, Class, Expression, LocalVariable, MainMethod, Method, Parameter, Program, Statement {
} }

View File

@ -12,5 +12,5 @@ import de.maishai.ast.records.Return;
import de.maishai.ast.records.ReturnVoid; import de.maishai.ast.records.ReturnVoid;
import de.maishai.ast.records.While; import de.maishai.ast.records.While;
public sealed interface Statement extends Node permits Break, Continue, DoWhile, For, IfElse, MethodCall, New, Return, ReturnVoid, While { public sealed interface Statement extends Node permits Assignment, Break, Continue, DoWhile, For, IfElse, MethodCall, New, Return, ReturnVoid, While {
} }