johns-branch #4
4
.idea/misc.xml
generated
4
.idea/misc.xml
generated
@ -6,10 +6,10 @@
|
||||
<PerGrammarGenerationSettings>
|
||||
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" />
|
||||
<option name="autoGen" value="true" />
|
||||
<option name="outputDir" value="C:\Users\Maxi\Documents\DHBW\Compilerbau\NichtHaskell2.0\src\main\java\parser" />
|
||||
<option name="outputDir" value="C:\Users\Johannes\Documents\Github\JavaCompiler\src\main\java" />
|
||||
<option name="libDir" value="" />
|
||||
<option name="encoding" value="" />
|
||||
<option name="pkg" value="generated" />
|
||||
<option name="pkg" value="parser.generated" />
|
||||
<option name="language" value="" />
|
||||
<option name="generateVisitor" value="true" />
|
||||
</PerGrammarGenerationSettings>
|
||||
|
@ -1,21 +1,13 @@
|
||||
public class Example {
|
||||
|
||||
public int test;
|
||||
public int testVar;
|
||||
|
||||
public static int testMethod(char b){
|
||||
|
||||
int a;
|
||||
a = 3;
|
||||
|
||||
public Example(int conInput) {
|
||||
|
||||
}
|
||||
|
||||
public static int test(char b){
|
||||
|
||||
test = 3;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class Test {
|
||||
|
||||
public char test;
|
||||
|
||||
}
|
@ -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);
|
||||
|
@ -1,8 +1,7 @@
|
||||
package ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
public class ASTNode {
|
||||
|
||||
public abstract class ASTNode { }
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,17 +7,19 @@ 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 IdentifierNode identifier;
|
||||
public class ClassNode extends ASTNode implements Visitable {
|
||||
public String identifier;
|
||||
public AccessTypeNode accessType;
|
||||
public String name;
|
||||
public List<MemberNode> members = new ArrayList<>();
|
||||
public boolean hasConstructor = false;
|
||||
|
||||
public ClassNode(AccessTypeNode accessType, String name){
|
||||
public ClassNode(AccessTypeNode accessType, String identifier){
|
||||
this.accessType = accessType;
|
||||
this.name = name;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addMember(MemberNode member) {
|
||||
@ -29,8 +31,13 @@ public class ClassNode extends ASTNode{
|
||||
|
||||
public void ensureConstructor(){
|
||||
if(!hasConstructor) {
|
||||
ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), name);
|
||||
ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), identifier);
|
||||
members.add(0,constructor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
package ast;
|
||||
|
||||
public class IdentifierNode {
|
||||
|
||||
private String name;
|
||||
|
||||
public IdentifierNode(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof IdentifierNode){
|
||||
IdentifierNode identifier = (IdentifierNode) obj;
|
||||
if(name.equals(identifier.getName())){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
}
|
23
src/main/java/ast/LiteralNode.java
Normal file
23
src/main/java/ast/LiteralNode.java
Normal file
@ -0,0 +1,23 @@
|
||||
package ast;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
|
||||
public class LiteralNode extends ExpressionNode {
|
||||
|
||||
int value;
|
||||
private String type;
|
||||
|
||||
public LiteralNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
21
src/main/java/ast/VarNode.java
Normal file
21
src/main/java/ast/VarNode.java
Normal file
@ -0,0 +1,21 @@
|
||||
package ast;
|
||||
|
||||
public class VarNode extends ASTNode{
|
||||
|
||||
private String identifier;
|
||||
private String type;
|
||||
|
||||
public VarNode(String type, String identifier){
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getType(){
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getIdentifier(){
|
||||
return identifier;
|
||||
}
|
||||
|
||||
}
|
@ -4,4 +4,6 @@ import ast.ASTNode;
|
||||
|
||||
public class ExpressionNode extends ASTNode {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,18 +1,24 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package ast.member;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.parameter.ParameterListNode;
|
||||
import ast.statement.StatementNode;
|
||||
import ast.type.AccessTypeNode;
|
||||
@ -8,29 +7,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);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,22 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.VarNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class AssignmentStatementNode extends StatementNode {
|
||||
public String identifier;
|
||||
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
||||
public VarNode varNode;
|
||||
public ExpressionNode expression;
|
||||
|
||||
public AssignmentStatementNode(String identifier, ExpressionNode expression) {
|
||||
this.identifier = identifier;
|
||||
public AssignmentStatementNode(VarNode varNode, ExpressionNode expression) {
|
||||
this.varNode = varNode;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ public class ClassCodeGen {
|
||||
public void generateClassCode(ClassNode classNode) {
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
Mapper mapper = new Mapper();
|
||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.name, null,
|
||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
||||
"java/lang/Object", null);
|
||||
|
||||
for (MemberNode memberNode : classNode.members) {
|
||||
@ -29,7 +29,7 @@ public class ClassCodeGen {
|
||||
}
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier);
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
@ -16,11 +16,10 @@ import ast.type.EnumAccessTypeNode;
|
||||
import ast.type.EnumTypeNode;
|
||||
import ast.type.TypeNode;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
import parser.generated.SimpleJavaBaseVisitor;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import parser.generated.*;
|
||||
import parser.generated.SimpleJavaParser.LiteralContext;
|
||||
|
||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@Override
|
||||
@ -35,7 +34,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 IdentifierNode(ctx.IDENTIFIER().getText());
|
||||
classNode.identifier = ctx.IDENTIFIER().getText();
|
||||
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
||||
classNode.addMember((MemberNode) visit(member));
|
||||
}
|
||||
@ -135,9 +134,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
@Override
|
||||
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
||||
String identifier = ctx.IDENTIFIER().getText();
|
||||
VarNode varNode = (VarNode) visit(ctx.var());
|
||||
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
|
||||
return new AssignmentStatementNode(identifier, expression);
|
||||
return new AssignmentStatementNode(varNode, expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
|
||||
return new VarNode("int", ctx.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -196,4 +200,18 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
return null; // Return null or throw an exception if no valid expression found
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
|
||||
LiteralContext literalContext = (LiteralContext) ctx;
|
||||
try {
|
||||
int intValue = Integer.parseInt(literalContext.getText());
|
||||
LiteralNode literalNode = new LiteralNode(intValue);
|
||||
literalNode.setType("int");
|
||||
return literalNode;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return null; // Return null or throw an exception if no valid expression found
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -29,7 +29,9 @@ statement
|
||||
|
||||
variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ;
|
||||
|
||||
assignmentStatement : IDENTIFIER '=' expression ';' ;
|
||||
assignmentStatement : var '=' expression ';' ;
|
||||
|
||||
var: IDENTIFIER;
|
||||
|
||||
ifStatement : 'if' '(' expression ')' statement ('else' statement)? ;
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
@ -168,6 +168,18 @@ public class SimpleJavaBaseListener implements SimpleJavaListener {
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterVar(SimpleJavaParser.VarContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitVar(SimpleJavaParser.VarContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||
|
||||
@ -103,6 +103,13 @@ public class SimpleJavaBaseVisitor<T> extends AbstractParseTreeVisitor<T> implem
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitVar(SimpleJavaParser.VarContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||
|
||||
@ -137,6 +137,16 @@ public interface SimpleJavaListener extends ParseTreeListener {
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link SimpleJavaParser#var}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterVar(SimpleJavaParser.VarContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link SimpleJavaParser#var}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitVar(SimpleJavaParser.VarContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link SimpleJavaParser#ifStatement}.
|
||||
* @param ctx the parse tree
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
@ -88,6 +88,12 @@ public interface SimpleJavaVisitor<T> extends ParseTreeVisitor<T> {
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link SimpleJavaParser#var}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitVar(SimpleJavaParser.VarContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link SimpleJavaParser#ifStatement}.
|
||||
* @param ctx the parse tree
|
||||
|
@ -2,60 +2,91 @@ package semantic;
|
||||
|
||||
|
||||
import ast.*;
|
||||
import ast.expression.ExpressionNode;
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MemberNode;
|
||||
|
||||
import ast.member.MethodNode;
|
||||
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 {
|
||||
|
||||
List<IdentifierNode> usedIdentifier = new ArrayList<>();
|
||||
private ArrayList<String> currentFields = new ArrayList<>();
|
||||
|
||||
public void analyze(ASTNode node) {
|
||||
if (node == null) return;
|
||||
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("Not Valid");
|
||||
}
|
||||
}
|
||||
|
||||
if (node instanceof ClassNode) {
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(ProgramNode node) {
|
||||
|
||||
ClassNode classNode = (ClassNode) node;
|
||||
if(classNode.identifier == null){
|
||||
System.out.println("Klasse besitzt keinen Identifier");
|
||||
}
|
||||
var valid = true;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
List<ClassNode> classes = node.classes;
|
||||
for (ClassNode classNode : classes) {
|
||||
classNode.accept(this);
|
||||
}
|
||||
return new TypeCheckResult(valid, null);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean identifierAlreadyUsed(IdentifierNode identifier){
|
||||
if(usedIdentifier.contains(identifier)){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(MethodNode methodNode) {
|
||||
List<StatementNode> statements = methodNode.statements;
|
||||
for (StatementNode statement : statements) {
|
||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||
assignmentStatementNode.accept(this);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(AssignmentStatementNode assignmentStatementNode) {
|
||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||
VarNode varNode = assignmentStatementNode.varNode;
|
||||
if(varNode.getType().equals(literalNode.getType())) {
|
||||
System.out.println("Type is same");
|
||||
} else {
|
||||
System.out.println("Type Mismatch");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
62
src/main/java/semantic/SemanticVisitor.java
Normal file
62
src/main/java/semantic/SemanticVisitor.java
Normal file
@ -0,0 +1,62 @@
|
||||
package semantic;
|
||||
|
||||
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.statement.AssignmentStatementNode;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public interface SemanticVisitor {
|
||||
// TypeCheckResult typeCheck(ASTNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(ProgramNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(ClassNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(MethodNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(FieldNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(AssignmentStatementNode 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);
|
||||
|
||||
}
|
5
src/main/java/typechecker/Type.java
Normal file
5
src/main/java/typechecker/Type.java
Normal file
@ -0,0 +1,5 @@
|
||||
package typechecker;
|
||||
|
||||
public interface Type {
|
||||
boolean equals(Object obj);
|
||||
}
|
21
src/main/java/typechecker/TypeCheckResult.java
Normal file
21
src/main/java/typechecker/TypeCheckResult.java
Normal 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;
|
||||
}
|
||||
}
|
16
src/main/java/typechecker/Visitable.java
Normal file
16
src/main/java/typechecker/Visitable.java
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user