abstract syntax tree as defined on 02.05.2024

This commit is contained in:
Krauß, Josefine 2024-05-02 13:12:39 +02:00
parent 7c91cee342
commit b34b13d485
41 changed files with 224 additions and 94 deletions

View File

@ -0,0 +1,15 @@
package TypeCheck;
import TypeCheck.TypeCheckResult;
public abstract class AbstractType {
TypeCheckResult typeCheckResult;
public void setTypeCheckResult(TypeCheckResult typeCheckResult) {
this.typeCheckResult = typeCheckResult;
}
public TypeCheckResult getTypeCheckResult() {
return typeCheckResult;
}
}

View File

@ -0,0 +1,23 @@
package TypeCheck;
import java.util.Objects;
public class TypeCheckHelper {
public String upperBound(String type1, String type2) throws Exception{
boolean type1Primitiv = Objects.equals(type1, "bool") || Objects.equals(type1, "int") || Objects.equals(type1, "char");
boolean type2Primitiv = Objects.equals(type2, "bool") || Objects.equals(type2, "int") || Objects.equals(type2, "char");
String result;
if(type1Primitiv && type2Primitiv){
if(Objects.equals(type1, type2)){
result = type1;
}
throw new Exception("no upper bound");
}else if(type1Primitiv || type2Primitiv){
throw new Exception("no upper bound");
}else{
result = "class";
}
return result;
}
}

View File

@ -0,0 +1,7 @@
package TypeCheck;
public class TypeCheckResult {
public String type;
public boolean valid;
}

View File

@ -0,0 +1,4 @@
package abstractSyntaxTree.Class;
public class FieldDecl implements IClass {
}

View File

@ -0,0 +1,7 @@
package abstractSyntaxTree.Class;
public interface IClass {
// not type or type check
// visit method for code generation
}

View File

@ -0,0 +1,4 @@
package abstractSyntaxTree.Class;
public class MethodDecl implements IClass {
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Comment;
public class CommentComment {
}

View File

@ -1,8 +0,0 @@
package abstractSyntaxTree.Comment;
public interface IComment {
// typeCheck method
// get type
// visit method for code generation
}

View File

@ -1,4 +1,6 @@
package abstractSyntaxTree.Datatype; package abstractSyntaxTree.Datatype;
public class BoolDatatype implements IDatatype{ import TypeCheck.AbstractType;
public class BoolDatatype extends AbstractType implements IDatatype{
} }

View File

@ -1,4 +1,6 @@
package abstractSyntaxTree.Datatype; package abstractSyntaxTree.Datatype;
public class CharDatatype implements IDatatype{ import TypeCheck.AbstractType;
public class CharDatatype extends AbstractType implements IDatatype{
} }

View File

@ -1,8 +1,10 @@
package abstractSyntaxTree.Datatype; package abstractSyntaxTree.Datatype;
import TypeCheck.TypeCheckResult;
public interface IDatatype { public interface IDatatype {
// typeCheck method // typeCheck method
// get type TypeCheckResult typeCheck() throws Exception;
// visit method for code generation // visit method for code generation
} }

View File

@ -1,4 +1,6 @@
package abstractSyntaxTree.Datatype; package abstractSyntaxTree.Datatype;
public class IntDatatype implements IDatatype{ import TypeCheck.AbstractType;
public class IntDatatype extends AbstractType implements IDatatype{
} }

View File

@ -0,0 +1,21 @@
package abstractSyntaxTree.Datatype;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import java.util.List;
public class RefType extends AbstractType implements IDatatype {
public RefType(List<FieldDecl> fieldDecls, List<MethodDecl> methodDecls){
this.fieldDecls = fieldDecls;
this.methodDecls = methodDecls;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
List<FieldDecl> fieldDecls;
List<MethodDecl> methodDecls;
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Expression;
public class AssignExpression implements IExpression{
}

View File

@ -1,4 +1,39 @@
package abstractSyntaxTree.Expression; package abstractSyntaxTree.Expression;
public class BinaryExpression implements IExpression{ import TypeCheck.TypeCheckResult;
import TypeCheck.TypeCheckHelper;
import TypeCheck.AbstractType;
import java.util.Objects;
public class BinaryExpression extends AbstractType implements IExpression{
public String operator;
public IExpression left;
public IExpression right;
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckHelper helper = new TypeCheckHelper();
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult leftType = left.typeCheck();
TypeCheckResult rightType = right.typeCheck();
switch (operator) {
case "and":
case "or" :{
if (Objects.equals(helper.upperBound(leftType.type, rightType.type), "bool")){
result.valid = true;
result.type = "bool";
}
break;
}
// ==, !=
}
setTypeCheckResult(result); // writes to attribute in abstract type class
return result;
}
} }

View File

@ -1,8 +1,10 @@
package abstractSyntaxTree.Expression; package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckResult;
public interface IExpression { public interface IExpression {
// typeCheck method // typeCheck method
// get type TypeCheckResult typeCheck() throws Exception;
// visit method for code generation // visit method for code generation
} }

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Expression;
public class MethodCall implements IExpression{
}

View File

@ -1,8 +1,7 @@
package abstractSyntaxTree.Modifier; package abstractSyntaxTree.Modifier;
public interface IModifier { public interface IModifier {
// typeCheck method // not type or type check
// get type
// visit method for code generation // visit method for code generation
} }

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Object;
public class ClassObject implements IObject{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Object;
public class FieldDeclObject implements IObject{
}

View File

@ -1,8 +0,0 @@
package abstractSyntaxTree.Object;
public interface IObject {
// typeCheck method
// get type
// visit method for code generation
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Object;
public class MethodeDeclObject implements IObject{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Object;
public class NewObject implements IObject{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Object;
public class ThisObject implements IObject{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Object;
public class VoidObject implements IObject{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Statement;
public class AssignmentStatement implements IStatement{
}

View File

@ -1,4 +1,11 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
public class BlockStatement implements IStatement{ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
public class BlockStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
} }

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Statement;
public class BreakStatement implements IStatement{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Statement;
public class ContinueStatement implements IStatement{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Statement;
public class DoWhileStatement implements IStatement{
}

View File

@ -1,4 +1,11 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
public class EmptyStatement implements IStatement{ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
public class EmptyStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
} }

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Statement;
public class ForStatement implements IStatement{
}

View File

@ -1,8 +1,10 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
public interface IStatement { public interface IStatement {
// typeCheck method
// get type TypeCheckResult typeCheck() throws Exception;
// visit method for code generation // visit method for code generation
} }

View File

@ -1,4 +1,11 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
public class IfElseStatement implements IStatement{ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
public class IfElseStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
} }

View File

@ -1,4 +1,11 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
public class IfStatement implements IStatement{ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
public class IfStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
} }

View File

@ -1,4 +1,11 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
public class ReturnStatement implements IStatement{ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
public class ReturnStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
} }

View File

@ -1,6 +0,0 @@
package abstractSyntaxTree.Statement;
import java.sql.Statement;
public class SwitchStatement implements IStatement {
}

View File

@ -1,4 +1,11 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
public class WhileStatement implements IStatement{ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
public class WhileStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
} }

View File

@ -0,0 +1,13 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Statement.IStatement;
public class AssignExpression extends AbstractType implements IExpression, IStatement {
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
}

View File

@ -0,0 +1,13 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Statement.IStatement;
public class MethodCall extends AbstractType implements IExpression, IStatement {
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
}

View File

@ -0,0 +1,13 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Statement.IStatement;
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
}