Merge remote-tracking branch 'origin/parser2.0' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
i22007 2024-05-31 11:20:38 +02:00
commit 514f7d724a
15 changed files with 401 additions and 381 deletions

4
.idea/misc.xml generated
View File

@ -6,7 +6,7 @@
<PerGrammarGenerationSettings>
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" />
<option name="autoGen" value="true" />
<option name="outputDir" value="C:\Users\Johannes\Documents\Github\JavaCompiler\src\main\java" />
<option name="outputDir" value="C:\Users\ARB00075\Documents\DH\Compilerbau\NichtHaskell2.0\src\main\java" />
<option name="libDir" value="" />
<option name="encoding" value="" />
<option name="pkg" value="parser.generated" />
@ -40,7 +40,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,11 @@
package ast.literal;
public class BooleanLiteralNode {
private String value;
public BooleanLiteralNode(String value) {this.value = value;}
public String getValue() {
return value;
}
}

View File

@ -0,0 +1,11 @@
package ast.literal;
public class CharLiteralNode {
public String value;
public CharLiteralNode(String value) {this.value = value;}
public String getValue() {
return value;
}
}

View File

@ -0,0 +1,30 @@
package ast.literal;
import ast.expression.ExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class LiteralNode implements ExpressionNode {
public String value;
private String type;
public LiteralNode(String value, String type) {
this.value = value;
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;
}
}

View File

@ -1,16 +1,19 @@
package ast.statement;
import ast.expression.BinaryExpressionNode;
import bytecode.visitor.MethodVisitor;
import ast.expression.ExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
public class AssignmentStatementNode extends StatementNode implements Visitable {
public BinaryExpressionNode expression;
import java.beans.Expression;
public AssignmentStatementNode(BinaryExpressionNode expression) {
public class AssignmentStatementNode extends StatementNode implements Visitable {
public String identifier;
public ExpressionNode expression;
public AssignmentStatementNode(String identifier, ExpressionNode expression) {
this.identifier = identifier;
this.expression = expression;
}

View File

@ -0,0 +1,9 @@
package ast.statement;
import java.util.List;
public class BlockStatementNode {
List<StatementNode> statements;
public BlockStatementNode(List<StatementNode> statements) {this.statements = statements;}
}

View File

@ -6,6 +6,9 @@ import ast.expression.ExpressionNode;
import ast.expression.ExpresssionOperator;
import ast.expression.IdentifierExpressionNode;
import ast.expression.UnaryExpressionNode;
import ast.literal.BooleanLiteralNode;
import ast.literal.CharLiteralNode;
import ast.literal.LiteralNode;
import ast.member.FieldNode;
import ast.member.MemberNode;
import ast.member.MethodNode;
@ -42,6 +45,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return classNode;
}
public ASTNode visitMemberDeclaration(SimpleJavaParser.MemberDeclarationContext ctx) {
if(ctx.fieldDeclaration() != null) {
return visitFieldDeclaration(ctx.fieldDeclaration());
} else if(ctx.methodDeclaration() != null) {
return visitMethodDeclaration(ctx.methodDeclaration());
} else if(ctx.constructorDeclaration() != null) {
return visitConstructorDeclaration(ctx.constructorDeclaration());
}
return null;
}
@Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType());
@ -116,12 +130,19 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) {
if (ctx.variableDeclarationStatement() != null) {
if(ctx.variableDeclarationStatement() != null) {
return visitVariableDeclarationStatement(ctx.variableDeclarationStatement());
} else if (ctx.assignmentStatement() != null) {
} else if(ctx.assignmentStatement() != null) {
return visitAssignmentStatement(ctx.assignmentStatement());
} else if(ctx.ifStatement() != null) {
return visitIfStatement(ctx.ifStatement());
} else if(ctx.whileStatement() != null) {
return visitWhileStatement(ctx.whileStatement());
} else if(ctx.returnStatement() != null) {
return visitReturnStatement(ctx.returnStatement());
} else if(ctx.block() != null) {
return visitBlock(ctx.block());
}
return null;
}
@ -130,7 +151,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
TypeNode type = (TypeNode) visit(ctx.type());
String identifier = ctx.IDENTIFIER().getText();
ExpressionNode expression = null;
if (ctx.expression() != null) {
if(ctx.expression() != null) {
expression = (ExpressionNode) visit(ctx.expression());
}
return new VariableDeclarationStatementNode(type, identifier, expression);
@ -138,14 +159,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
BinaryExpressionNode expression = (BinaryExpressionNode) visit(ctx.expression());
return new AssignmentStatementNode(expression);
}
@Override
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
return null;
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
return new AssignmentStatementNode(ctx.IDENTIFIER().getText(), expression);
}
@Override
@ -175,6 +190,15 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return new ReturnStatementNode(expression);
}
@Override
public ASTNode visitBlock(SimpleJavaParser.BlockContext ctx) {
List<StatementNode> statements = new ArrayList<>();
for(SimpleJavaParser.StatementContext stmtCtx : ctx.statement()) {
statements.add((StatementNode) visit(stmtCtx));
}
return (ASTNode) new BlockStatementNode(statements);
}
@Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
// Handle binary operations
@ -206,16 +230,33 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
LiteralContext literalContext = (LiteralContext) ctx;
try {
int intValue = Integer.parseInt(literalContext.getText());
LiteralNode literalNode = new LiteralNode(intValue);
literalNode.setType("int");
return literalNode;
} catch (NumberFormatException ignored) {}
return null; // Return null or throw an exception if no valid expression found
String type;
String value;
if(ctx.INTEGERLITERAL() != null) {
type = "int";
value = ctx.INTEGERLITERAL().getText();
return new LiteralNode(type, value);
} else if(ctx.booleanLiteral() != null) {
type= "boolean";
BooleanLiteralNode booleanNode = (BooleanLiteralNode) visitBooleanLiteral(ctx.booleanLiteral());
value = booleanNode.getValue();
return new LiteralNode(type, value);
} else if(ctx.charLiteral() != null) {
type= "char";
CharLiteralNode charNode = (CharLiteralNode) visitCharLiteral(ctx.charLiteral());
value = charNode.getValue();
return new LiteralNode(type, value);
}
return null;
}
@Override
public ASTNode visitBooleanLiteral(SimpleJavaParser.BooleanLiteralContext ctx) {
return super.visitBooleanLiteral(ctx);
}
@Override
public ASTNode visitCharLiteral(SimpleJavaParser.CharLiteralContext ctx) {
return super.visitCharLiteral(ctx);
}
}

