From a86bede5a78e6dc0af9a0881544d52aa00e6a4cd Mon Sep 17 00:00:00 2001 From: Bruder John Date: Thu, 9 May 2024 00:13:23 +0200 Subject: [PATCH] Added down to Expression --- src/main/java/CompilerInput.txt | 6 +++++ src/main/java/Main.java | 3 --- src/main/java/ast/AssignmentNode.java | 14 ++++++++++++ src/main/java/ast/ExpressionNode.java | 9 +++++++- src/main/java/classFileOutput/Example.class | Bin 100 -> 108 bytes src/main/java/parser/ASTBuilder.java | 23 ++++++++++++++++++++ 6 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ast/AssignmentNode.java diff --git a/src/main/java/CompilerInput.txt b/src/main/java/CompilerInput.txt index 0a31d5c..a7d717c 100644 --- a/src/main/java/CompilerInput.txt +++ b/src/main/java/CompilerInput.txt @@ -2,6 +2,12 @@ public class Example { public int test; + public static int test(char b){ + + test = 3; + + } + } public class Test { diff --git a/src/main/java/Main.java b/src/main/java/Main.java index e15eafa..bcb48c5 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -9,9 +9,6 @@ import parser.ASTBuilder; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; import semantic.SemanticAnalyzer; -import ast.ClassNode; -import ast.ProgramNode; -import bytecode.ByteCodeGenerator; import java.io.IOException; import java.nio.file.Paths; diff --git a/src/main/java/ast/AssignmentNode.java b/src/main/java/ast/AssignmentNode.java new file mode 100644 index 0000000..38e7fb9 --- /dev/null +++ b/src/main/java/ast/AssignmentNode.java @@ -0,0 +1,14 @@ +package ast; + +public class AssignmentNode extends StatementNode { + + public Identifier identifier; + + public ExpressionNode expression; + + public AssignmentNode(String identifier, ExpressionNode expression) { + this.identifier = new Identifier(identifier); + this.expression = expression; + } + +} diff --git a/src/main/java/ast/ExpressionNode.java b/src/main/java/ast/ExpressionNode.java index 891d87c..9e6f2a8 100644 --- a/src/main/java/ast/ExpressionNode.java +++ b/src/main/java/ast/ExpressionNode.java @@ -1,4 +1,11 @@ package ast; -public class ExpressionNode { +public class ExpressionNode extends ASTNode { + + int Value; + + public ExpressionNode(int value) { + this.Value = value; + } + } diff --git a/src/main/java/classFileOutput/Example.class b/src/main/java/classFileOutput/Example.class index 7490cc13f94b6ef8362c61528e6ed8b919e01a52..c4a896adb0f253a0ebeaa67c21c610091b4b047a 100644 GIT binary patch delta 24 XcmYevnGh+=#K6eF%D~3J0H+xMDQ^Mi delta 16 Vcmc~vnGng%$iT?J%D~0|1OOT80nz{f diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index f57181d..e90ba95 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -6,6 +6,7 @@ import parser.generated.SimpleJavaParser; import java.util.ArrayList; import java.util.List; +import parser.generated.SimpleJavaParser.LiteralContext; public class ASTBuilder extends SimpleJavaBaseVisitor { @Override @@ -107,4 +108,26 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return null; } + @Override + public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { + ExpressionNode expressionNode = (ExpressionNode) visit(ctx.expression()); + return new AssignmentNode(ctx.IDENTIFIER().getText(), expressionNode); + } + + @Override + public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) { + if(ctx.literal() != null){ + return visitLiteral(ctx.literal()); + } + return null; + } + + @Override + public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) { + if(ctx.INTEGERLITERAL() != null){ + return new ExpressionNode(Integer.parseInt(ctx.INTEGERLITERAL().getSymbol().getText())); + } + return null; + } + }