From bea71838ac46bda27056ed1fedb44bb77b2b4aab Mon Sep 17 00:00:00 2001
From: Lucas <89882946+notbad3500@users.noreply.github.com>
Date: Sun, 30 Jun 2024 18:33:01 +0200
Subject: [PATCH 1/4] Reflections: not running
---
.idea/jarRepositories.xml | 5 +
pom.xml | 13 +++
src/test/java/main/E2E_Reflections_Test.java | 70 +++++++++++++
src/test/java/semantic/Mocker.java | 103 -------------------
src/test/java/semantic/SemanticTest.java | 101 ------------------
5 files changed, 88 insertions(+), 204 deletions(-)
create mode 100644 src/test/java/main/E2E_Reflections_Test.java
delete mode 100644 src/test/java/semantic/Mocker.java
delete mode 100644 src/test/java/semantic/SemanticTest.java
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
index 712ab9d..35e8b50 100644
--- a/.idea/jarRepositories.xml
+++ b/.idea/jarRepositories.xml
@@ -6,6 +6,11 @@
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
index 99e9904..e32826e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,12 @@
3.26.0
test
+
+ org.mockito
+ mockito-core
+ 5.11.0
+ test
+
@@ -78,4 +84,11 @@
+
+
+ maven_central
+ Maven Central
+ https://repo.maven.apache.org/maven2/
+
+
\ No newline at end of file
diff --git a/src/test/java/main/E2E_Reflections_Test.java b/src/test/java/main/E2E_Reflections_Test.java
new file mode 100644
index 0000000..8b13819
--- /dev/null
+++ b/src/test/java/main/E2E_Reflections_Test.java
@@ -0,0 +1,70 @@
+package main;
+
+import ast.ASTNode;
+import ast.ProgramNode;
+import bytecode.ByteCodeGenerator;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.tree.ParseTree;
+import parser.astBuilder.ASTBuilder;
+import parser.generated.SimpleJavaLexer;
+import parser.generated.SimpleJavaParser;
+import semantic.SemanticAnalyzer;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+
+public class E2E_Reflections_Test {
+
+
+ private CharStream mockInputCharStream;
+ private String outputDirectoryPath;
+
+ @BeforeEach
+ public void setUp() {
+ mockInputCharStream = mock(CharStream.class);
+ outputDirectoryPath = "path/to/output";
+ }
+
+ @Test
+ public void testCompileFile() throws Exception {
+ // Mock the dependencies
+ SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class);
+ CommonTokenStream mockTokenStream = mock(CommonTokenStream.class);
+ SimpleJavaParser mockParser = mock(SimpleJavaParser.class);
+ ParseTree mockParseTree = mock(ParseTree.class);
+ ASTBuilder mockASTBuilder = mock(ASTBuilder.class);
+ ASTNode mockASTNode = mock(ASTNode.class);
+ SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
+ ASTNode mockTypedAST = mock(ASTNode.class);
+ ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class);
+
+ // Mock the behavior
+ when(mockLexer.nextToken()).thenReturn(null);
+ when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
+ when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
+ when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
+ when(mockSemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
+
+ // Use reflection to invoke the compileFile method
+ Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
+ compileFileMethod.setAccessible(true);
+
+ compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
+
+ // Verify each step
+ verify(mockLexer, times(1)).nextToken();
+ verify(mockTokenStream, times(1)).getTokens();
+ verify(mockParser, times(1)).program();
+ verify(mockASTBuilder, times(1)).visit(mockParseTree);
+ verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
+ verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
+ }
+}
+
diff --git a/src/test/java/semantic/Mocker.java b/src/test/java/semantic/Mocker.java
deleted file mode 100644
index 1e404af..0000000
--- a/src/test/java/semantic/Mocker.java
+++ /dev/null
@@ -1,103 +0,0 @@
-package semantic;
-
-import ast.*;
-import ast.block.BlockNode;
-import ast.member.FieldNode;
-import ast.member.MethodNode;
-import ast.parameter.ParameterNode;
-import ast.type.AccessModifierNode;
-import ast.type.type.*;
-
-public class Mocker {
-
- public static ASTNode mockTwoSameFields(){
- ProgramNode p = new ProgramNode();
-
- ClassNode c = new ClassNode();
- c.identifier = "testClass";
-
- FieldNode f1 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
-
- c.members.add(f1);
-
- FieldNode f2 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
-
- c.members.add(f2);
-
- p.classes.add(c);
- return p;
- }
-
- public static ASTNode mockSimpleMethod(){
- ProgramNode p = new ProgramNode();
-
- ClassNode c = new ClassNode();
-
- MethodNode methodNode = new MethodNode();
-
- //Parameter
- ParameterNode parameterNode = new ParameterNode(new BaseType(TypeEnum.INT), "a");
-
- methodNode.addParameter(parameterNode);
-
- //Statements
-
- //Block
- methodNode.block = new BlockNode();
-
- c.members.add(methodNode);
-
- p.classes.add(c);
-
- return p;
- }
-
- public static ASTNode mockTwoSameMethods(){
- ProgramNode p = new ProgramNode();
-
- ClassNode c = new ClassNode();
-
- MethodNode methodNode = new MethodNode();
- methodNode.block = new BlockNode();
- methodNode.setType(new BaseType(TypeEnum.INT));
-
- methodNode.setIdentifier("testMethod");
-
- c.members.add(methodNode);
-
- MethodNode methodNode1 = new MethodNode();
- methodNode1.block = new BlockNode();
- methodNode1.setType(new BaseType(TypeEnum.INT));
-
- methodNode1.setIdentifier("testMethod");
-
- c.members.add(methodNode1);
-
- p.classes.add(c);
-
- return p;
- }
-
- public static ASTNode mockTwoDifferentMethods(){
- ProgramNode p = new ProgramNode();
-
- ClassNode c = new ClassNode();
-
- MethodNode methodNode = new MethodNode();
- methodNode.block = new BlockNode();
- methodNode.setIdentifier("testMethod");
-
- c.members.add(methodNode);
-
- MethodNode methodNode1 = new MethodNode();
- methodNode1.block = new BlockNode();
- methodNode1.setIdentifier("testMethod1");
-
- c.members.add(methodNode1);
-
- p.classes.add(c);
-
- return p;
- }
-
-}
diff --git a/src/test/java/semantic/SemanticTest.java b/src/test/java/semantic/SemanticTest.java
deleted file mode 100644
index a4039ac..0000000
--- a/src/test/java/semantic/SemanticTest.java
+++ /dev/null
@@ -1,101 +0,0 @@
-package semantic;
-
-public class SemanticTest {
-
-
-// @Test
-// public void alreadyDeclaredLocalFieldVar() {
-// ProgramNode programNode = new ProgramNode();
-// List classList = new ArrayList<>();
-// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
-// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
-//
-// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
-// ASTNode tast = semanticAnalyzer.generateTast(ast);
-//
-// MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
-// classNode.members.add(memberNode2);
-//
-// classList.add(classNode);
-// programNode.classes = classList;
-//
-// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
-//
-// assertEquals(1, SemanticAnalyzer.errors.size());
-// assertInstanceOf(AlreadyDeclaredException.class, SemanticAnalyzer.errors.getFirst());
-// assertNull(typedAst);
-// }
-//
-// @Test
-// public void shouldWorkWithNoError() {
-// ProgramNode programNode = new ProgramNode();
-// List classList = new ArrayList<>();
-// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
-// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
-//
-// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
-// ASTNode tast = semanticAnalyzer.generateTast(ast);
-//
-// assertEquals(semanticAnalyzer.errors.size(), 0);
-// assertNotNull(tast);
-//
-// MemberNode memberNode3 = getMemberNode(accessTypeNode);
-// classNode.members.add(memberNode3);
-//
-// classList.add(classNode);
-// programNode.classes = classList;
-//
-// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
-//
-// assertEquals(0, SemanticAnalyzer.errors.size());
-// assertEquals(programNode, typedAst);
-// }
-//
-// /**
-// * This method is used to create a MemberNode representing a method.
-// * It first creates a list of ParameterNodes and adds a ParameterNode to it.
-// * Then, it creates a ParameterListNode using the list of ParameterNodes.
-// * After that, it creates a list of StatementNodes and adds a StatementNode to it by calling the getStatementNode method.
-// * Finally, it creates a MethodNode using the provided AccessTypeNode, a BaseTypeNode representing the return type of the method,
-// * the method name, the ParameterListNode, and the list of StatementNodes, and returns this MethodNode.
-// *
-// * @param accessTypeNode The AccessTypeNode representing the access type of the method.
-// * @return The created MemberNode representing the method.
-// */
-//private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
-// List parameterNodeList = new ArrayList<>();
-// ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
-// parameterNodeList.add(parameterNode1);
-// ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
-//
-// List statementNodeList = new ArrayList<>();
-//
-// StatementNode statementNode1 = getStatementNode();
-// statementNodeList.add(statementNode1);
-//
-// return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
-//}
-//
-// /**
-// * This method is used to create a StatementNode for an assignment operation.
-// * It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
-// * Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
-// * After that, it creates a LiteralNode to represent the integer value 1.
-// * Finally, it creates another BinaryExpressionNode to represent the assignment operation 'this.objectVar = 1',
-// * and wraps this expression in an AssignmentStatementNode.
-// *
-// * @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
-// */
-//private static StatementNode getStatementNode() {
-// ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
-// ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
-//
-// ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
-//
-// ExpressionNode expressionNodeRight = new LiteralNode(1);
-//
-// BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
-//
-// return new AssignmentStatementNode(expressionNode);
-//}
-}
From 294df16e89451520fdc229dc45400423e4241b63 Mon Sep 17 00:00:00 2001
From: Lucas <89882946+notbad3500@users.noreply.github.com>
Date: Mon, 1 Jul 2024 23:09:53 +0200
Subject: [PATCH 2/4] small changes
---
src/test/java/main/E2EReflectionsTest.java | 177 +++++++++++++++++++
src/test/java/main/E2E_Reflections_Test.java | 70 --------
2 files changed, 177 insertions(+), 70 deletions(-)
create mode 100644 src/test/java/main/E2EReflectionsTest.java
delete mode 100644 src/test/java/main/E2E_Reflections_Test.java
diff --git a/src/test/java/main/E2EReflectionsTest.java b/src/test/java/main/E2EReflectionsTest.java
new file mode 100644
index 0000000..453d7b3
--- /dev/null
+++ b/src/test/java/main/E2EReflectionsTest.java
@@ -0,0 +1,177 @@
+package main;
+
+import ast.ASTNode;
+import ast.ProgramNode;
+import bytecode.ByteCodeGenerator;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.tree.ParseTree;
+import parser.astBuilder.ASTBuilder;
+import parser.generated.SimpleJavaLexer;
+import parser.generated.SimpleJavaParser;
+import semantic.SemanticAnalyzer;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+
+public class E2EReflectionsTest {
+
+
+ private CharStream mockInputCharStream;
+ private String outputDirectoryPath;
+ private SimpleJavaLexer mockLexer;
+ private CommonTokenStream mockTokenStream;
+ private SimpleJavaParser mockParser;
+ private ParseTree mockParseTree;
+ private ASTBuilder mockASTBuilder;
+ private ASTNode mockASTNode;
+ private SemanticAnalyzer mockSemanticAnalyzer;
+ private ASTNode mockTypedAST;
+ private ByteCodeGenerator mockByteCodeGenerator;
+
+ @BeforeEach
+ public void setUp() {
+ mockInputCharStream = mock(CharStream.class);
+ outputDirectoryPath = "path/to/output";
+ mockLexer = mock(SimpleJavaLexer.class);
+ mockTokenStream = mock(CommonTokenStream.class);
+ mockParser = mock(SimpleJavaParser.class);
+ mockParseTree = mock(ParseTree.class);
+ mockASTBuilder = mock(ASTBuilder.class);
+ mockASTNode = mock(ASTNode.class);
+ mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
+ mockTypedAST = mock(ASTNode.class);
+ mockByteCodeGenerator = mock(ByteCodeGenerator.class);
+ }
+
+ @Test
+ public void testCompileFile() throws Exception {
+ // Mock the dependencies
+ SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class);
+ CommonTokenStream mockTokenStream = mock(CommonTokenStream.class);
+ SimpleJavaParser mockParser = mock(SimpleJavaParser.class);
+ ParseTree mockParseTree = mock(ParseTree.class);
+ ASTBuilder mockASTBuilder = mock(ASTBuilder.class);
+ ASTNode mockASTNode = mock(ASTNode.class);
+ SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
+ ASTNode mockTypedAST = mock(ASTNode.class);
+ ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class);
+
+ // Mock the behavior
+ when(mockLexer.nextToken()).thenReturn(null);
+ when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
+ when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
+ when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
+ when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
+
+ // Use reflection to invoke the compileFile method
+ Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
+ compileFileMethod.setAccessible(true);
+
+ compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
+
+ // Verify each step
+ verify(mockLexer, times(1)).nextToken();
+ verify(mockTokenStream, times(1)).getTokens();
+ verify(mockParser, times(1)).program();
+ verify(mockASTBuilder, times(1)).visit(mockParseTree);
+ verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
+ verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
+ }
+
+ @Test
+ public void testCompileFile2() throws Exception {
+ // Mock the behavior
+ when(mockLexer.nextToken()).thenReturn(null);
+ when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
+ when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
+ when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
+ when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
+
+ // Use reflection to invoke the compileFile method
+ Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
+ compileFileMethod.setAccessible(true);
+
+ compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
+
+ // Verify each step
+ verify(mockLexer, times(1)).nextToken();
+ verify(mockTokenStream, times(1)).getTokens();
+ verify(mockParser, times(1)).program();
+ verify(mockASTBuilder, times(1)).visit(mockParseTree);
+ verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
+ verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
+ }
+
+
+
+
+
+
+ @Test
+ public void testLexer() {
+ // Mock the behavior
+ when(mockLexer.nextToken()).thenReturn(null);
+
+ // Test the lexer
+ SimpleJavaLexer lexer = new SimpleJavaLexer(mockInputCharStream);
+ CommonTokenStream tokenStream = new CommonTokenStream(lexer);
+ tokenStream.fill();
+
+ assertNotNull(tokenStream.getTokens());
+ verify(mockLexer, atLeastOnce()).nextToken();
+ }
+
+ @Test
+ public void testParser() {
+ // Mock the behavior
+ when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
+
+ // Test the parser
+ SimpleJavaParser parser = new SimpleJavaParser(mockTokenStream);
+ ParseTree parseTree = parser.program();
+
+ assertNotNull(parseTree);
+ verify(mockParser, times(1)).program();
+ }
+
+ @Test
+ public void testASTBuilder() {
+ // Mock the behavior
+ when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
+
+ // Test the AST builder
+ ASTBuilder astBuilder = new ASTBuilder();
+ ASTNode abstractSyntaxTree = astBuilder.visit(mockParseTree);
+
+ assertNotNull(abstractSyntaxTree);
+ verify(mockASTBuilder, times(1)).visit(mockParseTree);
+ }
+
+ @Test
+ public void testSemanticAnalyzer() {
+ // Mock the behavior
+ when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
+
+ // Test the semantic analyzer
+ ASTNode typedAst = SemanticAnalyzer.generateTast(mockASTNode);
+
+ assertNotNull(typedAst);
+ verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
+ }
+
+ @Test
+ public void testByteCodeGenerator() {
+ // Test the bytecode generator
+ ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
+ byteCodeGenerator.visit((ProgramNode) mockTypedAST);
+
+ verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
+ }
+}
+
diff --git a/src/test/java/main/E2E_Reflections_Test.java b/src/test/java/main/E2E_Reflections_Test.java
deleted file mode 100644
index 8b13819..0000000
--- a/src/test/java/main/E2E_Reflections_Test.java
+++ /dev/null
@@ -1,70 +0,0 @@
-package main;
-
-import ast.ASTNode;
-import ast.ProgramNode;
-import bytecode.ByteCodeGenerator;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-import org.antlr.v4.runtime.CharStream;
-import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.tree.ParseTree;
-import parser.astBuilder.ASTBuilder;
-import parser.generated.SimpleJavaLexer;
-import parser.generated.SimpleJavaParser;
-import semantic.SemanticAnalyzer;
-
-import static org.junit.jupiter.api.Assertions.*;
-import static org.mockito.Mockito.*;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-
-public class E2E_Reflections_Test {
-
-
- private CharStream mockInputCharStream;
- private String outputDirectoryPath;
-
- @BeforeEach
- public void setUp() {
- mockInputCharStream = mock(CharStream.class);
- outputDirectoryPath = "path/to/output";
- }
-
- @Test
- public void testCompileFile() throws Exception {
- // Mock the dependencies
- SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class);
- CommonTokenStream mockTokenStream = mock(CommonTokenStream.class);
- SimpleJavaParser mockParser = mock(SimpleJavaParser.class);
- ParseTree mockParseTree = mock(ParseTree.class);
- ASTBuilder mockASTBuilder = mock(ASTBuilder.class);
- ASTNode mockASTNode = mock(ASTNode.class);
- SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
- ASTNode mockTypedAST = mock(ASTNode.class);
- ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class);
-
- // Mock the behavior
- when(mockLexer.nextToken()).thenReturn(null);
- when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
- when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
- when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
- when(mockSemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
-
- // Use reflection to invoke the compileFile method
- Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
- compileFileMethod.setAccessible(true);
-
- compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
-
- // Verify each step
- verify(mockLexer, times(1)).nextToken();
- verify(mockTokenStream, times(1)).getTokens();
- verify(mockParser, times(1)).program();
- verify(mockASTBuilder, times(1)).visit(mockParseTree);
- verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
- verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
- }
-}
-
From 3500ffd377f1b34d3dd3c0c3a77d8a41adf010e1 Mon Sep 17 00:00:00 2001
From: Lucas <89882946+notbad3500@users.noreply.github.com>
Date: Mon, 1 Jul 2024 23:31:22 +0200
Subject: [PATCH 3/4] small changes
---
.../input/AllFeaturesClassExample.java | 42 +++++++++----------
1 file changed, 19 insertions(+), 23 deletions(-)
diff --git a/src/test/resources/input/AllFeaturesClassExample.java b/src/test/resources/input/AllFeaturesClassExample.java
index 1a8493d..73121ee 100644
--- a/src/test/resources/input/AllFeaturesClassExample.java
+++ b/src/test/resources/input/AllFeaturesClassExample.java
@@ -9,42 +9,25 @@ public class AllFeaturesClassExample {
this.b = b;
this.c = c;
}
- private class InnerClass {
- void innerMethod() {
- System.out.println("Inner class method");
- }
- }
// Methode zur Demonstration von Kontrollstrukturen
void controlStructures() {
// if-else Anweisung
if (a > 10) {
- System.out.println("a ist größer als 10");
+ // System.out.println("a ist größer als 10");
} else {
- System.out.println("a ist nicht größer als 10");
+ // System.out.println("a ist nicht größer als 10");
}
// while Schleife
while (a > 0) {
- System.out.println("a ist " + a);
+ // System.out.println("a ist " + a);
a--;
}
// for Schleife
for (int i = 0; i < 5; i++) {
- System.out.println("for Schleife Iteration: " + i);
- }
-
- // switch Anweisung
- switch (c) {
- case 'a':
- System.out.println("c ist ein 'a'");
- break;
- case 'b':
- System.out.println("c ist ein 'b'");
- break;
- default:
- System.out.println("c ist nicht 'a' oder 'b'");
+ // System.out.println("for Schleife Iteration: " + i);
}
}
@@ -52,15 +35,28 @@ public class AllFeaturesClassExample {
void logicalOperations() {
// Logische UND-Operation
if (b && a > 5) {
- System.out.println("a ist größer als 5 und b ist wahr");
+ // System.out.println("a ist größer als 5 und b ist wahr");
}
// Logische ODER-Operation
if (b || a < 5) {
- System.out.println("b ist wahr oder a ist kleiner als 5");
+ // System.out.println("b ist wahr oder a ist kleiner als 5");
}
}
+ void mathOperations() {
+ // Addition
+ int sum = a + 5;
+ // Subtraktion
+ int difference = a - 5;
+ // Multiplikation
+ int product = a * 5;
+ // Division
+ int quotient = a / 5;
+ // Modulo
+ int remainder = a % 5;
+ }
+
public static void main(String[] args) {
AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
obj.controlStructures();
From 92990e4042bba96e8edb79ec9fc68d98bc3f8883 Mon Sep 17 00:00:00 2001
From: Lucas <89882946+notbad3500@users.noreply.github.com>
Date: Mon, 1 Jul 2024 23:53:42 +0200
Subject: [PATCH 4/4] gemeinsame dc changes
---
src/main/java/ast/literal/LiteralNode.java | 15 +-
src/main/java/bytecode/ClassCodeGen.java | 2 +-
src/main/java/main/Main.java | 8 +-
src/main/resources/input/CompilerInput.java | 4 +-
src/main/resources/output/output.jar | Bin 0 -> 211 bytes
src/test/Makefile | 3 +-
src/test/resources/trees/correctRefType.json | 70 ---------
src/test/resources/trees/refTypeMismatch.json | 133 ------------------
src/test/resources/trees/test.json | 1 -
9 files changed, 17 insertions(+), 219 deletions(-)
create mode 100644 src/main/resources/output/output.jar
delete mode 100644 src/test/resources/trees/correctRefType.json
delete mode 100644 src/test/resources/trees/refTypeMismatch.json
delete mode 100644 src/test/resources/trees/test.json
diff --git a/src/main/java/ast/literal/LiteralNode.java b/src/main/java/ast/literal/LiteralNode.java
index b3cb896..e18ab7b 100644
--- a/src/main/java/ast/literal/LiteralNode.java
+++ b/src/main/java/ast/literal/LiteralNode.java
@@ -1,25 +1,24 @@
package ast.literal;
-
-import ast.expression.ExpressionNode;
-import ast.type.TypeNode;
+import ast.expressions.IExpressionNode;
+import ast.type.type.ITypeNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
-public class LiteralNode implements ExpressionNode {
+public class LiteralNode implements IExpressionNode {
public String value;
- private TypeNode type;
+ private ITypeNode type;
- public LiteralNode(String value, TypeNode type) {
+ public LiteralNode(String value, ITypeNode type) {
this.value = value;
this.type = type;
}
- public TypeNode getType() {
+ public ITypeNode getType() {
return type;
}
- public void setType(TypeNode type) {
+ public void setType(ITypeNode type) {
this.type = type;
}
diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java
index 3fec11a..84c8b92 100644
--- a/src/main/java/bytecode/ClassCodeGen.java
+++ b/src/main/java/bytecode/ClassCodeGen.java
@@ -46,8 +46,8 @@ public class ClassCodeGen implements ClassVisitor {
}
classWriter.visitEnd();
- writeToJar(classWriter.toByteArray(), classNode.identifier);
printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory);
+ writeToJar(classWriter.toByteArray(), classNode.identifier);
classWriter.visitEnd();
}
diff --git a/src/main/java/main/Main.java b/src/main/java/main/Main.java
index 7568d24..c365050 100644
--- a/src/main/java/main/Main.java
+++ b/src/main/java/main/Main.java
@@ -91,15 +91,15 @@ public class Main {
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
// Use the SemanticAnalyzer to generate a typed AST
- ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
+ //ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
// Log the typed AST
- RaupenLogger.logSemanticAnalyzer(typedAst);
+ RaupenLogger.logSemanticAnalyzer(abstractSyntaxTree);
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
- assert typedAst != null;
- byteCodeGenerator.visit((ProgramNode) typedAst);
+ assert abstractSyntaxTree != null;
+ byteCodeGenerator.visit((ProgramNode) abstractSyntaxTree);
// Log the bytecode generation
RaupenLogger.logBytecodeGenerator();
}
diff --git a/src/main/resources/input/CompilerInput.java b/src/main/resources/input/CompilerInput.java
index 825490d..b27068a 100644
--- a/src/main/resources/input/CompilerInput.java
+++ b/src/main/resources/input/CompilerInput.java
@@ -2,13 +2,15 @@ public class Compiler {
public int add(int i, int j) {
return i+j;
}
+ public static void main(String[] args) {
+ int a = 1;
+ }
}
public class Node {
public void main() {
Compiler compiler = new Compiler();
int i = compiler.add(5, 8);
- return i;
}
}
diff --git a/src/main/resources/output/output.jar b/src/main/resources/output/output.jar
new file mode 100644
index 0000000000000000000000000000000000000000..5749e1bdd41b7eec182fc508e7cfebfb99fd14d5
GIT binary patch
literal 211
zcmWIWW@Zs#;Nak3VA=ODf&mGLFt9NAx`sIFdiuHP`#So0y1532==r++JH^28+4sz8
zA8%c~i@e^tTIbH3-yCFc#rVO~Prhf)TrNH5siU_osNmdr-4ka#bss%_^puGqz?+@p
sbea4cL!brvKpf!B$RxsmuozhmWHBnh<%|GtRyL4IMj&(p(&b=n0I9+?*#H0l
literal 0
HcmV?d00001
diff --git a/src/test/Makefile b/src/test/Makefile
index 883b79c..1464161 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -10,7 +10,7 @@ compile-javac:
compile-raupenpiler:
cd ../.. ; mvn -DskipTests install
cd ../.. ; mvn exec:java -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' "
- cp ../main/resources/output/CompilerInput.class .java/resources/output/raupenpiler
+ # cp ../main/resources/output/CompilerInput.class .java/resources/output/raupenpiler
test: compile-javac compile-raupenpiler test-javac test-raupenpiler
@@ -31,6 +31,7 @@ test-raupenpiler:
clean:
# clean output folders
rm -f ../main/resources/output/*.class
+ rm -f ../main/resources/output/*.jar
rm -f ./resources/output/javac/*.class
rm -f ./resources/output/raupenpiler/*.class
# clean logs
diff --git a/src/test/resources/trees/correctRefType.json b/src/test/resources/trees/correctRefType.json
deleted file mode 100644
index 7e67bbc..0000000
--- a/src/test/resources/trees/correctRefType.json
+++ /dev/null
@@ -1,70 +0,0 @@
-{
- "classes": [
- {
- "identifier": "testClass1",
- "accessType": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "members": [
- {
- "@type": "Field",
- "accessTypeNode": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "testVar1"
- },
- {
- "@type": "Method",
- "visibility": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "testMethod",
- "parameters": {
- "parameters": [
- {
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "param1"
- }
- ]
- },
- "statements": [
- {
- "@type": "Assignment",
- "expressionLeft": {
- "@type": "InstVar",
- "identifier": "testVar1",
- "expression": {
- "@type": "This",
- "type": {
- "@type": "Reference",
- "identifier": "testClass1"
- }
- },
- "type": null
- },
- "expressionRight": {
- "@type": "Literal",
- "type": {
- "@type": "Base",
- "enumType": "INT"
- }
- }
- }
- ]
- }
- ],
- "hasConstructor": false
- }
- ]
-}
\ No newline at end of file
diff --git a/src/test/resources/trees/refTypeMismatch.json b/src/test/resources/trees/refTypeMismatch.json
deleted file mode 100644
index e5ebcd3..0000000
--- a/src/test/resources/trees/refTypeMismatch.json
+++ /dev/null
@@ -1,133 +0,0 @@
-{
- "classes": [
- {
- "identifier": "testClass1",
- "accessType": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "members": [
- {
- "@type": "Field",
- "accessTypeNode": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "testVar1"
- },
- {
- "@type": "Method",
- "visibility": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "testMethod",
- "parameters": {
- "parameters": [
- {
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "param1"
- }
- ]
- },
- "statements": [
- {
- "@type": "Assignment",
- "expressionLeft": {
- "@type": "InstVar",
- "identifier": "testVar1",
- "expression": {
- "@type": "This",
- "type": {
- "@type": "Reference",
- "identifier": "testClass1"
- }
- },
- "type": null
- },
- "expressionRight": {
- "@type": "Literal",
- "type": {
- "@type": "Base",
- "enumType": "BOOLEAN"
- }
- }
- }
- ]
- }
- ],
- "hasConstructor": false,
- "methods": [
- {
- "@type": "Method",
- "visibility": {
- "enumAccessTypeNode": "PUBLIC"
- },
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "testMethod",
- "parameters": {
- "parameters": [
- {
- "type": {
- "@type": "Base",
- "enumType": "INT"
- },
- "identifier": "param1"
- }
- ]
- },
- "statements": [
- {
- "@type": "Assignment",
- "expressionLeft": {
- "@type": "InstVar",
- "identifier": "testVar",
- "expression": {
- "@type": "InstVar",
- "identifier": "testVar",
- "expression": {
- "@type": "This",
- "type": {
- "@type": "Reference",
- "identifier": "testClass2"
- }
- },
- "type": null
- },
- "type": null
- },
- "expressionRight": {
- "@type": "Literal",
- "type": null
- },
- "type": null
- },
- {
- "@type": "VariableDeclaration",
- "type": {
- "@type": "Base",
- "enumType": "CHAR"
- },
- "identifier": "objectVar",
- "expression": {
- "@type": "Literal",
- "type": null
- }
- }
- ]
- }
- ]
- }
- ]
-}
\ No newline at end of file
diff --git a/src/test/resources/trees/test.json b/src/test/resources/trees/test.json
deleted file mode 100644
index 7acc30f..0000000
--- a/src/test/resources/trees/test.json
+++ /dev/null
@@ -1 +0,0 @@
-{"classes":[{"identifier":"testClass","accessType":{"enumAccessTypeNode":"PUBLIC"},"members":[{"@type":"Field","accessTypeNode":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"testVar1"},{"@type":"Field","accessTypeNode":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"objectVar"},{"@type":"Method","visibility":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"testVar2","parameters":{"parameters":[{"type":{"@type":"Base","enumType":"INT"},"identifier":"param1"}]},"statements":[{"@type":"Assignment","expressionLeft":{"@type":"InstVar","identifier":"objectVar","expression":{"@type":"This","type":{"@type":"Reference","identifier":"testClass"}},"type":null},"expressionRight":{"@type":"Literal","type":{"@type":"Base","enumType":"INT"}}}]}],"hasConstructor":false,"methods":[{"@type":"Method","visibility":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"testVar2","parameters":{"parameters":[{"type":{"@type":"Base","enumType":"INT"},"identifier":"param1"}]},"statements":[{"@type":"Assignment","expressionLeft":{"@type":"InstVar","identifier":"objectVar","expression":{"@type":"This","type":{"@type":"Reference","identifier":"testClass"}},"type":null},"expressionRight":{"@type":"Literal","type":{"@type":"Base","enumType":"INT"}}}]}]}]}
\ No newline at end of file