Compare commits

..

No commits in common. "16a905489268c4019384751717445e59273b9848" and "d0abd3bff781dc7c764b2b43e55ef653c73297e0" have entirely different histories.

10 changed files with 16 additions and 192 deletions

View File

@ -2,4 +2,6 @@ package TypeCheck;
public class TypeCheckResult {
public String type;
public boolean valid;
}

View File

@ -8,6 +8,7 @@ import java.util.Objects;
public class BinaryExpression extends AbstractType implements IExpression{
public String operator;
public IExpression left;
public IExpression right;
@ -21,33 +22,18 @@ public class BinaryExpression extends AbstractType implements IExpression{
TypeCheckResult rightType = right.typeCheck();
switch (operator) {
case "&&":
case "||" :{
case "and":
case "or" :{
if (Objects.equals(helper.upperBound(leftType.type, rightType.type), "bool")){
result.valid = true;
result.type = "bool";
}
break;
}
case "==":
case "!=":
result.type = helper.upperBound(leftType.type, rightType.type);
break;
case "-":
case "+":
case "*":
case "/":
if (Objects.equals(helper.upperBound(leftType.type, rightType.type), "int")){
result.type = "int";
}
break;
//case "&" ist für logisches und auf bit level
//case "|" ist für logisches oder auf bit level
// ==, !=
}
setTypeCheckResult(result);
setTypeCheckResult(result); // writes to attribute in abstract type class
return result;
}
}

View File

@ -1,41 +1,4 @@
package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Datatype.IDatatype;
import java.util.Objects;
public class UnaryExpression extends AbstractType implements IExpression{
public String operator;
public IDatatype operand;
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult operandTypeCheckResult = operand.typeCheck();
String operandType = operandTypeCheckResult.type;
switch (operator) {
case "!" :{
if (Objects.equals(operandType, "bool")){
result.type = "bool";
}
break;
}
case "-":
case "+":
if (Objects.equals(operandType, "int")){
result.type = "int";
}
break;
}
setTypeCheckResult(result);
return result;
}
public class UnaryExpression implements IExpression{
}

View File

@ -2,33 +2,10 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
import java.util.List;
public class BlockStatement extends AbstractType implements IStatement{
List<IStatement> statements;
public BlockStatement(List<IStatement> statements){
this.statements = statements;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
if(statements.size() == 0){
result.type = "void";
}
TypeCheckResult blockType = null;
for (IStatement statement : statements) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck();
if (blockType == null) {
blockType = typeOfCurrentStatement;
} else if (!typeOfCurrentStatement.equals(blockType) && !blockType.equals("void")) {
throw new IllegalArgumentException("different statement types");
}
}
return result;
return null;
}
}

View File

@ -6,8 +6,6 @@ import TypeCheck.AbstractType;
public class EmptyStatement extends AbstractType implements IStatement{
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
result.type = "void";
return result;
return null;
}
}

View File

@ -2,37 +2,10 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
public class IfElseStatement extends AbstractType implements IStatement{
IExpression condition;
IStatement ifStatement;
IStatement elseStatement;
public IfElseStatement(IExpression condition, IStatement ifStatement, IStatement elseStatement) {
this.condition = condition;
this.ifStatement = ifStatement;
this.elseStatement = elseStatement;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("bool")) {
throw new IllegalArgumentException("should be boolean");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck();
TypeCheckResult elseStatementType = elseStatement.typeCheck();
if (!ifStatementType.equals(elseStatementType)) {
throw new IllegalArgumentException("if and else have different types");
}
result.type = elseStatementType.type;
return result;
return null;
}
}

View File

@ -2,33 +2,10 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
public class IfStatement extends AbstractType implements IStatement{
IExpression condition;
IStatement ifStatement;
public IfStatement(IExpression condition, IStatement ifStatement) {
this.condition = condition;
this.ifStatement = ifStatement;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("boolean")) {
throw new IllegalArgumentException("should be boolean");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck();
result.type = ifStatementType.type;
return result;
return null;
}
}

View File

@ -2,26 +2,10 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
public class ReturnStatement extends AbstractType implements IStatement{
IExpression expression;
public ReturnStatement(IExpression expression) {
this.expression = expression;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
if (expression == null) {
result.type = "void";
} else {
TypeCheckResult typedExpression = expression.typeCheck();
result.type = typedExpression.type;
}
return result;
return null;
}
}

View File

@ -2,28 +2,10 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
public class WhileStatement extends AbstractType implements IStatement{
IExpression condition;
IStatement statement;
public WhileStatement(IExpression condition, IStatement statement) {
this.condition = condition;
this.statement = statement;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("bool")) {
throw new IllegalArgumentException("Expected boolean");
}
TypeCheckResult statementType = statement.typeCheck();
result.type = statementType.type;
return result;
return null;
}
}

View File

@ -1,31 +1,13 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Statement.IStatement;
import java.util.Objects;
public class AssignExpression extends AbstractType implements IExpression, IStatement {
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();
String upperbound = helper.upperBound(leftType.type, rightType.type);
if (Objects.equals(upperbound, leftType.type)) {
result.type = leftType.type;
}
setTypeCheckResult(result);
return result;
return null;
}
}