abstract syntax tree as defined on 02.05.2024
This commit is contained in:
parent
7c91cee342
commit
b34b13d485
15
Source/TypeCheck/AbstractType.java
Normal file
15
Source/TypeCheck/AbstractType.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
23
Source/TypeCheck/TypeCheckHelper.java
Normal file
23
Source/TypeCheck/TypeCheckHelper.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
7
Source/TypeCheck/TypeCheckResult.java
Normal file
7
Source/TypeCheck/TypeCheckResult.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package TypeCheck;
|
||||||
|
|
||||||
|
public class TypeCheckResult {
|
||||||
|
public String type;
|
||||||
|
public boolean valid;
|
||||||
|
|
||||||
|
}
|
4
Source/abstractSyntaxTree/Class/FieldDecl.java
Normal file
4
Source/abstractSyntaxTree/Class/FieldDecl.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package abstractSyntaxTree.Class;
|
||||||
|
|
||||||
|
public class FieldDecl implements IClass {
|
||||||
|
}
|
7
Source/abstractSyntaxTree/Class/IClass.java
Normal file
7
Source/abstractSyntaxTree/Class/IClass.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package abstractSyntaxTree.Class;
|
||||||
|
|
||||||
|
public interface IClass {
|
||||||
|
// not type or type check
|
||||||
|
|
||||||
|
// visit method for code generation
|
||||||
|
}
|
4
Source/abstractSyntaxTree/Class/MethodDecl.java
Normal file
4
Source/abstractSyntaxTree/Class/MethodDecl.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package abstractSyntaxTree.Class;
|
||||||
|
|
||||||
|
public class MethodDecl implements IClass {
|
||||||
|
}
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Comment;
|
|
||||||
|
|
||||||
public class CommentComment {
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package abstractSyntaxTree.Comment;
|
|
||||||
|
|
||||||
public interface IComment {
|
|
||||||
// typeCheck method
|
|
||||||
// get type
|
|
||||||
|
|
||||||
// visit method for code generation
|
|
||||||
}
|
|
@ -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{
|
||||||
}
|
}
|
||||||
|
@ -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{
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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{
|
||||||
}
|
}
|
||||||
|
21
Source/abstractSyntaxTree/Datatype/RefType.java
Normal file
21
Source/abstractSyntaxTree/Datatype/RefType.java
Normal 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;
|
||||||
|
}
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Expression;
|
|
||||||
|
|
||||||
public class AssignExpression implements IExpression{
|
|
||||||
}
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Expression;
|
|
||||||
|
|
||||||
public class MethodCall implements IExpression{
|
|
||||||
}
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public class ClassObject implements IObject{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public class FieldDeclObject implements IObject{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public interface IObject {
|
|
||||||
// typeCheck method
|
|
||||||
// get type
|
|
||||||
|
|
||||||
// visit method for code generation
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public class MethodeDeclObject implements IObject{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public class NewObject implements IObject{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public class ThisObject implements IObject{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Object;
|
|
||||||
|
|
||||||
public class VoidObject implements IObject{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
|
||||||
|
|
||||||
public class AssignmentStatement implements IStatement{
|
|
||||||
}
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
|
||||||
|
|
||||||
public class BreakStatement implements IStatement{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
|
||||||
|
|
||||||
public class ContinueStatement implements IStatement{
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
|
||||||
|
|
||||||
public class DoWhileStatement implements IStatement{
|
|
||||||
}
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
|
||||||
|
|
||||||
public class ForStatement implements IStatement{
|
|
||||||
}
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
|
||||||
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class SwitchStatement implements IStatement {
|
|
||||||
}
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user