Compare commits
2 Commits
d0abd3bff7
...
16a9054892
Author | SHA1 | Date | |
---|---|---|---|
|
16a9054892 | ||
|
bdcc03bcfa |
@ -2,6 +2,4 @@ package TypeCheck;
|
||||
|
||||
public class TypeCheckResult {
|
||||
public String type;
|
||||
public boolean valid;
|
||||
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import java.util.Objects;
|
||||
|
||||
public class BinaryExpression extends AbstractType implements IExpression{
|
||||
|
||||
|
||||
public String operator;
|
||||
public IExpression left;
|
||||
public IExpression right;
|
||||
@ -22,18 +21,33 @@ public class BinaryExpression extends AbstractType implements IExpression{
|
||||
TypeCheckResult rightType = right.typeCheck();
|
||||
|
||||
switch (operator) {
|
||||
case "and":
|
||||
case "or" :{
|
||||
|
||||
case "&&":
|
||||
case "||" :{
|
||||
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); // writes to attribute in abstract type class
|
||||
setTypeCheckResult(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,41 @@
|
||||
package abstractSyntaxTree.Expression;
|
||||
|
||||
public class UnaryExpression implements IExpression{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,33 @@ 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 {
|
||||
return null;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import TypeCheck.AbstractType;
|
||||
public class EmptyStatement extends AbstractType implements IStatement{
|
||||
@Override
|
||||
public TypeCheckResult typeCheck() throws Exception {
|
||||
return null;
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
result.type = "void";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,37 @@ 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 {
|
||||
return null;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,33 @@ 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 {
|
||||
return null;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -2,10 +2,26 @@ 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 {
|
||||
return null;
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
|
||||
if (expression == null) {
|
||||
result.type = "void";
|
||||
} else {
|
||||
TypeCheckResult typedExpression = expression.typeCheck();
|
||||
result.type = typedExpression.type;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,28 @@ 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 {
|
||||
return null;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,31 @@
|
||||
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 {
|
||||
return null;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user