Compare commits

..

2 Commits

Author SHA1 Message Date
514f7d724a Merge remote-tracking branch 'origin/parser2.0' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-05-31 11:20:38 +02:00
Maximilian Stahl
de5c2a5002 Added Parser Visitors
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-05-31 11:16:25 +02:00
15 changed files with 401 additions and 381 deletions

4
.idea/misc.xml generated
View File

@ -6,7 +6,7 @@
<PerGrammarGenerationSettings> <PerGrammarGenerationSettings>
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" /> <option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" />
<option name="autoGen" value="true" /> <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="libDir" value="" />
<option name="encoding" value="" /> <option name="encoding" value="" />
<option name="pkg" value="parser.generated" /> <option name="pkg" value="parser.generated" />
@ -40,7 +40,7 @@
</list> </list>
</option> </option>
</component> </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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </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; package ast.statement;
import ast.expression.BinaryExpressionNode; import ast.expression.BinaryExpressionNode;
import ast.expression.ExpressionNode;
import bytecode.visitor.MethodVisitor;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
import visitor.Visitable; import visitor.Visitable;
public class AssignmentStatementNode extends StatementNode implements Visitable { import java.beans.Expression;
public BinaryExpressionNode 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; 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.ExpresssionOperator;
import ast.expression.IdentifierExpressionNode; import ast.expression.IdentifierExpressionNode;
import ast.expression.UnaryExpressionNode; import ast.expression.UnaryExpressionNode;
import ast.literal.BooleanLiteralNode;
import ast.literal.CharLiteralNode;
import ast.literal.LiteralNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MemberNode; import ast.member.MemberNode;
import ast.member.MethodNode; import ast.member.MethodNode;
@ -42,6 +45,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return classNode; 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 @Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType()); AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType());
@ -116,12 +130,19 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) { public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) {
if (ctx.variableDeclarationStatement() != null) { if(ctx.variableDeclarationStatement() != null) {
return visitVariableDeclarationStatement(ctx.variableDeclarationStatement()); return visitVariableDeclarationStatement(ctx.variableDeclarationStatement());
} else if (ctx.assignmentStatement() != null) { } else if(ctx.assignmentStatement() != null) {
return visitAssignmentStatement(ctx.assignmentStatement()); 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; return null;
} }
@ -130,7 +151,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
TypeNode type = (TypeNode) visit(ctx.type()); TypeNode type = (TypeNode) visit(ctx.type());
String identifier = ctx.IDENTIFIER().getText(); String identifier = ctx.IDENTIFIER().getText();
ExpressionNode expression = null; ExpressionNode expression = null;
if (ctx.expression() != null) { if(ctx.expression() != null) {
expression = (ExpressionNode) visit(ctx.expression()); expression = (ExpressionNode) visit(ctx.expression());
} }
return new VariableDeclarationStatementNode(type, identifier, expression); return new VariableDeclarationStatementNode(type, identifier, expression);
@ -138,14 +159,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
BinaryExpressionNode expression = (BinaryExpressionNode) visit(ctx.expression()); return new AssignmentStatementNode(ctx.IDENTIFIER().getText(), expression);
return new AssignmentStatementNode(expression);
}
@Override
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
return null;
} }
@Override @Override
@ -175,6 +190,15 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return new ReturnStatementNode(expression); 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 @Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) { public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
// Handle binary operations // Handle binary operations
@ -206,16 +230,33 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) { public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
LiteralContext literalContext = (LiteralContext) ctx; String type;
try { String value;
int intValue = Integer.parseInt(literalContext.getText()); if(ctx.INTEGERLITERAL() != null) {
LiteralNode literalNode = new LiteralNode(intValue); type = "int";
value = ctx.INTEGERLITERAL().getText();
literalNode.setType("int"); return new LiteralNode(type, value);
return literalNode; } else if(ctx.booleanLiteral() != null) {
} catch (NumberFormatException ignored) {} type= "boolean";
BooleanLiteralNode booleanNode = (BooleanLiteralNode) visitBooleanLiteral(ctx.booleanLiteral());
return null; // Return null or throw an exception if no valid expression found 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)? ';' ; variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ;
assignmentStatement : var '=' expression ';' ; assignmentStatement : IDENTIFIER '=' expression ';' ;
var: IDENTIFIER;
ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; 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; package parser.generated;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.ParserRuleContext;
@ -168,18 +168,6 @@ public class SimpleJavaBaseListener implements SimpleJavaListener {
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { } @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} * {@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; package parser.generated;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; 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> * {@link #visitChildren} on {@code ctx}.</p>
*/ */
@Override public T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { return visitChildren(ctx); } @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} * {@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; package parser.generated;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream; 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; package parser.generated;
import org.antlr.v4.runtime.tree.ParseTreeListener; import org.antlr.v4.runtime.tree.ParseTreeListener;
@ -137,16 +137,6 @@ public interface SimpleJavaListener extends ParseTreeListener {
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx); 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}. * Enter a parse tree produced by {@link SimpleJavaParser#ifStatement}.
* @param ctx the parse tree * @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; package parser.generated;
import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.ParseTreeVisitor;
@ -88,12 +88,6 @@ public interface SimpleJavaVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result * @return the visitor result
*/ */
T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx); 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}. * Visit a parse tree produced by {@link SimpleJavaParser#ifStatement}.
* @param ctx the parse tree * @param ctx the parse tree