Compare commits

..

No commits in common. "b7737939373a025c78ca009fd30cc94114a81837" and "43cafcd01f757c7eb033375906368f4271a1bc64" have entirely different histories.

42 changed files with 352 additions and 679 deletions

4
.idea/misc.xml generated
View File

@ -6,10 +6,10 @@
<PerGrammarGenerationSettings>
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" />
<option name="autoGen" value="true" />
<option name="outputDir" value="C:\Users\Maxi\Documents\DHBW\Compilerbau\NichtHaskell2.0\src\main\java\parser" />
<option name="outputDir" value="C:\Users\ARB00075\Documents\DH\Compilerbau\NichtHaskell2.0\src\main\java\parser\out" />
<option name="libDir" value="" />
<option name="encoding" value="" />
<option name="pkg" value="generated" />
<option name="pkg" value="" />
<option name="language" value="" />
<option name="generateVisitor" value="true" />
</PerGrammarGenerationSettings>

View File

@ -2,11 +2,7 @@ public class Example {
public int test;
public Example(int conInput) {
}
public static int test(char b){
public static int TestMethod(char b){
test = 3;

View File

@ -1,6 +1,4 @@
package ast.type;
import ast.ASTNode;
package ast;
public class AccessTypeNode extends ASTNode {
public EnumAccessTypeNode enumAccessTypeNode;

View File

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

View File

@ -1,4 +0,0 @@
package ast;
public class BlockNode {
}

View File

@ -1,15 +1,10 @@
package ast;
import ast.member.ConstructorNode;
import ast.member.MemberNode;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import java.util.ArrayList;
import java.util.List;
public class ClassNode extends ASTNode{
public IdentifierNode identifier;
public Identifier identifier;
public AccessTypeNode accessType;
public String name;
public List<MemberNode> members = new ArrayList<>();

View File

@ -1,8 +1,6 @@
package ast.member;
package ast;
import ast.type.AccessTypeNode;
public class ConstructorNode extends MethodNode {
public class ConstructorNode extends MethodNode{
public ConstructorNode(AccessTypeNode visibility, String name) {
super(visibility, name);
}

View File

@ -1,4 +1,4 @@
package ast.type;
package ast;
public enum EnumAccessTypeNode {
PUBLIC, PRIVATE

View File

@ -1,4 +1,4 @@
package ast.type;
package ast;
public enum EnumTypeNode {
INT, BOOLEAN, CHAR

View File

@ -0,0 +1,11 @@
package ast;
public class ExpressionNode extends ASTNode {
public int value;
public ExpressionNode(int value) {
this.value = value;
}
}

View File

@ -1,18 +1,14 @@
package ast.member;
import ast.IdentifierNode;
import ast.type.AccessTypeNode;
import ast.type.TypeNode;
package ast;
public class FieldNode extends MemberNode {
public AccessTypeNode accessTypeNode;
public TypeNode type;
public IdentifierNode identifier;
public Identifier identifier;
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
this.accessTypeNode = accessTypeNode;
this.type = type;
this.identifier = new IdentifierNode(name);
this.identifier = new Identifier(name);
}
}

View File

@ -1,10 +1,10 @@
package ast;
public class IdentifierNode {
public class Identifier {
private String name;
public IdentifierNode(String name){
public Identifier(String name){
this.name = name;
}
@ -13,8 +13,8 @@ public class IdentifierNode {
}
public boolean equals(Object obj) {
if(obj instanceof IdentifierNode){
IdentifierNode identifier = (IdentifierNode) obj;
if(obj instanceof Identifier){
Identifier identifier = (Identifier) obj;
if(name.equals(identifier.getName())){
return true;
} else {

View File

@ -0,0 +1,4 @@
package ast;
public class MemberNode extends ASTNode{
}

View File

@ -1,16 +1,10 @@
package ast.member;
import ast.IdentifierNode;
import ast.parameter.ParameterListNode;
import ast.statement.StatementNode;
import ast.type.AccessTypeNode;
import ast.type.TypeNode;
package ast;
import java.util.ArrayList;
import java.util.List;
public class MethodNode extends MemberNode {
public IdentifierNode identifier;
public class MethodNode extends MemberNode{
public Identifier identifier;
public AccessTypeNode visibility;
public TypeNode type;
public String name;
@ -22,7 +16,7 @@ public class MethodNode extends MemberNode {
public MethodNode(AccessTypeNode visibility, TypeNode type, String name, ParameterListNode parameters,
List<StatementNode> statements){
this.visibility = visibility;
this.identifier = new IdentifierNode(name);
this.identifier = new Identifier(name);
this.type = type;
this.name = name;
this.parameters = parameters;
@ -31,6 +25,6 @@ public class MethodNode extends MemberNode {
public MethodNode(AccessTypeNode visibility, String name){
this.visibility = visibility;
this.identifier = new IdentifierNode(name);
this.identifier = new Identifier(name);
}
}

View File

@ -1,6 +1,4 @@
package ast.parameter;
import ast.ASTNode;
package ast;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,7 +1,4 @@
package ast.parameter;
import ast.ASTNode;
import ast.type.TypeNode;
package ast;
public class ParameterNode extends ASTNode {
public TypeNode type;

View File

@ -1,4 +1,4 @@
package ast.statement;
package ast;
import ast.ASTNode;

View File

@ -1,6 +1,4 @@
package ast.type;
import ast.ASTNode;
package ast;
public class TypeNode extends ASTNode {
public EnumTypeNode enumTypeNode;

View File

@ -1,12 +1,10 @@
package ast.statement;
import ast.type.TypeNode;
import ast.expression.ExpressionNode;
package ast;
public class VariableDeclarationStatementNode extends StatementNode {
public TypeNode type;
public String identifier;
public ExpressionNode expression;
public ExpressionNode expression; // Nullable for cases without initialization
public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) {
this.type = type;
this.identifier = identifier;

View File

@ -1,13 +0,0 @@
package ast.expression;
public class BinaryExpressionNode extends ExpressionNode {
public ExpressionNode left;
public ExpressionNode right;
public String operator; // Stores the operator as a string (e.g., "+", "-", "&&")
public BinaryExpressionNode(ExpressionNode left, ExpressionNode right, String operator) {
this.left = left;
this.right = right;
this.operator = operator;
}
}

View File

@ -1,7 +0,0 @@
package ast.expression;
import ast.ASTNode;
public class ExpressionNode extends ASTNode {
}

View File

@ -1,9 +0,0 @@
package ast.expression;
public class IdentifierExpressionNode extends ExpressionNode {
public String name;
public IdentifierExpressionNode(String name) {
this.name = name;
}
}

View File

@ -1,11 +0,0 @@
package ast.expression;
public class UnaryExpressionNode extends ExpressionNode {
public ExpressionNode expression;
public String operator; // Stores the operator (e.g., "-", "!")
public UnaryExpressionNode(ExpressionNode expression, String operator) {
this.expression = expression;
this.operator = operator;
}
}

View File

@ -1,6 +0,0 @@
package ast.member;
import ast.ASTNode;
public class MemberNode extends ASTNode {
}

View File

@ -1,13 +0,0 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class AssignmentStatementNode extends StatementNode {
public String identifier;
public ExpressionNode expression;
public AssignmentStatementNode(String identifier, ExpressionNode expression) {
this.identifier = identifier;
this.expression = expression;
}
}

View File

@ -1,15 +0,0 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class IfStatementNode extends StatementNode {
public ExpressionNode condition;
public StatementNode thenStatement;
public StatementNode elseStatement;
public IfStatementNode(ExpressionNode condition, StatementNode thenStatement, StatementNode elseStatement) {
this.condition = condition;
this.thenStatement = thenStatement;
this.elseStatement = elseStatement;
}
}

View File

@ -1,11 +0,0 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class ReturnStatementNode extends StatementNode {
public ExpressionNode expression;
public ReturnStatementNode(ExpressionNode expression) {
this.expression = expression;
}
}

View File

@ -1,13 +0,0 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class WhileStatementNode extends StatementNode {
public ExpressionNode condition;
public StatementNode body;
public WhileStatementNode(ExpressionNode condition, StatementNode body) {
this.condition = condition;
this.body = body;
}
}

View File

@ -1,9 +1,9 @@
package bytecode;
import ast.ClassNode;
import ast.member.FieldNode;
import ast.member.MemberNode;
import ast.member.MethodNode;
import ast.FieldNode;
import ast.MemberNode;
import ast.MethodNode;
import java.io.File;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

View File

@ -1,8 +1,9 @@
package bytecode;
import ast.member.FieldNode;
import ast.FieldNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
public class FieldCodeGen {

View File

@ -1,7 +1,7 @@
package bytecode;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import ast.AccessTypeNode;
import ast.EnumAccessTypeNode;
import org.objectweb.asm.Opcodes;
public class Mapper {

View File

@ -1,8 +1,9 @@
package bytecode;
import ast.member.MethodNode;
import ast.MethodNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class MethodCodeGen {
public void generateMethodCode(ClassWriter classWriter, MethodNode methodNode) {

View File

@ -1,26 +1,12 @@
package parser;
import ast.*;
import ast.expression.BinaryExpressionNode;
import ast.expression.ExpressionNode;
import ast.expression.IdentifierExpressionNode;
import ast.expression.UnaryExpressionNode;
import ast.member.FieldNode;
import ast.member.MemberNode;
import ast.member.MethodNode;
import ast.parameter.ParameterListNode;
import ast.parameter.ParameterNode;
import ast.statement.*;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import ast.type.EnumTypeNode;
import ast.type.TypeNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import parser.generated.SimpleJavaBaseVisitor;
import parser.generated.SimpleJavaParser;
import java.util.ArrayList;
import java.util.List;
import parser.generated.SimpleJavaParser.LiteralContext;
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
@ -35,7 +21,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
classNode.identifier = new IdentifierNode(ctx.IDENTIFIER().getText());
classNode.identifier = new Identifier(ctx.IDENTIFIER().getText());
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
classNode.addMember((MemberNode) visit(member));
}
@ -122,78 +108,26 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null;
}
@Override
public ASTNode visitVariableDeclarationStatement(SimpleJavaParser.VariableDeclarationStatementContext ctx) {
TypeNode type = (TypeNode) visit(ctx.type());
String identifier = ctx.IDENTIFIER().getText();
ExpressionNode expression = null;
if (ctx.expression() != null) {
expression = (ExpressionNode) visit(ctx.expression());
}
return new VariableDeclarationStatementNode(type, identifier, expression);
}
@Override
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
String identifier = ctx.IDENTIFIER().getText();
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
return new AssignmentStatementNode(identifier, expression);
}
@Override
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
ExpressionNode condition = (ExpressionNode) visit(ctx.expression());
StatementNode thenStatement = (StatementNode) visit(ctx.statement(0)); // The 'then' branch
StatementNode elseStatement = null;
if (ctx.statement().size() > 1) {
elseStatement = (StatementNode) visit(ctx.statement(1)); // The 'else' branch, if present
}
return new IfStatementNode(condition, thenStatement, elseStatement);
}
@Override
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
ExpressionNode condition = (ExpressionNode) visit(ctx.expression()); // Visit the condition part of the while statement
StatementNode body = (StatementNode) visit(ctx.statement()); // Visit the body part of the while statement
return new WhileStatementNode(condition, body);
}
@Override
public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) {
ExpressionNode expression = null;
if (ctx.expression() != null) {
expression = (ExpressionNode) visit(ctx.expression()); // Visit the expression part of the return statement, if it exists
}
return new ReturnStatementNode(expression);
ExpressionNode expressionNode = (ExpressionNode) visit(ctx.expression());
return new AssignmentNode(ctx.IDENTIFIER().getText(), expressionNode);
}
@Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
// Handle binary operations
if (ctx.getChildCount() == 3 && ctx.getChild(1) instanceof TerminalNode) {
ExpressionNode left = (ExpressionNode) visit(ctx.expression(0));
ExpressionNode right = (ExpressionNode) visit(ctx.expression(1));
String operator = ctx.getChild(1).getText();
return new BinaryExpressionNode(left, right, operator);
if(ctx.literal() != null){
return visitLiteral(ctx.literal());
}
// Handle unary operations
else if (ctx.getChildCount() == 2) {
String operator = ctx.getChild(0).getText();
ExpressionNode expression = (ExpressionNode) visit(ctx.expression(0));
return new UnaryExpressionNode(expression, operator);
}
// Handle parentheses
else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) {
return visit(ctx.expression(0)); // Simply return the inner expression
}
// Handle literals and identifiers
else if (ctx.literal() != null) {
return visit(ctx.literal());
}
else if (ctx.IDENTIFIER() != null) {
return new IdentifierExpressionNode(ctx.IDENTIFIER().getText());
}
return null; // Return null or throw an exception if no valid expression found
return null;
}
@Override
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
if(ctx.INTEGERLITERAL() != null){
return new ExpressionNode(Integer.parseInt(ctx.INTEGERLITERAL().getSymbol().getText()));
}
return null;
}
}

View File

@ -4,14 +4,12 @@ program : classDeclaration+;
classDeclaration : accessType 'class' IDENTIFIER '{' memberDeclaration* '}';
memberDeclaration : fieldDeclaration | methodDeclaration | constructorDeclaration;
memberDeclaration : fieldDeclaration | methodDeclaration;
fieldDeclaration : accessType type IDENTIFIER ';';
methodDeclaration : accessType 'static' type IDENTIFIER '(' parameterList? ')' '{' statement* '}' ;
constructorDeclaration : accessType IDENTIFIER '(' parameterList? ')' '{' statement* '}' ;
parameterList : parameter (',' parameter)* ;
parameter : type IDENTIFIER ;

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00072/Desktop/DHBW/4. Semester/Compilerbau/Endprojekt/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.ParserRuleContext;
@ -72,18 +72,6 @@ public class SimpleJavaBaseListener implements SimpleJavaListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { }
/**
* {@inheritDoc}
*

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00072/Desktop/DHBW/4. Semester/Compilerbau/Endprojekt/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
@ -47,13 +47,6 @@ public class SimpleJavaBaseVisitor<T> extends AbstractParseTreeVisitor<T> implem
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00072/Desktop/DHBW/4. Semester/Compilerbau/Endprojekt/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00072/Desktop/DHBW/4. Semester/Compilerbau/Endprojekt/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.tree.ParseTreeListener;
@ -57,16 +57,6 @@ public interface SimpleJavaListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx);
/**
* Enter a parse tree produced by {@link SimpleJavaParser#constructorDeclaration}.
* @param ctx the parse tree
*/
void enterConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx);
/**
* Exit a parse tree produced by {@link SimpleJavaParser#constructorDeclaration}.
* @param ctx the parse tree
*/
void exitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx);
/**
* Enter a parse tree produced by {@link SimpleJavaParser#parameterList}.
* @param ctx the parse tree

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00072/Desktop/DHBW/4. Semester/Compilerbau/Endprojekt/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
@ -40,12 +40,6 @@ public interface SimpleJavaVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx);
/**
* Visit a parse tree produced by {@link SimpleJavaParser#constructorDeclaration}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx);
/**
* Visit a parse tree produced by {@link SimpleJavaParser#parameterList}.
* @param ctx the parse tree

View File

@ -2,9 +2,6 @@ package semantic;
import ast.*;
import ast.member.ConstructorNode;
import ast.member.FieldNode;
import ast.member.MemberNode;
import java.util.ArrayList;
import java.util.List;