Added Identifier

This commit is contained in:
Bruder John 2024-05-08 14:40:34 +02:00
parent 7249054da5
commit c0b30f9620
9 changed files with 62 additions and 16 deletions

View File

@ -1,6 +1,7 @@
class Example { class Example {
int test; int test;
char test;
} }

View File

@ -9,9 +9,6 @@ import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser; import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer; import semantic.SemanticAnalyzer;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -20,7 +17,7 @@ public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CharStream codeCharStream = null; CharStream codeCharStream = null;
try { try {
codeCharStream = CharStreams.fromPath(Paths.get("./Example.java")); codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.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());
@ -42,7 +39,7 @@ public class Main {
// Optionally print or process the AST // Optionally print or process the AST
System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:"); System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:");
for (ClassNode classNode : ast.classes) { for (ClassNode classNode : ast.classes) {
System.out.println(classNode.name); System.out.println(classNode.identifier.getName());
} }
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ClassNode extends ASTNode{ public class ClassNode extends ASTNode{
public String name; public Identifier identifier;
public List<MemberNode> members = new ArrayList<>(); public List<MemberNode> members = new ArrayList<>();
public boolean hasConstructor = false; public boolean hasConstructor = false;
@ -17,7 +17,7 @@ public class ClassNode extends ASTNode{
public void ensureConstructor(){ public void ensureConstructor(){
if(!hasConstructor) { if(!hasConstructor) {
ConstructorNode constructor = new ConstructorNode(new TypeNode("public"), name); ConstructorNode constructor = new ConstructorNode(new TypeNode("public"), identifier.getName());
members.add(0,constructor); members.add(0,constructor);
} }
} }

View File

@ -2,11 +2,11 @@ package ast;
public class FieldNode extends MemberNode { public class FieldNode extends MemberNode {
public TypeNode type; public TypeNode type;
public String name; public Identifier identifier;
public FieldNode(TypeNode type, String name){ public FieldNode(TypeNode type, String name){
this.type = type; this.type = type;
this.name = name; this.identifier = new Identifier(name);
} }
} }

View File

@ -0,0 +1,27 @@
package ast;
public class Identifier {
private String name;
public Identifier(String name){
this.name = name;
}
public String getName(){
return name;
}
public boolean equals(Object obj) {
if(obj instanceof Identifier){
Identifier identifier = (Identifier) obj;
if(name.equals(identifier.getName())){
return true;
} else {
return false;
}
}
return super.equals(obj);
}
}

View File

@ -5,7 +5,7 @@ import java.util.List;
public class MethodNode extends MemberNode{ public class MethodNode extends MemberNode{
public TypeNode visibility; public TypeNode visibility;
public String name; public Identifier identifier;
public ParameterListNode parameters; public ParameterListNode parameters;
@ -14,13 +14,13 @@ public class MethodNode extends MemberNode{
public MethodNode(TypeNode visibility, String name, ParameterListNode parameters, public MethodNode(TypeNode visibility, String name, ParameterListNode parameters,
List<StatementNode> statements){ List<StatementNode> statements){
this.visibility = visibility; this.visibility = visibility;
this.name = name; this.identifier = new Identifier(name);
this.parameters = parameters; this.parameters = parameters;
this.statements = statements; this.statements = statements;
} }
public MethodNode(TypeNode visibility, String name){ public MethodNode(TypeNode visibility, String name){
this.visibility = visibility; this.visibility = visibility;
this.name = name; this.identifier = new Identifier(name);
} }
} }

View File

@ -10,7 +10,7 @@ import java.io.IOException;
public class ClassCodeGen { public class ClassCodeGen {
public void generateClassCode(ClassNode classNode) { public void generateClassCode(ClassNode classNode) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.identifier.getName(), null,
"java/lang/Object", null); "java/lang/Object", null);
FieldCodeGen fieldCodeGen = new FieldCodeGen(); FieldCodeGen fieldCodeGen = new FieldCodeGen();
@ -20,7 +20,7 @@ public class ClassCodeGen {
methodCodeGen.generateMethodCode(classWriter); methodCodeGen.generateMethodCode(classWriter);
classWriter.visitEnd(); classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classNode.name); printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
classWriter.visitEnd(); classWriter.visitEnd();
} }

View File

@ -20,7 +20,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(); ClassNode classNode = new ClassNode();
classNode.name = ctx.IDENTIFIER().getText(); classNode.identifier = new 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));
} }

View File

@ -3,16 +3,20 @@ package semantic;
import ast.*; import ast.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SemanticAnalyzer { public class SemanticAnalyzer {
List<Identifier> usedIdentifier = new ArrayList<>();
public void analyze(ASTNode node) { public void analyze(ASTNode node) {
if (node == null) return; if (node == null) return;
if (node instanceof ClassNode) { if (node instanceof ClassNode) {
ClassNode classNode = (ClassNode) node; ClassNode classNode = (ClassNode) node;
if(classNode.name == null){ if(classNode.identifier == null){
System.out.println("Klasse besitzt keinen Identifier"); System.out.println("Klasse besitzt keinen Identifier");
} }
@ -24,6 +28,14 @@ public class SemanticAnalyzer {
}else if (node instanceof ConstructorNode) { }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");
}
usedIdentifier.add(fieldNode.identifier);
}else if (node instanceof ProgramNode) { }else if (node instanceof ProgramNode) {
ProgramNode programNode = (ProgramNode) node; ProgramNode programNode = (ProgramNode) node;
List<ClassNode> classes = programNode.classes; List<ClassNode> classes = programNode.classes;
@ -33,4 +45,13 @@ public class SemanticAnalyzer {
} }
} }
public boolean identifierAlreadyUsed(Identifier identifier){
if(usedIdentifier.contains(identifier)){
return true;
} else {
return false;
}
}
} }