Compare commits
12 Commits
458983fc68
...
03645f7a84
Author | SHA1 | Date | |
---|---|---|---|
|
03645f7a84 | ||
|
73b9eeddb0 | ||
|
159413dd7d | ||
|
c4a35decba | ||
|
b2e86ca631 | ||
|
c0b30f9620 | ||
|
7249054da5 | ||
|
f60fa05153 | ||
|
a98cb26083 | ||
|
2539b7cff1 | ||
|
5344b01384 | ||
|
6e8079807a |
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -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>
|
@ -1,3 +1,11 @@
|
||||
public class Example {
|
||||
|
||||
public int test;
|
||||
|
||||
}
|
||||
|
||||
public class Test {
|
||||
|
||||
public char test;
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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<>();
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
27
src/main/java/ast/Identifier.java
Normal file
27
src/main/java/ast/Identifier.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public class ClassCodeGen {
|
||||
}
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.name);
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
@ -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.
BIN
src/main/java/classFileOutput/Test.class
Normal file
BIN
src/main/java/classFileOutput/Test.class
Normal file
Binary file not shown.
@ -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));
|
||||
}
|
||||
|
58
src/main/java/semantic/SemanticAnalyzer.java
Normal file
58
src/main/java/semantic/SemanticAnalyzer.java
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
9
src/main/java/typechecker/Typer.java
Normal file
9
src/main/java/typechecker/Typer.java
Normal file
@ -0,0 +1,9 @@
|
||||
package typechecker;
|
||||
|
||||
public class Typer {
|
||||
|
||||
public static void Typeify(){
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user