Example #1
0
void ArrayDeclarationAST::typeCheck(TypeChecker& checker) {
	mLengthExpression->typeCheck(checker);

	//Check length
	checker.assertSameType(
		*checker.makeType("Int"),
		*mLengthExpression->expressionType(checker),
		"Expected the length to be of type 'Int'.");

	//Check if the element type exists
	checker.assertTypeExists(elementType(), false);
	checker.assertNotVoid(*checker.findType(elementType()), "Arrays of type 'Void' is not allowed.");

	//Create the array type if not created
	checker.makeType(elementType() + "[]");
}
Example #2
0
void MultiDimArrayDeclarationAST::typeCheck(TypeChecker& checker) {
	for (auto lengthExpr : mLengthExpressions) {
		lengthExpr->typeCheck(checker);
	}

	//Check lengths
	int dim = 0;
	for (auto lengthExpr : mLengthExpressions) {
		checker.assertSameType(
			*checker.makeType("Int"),
			*lengthExpr->expressionType(checker),
			"Expected the length of dimension " + std::to_string(dim) + " to be of type 'Int'.");
		dim++;
	}

	//Check if the element type exists
	checker.assertTypeExists(elementType(), false);
	checker.assertNotVoid(*checker.findType(elementType()), "Arrays of type 'Void' is not allowed.");

	//Create all array types
	for (int i = 0; i < mLengthExpressions.size(); i++) {
		checker.makeType(typeString(i));
	}
}