changed to visitor pattern

This commit is contained in:
Bruder John 2024-05-09 22:39:26 +02:00
parent 0052b5b547
commit 2bae802cb2
15 changed files with 218 additions and 71 deletions

View File

@ -1,16 +1,12 @@
public class Example {
public int Example;
public int testVar;
public Example(int conInput) {
public static int testMethod(char b){
}
int a;
a = 3;
public static int test(char b){
char Example;
boolean Example;
int c;
}

View File

@ -15,11 +15,8 @@ import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws Exception {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
@ -28,7 +25,6 @@ public class Main {
static void parsefile(CharStream codeCharStream){
// CharStream codeCharStream = CharStreams.fromString("class javaFileInput.Example { } class Example2 { }");
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SimpleJavaParser parser = new SimpleJavaParser(tokens);
@ -38,14 +34,7 @@ public class Main {
ASTBuilder builder = new ASTBuilder();
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
// Optionally print or process the 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);
SemanticAnalyzer.generateTast(ast);
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(ast);

View File

@ -1,8 +1,7 @@
package ast;
import java.util.ArrayList;
import java.util.List;
public class ASTNode {
public abstract class ASTNode { }
}

View File

@ -7,8 +7,11 @@ import ast.type.EnumAccessTypeNode;
import java.util.ArrayList;
import java.util.List;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class ClassNode extends ASTNode{
public class ClassNode extends ASTNode implements Visitable {
public IdentifierNode identifier;
public AccessTypeNode accessType;
public String name;
@ -33,4 +36,9 @@ public class ClassNode extends ASTNode{
members.add(0,constructor);
}
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
}
}

View File

@ -1,6 +1,10 @@
package ast;
public class IdentifierNode extends ASTNode{
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class IdentifierNode extends ASTNode implements Visitable {
private String name;
@ -24,4 +28,8 @@ public class IdentifierNode extends ASTNode{
return super.equals(obj);
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
}
}

View File

@ -2,11 +2,19 @@ package ast;
import java.util.ArrayList;
import java.util.List;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class ProgramNode extends ASTNode {
public class ProgramNode extends ASTNode implements Visitable{
public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) {
classes.add(classNode);
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
}
}

View File

@ -3,16 +3,23 @@ package ast.member;
import ast.IdentifierNode;
import ast.type.AccessTypeNode;
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 TypeNode type;
public IdentifierNode identifier;
public String identifier;
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
this.accessTypeNode = accessTypeNode;
this.type = type;
this.identifier = new IdentifierNode(name);
this.identifier = name;
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
}
}

View File

@ -8,29 +8,35 @@ import ast.type.TypeNode;
import java.util.ArrayList;
import java.util.List;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.Visitable;
public class MethodNode extends MemberNode {
public IdentifierNode identifier;
public class MethodNode extends MemberNode implements Visitable {
public AccessTypeNode visibility;
public TypeNode type;
public String name;
public String identifier;
public ParameterListNode parameters;
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){
this.visibility = visibility;
this.identifier = new IdentifierNode(name);
this.type = type;
this.name = name;
this.identifier = identifier;
this.parameters = parameters;
this.statements = statements;
}
public MethodNode(AccessTypeNode visibility, String name){
public MethodNode(AccessTypeNode visibility, String identifier){
this.visibility = visibility;
this.identifier = new IdentifierNode(name);
this.identifier = identifier;
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.typeCheck(this);
}
}

View File

@ -8,6 +8,6 @@ public class FieldCodeGen {
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
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);
}
}

View File

@ -12,49 +12,68 @@ import ast.statement.AssignmentStatementNode;
import ast.statement.StatementNode;
import java.util.ArrayList;
import java.util.List;
import typechecker.TypeCheckResult;
public class SemanticAnalyzer {
public class SemanticAnalyzer implements SemanticVisitor {
public String analyze(ASTNode node) {
if (node == null) {
return null;
}
private ArrayList<String> currentFields = new ArrayList<>();
if (node instanceof ClassNode) {
ClassNode classNode = (ClassNode) node;
public static ASTNode generateTast(ASTNode node) throws RuntimeException {
SemanticAnalyzer semanticCheck = new SemanticAnalyzer();
ProgramNode programNode = (ProgramNode) node;
var result = programNode.accept(semanticCheck);
if (result.isValid()) {
return node;
} else {
throw new RuntimeException("errorString ERROR");
}
}
List<MemberNode> memberNodes = classNode.members;
for (MemberNode member : memberNodes) {
analyze(member);
}
@Override
public TypeCheckResult typeCheck(ProgramNode node) {
} else if (node instanceof AssignmentStatementNode) {
AssignmentStatementNode assignmentStatementNode = (AssignmentStatementNode) node;
var valid = true;
IdentifierNode identifierNode = assignmentStatementNode.identifier;
List<ClassNode> classes = node.classes;
for (ClassNode classNode : classes) {
classNode.accept(this);
}
return new TypeCheckResult(valid, null);
}
if(analyze(assignmentStatementNode.expression).equals("int")){
System.out.println("INTEGER");
}
} else if (node instanceof LiteralNode) {
return "int";
} else if (node instanceof MethodNode) {
MethodNode methodNode = (MethodNode) node;
List<StatementNode> statementNodes = methodNode.statements;
for (StatementNode statement : statementNodes) {
analyze(statement);
}
} else if (node instanceof ProgramNode) {
ProgramNode programNode = (ProgramNode) node;
List<ClassNode> classes = programNode.classes;
for (ClassNode classNode : classes) {
analyze(classNode);
@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(IdentifierNode identifierNode) {
return null;
}
@Override
public TypeCheckResult typeCheck(MethodNode methodNode) {
List<StatementNode> statements = methodNode.statements;
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;
}
}

View File

@ -0,0 +1,65 @@
package semantic;
import ast.ASTNode;
import ast.ClassNode;
import ast.IdentifierNode;
import ast.ProgramNode;
import ast.member.FieldNode;
import ast.member.MethodNode;
import typechecker.TypeCheckResult;
public interface SemanticVisitor {
// TypeCheckResult typeCheck(ASTNode toCheck);
TypeCheckResult typeCheck(ProgramNode toCheck);
TypeCheckResult typeCheck(ClassNode toCheck);
TypeCheckResult typeCheck(IdentifierNode toCheck);
TypeCheckResult typeCheck(MethodNode toCheck);
TypeCheckResult typeCheck(FieldNode toCheck);
//
// TypeCheckResult typeCheck(Assign 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);
}

View File

@ -0,0 +1,5 @@
package typechecker;
public interface Type {
boolean equals(Object obj);
}

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

View 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);
}