From 8cc109275a7cd0abebda30c4354fa2c3638a7a53 Mon Sep 17 00:00:00 2001 From: laurenz Date: Thu, 2 May 2024 13:56:20 +0200 Subject: [PATCH 1/2] added unary operations to ast --- src/main/java/de/maishai/ExpressionGenerator.java | 4 ++-- src/main/java/de/maishai/ast/UnaryOperator.java | 6 ++++++ src/main/java/de/maishai/ast/records/BoolConstant.java | 5 ----- src/main/java/de/maishai/ast/records/BoolLiteral.java | 5 +++++ src/main/java/de/maishai/ast/records/CharConstant.java | 5 ----- src/main/java/de/maishai/ast/records/CharLiteral.java | 5 +++++ src/main/java/de/maishai/ast/records/Expression.java | 2 +- src/main/java/de/maishai/ast/records/IntConstant.java | 5 ----- src/main/java/de/maishai/ast/records/IntLiteral.java | 5 +++++ src/main/java/de/maishai/ast/records/Statement.java | 2 +- 10 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 src/main/java/de/maishai/ast/UnaryOperator.java delete mode 100644 src/main/java/de/maishai/ast/records/BoolConstant.java create mode 100644 src/main/java/de/maishai/ast/records/BoolLiteral.java delete mode 100644 src/main/java/de/maishai/ast/records/CharConstant.java create mode 100644 src/main/java/de/maishai/ast/records/CharLiteral.java delete mode 100644 src/main/java/de/maishai/ast/records/IntConstant.java create mode 100644 src/main/java/de/maishai/ast/records/IntLiteral.java diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index d9e1698..da842f9 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -46,9 +46,9 @@ public class ExpressionGenerator extends DecafBaseVisitor { public static Expression generateConstant(DecafParser.LiteralContext ctx){ if(ctx.NUMBER() != null) - return new IntConstant(Integer.valueOf(ctx.NUMBER().getText())); + return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText())); if(ctx.boolean_() != null) - return new BoolConstant(Boolean.valueOf(ctx.boolean_().getText())); + return new BoolLiteral(Boolean.valueOf(ctx.boolean_().getText())); throw new RuntimeException(); } diff --git a/src/main/java/de/maishai/ast/UnaryOperator.java b/src/main/java/de/maishai/ast/UnaryOperator.java new file mode 100644 index 0000000..cfa42dd --- /dev/null +++ b/src/main/java/de/maishai/ast/UnaryOperator.java @@ -0,0 +1,6 @@ +package de.maishai.ast; + +public enum UnaryOperator { + SUB, + NOT +} diff --git a/src/main/java/de/maishai/ast/records/BoolConstant.java b/src/main/java/de/maishai/ast/records/BoolConstant.java deleted file mode 100644 index 1e4e7a3..0000000 --- a/src/main/java/de/maishai/ast/records/BoolConstant.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.maishai.ast.records; - - -public record BoolConstant(Boolean value) implements Expression { -} diff --git a/src/main/java/de/maishai/ast/records/BoolLiteral.java b/src/main/java/de/maishai/ast/records/BoolLiteral.java new file mode 100644 index 0000000..f09b222 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/BoolLiteral.java @@ -0,0 +1,5 @@ +package de.maishai.ast.records; + + +public record BoolLiteral(Boolean value) implements Expression { +} diff --git a/src/main/java/de/maishai/ast/records/CharConstant.java b/src/main/java/de/maishai/ast/records/CharConstant.java deleted file mode 100644 index ad2c671..0000000 --- a/src/main/java/de/maishai/ast/records/CharConstant.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.maishai.ast.records; - - -public record CharConstant(char value) implements Expression { -} diff --git a/src/main/java/de/maishai/ast/records/CharLiteral.java b/src/main/java/de/maishai/ast/records/CharLiteral.java new file mode 100644 index 0000000..af8454c --- /dev/null +++ b/src/main/java/de/maishai/ast/records/CharLiteral.java @@ -0,0 +1,5 @@ +package de.maishai.ast.records; + + +public record CharLiteral(char value) implements Expression { +} diff --git a/src/main/java/de/maishai/ast/records/Expression.java b/src/main/java/de/maishai/ast/records/Expression.java index 815e7f9..306750f 100644 --- a/src/main/java/de/maishai/ast/records/Expression.java +++ b/src/main/java/de/maishai/ast/records/Expression.java @@ -1,5 +1,5 @@ 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 Assignment, Binary, BoolLiteral, CharLiteral, Id, IntLiteral, MethodCall, New, Unary { } diff --git a/src/main/java/de/maishai/ast/records/IntConstant.java b/src/main/java/de/maishai/ast/records/IntConstant.java deleted file mode 100644 index 2724a49..0000000 --- a/src/main/java/de/maishai/ast/records/IntConstant.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.maishai.ast.records; - - -public record IntConstant(Integer value) implements Expression { -} diff --git a/src/main/java/de/maishai/ast/records/IntLiteral.java b/src/main/java/de/maishai/ast/records/IntLiteral.java new file mode 100644 index 0000000..7b5c234 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/IntLiteral.java @@ -0,0 +1,5 @@ +package de.maishai.ast.records; + + +public record IntLiteral(Integer value) implements Expression { +} diff --git a/src/main/java/de/maishai/ast/records/Statement.java b/src/main/java/de/maishai/ast/records/Statement.java index 9351511..a776dc9 100644 --- a/src/main/java/de/maishai/ast/records/Statement.java +++ b/src/main/java/de/maishai/ast/records/Statement.java @@ -12,5 +12,5 @@ import de.maishai.ast.records.Return; import de.maishai.ast.records.ReturnVoid; 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 { } From b1148d5f0cb4c0121137e2bd2356195103672637 Mon Sep 17 00:00:00 2001 From: laurenz Date: Thu, 2 May 2024 13:57:09 +0200 Subject: [PATCH 2/2] added unary operations to ast --- src/main/java/de/maishai/ast/records/Unary.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/de/maishai/ast/records/Unary.java diff --git a/src/main/java/de/maishai/ast/records/Unary.java b/src/main/java/de/maishai/ast/records/Unary.java new file mode 100644 index 0000000..aa1300f --- /dev/null +++ b/src/main/java/de/maishai/ast/records/Unary.java @@ -0,0 +1,7 @@ +package de.maishai.ast.records; + + +import de.maishai.ast.UnaryOperator; + +public record Unary(UnaryOperator op, Expression right) implements Expression { +}