johns-branch #9

Merged
i22005 merged 13 commits from johns-branch into main 2024-05-30 17:11:51 +00:00
15 changed files with 179 additions and 11 deletions
Showing only changes of commit 1d9d7e1f00 - Show all commits

View File

@ -1,13 +1,7 @@
public class Example {
public int testVar;
public static int testMethod(char b){
int a;
a = 3;
}
}

View File

@ -1,9 +1,9 @@
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
@ -12,6 +12,8 @@ import semantic.SemanticAnalyzer;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.SQLOutput;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
@ -25,18 +27,44 @@ public class Main {
static void parsefile(CharStream codeCharStream){
/* ------------------------- Scanner -> tokens ------------------------- */
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SimpleJavaParser parser = new SimpleJavaParser(tokens);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
// Printing the tokens
tokenStream.fill();
List<Token> tokens = tokenStream.getTokens();
for (Token token : tokens) {
String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
String tokenText = token.getText();
// System.out.println("Token Type: " + tokenType + ", Token Text: " + tokenText);
System.out.println(tokenType + " " + tokenText);
}
/* ------------------------- Parser -> Parsetree ------------------------- */
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
ParseTree tree = parser.program(); // parse the input
// Printing the parse tree
System.out.println ("Parse tree: " + tree.toStringTree(parser));
/* AST builder -> AST*/
ASTBuilder builder = new ASTBuilder();
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
// Printing the AST
System.out.println("AST: " + ast.toString());
/* ------------------------- Semantic Analyzer -> Tast ------------------------- */
SemanticAnalyzer.generateTast(ast);
// Printing the Tast
System.out.println("Tast generated");
/* ------------------------- Bytecode Generator -> Bytecode ------------------------- */
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(ast);
System.out.println("Bytecode generated");
}
}

Binary file not shown.

View File

@ -21,7 +21,7 @@ public class MainTest {
void testEmptyClass() {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/java/EmptyClassExample.java"));
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/EmptyClassExample.java"));
Main.parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());

View File

@ -1,5 +1,6 @@
//import main.test.resources.EmptyClassExample.java
public class Tester {
public static void main(String[] args) {
new EmptyClassExample();
// new EmptyClassExample();
}
}

View File

@ -1,3 +1,5 @@
package resources;
public class AllFeaturesClassExample {
int a;
boolean b;

View File

@ -0,0 +1,25 @@
package resources;
public class CombinedExample {
int number;
boolean flag;
char letter;
public CombinedExample(int number, boolean flag, char letter) {
this.number = number;
this.flag = flag;
this.letter = letter;
}
public void displayValues() {
System.out.println("Number: " + number);
System.out.println("Flag: " + flag);
System.out.println("Letter: " + letter);
}
public static void main(String[] args) {
CombinedExample obj = new CombinedExample(10, true, 'X');
obj.displayValues();
}
}

View File

@ -1,3 +1,5 @@
package resources;
public class EmptyClassExample {
private class Inner {
}

View File

@ -1,3 +1,5 @@
package resources;
public class MoreFeaturesClassExample {
int hallo;
private class Inner {

View File

@ -0,0 +1,24 @@
package resources.featureTests;
public class BooleanOperations {
boolean flag;
public BooleanOperations(boolean flag) {
this.flag = flag;
}
public boolean isFlag() {
return flag;
}
public void toggleFlag() {
flag = !flag;
}
public static void main(String[] args) {
BooleanOperations obj = new BooleanOperations(true);
System.out.println(obj.isFlag());
obj.toggleFlag();
System.out.println(obj.isFlag());
}
}

View File

@ -0,0 +1,24 @@
package resources.featureTests;
public class CharManipulation {
char letter;
public CharManipulation(char letter) {
this.letter = letter;
}
public char getLetter() {
return letter;
}
public void setLetter(char letter) {
this.letter = letter;
}
public static void main(String[] args) {
CharManipulation obj = new CharManipulation('A');
System.out.println(obj.getLetter());
obj.setLetter('B');
System.out.println(obj.getLetter());
}
}

View File

@ -0,0 +1,30 @@
package resources.featureTests;
public class ConditionalStatements {
int number;
public ConditionalStatements(int number) {
this.number = number;
}
public void checkNumber() {
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
public static void main(String[] args) {
ConditionalStatements obj1 = new ConditionalStatements(5);
ConditionalStatements obj2 = new ConditionalStatements(-3);
ConditionalStatements obj3 = new ConditionalStatements(0);
obj1.checkNumber();
obj2.checkNumber();
obj3.checkNumber();
}
}

View File

@ -0,0 +1,18 @@
package resources.featureTests;
public class LoopExamples {
public static void main(String[] args) {
// For loop example
for (int i = 0; i < 5; i++) {
System.out.println("For loop iteration: " + i);
}
// While loop example
int j = 0;
while (j < 5) {
System.out.println("While loop iteration: " + j);
j++;
}
}
}

View File

@ -0,0 +1,18 @@
package resources.featureTests;
public class MethodOverloading {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
MethodOverloading obj = new MethodOverloading();
System.out.println("Sum of 2 and 3: " + obj.add(2, 3));
System.out.println("Sum of 1, 2, and 3: " + obj.add(1, 2, 3));
}
}