Compare commits

..

25 Commits

Author SHA1 Message Date
Lucas
6cec17eb45 Merge remote-tracking branch 'origin/code-generator' into Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-03 00:47:49 +02:00
87be850a0e Revert "Merge branch 'main' into code-generator"
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This reverts commit 8956362033, reversing
changes made to b58fa00c9a.
2024-07-02 18:07:48 -04:00
72f82ff863 Revert "Merge branch 'NewParser' into code-generator"
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This reverts commit f0dd6d5eb6, reversing
changes made to 8956362033.
2024-07-02 18:07:42 -04:00
7419953510 Revert "Merge branch 'johns-branch' into code-generator"
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This reverts commit 449b895d20, reversing
changes made to f0dd6d5eb6.
2024-07-02 18:07:26 -04:00
449b895d20 Merge branch 'johns-branch' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 17:52:23 -04:00
f0dd6d5eb6 Merge branch 'NewParser' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 17:52:04 -04:00
8956362033 Merge branch 'main' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 17:51:24 -04:00
3227d69fc1 Merged Main and Code-Generator
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
2024-07-02 20:25:15 +02:00
4ca6972ccd Merge branch 'code-generator' of https://gitea.hb.dhbw-stuttgart.de/i22005/NichtHaskell2.0 into NewParser 2024-07-02 20:10:54 +02:00
0047f6c08e Merge remote-tracking branch 'origin/NewParser' into NewParser
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 20:08:23 +02:00
88ce9e52f0 More Tests for ASTBuilder
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 20:08:08 +02:00
Purplumbi504
437de74cc6 New Grammer
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 19:13:59 +02:00
Purplumbi504
ca77307f0c Merge remote-tracking branch 'origin/NewParser' into NewParser
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 19:12:29 +02:00
Purplumbi504
5f46130439 Adding Switch Case AST 2024-07-02 19:12:19 +02:00
3e1456351c Merge remote-tracking branch 'origin/NewParser' into NewParser
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
# Conflicts:
#	src/test/java/parser/AstBuilderTest.java
2024-07-02 18:00:29 +02:00
d26cd0c13a For Test 2024-07-02 17:57:48 +02:00
Purplumbi504
cfde5219a4 Merge remote-tracking branch 'origin/NewParser' into NewParser
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 17:51:11 +02:00
Purplumbi504
f414e278bb Adding Test Cases 2024-07-02 17:50:58 +02:00
Maximilian Stahl
741a56cb99 Merge remote-tracking branch 'origin/NewParser' into NewParser
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
# Conflicts:
#	src/main/java/parser/astBuilder/ASTBuilder.java
2024-07-02 17:39:05 +02:00
Maximilian Stahl
02e5f3a729 Fixes and changed For Builder 2024-07-02 17:36:48 +02:00
Bruder John
f7338a06b3 added MemberAccess to Assignable
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 15:47:33 +02:00
Bruder John
3996082fa7 Merge branch 'main' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 14:52:13 +02:00
Bruder John
2537051668 fix if field and parameter have same identifier
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 14:31:44 +02:00
Bruder John
82356ec189 set types of expressions
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 10:36:22 +02:00
Bruder John
561eafbf4c fixed constructor node parameter decleration
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-02 08:48:37 +02:00
12 changed files with 234 additions and 229 deletions

View File

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

13
pom.xml
View File

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

View File

@ -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(abstractSyntaxTree);
RaupenLogger.logSemanticAnalyzer(typedAst);
/*------------------------- 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 abstractSyntaxTree != null;
byteCodeGenerator.visit((ProgramNode) abstractSyntaxTree);
assert typedAst != null;
byteCodeGenerator.visit((ProgramNode) typedAst);
// Log the bytecode generation
RaupenLogger.logBytecodeGenerator();
}

View File

@ -75,12 +75,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
ConstructorNode constructorNode;
if(ctx.AccessModifier() != null) {
constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
} else {
constructorNode = new ConstructorNode(null, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
}
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
constructorNode.addParameter((ParameterNode) visit(parameter));

View File

@ -2,15 +2,13 @@ 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;
}
}

Binary file not shown.

View File

@ -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,7 +31,6 @@ 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

View File

@ -1,177 +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.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,25 +9,42 @@ 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);
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'");
}
}
@ -35,28 +52,15 @@ 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();

View File

@ -0,0 +1,70 @@
{
"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

@ -0,0 +1,133 @@
{
"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

@ -0,0 +1 @@
{"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"}}}]}]}]}