Compare commits

...

12 Commits

Author SHA1 Message Date
Bruder John
03645f7a84 fixed bugs after update branch 2024-05-08 23:47:37 +02:00
Bruder John
73b9eeddb0 Merge branch 'main' into johns-branch 2024-05-08 23:46:28 +02:00
Bruder John
159413dd7d fixed bugs 2024-05-08 23:44:17 +02:00
Bruder John
c4a35decba Merge branch 'main' into johns-branch 2024-05-08 15:22:05 +02:00
Bruder John
b2e86ca631 moved Eample to resources 2024-05-08 15:04:57 +02:00
Bruder John
c0b30f9620 Added Identifier 2024-05-08 14:40:34 +02:00
Bruder John
7249054da5 Merge branch 'main' into johns-branch 2024-05-08 14:21:14 +02:00
Bruder John
f60fa05153 Merge branch 'main' into johns-branch 2024-05-08 13:34:09 +02:00
Bruder John
a98cb26083 first change 2024-05-08 13:33:20 +02:00
Bruder John
2539b7cff1 Merge branch 'main' into johns-branch 2024-05-08 11:50:18 +02:00
Bruder John
5344b01384 Merge branch 'main' into johns-branch 2024-05-08 11:41:45 +02:00
Bruder John
6e8079807a Added Typchecker and Semantic analyzer 2024-05-08 11:41:38 +02:00
14 changed files with 120 additions and 7 deletions

2
.idea/misc.xml generated
View File

@ -40,7 +40,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -1,3 +1,11 @@
public class Example {
public int test;
}
public class Test {
public char test;
}

View File

@ -1,3 +1,6 @@
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
@ -5,6 +8,7 @@ import org.antlr.v4.runtime.tree.ParseTree;
import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
@ -40,9 +44,12 @@ 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();
semanticAnalyzer.analyze(ast);
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(ast);
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
public class ClassNode extends ASTNode{
public Identifier identifier;
public AccessTypeNode accessType;
public String name;
public List<MemberNode> members = new ArrayList<>();

View File

@ -3,12 +3,12 @@ package ast;
public class FieldNode extends MemberNode {
public AccessTypeNode accessTypeNode;
public TypeNode type;
public String name;
public Identifier identifier;
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
this.accessTypeNode = accessTypeNode;
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

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
public class MethodNode extends MemberNode{
public Identifier identifier;
public AccessTypeNode visibility;
public TypeNode type;
public String name;
@ -15,6 +16,7 @@ public class MethodNode extends MemberNode{
public MethodNode(AccessTypeNode visibility, TypeNode type, String name, ParameterListNode parameters,
List<StatementNode> statements){
this.visibility = visibility;
this.identifier = new Identifier(name);
this.type = type;
this.name = name;
this.parameters = parameters;
@ -23,6 +25,6 @@ public class MethodNode extends MemberNode{
public MethodNode(AccessTypeNode visibility, String name){
this.visibility = visibility;
this.name = name;
this.identifier = new Identifier(name);
}
}

View File

@ -29,7 +29,7 @@ public class ClassCodeGen {
}
classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classNode.name);
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
classWriter.visitEnd();
}

View File

@ -9,6 +9,6 @@ public class FieldCodeGen {
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
Mapper mapper = new Mapper();
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.name, "", null, null);
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier.getName(), "", null, null);
}
}

Binary file not shown.

View File

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

View File

@ -0,0 +1,58 @@
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.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;
List<ClassNode> classes = programNode.classes;
for (ClassNode classNode: classes) {
analyze(classNode);
}
}
}
public boolean identifierAlreadyUsed(Identifier identifier){
if(usedIdentifier.contains(identifier)){
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,9 @@
package typechecker;
public class Typer {
public static void Typeify(){
}
}