Fix errors in if Statments.
For and Do While loop as While loop.
This commit is contained in:
parent
3639b2a4f8
commit
12ab402746
19
.idea/ANTLRv4ToolGrammarProperties.xml
generated
Normal file
19
.idea/ANTLRv4ToolGrammarProperties.xml
generated
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ANTLRv4ToolGrammarProperties">
|
||||||
|
<option name="perGrammarGenerationSettings">
|
||||||
|
<list>
|
||||||
|
<PerGrammarGenerationSettings>
|
||||||
|
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/grammar/SimpleJava.g4" />
|
||||||
|
<option name="autoGen" value="true" />
|
||||||
|
<option name="outputDir" value="C:\Users\janni\Desktop\NichtHaskell2.0\src\main\java" />
|
||||||
|
<option name="libDir" value="" />
|
||||||
|
<option name="encoding" value="" />
|
||||||
|
<option name="pkg" value="parser.generated" />
|
||||||
|
<option name="language" value="" />
|
||||||
|
<option name="generateVisitor" value="true" />
|
||||||
|
</PerGrammarGenerationSettings>
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -1,5 +1,20 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public interface ASTNode { }
|
import bytecode.visitor.ClassVisitor;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
|
public interface ASTNode {
|
||||||
|
|
||||||
|
//Todo: @BruderJohn & @i22007 Interface anwenden + geeignetetn Methodename.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Typecheck:
|
||||||
|
public TypeCheckResult acceptType(SemanticVisitor visitor);
|
||||||
|
|
||||||
|
Bytecode:
|
||||||
|
public void accepByteCode(ClassVisitor classVisitor);
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
import ast.type.EnumAccessModifierNode;
|
|
||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import ast.member.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.member.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.member.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package ast.expression;
|
package ast.expressions;
|
||||||
|
|
||||||
public class AssignableExpressionNode {
|
public class AssignableExpressionNode {
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package ast.expression;
|
package ast.expressions;
|
||||||
|
|
||||||
public class ExpressionNode {
|
public class ExpressionNode {
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package ast.member;
|
package ast.members;
|
||||||
|
|
||||||
import ast.block.BlockNode;
|
import ast.statements.BlockStatementNode;
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
@ -13,9 +13,9 @@ public class ConstructorNode extends MethodNode implements Visitable {
|
|||||||
public AccessModifierNode accessType;
|
public AccessModifierNode accessType;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
public List<ParameterNode> parameters = new ArrayList<>();
|
||||||
public BlockNode body;
|
public BlockStatementNode body;
|
||||||
|
|
||||||
public ConstructorNode(String accessType, String identifier, BlockNode body) {
|
public ConstructorNode(String accessType, String identifier, BlockStatementNode body) {
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accessType = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.body = body;
|
this.body = body;
|
@ -1,4 +1,4 @@
|
|||||||
package ast.member;
|
package ast.members;
|
||||||
|
|
||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
@ -1,4 +1,4 @@
|
|||||||
package ast.member;
|
package ast.members;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
@ -1,4 +1,4 @@
|
|||||||
package ast.member;
|
package ast.members;
|
||||||
|
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
@ -1,4 +1,4 @@
|
|||||||
package ast.parameter;
|
package ast.parameters;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
@ -1,23 +0,0 @@
|
|||||||
package ast.statement;
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.expression.ExpressionNode;
|
|
||||||
|
|
||||||
public class ForStatementNode implements ASTNode {
|
|
||||||
ExpressionNode statementExpressionInit;
|
|
||||||
StatementNode localVariableDeclarationInit;
|
|
||||||
ExpressionNode expression;
|
|
||||||
ExpressionNode statementExpression;
|
|
||||||
|
|
||||||
public ForStatementNode(ExpressionNode statementExpressionInit, ExpressionNode expression, ExpressionNode statementExpression) {
|
|
||||||
this.statementExpressionInit = statementExpressionInit;
|
|
||||||
this.expression = expression;
|
|
||||||
this.statementExpression = statementExpression;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ForStatementNode(StatementNode localVariableDeclarationInit, ExpressionNode expression, ExpressionNode statementExpression) {
|
|
||||||
this.localVariableDeclarationInit = localVariableDeclarationInit;
|
|
||||||
this.expression = expression;
|
|
||||||
this.statementExpression = statementExpression;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,2 +0,0 @@
|
|||||||
package ast.statement;public class StatementNode {
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package ast.statement;
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.block.BlockNode;
|
|
||||||
import ast.expression.ExpressionNode;
|
|
||||||
|
|
||||||
public class WhileStatementNode implements ASTNode {
|
|
||||||
ExpressionNode expression;
|
|
||||||
BlockNode block;
|
|
||||||
|
|
||||||
public WhileStatementNode(ExpressionNode expression, BlockNode block) {
|
|
||||||
this.expression = expression;
|
|
||||||
this.block = block;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package ast.statement.ifstatement;
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.block.BlockNode;
|
|
||||||
|
|
||||||
public class ElseStatementNode implements ASTNode {
|
|
||||||
BlockNode block;
|
|
||||||
|
|
||||||
public ElseStatementNode(BlockNode block) {
|
|
||||||
this.block = block;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package ast.statement.ifstatement;
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class IfElseStatementNode implements ASTNode {
|
|
||||||
IfStatementNode ifStatement;
|
|
||||||
List<ElseStatementNode> elseStatements = new ArrayList<>();
|
|
||||||
|
|
||||||
public IfElseStatementNode(IfStatementNode ifStatement) {
|
|
||||||
this.ifStatement = ifStatement;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addElseStatement(ElseStatementNode elseStatement) {
|
|
||||||
elseStatements.add(elseStatement);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package ast.statement.ifstatement;
|
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.block.BlockNode;
|
|
||||||
import ast.expression.ExpressionNode;
|
|
||||||
|
|
||||||
public class IfStatementNode implements ASTNode {
|
|
||||||
ExpressionNode expression;
|
|
||||||
BlockNode block;
|
|
||||||
|
|
||||||
public IfStatementNode(ExpressionNode expression, BlockNode block) {
|
|
||||||
this.expression = expression;
|
|
||||||
this.block = block;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package ast.statement.statementexpression.methodcallstatementnexpression;
|
|
||||||
|
|
||||||
public class TargetNode {
|
|
||||||
String thisTarget;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +1,8 @@
|
|||||||
package ast.statement.statementexpression;
|
package ast.statementexpressions;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.AssignableExpressionNode;
|
import ast.expressions.AssignableExpressionNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
public class AssignStatementExpressionNode implements ASTNode {
|
public class AssignStatementExpressionNode implements ASTNode {
|
||||||
AssignableExpressionNode assignable;
|
AssignableExpressionNode assignable;
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statement.statementexpression;
|
package ast.statementexpressions;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statement.statementexpression;
|
package ast.statementexpressions;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statement.statementexpression.methodcallstatementnexpression;
|
package ast.statementexpressions.methodcallstatementnexpression;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statement.statementexpression.methodcallstatementnexpression;
|
package ast.statementexpressions.methodcallstatementnexpression;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -0,0 +1,7 @@
|
|||||||
|
package ast.statementexpressions.methodcallstatementnexpression;
|
||||||
|
|
||||||
|
public class TargetNode {
|
||||||
|
String thisTarget;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,16 +1,13 @@
|
|||||||
package ast.block;
|
package ast.statements;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.statement.StatementNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BlockNode implements ASTNode {
|
public class BlockStatementNode extends StatementNode implements ASTNode {
|
||||||
public List<StatementNode> statements = new ArrayList<>();
|
public List<StatementNode> statements = new ArrayList<>();
|
||||||
|
|
||||||
public BlockNode() {}
|
|
||||||
|
|
||||||
public void addStatement(StatementNode statement) {
|
public void addStatement(StatementNode statement) {
|
||||||
statements.add(statement);
|
statements.add(statement);
|
||||||
}
|
}
|
13
src/main/java/ast/statements/ElseStatementNode.java
Normal file
13
src/main/java/ast/statements/ElseStatementNode.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ast.statements;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
public class ElseStatementNode implements ASTNode {
|
||||||
|
BlockStatementNode block;
|
||||||
|
|
||||||
|
public ElseStatementNode(BlockStatementNode block) {
|
||||||
|
this.block = block;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
22
src/main/java/ast/statements/IfElseStatementNode.java
Normal file
22
src/main/java/ast/statements/IfElseStatementNode.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package ast.statements;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class IfElseStatementNode implements ASTNode {
|
||||||
|
IfStatementNode ifStatement;
|
||||||
|
List<IfStatementNode> elseIfStatements = new ArrayList<>();
|
||||||
|
ElseStatementNode elseStatement;
|
||||||
|
|
||||||
|
public IfElseStatementNode(IfStatementNode ifStatement, ElseStatementNode elseStatementNode) {
|
||||||
|
this.ifStatement = ifStatement;
|
||||||
|
this.elseStatement = elseStatementNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addElseIfStatement(IfStatementNode elseIfStament) {
|
||||||
|
elseIfStatements.add(elseIfStament);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
src/main/java/ast/statements/IfStatementNode.java
Normal file
14
src/main/java/ast/statements/IfStatementNode.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package ast.statements;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
|
public class IfStatementNode implements ASTNode {
|
||||||
|
ExpressionNode expression;
|
||||||
|
BlockStatementNode block;
|
||||||
|
|
||||||
|
public IfStatementNode(ExpressionNode expression, BlockStatementNode block) {
|
||||||
|
this.expression = expression;
|
||||||
|
this.block = block;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statement;
|
package ast.statements;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
||||||
|
|
||||||
public class LocalVariableDeclarationNode implements ASTNode {
|
public class LocalVariableDeclarationNode implements ASTNode {
|
@ -1,11 +1,7 @@
|
|||||||
package ast.statement;
|
package ast.statements;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
import ast.type.TypeNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ReturnStatementNode implements ASTNode {
|
public class ReturnStatementNode implements ASTNode {
|
||||||
public ExpressionNode expression;
|
public ExpressionNode expression;
|
2
src/main/java/ast/statements/StatementNode.java
Normal file
2
src/main/java/ast/statements/StatementNode.java
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
package ast.statements;public class StatementNode {
|
||||||
|
}
|
14
src/main/java/ast/statements/WhileStatementNode.java
Normal file
14
src/main/java/ast/statements/WhileStatementNode.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package ast.statements;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
import ast.expressions.ExpressionNode;
|
||||||
|
|
||||||
|
public class WhileStatementNode extends StatementNode implements ASTNode {
|
||||||
|
ExpressionNode expression;
|
||||||
|
BlockStatementNode block;
|
||||||
|
|
||||||
|
public WhileStatementNode(ExpressionNode expression, BlockStatementNode block) {
|
||||||
|
this.expression = expression;
|
||||||
|
this.block = block;
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.member.FieldNode;
|
import ast.members.FieldNode;
|
||||||
import ast.member.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.member.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import ast.type.BaseTypeNode;
|
import ast.type.BaseTypeNode;
|
||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
import ast.type.*;
|
import ast.type.*;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import ast.type.BaseTypeNode;
|
import ast.type.BaseTypeNode;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
import ast.member.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.member.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
import ast.type.BaseTypeNode;
|
import ast.type.BaseTypeNode;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package bytecode.visitor;
|
package bytecode.visitor;
|
||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.member.FieldNode;
|
import ast.members.FieldNode;
|
||||||
|
|
||||||
public interface ClassVisitor {
|
public interface ClassVisitor {
|
||||||
void visit(ClassNode classNode);
|
void visit(ClassNode classNode);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package bytecode.visitor;
|
package bytecode.visitor;
|
||||||
|
|
||||||
import ast.member.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.member.MethodNode;
|
import ast.members.MethodNode;
|
||||||
|
|
||||||
public interface MethodVisitor {
|
public interface MethodVisitor {
|
||||||
void visit(ConstructorNode constructorNode);
|
void visit(ConstructorNode constructorNode);
|
||||||
|
@ -1,24 +1,27 @@
|
|||||||
package parser.astBuilder;
|
package parser.astBuilder;
|
||||||
|
|
||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.block.BlockNode;
|
import ast.statements.BlockStatementNode;
|
||||||
import ast.expression.AssignableExpressionNode;
|
import ast.expressions.AssignableExpressionNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expressions.ExpressionNode;
|
||||||
import ast.statement.ifstatement.ElseStatementNode;
|
import ast.statements.ElseStatementNode;
|
||||||
import ast.statement.ifstatement.IfElseStatementNode;
|
import ast.statements.IfElseStatementNode;
|
||||||
import ast.member.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.member.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
import ast.statement.*;
|
import ast.statements.*;
|
||||||
import ast.statement.ifstatement.IfStatementNode;
|
import ast.statements.IfStatementNode;
|
||||||
import ast.statement.statementexpression.AssignStatementExpressionNode;
|
import ast.statementexpressions.AssignStatementExpressionNode;
|
||||||
import ast.statement.statementexpression.CrementExpressionStatementExpressionNode;
|
import ast.statementexpressions.CrementExpressionStatementExpressionNode;
|
||||||
import ast.statement.statementexpression.methodcallstatementnexpression.ChainedMethodNode;
|
import ast.statementexpressions.methodcallstatementnexpression.ChainedMethodNode;
|
||||||
import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode;
|
import ast.statementexpressions.methodcallstatementnexpression.MethodCallStatementExpressionNode;
|
||||||
import ast.statement.statementexpression.methodcallstatementnexpression.TargetNode;
|
import ast.statementexpressions.methodcallstatementnexpression.TargetNode;
|
||||||
import ast.type.*;
|
import ast.type.*;
|
||||||
import parser.generated.*;
|
import parser.generated.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) {
|
public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) {
|
||||||
@ -41,7 +44,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
|
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
|
||||||
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.block()));
|
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockStatementNode) visit(ctx.block()));
|
||||||
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
||||||
constructorNode.addParameter((ParameterNode) visit(parameter));
|
constructorNode.addParameter((ParameterNode) visit(parameter));
|
||||||
}
|
}
|
||||||
@ -59,8 +62,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
return visitReturnStatement(ctx.returnStatement());
|
return visitReturnStatement(ctx.returnStatement());
|
||||||
} else if(ctx.localVariableDeclaration() != null) {
|
} else if(ctx.localVariableDeclaration() != null) {
|
||||||
return visitLocalVariableDeclaration(ctx.localVariableDeclaration());
|
return visitLocalVariableDeclaration(ctx.localVariableDeclaration());
|
||||||
} else if(ctx.block() != null) {
|
} else if(ctx.blockStatement() != null) {
|
||||||
return visitBlock(ctx.block());
|
return visitBlockStatement(ctx.blockStatement());
|
||||||
} else if(ctx.whileStatement() != null) {
|
} else if(ctx.whileStatement() != null) {
|
||||||
return visitWhileStatement(ctx.whileStatement());
|
return visitWhileStatement(ctx.whileStatement());
|
||||||
} else if(ctx.forStatement() != null) {
|
} else if(ctx.forStatement() != null) {
|
||||||
@ -84,8 +87,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitBlock(SimpleJavaParser.BlockContext ctx) {
|
public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) {
|
||||||
BlockNode blockNode = new BlockNode();
|
BlockStatementNode blockNode = new BlockStatementNode();
|
||||||
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
|
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
|
||||||
blockNode.addStatement((StatementNode) visit(statement));
|
blockNode.addStatement((StatementNode) visit(statement));
|
||||||
}
|
}
|
||||||
@ -94,36 +97,86 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
|
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
|
||||||
return new WhileStatementNode((ExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block()));
|
return new WhileStatementNode((ExpressionNode) visit(ctx.expression()), (BlockStatementNode) visit(ctx.blockStatement()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ASTNode visitDoWhileStatement(SimpleJavaParser.DoWhileStatementContext ctx) {
|
||||||
|
ExpressionNode condition = (ExpressionNode) visit(ctx.expression());
|
||||||
|
BlockStatementNode doBlock = (BlockStatementNode) visit(ctx.blockStatement());
|
||||||
|
|
||||||
|
WhileStatementNode whileStatement = new WhileStatementNode(condition, doBlock);
|
||||||
|
BlockStatementNode resultBlock = new BlockStatementNode();
|
||||||
|
resultBlock.addStatement(doBlock);
|
||||||
|
resultBlock.addStatement(whileStatement);
|
||||||
|
|
||||||
|
return resultBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
|
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
|
||||||
|
|
||||||
|
List<StatementNode> statements = new ArrayList<>();
|
||||||
|
|
||||||
|
//init
|
||||||
if(ctx.statementExpression(0) != null){
|
if(ctx.statementExpression(0) != null){
|
||||||
return new ForStatementNode((ExpressionNode) visit(ctx.statementExpression(0)), (ExpressionNode) visit(ctx.expression()), (ExpressionNode) visit(ctx.statementExpression(1)));
|
statements.add((StatementNode) visit(ctx.statementExpression(0)));
|
||||||
} else if (ctx.localVariableDeclaration() != null) {
|
} else if (ctx.localVariableDeclaration() != null) {
|
||||||
return new ForStatementNode((StatementNode) visit(ctx.localVariableDeclaration()), (ExpressionNode) visit(ctx.expression()), (ExpressionNode) visit(ctx.statementExpression(1)));
|
statements.add((StatementNode) visit(ctx.localVariableDeclaration()));
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
//condition
|
||||||
|
ExpressionNode condition = (ExpressionNode) visit(ctx.expression());
|
||||||
|
|
||||||
|
//ink
|
||||||
|
StatementNode increment = null;
|
||||||
|
if(ctx.statementExpression(1) != null){
|
||||||
|
increment = (StatementNode) visit(ctx.statementExpression(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockStatementNode forBlock = (BlockStatementNode) visit(ctx.blockStatement());
|
||||||
|
|
||||||
|
if(increment != null){
|
||||||
|
forBlock.addStatement((increment));
|
||||||
|
}
|
||||||
|
|
||||||
|
WhileStatementNode whileStatement = new WhileStatementNode(condition, forBlock);
|
||||||
|
|
||||||
|
statements.add(whileStatement);
|
||||||
|
|
||||||
|
BlockStatementNode resultBlock = new BlockStatementNode();
|
||||||
|
for(StatementNode statement : statements) {
|
||||||
|
resultBlock.addStatement(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
|
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
|
||||||
IfElseStatementNode ifElseStatementNode = new IfElseStatementNode((IfStatementNode) visit(ctx.ifStatement()));
|
IfElseStatementNode ifElseStatementNode = new IfElseStatementNode((IfStatementNode) visit(ctx.ifStatement()),
|
||||||
for(SimpleJavaParser.ElseStatementContext elseStatement : ctx.elseStatement()) {
|
(ElseStatementNode) visit(ctx.elseStatement()));
|
||||||
ifElseStatementNode.addElseStatement((ElseStatementNode) visit(elseStatement));
|
|
||||||
|
for (SimpleJavaParser.ElseIfStatementContext elseIfStatement : ctx.elseIfStatement()){
|
||||||
|
ifElseStatementNode.addElseIfStatement(((IfStatementNode) visit(elseIfStatement)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ifElseStatementNode;
|
return ifElseStatementNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
|
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
|
||||||
return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block()));
|
return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockStatementNode) visit(ctx.blockStatement()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ASTNode visitElseIfStatement(SimpleJavaParser.ElseIfStatementContext ctx) {
|
||||||
|
return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockStatementNode) visit(ctx.blockStatement()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) {
|
public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) {
|
||||||
return new ElseStatementNode((BlockNode) visit(ctx.block()));
|
return new ElseStatementNode((BlockStatementNode) visit(ctx.blockStatement()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -6,9 +6,9 @@ program: classDeclaration+;
|
|||||||
classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket;
|
classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket;
|
||||||
memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration;
|
memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration;
|
||||||
|
|
||||||
constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block;
|
constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement;
|
||||||
fieldDeclaration: AccessModifier? type Identifier Semicolon;
|
fieldDeclaration: AccessModifier? type Identifier Semicolon;
|
||||||
methodDeclaration: MainMethodDeclaration block | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block;
|
methodDeclaration: MainMethodDeclaration blockStatement | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement;
|
||||||
|
|
||||||
parameterList: parameter (Comma parameter)*;
|
parameterList: parameter (Comma parameter)*;
|
||||||
parameter: type Identifier;
|
parameter: type Identifier;
|
||||||
@ -17,23 +17,26 @@ argumentList: (expression (Comma expression)*)?;
|
|||||||
// Anweisungen
|
// Anweisungen
|
||||||
statement: returnStatement Semicolon
|
statement: returnStatement Semicolon
|
||||||
| localVariableDeclaration Semicolon
|
| localVariableDeclaration Semicolon
|
||||||
| block
|
| blockStatement
|
||||||
| whileStatement
|
| whileStatement
|
||||||
|
| doWhileStatement
|
||||||
| forStatement
|
| forStatement
|
||||||
| ifElseStatement
|
| ifElseStatement
|
||||||
| statementExpression Semicolon;
|
| statementExpression Semicolon;
|
||||||
|
|
||||||
block: OpenCurlyBracket statement* ClosedCurlyBracket;
|
blockStatement: OpenCurlyBracket statement* ClosedCurlyBracket;
|
||||||
|
|
||||||
returnStatement: Return (expression)?;
|
returnStatement: Return (expression)?;
|
||||||
localVariableDeclaration: type Identifier (Assign expression)?;
|
localVariableDeclaration: type Identifier (Assign expression)?;
|
||||||
|
|
||||||
whileStatement: While OpenRoundBracket expression ClosedRoundBracket block;
|
whileStatement: While OpenRoundBracket expression ClosedRoundBracket blockStatement;
|
||||||
forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket block;
|
doWhileStatement: Do blockStatement While OpenRoundBracket expression ClosedRoundBracket Semicolon;
|
||||||
|
forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket blockStatement;
|
||||||
|
|
||||||
ifElseStatement: ifStatement elseStatement*;
|
ifElseStatement: ifStatement elseIfStatement* elseStatement?;
|
||||||
ifStatement: If OpenRoundBracket expression ClosedRoundBracket block;
|
ifStatement: If OpenRoundBracket expression ClosedRoundBracket blockStatement;
|
||||||
elseStatement: Else block;
|
elseIfStatement: Else If OpenRoundBracket expression ClosedRoundBracket blockStatement;
|
||||||
|
elseStatement: Else blockStatement;
|
||||||
|
|
||||||
statementExpression: assign | newDeclaration | methodCall | crementExpression;
|
statementExpression: assign | newDeclaration | methodCall | crementExpression;
|
||||||
assign: assignableExpression Assign expression;
|
assign: assignableExpression Assign expression;
|
||||||
@ -144,6 +147,7 @@ Comma: ',';
|
|||||||
Class: 'class';
|
Class: 'class';
|
||||||
This: 'this';
|
This: 'this';
|
||||||
While: 'while';
|
While: 'while';
|
||||||
|
Do: 'do';
|
||||||
If: 'if';
|
If: 'if';
|
||||||
Else: 'else';
|
Else: 'else';
|
||||||
For: 'for';
|
For: 'for';
|
||||||
|
@ -2,17 +2,17 @@ package semantic;
|
|||||||
|
|
||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.expression.LiteralNode;
|
import ast.expressions.LiteralNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.expression.BinaryExpressionNode;
|
import ast.expressions.BinaryExpressionNode;
|
||||||
import ast.expression.IdentifierExpressionNode;
|
import ast.expressions.IdentifierExpressionNode;
|
||||||
import ast.expression.InstVar;
|
import ast.expressions.InstVar;
|
||||||
import ast.expression.UnaryExpressionNode;
|
import ast.expressions.UnaryExpressionNode;
|
||||||
import ast.member.FieldNode;
|
import ast.members.FieldNode;
|
||||||
import ast.member.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import ast.statement.*;
|
import ast.statements.*;
|
||||||
import ast.expression.This;
|
import ast.expressions.This;
|
||||||
import ast.statement.ifstatement.IfStatementNode;
|
import ast.statements.IfStatementNode;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public interface SemanticVisitor {
|
public interface SemanticVisitor {
|
||||||
|
Loading…
Reference in New Issue
Block a user