View File

@ -29,9 +29,7 @@ statement
variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ;
assignmentStatement : var '=' expression ';' ;
var: IDENTIFIER;
assignmentStatement : IDENTIFIER '=' expression ';' ;
ifStatement : 'if' '(' expression ')' statement ('else' statement)? ;

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00075/Documents/DH/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.ParserRuleContext;
@ -168,18 +168,6 @@ public class SimpleJavaBaseListener implements SimpleJavaListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterVar(SimpleJavaParser.VarContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitVar(SimpleJavaParser.VarContext ctx) { }
/**
* {@inheritDoc}
*

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00075/Documents/DH/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
@ -103,13 +103,6 @@ public class SimpleJavaBaseVisitor<T> extends AbstractParseTreeVisitor<T> implem
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitVar(SimpleJavaParser.VarContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00075/Documents/DH/Compilerbau/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/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00075/Documents/DH/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.tree.ParseTreeListener;
@ -137,16 +137,6 @@ public interface SimpleJavaListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx);
/**
* Enter a parse tree produced by {@link SimpleJavaParser#var}.
* @param ctx the parse tree
*/
void enterVar(SimpleJavaParser.VarContext ctx);
/**
* Exit a parse tree produced by {@link SimpleJavaParser#var}.
* @param ctx the parse tree
*/
void exitVar(SimpleJavaParser.VarContext ctx);
/**
* Enter a parse tree produced by {@link SimpleJavaParser#ifStatement}.
* @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/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
// Generated from C:/Users/ARB00075/Documents/DH/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
package parser.generated;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
@ -88,12 +88,6 @@ public interface SimpleJavaVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx);
/**
* Visit a parse tree produced by {@link SimpleJavaParser#var}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitVar(SimpleJavaParser.VarContext ctx);
/**
* Visit a parse tree produced by {@link SimpleJavaParser#ifStatement}.
* @param ctx the parse tree