Compare commits

...

8 Commits

Author SHA1 Message Date
775beb60fe Merge pull request 'Gemeinsame Changes von Discord' (#19) from Tests into main
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Reviewed-on: #19
Reviewed-by: Johannes Ehlert <i22005@hb.dhbw-stuttgart.de>
2024-07-02 12:50:57 +00:00
Lucas
92990e4042 gemeinsame dc changes
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 23:53:42 +02:00
Lucas
3500ffd377 small changes
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 23:31:22 +02:00
Lucas
771b92bfd7 Merge branch 'main' into Tests 2024-07-01 23:31:00 +02:00
Lucas
21da31dee3 Merge branch 'main' into Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 23:10:16 +02:00
Lucas
294df16e89 small changes
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 23:09:53 +02:00
Lucas
18fc17b707 Merge branch 'main' into Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-30 18:34:02 +02:00
Lucas
bea71838ac Reflections: not running
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-30 18:33:01 +02:00
13 changed files with 231 additions and 242 deletions

View File

@ -6,6 +6,11 @@
<option name="name" value="Central Repository" /> <option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" /> <option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="maven_central" />
<option name="name" value="Maven Central" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
<remote-repository> <remote-repository>
<option name="id" value="central" /> <option name="id" value="central" />
<option name="name" value="Maven Central repository" /> <option name="name" value="Maven Central repository" />

13
pom.xml
View File

@ -44,6 +44,12 @@
<version>3.26.0</version> <version>3.26.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -78,4 +84,11 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
</project> </project>

View File

@ -1,25 +1,24 @@
package ast.literal; package ast.literal;
import ast.expressions.IExpressionNode;
import ast.expression.ExpressionNode; import ast.type.type.ITypeNode;
import ast.type.TypeNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
public class LiteralNode implements ExpressionNode { public class LiteralNode implements IExpressionNode {
public String value; 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.value = value;
this.type = type; this.type = type;
} }
public TypeNode getType() { public ITypeNode getType() {
return type; return type;
} }
public void setType(TypeNode type) { public void setType(ITypeNode type) {
this.type = type; this.type = type;
} }

View File

@ -46,8 +46,8 @@ public class ClassCodeGen implements ClassVisitor {
} }
classWriter.visitEnd(); classWriter.visitEnd();
writeToJar(classWriter.toByteArray(), classNode.identifier);
printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory); printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory);
writeToJar(classWriter.toByteArray(), classNode.identifier);
classWriter.visitEnd(); classWriter.visitEnd();
} }

View File

@ -91,15 +91,15 @@ public class Main {
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/ /*------------------------- Semantic Analyzer -> typed AST -------------------------*/
// Use the SemanticAnalyzer to generate a typed AST // Use the SemanticAnalyzer to generate a typed AST
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree); //ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
// Log the typed AST // Log the typed AST
RaupenLogger.logSemanticAnalyzer(typedAst); RaupenLogger.logSemanticAnalyzer(abstractSyntaxTree);
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/ /*------------------------- Bytecode Generator -> Bytecode -------------------------*/
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory // Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath); ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
assert typedAst != null; assert abstractSyntaxTree != null;
byteCodeGenerator.visit((ProgramNode) typedAst); byteCodeGenerator.visit((ProgramNode) abstractSyntaxTree);
// Log the bytecode generation // Log the bytecode generation
RaupenLogger.logBytecodeGenerator(); RaupenLogger.logBytecodeGenerator();
} }

View File

@ -2,13 +2,15 @@ public class Compiler {
public int add(int i, int j) { public int add(int i, int j) {
return i+j; return i+j;
} }
public static void main(String[] args) {
int a = 1;
}
} }
public class Node { public class Node {
public void main() { public void main() {
Compiler compiler = new Compiler(); Compiler compiler = new Compiler();
int i = compiler.add(5, 8); int i = compiler.add(5, 8);
return i;
} }
} }

Binary file not shown.

View File

@ -10,7 +10,7 @@ compile-javac:
compile-raupenpiler: compile-raupenpiler:
cd ../.. ; mvn -DskipTests install cd ../.. ; mvn -DskipTests install
cd ../.. ; mvn exec:java -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' " 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 test: compile-javac compile-raupenpiler test-javac test-raupenpiler
@ -31,6 +31,7 @@ test-raupenpiler:
clean: clean:
# clean output folders # clean output folders
rm -f ../main/resources/output/*.class rm -f ../main/resources/output/*.class
rm -f ../main/resources/output/*.jar
rm -f ./resources/output/javac/*.class rm -f ./resources/output/javac/*.class
rm -f ./resources/output/raupenpiler/*.class rm -f ./resources/output/raupenpiler/*.class
# clean logs # clean logs

View File

@ -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);
}
}

View File

@ -9,42 +9,25 @@ public class AllFeaturesClassExample {
this.b = b; this.b = b;
this.c = c; this.c = c;
} }
private class InnerClass {
void innerMethod() {
System.out.println("Inner class method");
}
}
// Methode zur Demonstration von Kontrollstrukturen // Methode zur Demonstration von Kontrollstrukturen
void controlStructures() { void controlStructures() {
// if-else Anweisung // if-else Anweisung
if (a > 10) { if (a > 10) {
System.out.println("a ist größer als 10"); // System.out.println("a ist größer als 10");
} else { } else {
System.out.println("a ist nicht größer als 10"); // System.out.println("a ist nicht größer als 10");
} }
// while Schleife // while Schleife
while (a > 0) { while (a > 0) {
System.out.println("a ist " + a); // System.out.println("a ist " + a);
a--; a--;
} }
// for Schleife // for Schleife
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
System.out.println("for Schleife Iteration: " + 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'");
} }
} }
@ -52,15 +35,28 @@ public class AllFeaturesClassExample {
void logicalOperations() { void logicalOperations() {
// Logische UND-Operation // Logische UND-Operation
if (b && a > 5) { 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 // Logische ODER-Operation
if (b || a < 5) { 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) { public static void main(String[] args) {
AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a'); AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
obj.controlStructures(); obj.controlStructures();

View File

@ -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
}
]
}

View File

@ -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
}
}
]
}
]
}
]
}

View File

@ -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"}}}]}]}]}