Пример #1
0
 private void VisitArrayInitializer(array_initializer en)
 {
 	foreach (expression_node expr in en.element_values)
 		VisitExpression(expr);
 }
 private void VisitArrayInitializer(array_initializer node)
 {
 	bw.Write(node.element_values.Count);
     foreach (expression_node en in node.element_values)
         VisitExpression(en);
     WriteTypeReference(node.type);
 }
 public override void visit(SyntaxTree.array_const _array_const)
 {
     if (_array_const.elements != null)
     {
         List<constant_node> elements = new List<constant_node>();
         List<expression_node> exprs = new List<expression_node>();
         foreach (SyntaxTree.expression expr in _array_const.elements.expressions)
         {
         	if (is_typed_const_def)
         		elements.Add(convert_strong_to_constant_node(expr));
         	else
         		exprs.Add(convert_strong(expr));
         }
         if (is_typed_const_def)
         {
         	array_const arrc = new array_const(elements, get_location(_array_const));
         	arrc.SetType(new ArrayConstType(elements[0].type, elements.Count, arrc.location));
         	return_value(arrc);
         }
         else
         {
         	array_initializer arrc = new array_initializer(exprs, get_location(_array_const));
         	arrc.type = new ArrayConstType(exprs[0].type, exprs.Count, arrc.location);
         	return_value(arrc);
         }
     }
     else
     {
         //это пустой инициализатор записи
         record_constant rc = new record_constant(new List<PascalABCCompiler.SyntaxTree.record_const_definition>(), get_location(_array_const));
         rc.SetType(new RecordConstType(get_location(_array_const)));
         return_value(rc);
     }
 }
 private array_initializer ConvertArrayInitializer(type_node tn, array_initializer constant)
 {
 	type_node element_type = null;
     if (IsBoundedArray(tn))
     {
         bounded_array_interface bai = tn.get_internal_interface(internal_interface_kind.bounded_array_interface) as bounded_array_interface;
         element_type = bai.element_type;
         ordinal_type_interface oti_indexer = bai.ordinal_type_interface;
         int arr_length = oti_indexer.ordinal_type_to_int(oti_indexer.upper_value) - oti_indexer.ordinal_type_to_int(oti_indexer.lower_value) + 1;
         if (arr_length != constant.element_values.Count)
             AddError(constant.location, "ARRAY_CONST_{0}_ELEMENTS_EXPECTED", arr_length);
     }
     else
         if (IsUnsizedArray(tn))
         {
             array_internal_interface aii = tn.get_internal_interface(internal_interface_kind.unsized_array_interface) as array_internal_interface;
             element_type = aii.element_type;
             if (aii.rank > 1)
             {
             	array_initializer cnst = ConvertNDimArrayInitializer(tn,1,element_type,constant);
             	cnst.type = tn;
             	return cnst;
             }
         }
         else
         	AddError(new CanNotConvertTypes(constant,constant.type,tn,constant.location));//CompilerInternalError("Unexpected array type");
     for (int i = 0; i < constant.element_values.Count; i++)
     if (constant.element_values[i] is array_initializer)
     	constant.element_values[i] = ConvertArrayInitializer(element_type, constant.element_values[i] as array_initializer);
     else if (constant.element_values[i] is record_initializer)
     {
     	if (element_type is common_type_node)
     		constant.element_values[i] = ConvertRecordInitializer(element_type as common_type_node, constant.element_values[i] as record_initializer);
     	else throw new NotSupportedError(constant.element_values[i].location);
     }
     else
     {
     	constant.element_values[i] = convertion_data_and_alghoritms.convert_type(constant.element_values[i], element_type);
     }
     constant.type = tn;
     return constant;
 }
 private array_initializer ConvertNDimArrayInitializer(type_node tn, int cur_rank, type_node element_type, array_initializer constant)
 {
 	array_internal_interface aii = tn.get_internal_interface(internal_interface_kind.unsized_array_interface) as array_internal_interface;
 	int rank = aii.rank;
 	for (int i=0; i<constant.element_values.Count; i++)
 	{
 		expression_node e = constant.element_values[i];
 		if (e is array_initializer)
 		{
 			if (cur_rank>=rank)
 				constant.element_values[i] = ConvertArrayInitializer(tn.element_type,e as array_initializer);
 				//AddError(new CanNotConvertTypes(e,e.type,tn.element_type,e.location));
 			else
 			constant.element_values[i] = ConvertNDimArrayInitializer(tn,cur_rank+1,element_type,e as array_initializer);
 		}
 		else if (e is record_initializer)
     	{
     		if (element_type is common_type_node)
     			constant.element_values[i] = ConvertRecordInitializer(element_type as common_type_node, e as record_initializer);
     		else throw new NotSupportedError(constant.element_values[i].location);
     	}
 		else
     	{
 			if (cur_rank != rank)
                 AddError(constant.location, "RANK_MISMATCH_IN_INITILIALIZER");
 			constant.element_values[i] = convertion_data_and_alghoritms.convert_type(constant.element_values[i], element_type);
     	}
 	}
 	constant.type = tn;
 	return constant;
 }
 private array_initializer CreateArrayInitializer()
 {
 	List<expression_node> element_values = new List<expression_node>();
     int count = br.ReadInt32();
     for (int i = 0; i < count; i++)
         element_values.Add(CreateExpression());
     array_initializer arrc = new array_initializer(element_values, null);
     arrc.type = GetTypeReference();
     return arrc;
 }