working on class fields

This commit is contained in:
Krauß, Josefine 2024-05-08 11:22:12 +02:00
parent 61a9025274
commit 10a5d41aa8
7 changed files with 66 additions and 5 deletions

View File

@ -1,5 +1,6 @@
package TypeCheck; package TypeCheck;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class TypeCheckHelper { public class TypeCheckHelper {
@ -20,4 +21,12 @@ public class TypeCheckHelper {
} }
return result; return result;
} }
public static boolean typeExists(String type, List<String> typeslist) {
if(type.equals("int") || type.equals("bool") || type.equals("char")){
return true;
}
return typeslist.contains(type);
}
} }

View File

@ -1,14 +1,20 @@
package abstractSyntaxTree.Class; package abstractSyntaxTree.Class;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Program;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class FieldDecl implements IClass { public class FieldDecl extends AbstractType implements IClass{
private String type; private Program program;
private String identifier; private String identifier;
public FieldDecl(Program program){
this.program = program;
}
public TypeCheckResult typeCheck(List<FieldDecl> classFieldsIdentifier) throws Exception { public TypeCheckResult typeCheck(List<FieldDecl> classFieldsIdentifier) throws Exception {
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
if (classFieldsIdentifier.contains(this.identifier)){ if (classFieldsIdentifier.contains(this.identifier)){
@ -16,8 +22,11 @@ public class FieldDecl implements IClass {
} else { } else {
classFieldsIdentifier.add(this); classFieldsIdentifier.add(this);
} }
result.type = this.type; //TypeCheckHelper.typeExists(type, ) // need all types of classes
setTypeCheckResult(result);
return result; return result;
//write field table
} }
} }

View File

@ -1,12 +1,24 @@
package abstractSyntaxTree.Class; package abstractSyntaxTree.Class;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Program;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class MethodDecl implements IClass { public class MethodDecl implements IClass {
private Program program;
private HashMap<String, String> localVars;
public MethodDecl(Program program){
this.program = program;
}
public TypeCheckResult typeCheck(List<MethodDecl> fieldsOrMethods) throws Exception { public TypeCheckResult typeCheck(List<MethodDecl> fieldsOrMethods) throws Exception {
return null; return null;
// write localvars
// write method table
} }
} }

View File

@ -4,14 +4,28 @@ import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Program;
import jdk.jshell.spi.ExecutionControl; import jdk.jshell.spi.ExecutionControl;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class RefType extends AbstractType implements IDatatype { public class RefType extends AbstractType implements IDatatype {
String name;
public List<FieldDecl> fieldDecls; public List<FieldDecl> fieldDecls;
private HashMap<String, String> fieldsTable; // type, identifier
public List<MethodDecl> methodDecls; public List<MethodDecl> methodDecls;
private HashMap<String, String> methodsTable; // return type, method name without parameter
private Program program;
private boolean hasMain;
public RefType(Program program){
this.program = program;
}
public RefType(List<FieldDecl> fieldDecls, List<MethodDecl> methodDecls){ public RefType(List<FieldDecl> fieldDecls, List<MethodDecl> methodDecls){
this.fieldDecls = fieldDecls; this.fieldDecls = fieldDecls;
this.methodDecls = methodDecls; this.methodDecls = methodDecls;

View File

@ -3,13 +3,24 @@ package abstractSyntaxTree;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Datatype.RefType; import abstractSyntaxTree.Datatype.RefType;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class Program { public class Program {
public List<RefType> classes; public List<RefType> classes;
public HashMap<String, HashMap<String, String>> classTypeIndentifierTable; // (class, (type, identifier))
public TypeCheckResult typeCheck() throws Exception{ public TypeCheckResult typeCheck() throws Exception{
for(RefType oneClass : classes){
oneClass.typeCheck();
}
return null; return null;
} }
public void codeGen() throws Exception{
for(RefType oneClass : classes){
oneClass.codeGen();
}
}
} }

View File

@ -5,13 +5,17 @@ import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Expression.IExpression;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class BlockStatement extends AbstractType implements IStatement{ public class BlockStatement extends AbstractType implements IStatement{
//We will need a parameter which holds the symbol table //We will need a parameter which holds the symbol table
HashMap<String, String > localVars;
HashMap<String, String > typeIndentifierTable; // from program
List<IStatement> statements; List<IStatement> statements;
public BlockStatement(List<IStatement> statements){ public BlockStatement(List<IStatement> statements, HashMap<String, String> localVars, HashMap<String, String> typeIndentifierTable){
this.statements = statements; this.statements = statements;
} }
@Override @Override

View File

@ -5,6 +5,8 @@ import org.objectweb.asm.MethodVisitor;
public interface IStatement { public interface IStatement {
TypeCheckResult typeCheck() throws Exception; TypeCheckResult typeCheck() throws Exception;
void CodeGen(MethodVisitor mv) throws Exception; void CodeGen(MethodVisitor mv) throws Exception;