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 {
int test;
char test;
}

View File

@ -9,9 +9,6 @@ import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import java.io.IOException;
import java.nio.file.Paths;
@ -20,7 +17,7 @@ public class Main {
public static void main(String[] args) throws Exception {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("./Example.java"));
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.txt"));
parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
@ -42,7 +39,7 @@ public class Main {
// 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.name);
System.out.println(classNode.identifier.getName());
}
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();

View File

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

View File

@ -2,11 +2,11 @@ package ast;
public class FieldNode extends MemberNode {
public TypeNode type;
public String name;
public Identifier identifier;
public FieldNode(TypeNode type, String name){
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 TypeNode visibility;
public String name;
public Identifier identifier;
public ParameterListNode parameters;
@ -14,13 +14,13 @@ public class MethodNode extends MemberNode{
public MethodNode(TypeNode visibility, String name, ParameterListNode parameters,
List<StatementNode> statements){
this.visibility = visibility;
this.name = name;
this.identifier = new Identifier(name);
this.parameters = parameters;
this.statements = statements;
}
public MethodNode(TypeNode visibility, String name){
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 void generateClassCode(ClassNode classNode) {
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);
FieldCodeGen fieldCodeGen = new FieldCodeGen();
@ -20,7 +20,7 @@ public class ClassCodeGen {
methodCodeGen.generateMethodCode(classWriter);
classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classNode.name);
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
classWriter.visitEnd();
}

View File

@ -20,7 +20,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
ClassNode classNode = new ClassNode();
classNode.name = ctx.IDENTIFIER().getText();
classNode.identifier = new Identifier(ctx.IDENTIFIER().getText());
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
classNode.addMember((MemberNode) visit(member));
}

View File

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