Merge branch 'main' of https://gitea.hb.dhbw-stuttgart.de/i22005/NichtHaskell2.0 into code-generator
This commit is contained in:
commit
79a140b683
4
.idea/misc.xml
generated
4
.idea/misc.xml
generated
@ -6,10 +6,10 @@
|
|||||||
<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\Maxi\Documents\DHBW\Compilerbau\NichtHaskell2.0\src\main\java\parser" />
|
<option name="outputDir" value="C:\Users\Johannes\Documents\Github\JavaCompiler\src\main\java" />
|
||||||
<option name="libDir" value="" />
|
<option name="libDir" value="" />
|
||||||
<option name="encoding" value="" />
|
<option name="encoding" value="" />
|
||||||
<option name="pkg" value="generated" />
|
<option name="pkg" value="parser.generated" />
|
||||||
<option name="language" value="" />
|
<option name="language" value="" />
|
||||||
<option name="generateVisitor" value="true" />
|
<option name="generateVisitor" value="true" />
|
||||||
</PerGrammarGenerationSettings>
|
</PerGrammarGenerationSettings>
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
public class Example {
|
public class Example {
|
||||||
|
|
||||||
public int test;
|
public int testVar;
|
||||||
|
|
||||||
public Example(int conInput) {
|
public static int testMethod(char b){
|
||||||
|
|
||||||
}
|
int a;
|
||||||
|
a = 3;
|
||||||
|
|
||||||
public static int test(char b){
|
|
||||||
|
|
||||||
test = 3;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
public char test;
|
|
||||||
|
|
||||||
}
|
|
@ -15,11 +15,8 @@ import java.nio.file.Paths;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
CharStream codeCharStream = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
|
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
|
||||||
parsefile(codeCharStream);
|
parsefile(codeCharStream);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error reading the file: " + e.getMessage());
|
System.err.println("Error reading the file: " + e.getMessage());
|
||||||
@ -28,7 +25,6 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
static void parsefile(CharStream codeCharStream){
|
static void parsefile(CharStream codeCharStream){
|
||||||
// CharStream codeCharStream = CharStreams.fromString("class javaFileInput.Example { } class Example2 { }");
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
||||||
@ -38,14 +34,7 @@ public class Main {
|
|||||||
ASTBuilder builder = new ASTBuilder();
|
ASTBuilder builder = new ASTBuilder();
|
||||||
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
||||||
|
|
||||||
// Optionally print or process the AST
|
SemanticAnalyzer.generateTast(ast);
|
||||||
System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:");
|
|
||||||
for (ClassNode classNode : ast.classes) {
|
|
||||||
System.out.println(classNode.identifier.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
|
||||||
semanticAnalyzer.analyze(ast);
|
|
||||||
|
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||||
byteCodeGenerator.visit(ast);
|
byteCodeGenerator.visit(ast);
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
public class ASTNode {
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public abstract class ASTNode { }
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,17 +9,19 @@ import bytecode.visitor.ClassVisitor;
|
|||||||
import bytecode.visitor.Visitable;
|
import bytecode.visitor.Visitable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class ClassNode extends ASTNode implements Visitable {
|
public class ClassNode extends ASTNode implements Visitable {
|
||||||
public IdentifierNode identifier;
|
public String identifier;
|
||||||
public AccessTypeNode accessType;
|
public AccessTypeNode accessType;
|
||||||
public String name;
|
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
public boolean hasConstructor = false;
|
public boolean hasConstructor = false;
|
||||||
|
|
||||||
public ClassNode(AccessTypeNode accessType, String name){
|
public ClassNode(AccessTypeNode accessType, String identifier){
|
||||||
this.accessType = accessType;
|
this.accessType = accessType;
|
||||||
this.name = name;
|
this.identifier = identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMember(MemberNode member) {
|
public void addMember(MemberNode member) {
|
||||||
@ -31,13 +33,13 @@ public class ClassNode extends ASTNode implements Visitable {
|
|||||||
|
|
||||||
public void ensureConstructor(){
|
public void ensureConstructor(){
|
||||||
if(!hasConstructor) {
|
if(!hasConstructor) {
|
||||||
ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), name);
|
ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), identifier);
|
||||||
members.add(0,constructor);
|
members.add(0,constructor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(ClassVisitor classVisitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
classVisitor.visit(this);
|
return visitor.typeCheck(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
package ast;
|
|
||||||
|
|
||||||
public class IdentifierNode {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public IdentifierNode(String name){
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if(obj instanceof IdentifierNode){
|
|
||||||
IdentifierNode identifier = (IdentifierNode) obj;
|
|
||||||
if(name.equals(identifier.getName())){
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.equals(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
23
src/main/java/ast/LiteralNode.java
Normal file
23
src/main/java/ast/LiteralNode.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package ast;
|
||||||
|
|
||||||
|
import ast.expression.ExpressionNode;
|
||||||
|
|
||||||
|
public class LiteralNode extends ExpressionNode {
|
||||||
|
|
||||||
|
int value;
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
public LiteralNode(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -4,8 +4,11 @@ import bytecode.visitor.ProgramVisitor;
|
|||||||
import bytecode.visitor.Visitable;
|
import bytecode.visitor.Visitable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class ProgramNode extends ASTNode implements Visitable {
|
public class ProgramNode extends ASTNode implements Visitable{
|
||||||
public List<ClassNode> classes = new ArrayList<>();
|
public List<ClassNode> classes = new ArrayList<>();
|
||||||
|
|
||||||
public void addClass(ClassNode classNode) {
|
public void addClass(ClassNode classNode) {
|
||||||
@ -13,7 +16,7 @@ public class ProgramNode extends ASTNode implements Visitable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(ProgramVisitor programVisitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
programVisitor.visit(this);
|
return visitor.typeCheck(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
21
src/main/java/ast/VarNode.java
Normal file
21
src/main/java/ast/VarNode.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package ast;
|
||||||
|
|
||||||
|
public class VarNode extends ASTNode{
|
||||||
|
|
||||||
|
private String identifier;
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
public VarNode(String type, String identifier){
|
||||||
|
this.type = type;
|
||||||
|
this.identifier = identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType(){
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdentifier(){
|
||||||
|
return identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,4 +4,6 @@ import ast.ASTNode;
|
|||||||
|
|
||||||
public class ExpressionNode extends ASTNode {
|
public class ExpressionNode extends ASTNode {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,24 @@
|
|||||||
package ast.member;
|
package ast.member;
|
||||||
|
|
||||||
import ast.IdentifierNode;
|
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class FieldNode extends MemberNode {
|
public class FieldNode extends MemberNode implements Visitable {
|
||||||
public AccessTypeNode accessTypeNode;
|
public AccessTypeNode accessTypeNode;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public IdentifierNode identifier;
|
public String identifier;
|
||||||
|
|
||||||
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
|
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
|
||||||
this.accessTypeNode = accessTypeNode;
|
this.accessTypeNode = accessTypeNode;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.identifier = new IdentifierNode(name);
|
this.identifier = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.typeCheck(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package ast.member;
|
package ast.member;
|
||||||
|
|
||||||
import ast.IdentifierNode;
|
|
||||||
import ast.parameter.ParameterListNode;
|
import ast.parameter.ParameterListNode;
|
||||||
import ast.statement.StatementNode;
|
import ast.statement.StatementNode;
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
@ -8,29 +7,35 @@ import ast.type.TypeNode;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class MethodNode extends MemberNode {
|
public class MethodNode extends MemberNode implements Visitable {
|
||||||
public IdentifierNode identifier;
|
|
||||||
public AccessTypeNode visibility;
|
public AccessTypeNode visibility;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String name;
|
public String identifier;
|
||||||
|
|
||||||
public ParameterListNode parameters;
|
public ParameterListNode parameters;
|
||||||
|
|
||||||
public List<StatementNode> statements = new ArrayList<>();
|
public List<StatementNode> statements = new ArrayList<>();
|
||||||
|
|
||||||
public MethodNode(AccessTypeNode visibility, TypeNode type, String name, ParameterListNode parameters,
|
public MethodNode(AccessTypeNode visibility, TypeNode type, String identifier, ParameterListNode parameters,
|
||||||
List<StatementNode> statements){
|
List<StatementNode> statements){
|
||||||
this.visibility = visibility;
|
this.visibility = visibility;
|
||||||
this.identifier = new IdentifierNode(name);
|
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.name = name;
|
this.identifier = identifier;
|
||||||
this.parameters = parameters;
|
this.parameters = parameters;
|
||||||
this.statements = statements;
|
this.statements = statements;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodNode(AccessTypeNode visibility, String name){
|
public MethodNode(AccessTypeNode visibility, String identifier){
|
||||||
this.visibility = visibility;
|
this.visibility = visibility;
|
||||||
this.identifier = new IdentifierNode(name);
|
this.identifier = identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.typeCheck(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
|
import ast.VarNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import typechecker.Visitable;
|
||||||
|
|
||||||
public class AssignmentStatementNode extends StatementNode {
|
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
||||||
public String identifier;
|
public VarNode varNode;
|
||||||
public ExpressionNode expression;
|
public ExpressionNode expression;
|
||||||
|
|
||||||
public AssignmentStatementNode(String identifier, ExpressionNode expression) {
|
public AssignmentStatementNode(VarNode varNode, ExpressionNode expression) {
|
||||||
this.identifier = identifier;
|
this.varNode = varNode;
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.typeCheck(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
public void visit(ClassNode classNode) {
|
public void visit(ClassNode classNode) {
|
||||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||||
Mapper mapper = new Mapper();
|
Mapper mapper = new Mapper();
|
||||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.name, null,
|
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
||||||
"java/lang/Object", null);
|
"java/lang/Object", null);
|
||||||
|
|
||||||
for (MemberNode memberNode : classNode.members) {
|
for (MemberNode memberNode : classNode.members) {
|
||||||
@ -34,7 +34,7 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
|
printIntoClassFile(classWriter.toByteArray(), classNode.identifier);
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,6 @@ public class FieldCodeGen {
|
|||||||
|
|
||||||
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
|
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
|
||||||
Mapper mapper = new Mapper();
|
Mapper mapper = new Mapper();
|
||||||
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier.getName(), "", null, null);
|
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, "", null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -16,11 +16,10 @@ import ast.type.EnumAccessTypeNode;
|
|||||||
import ast.type.EnumTypeNode;
|
import ast.type.EnumTypeNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
import parser.generated.SimpleJavaBaseVisitor;
|
|
||||||
import parser.generated.SimpleJavaParser;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import parser.generated.*;
|
||||||
|
import parser.generated.SimpleJavaParser.LiteralContext;
|
||||||
|
|
||||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||||
@Override
|
@Override
|
||||||
@ -35,7 +34,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
@Override
|
@Override
|
||||||
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
||||||
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
|
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
|
||||||
classNode.identifier = new IdentifierNode(ctx.IDENTIFIER().getText());
|
classNode.identifier = ctx.IDENTIFIER().getText();
|
||||||
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
||||||
classNode.addMember((MemberNode) visit(member));
|
classNode.addMember((MemberNode) visit(member));
|
||||||
}
|
}
|
||||||
@ -135,9 +134,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
||||||
String identifier = ctx.IDENTIFIER().getText();
|
VarNode varNode = (VarNode) visit(ctx.var());
|
||||||
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
|
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
|
||||||
return new AssignmentStatementNode(identifier, expression);
|
return new AssignmentStatementNode(varNode, expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
|
||||||
|
return new VarNode("int", ctx.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -196,4 +200,18 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
return null; // Return null or throw an exception if no valid expression found
|
return null; // Return null or throw an exception if no valid expression found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,9 @@ statement
|
|||||||
|
|
||||||
variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ;
|
variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ;
|
||||||
|
|
||||||
assignmentStatement : IDENTIFIER '=' expression ';' ;
|
assignmentStatement : var '=' 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
@ -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/Johannes/Documents/Github/JavaCompiler/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,6 +168,18 @@ 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}
|
||||||
*
|
*
|
||||||
|
@ -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/Johannes/Documents/Github/JavaCompiler/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,6 +103,13 @@ 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}
|
||||||
*
|
*
|
||||||
|
@ -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/Johannes/Documents/Github/JavaCompiler/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;
|
||||||
|
@ -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/Johannes/Documents/Github/JavaCompiler/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,6 +137,16 @@ 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
@ -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/Johannes/Documents/Github/JavaCompiler/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,6 +88,12 @@ 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
|
||||||
|
@ -2,60 +2,91 @@ package semantic;
|
|||||||
|
|
||||||
|
|
||||||
import ast.*;
|
import ast.*;
|
||||||
|
import ast.expression.ExpressionNode;
|
||||||
import ast.member.ConstructorNode;
|
import ast.member.ConstructorNode;
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MemberNode;
|
import ast.member.MemberNode;
|
||||||
|
|
||||||
|
import ast.member.MethodNode;
|
||||||
|
import ast.statement.AssignmentStatementNode;
|
||||||
|
import ast.statement.StatementNode;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class SemanticAnalyzer {
|
public class SemanticAnalyzer implements SemanticVisitor {
|
||||||
|
|
||||||
List<IdentifierNode> usedIdentifier = new ArrayList<>();
|
private ArrayList<String> currentFields = new ArrayList<>();
|
||||||
|
|
||||||
public void analyze(ASTNode node) {
|
public static ASTNode generateTast(ASTNode node) throws RuntimeException {
|
||||||
if (node == null) return;
|
SemanticAnalyzer semanticCheck = new SemanticAnalyzer();
|
||||||
|
|
||||||
if (node instanceof ClassNode) {
|
|
||||||
|
|
||||||
ClassNode classNode = (ClassNode) node;
|
|
||||||
if(classNode.identifier == null){
|
|
||||||
System.out.println("Klasse besitzt keinen Identifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<MemberNode> memberNodes = classNode.members;
|
|
||||||
for (MemberNode member: memberNodes) {
|
|
||||||
analyze(member);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}else if (node instanceof ConstructorNode) {
|
|
||||||
|
|
||||||
}else if (node instanceof FieldNode) {
|
|
||||||
|
|
||||||
FieldNode fieldNode = (FieldNode) node;
|
|
||||||
if(identifierAlreadyUsed(fieldNode.identifier)){
|
|
||||||
//throw new RuntimeException("Error: Identifier already used");
|
|
||||||
System.out.println("Error: Identifier already used");
|
|
||||||
}
|
|
||||||
usedIdentifier.add(fieldNode.identifier);
|
|
||||||
|
|
||||||
}else if (node instanceof ProgramNode) {
|
|
||||||
ProgramNode programNode = (ProgramNode) node;
|
ProgramNode programNode = (ProgramNode) node;
|
||||||
List<ClassNode> classes = programNode.classes;
|
var result = programNode.accept(semanticCheck);
|
||||||
for (ClassNode classNode: classes) {
|
if (result.isValid()) {
|
||||||
analyze(classNode);
|
return node;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean identifierAlreadyUsed(IdentifierNode identifier){
|
|
||||||
if(usedIdentifier.contains(identifier)){
|
|
||||||
return true;
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
throw new RuntimeException("Not Valid");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult typeCheck(ProgramNode node) {
|
||||||
|
|
||||||
|
var valid = true;
|
||||||
|
|
||||||
|
List<ClassNode> classes = node.classes;
|
||||||
|
for (ClassNode classNode : classes) {
|
||||||
|
classNode.accept(this);
|
||||||
|
}
|
||||||
|
return new TypeCheckResult(valid, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult typeCheck(ClassNode classNode) {
|
||||||
|
List<MemberNode> members = classNode.members;
|
||||||
|
for (MemberNode memberNode : members) {
|
||||||
|
if (memberNode instanceof FieldNode fieldNode) {
|
||||||
|
fieldNode.accept(this);
|
||||||
|
} else if (memberNode instanceof MethodNode methodNode) {
|
||||||
|
methodNode.accept(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult typeCheck(MethodNode methodNode) {
|
||||||
|
List<StatementNode> statements = methodNode.statements;
|
||||||
|
for (StatementNode statement : statements) {
|
||||||
|
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||||
|
assignmentStatementNode.accept(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult typeCheck(FieldNode toCheck) {
|
||||||
|
if(currentFields.contains(toCheck.identifier)){
|
||||||
|
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
||||||
|
}else {
|
||||||
|
currentFields.add(toCheck.identifier);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult typeCheck(AssignmentStatementNode assignmentStatementNode) {
|
||||||
|
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||||
|
VarNode varNode = assignmentStatementNode.varNode;
|
||||||
|
if(varNode.getType().equals(literalNode.getType())) {
|
||||||
|
System.out.println("Type is same");
|
||||||
|
} else {
|
||||||
|
System.out.println("Type Mismatch");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
62
src/main/java/semantic/SemanticVisitor.java
Normal file
62
src/main/java/semantic/SemanticVisitor.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package semantic;
|
||||||
|
|
||||||
|
|
||||||
|
import ast.ClassNode;
|
||||||
|
import ast.ProgramNode;
|
||||||
|
import ast.member.FieldNode;
|
||||||
|
import ast.member.MethodNode;
|
||||||
|
import ast.statement.AssignmentStatementNode;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
|
public interface SemanticVisitor {
|
||||||
|
// TypeCheckResult typeCheck(ASTNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult typeCheck(ProgramNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult typeCheck(ClassNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult typeCheck(MethodNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult typeCheck(FieldNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult typeCheck(AssignmentStatementNode toCheck);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(MethodParameter toCheck);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(ForStmt forStmt);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(WhileStmt whileStmt);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(ReturnStmt returnStmt);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(LocalVarDecl localVarDecl);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(IfStmt ifStmt);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(Block block);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(NewDecl newDecl);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(MethodCall methodCall);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(Unary unary);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(This aThis);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(Null aNull);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(LocalOrFieldVar localOrFieldVar);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(IntegerExpr integerExpr);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(InstVar instVar);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(CharExpr charExpr);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(BoolExpr boolExpr);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(Binary binary);
|
||||||
|
//
|
||||||
|
// TypeCheckResult typeCheck(StringExpr instVar);
|
||||||
|
|
||||||
|
}
|
5
src/main/java/typechecker/Type.java
Normal file
5
src/main/java/typechecker/Type.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package typechecker;
|
||||||
|
|
||||||
|
public interface Type {
|
||||||
|
boolean equals(Object obj);
|
||||||
|
}
|
21
src/main/java/typechecker/TypeCheckResult.java
Normal file
21
src/main/java/typechecker/TypeCheckResult.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package typechecker;
|
||||||
|
|
||||||
|
|
||||||
|
public class TypeCheckResult {
|
||||||
|
|
||||||
|
private boolean valid;
|
||||||
|
private Type type;
|
||||||
|
|
||||||
|
public TypeCheckResult(boolean valid, Type type) {
|
||||||
|
this.valid = valid;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValid() {
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
16
src/main/java/typechecker/Visitable.java
Normal file
16
src/main/java/typechecker/Visitable.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package typechecker;
|
||||||
|
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
|
||||||
|
public interface Visitable {
|
||||||
|
// default void accept(ProgramCodeVisitor visitor) {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// default void accept(ClassCodeVisitor visitor) {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// default void accept(MethodCodeVisitor visitor) {
|
||||||
|
// }
|
||||||
|
|
||||||
|
TypeCheckResult accept(SemanticVisitor visitor);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user