public override void visit(foreach_stmt _foreach_stmt) { statements_list sl2 = new statements_list(get_location(_foreach_stmt)); convertion_data_and_alghoritms.statement_list_stack_push(sl2); expression_node foreachCollection; var_definition_node foreachVariable; ForeachCheckAndConvert(_foreach_stmt, out foreachCollection, out foreachVariable); // SSM 29.07.16 - если in_what - одномерный массив, то заменить код foreach на for // if (OptimizeForeachInCase1DArray(_foreach_stmt, foreachCollection)) return; statements_list sl = new statements_list(get_location(_foreach_stmt.stmt)); convertion_data_and_alghoritms.statement_list_stack_push(sl); foreach_node foreachNode = new foreach_node(foreachVariable, foreachCollection, null, get_location(_foreach_stmt)); context.cycle_stack.push(foreachNode); context.loop_var_stack.Push(foreachVariable); context.enter_code_block_with_bind(); statement_node body = convert_strong(_foreach_stmt.stmt); context.leave_code_block(); context.loop_var_stack.Pop(); context.cycle_stack.pop(); sl = convertion_data_and_alghoritms.statement_list_stack.pop(); if (sl.statements.Count > 0 || sl.local_variables.Count > 0) { sl.statements.AddElement(body); body = sl; } foreachNode.what_do = body; convertion_data_and_alghoritms.statement_list_stack.pop(); sl2.statements.AddElement(foreachNode); return_value(sl2); }
private void VisitForeach(foreach_node stmt) { IncreaseNumUseVar(stmt.ident); VisitExpression(stmt.in_what); VisitStatement(stmt.what_do); }
public override void visit(foreach_stmt _foreach_stmt) { var loopIdentName = _foreach_stmt.identifier.name.ToLower(); definition_node dn = null; var_definition_node vdn = null; var sl2 = new statements_list(_visitor.get_location(_foreach_stmt)); _visitor.convertion_data_and_alghoritms.statement_list_stack_push(sl2); var newTreeNode = new CapturedVariablesTreeNodeForEachScope(_currentTreeNode, sl2.Scope.ScopeNum, _foreach_stmt); if (_currentTreeNode != null) { _currentTreeNode.ChildNodes.Add(newTreeNode); } _currentTreeNode = newTreeNode; _scopesCapturedVarsNodesDictionary.Add(sl2.Scope.ScopeNum, _currentTreeNode); var inWhat = _visitor.convert_strong(_foreach_stmt.in_what); var tmp = inWhat; if (inWhat is typed_expression) { inWhat = _visitor.convert_typed_expression_to_function_call(inWhat as typed_expression); } type_node elemType = null; if (inWhat.type == null) { inWhat = tmp; } _visitor.FindIEnumerableElementType(/*_foreach_stmt, */ inWhat.type, ref elemType); if (_foreach_stmt.type_name == null) { var loc1 = _visitor.get_location(_foreach_stmt.identifier); dn = _visitor.context.check_name_node_type(loopIdentName, loc1, general_node_type.variable_node); vdn = (var_definition_node)dn; } else { vdn = _visitor.context.add_var_definition(loopIdentName, _visitor.get_location(_foreach_stmt.identifier)); type_node tn; if (_foreach_stmt.type_name is no_type_foreach) { tn = elemType; } else { tn = _visitor.convert_strong(_foreach_stmt.type_name); _visitor.check_for_type_allowed(tn, _visitor.get_location(_foreach_stmt.type_name)); } _visitor.context.close_var_definition_list(tn, null); _currentTreeNode.VariablesDefinedInScope.Add(new CapturedVariablesTreeNode.CapturedSymbolInfo(_foreach_stmt, _visitor.context.find(loopIdentName))); } newTreeNode.SymbolInfoLoopVar = _visitor.context.find(loopIdentName); if (!(vdn.type is compiled_generic_instance_type_node)) { _visitor.convertion_data_and_alghoritms.check_convert_type_with_inheritance(vdn.type, elemType, _visitor.get_location(_foreach_stmt.identifier)); } var fn = new foreach_node(vdn, inWhat, null, _visitor.get_location(_foreach_stmt)); _visitor.context.cycle_stack.push(fn); _visitor.context.loop_var_stack.Push(vdn); ProcessNode(_foreach_stmt.in_what); if (!(_foreach_stmt.stmt is statement_list)) { var stmtList = new statement_list(_foreach_stmt.stmt, _foreach_stmt.stmt.source_context); _foreach_stmt.stmt = stmtList; } ProcessNode(_foreach_stmt.stmt); _visitor.context.loop_var_stack.Pop(); _visitor.convertion_data_and_alghoritms.statement_list_stack.pop(); _visitor.context.cycle_stack.pop(); _currentTreeNode = _currentTreeNode.ParentNode; }
public override void visit(foreach_stmt _foreach_stmt) { // SSM 24.12.19 lambda_capture_foreach_counter.pas не проходит если откомментировать эти 2 строки // Причина - лямбда уже начала разбираться // Пробую сделать так: если foreach в лямбде, то оптимизация не делается, а если нет, то делается // Пока не получилось - ошибка err0157.pas начинает быть правильной программой // Надо как-то переменную x в стек context.loop_var_stack.Push(foreachVariable); помещать !!! Но не сейчас... (SSM 12.01.20) /*syntax_tree_node p = _foreach_stmt; * do * { * p = p.Parent; * } while (!(p == null || p is procedure_definition || p is function_lambda_definition)); * * if (!(p is function_lambda_definition)) // тогда оптимизируем * { * var feWhat = convert_strong(_foreach_stmt.in_what); * if (OptimizeForeachInCase1DArray(_foreach_stmt, feWhat)) return; * }*/ statements_list sl2 = new statements_list(get_location(_foreach_stmt)); convertion_data_and_alghoritms.statement_list_stack_push(sl2); expression_node foreachCollection; var_definition_node foreachVariable; ForeachCheckAndConvert(_foreach_stmt, out foreachCollection, out foreachVariable); // SSM 29.07.16 - если in_what - одномерный массив, то заменить код foreach на for // if (OptimizeForeachInCase1DArray(_foreach_stmt, foreachCollection)) return; statements_list sl = new statements_list(get_location(_foreach_stmt.stmt)); convertion_data_and_alghoritms.statement_list_stack_push(sl); foreach_node foreachNode = new foreach_node(foreachVariable, foreachCollection, null, get_location(_foreach_stmt)); context.cycle_stack.push(foreachNode); context.loop_var_stack.Push(foreachVariable); context.enter_code_block_with_bind(); statement_node body = convert_strong(_foreach_stmt.stmt); context.leave_code_block(); context.loop_var_stack.Pop(); context.cycle_stack.pop(); sl = convertion_data_and_alghoritms.statement_list_stack.pop(); if (sl.statements.Count > 0 || sl.local_variables.Count > 0) { sl.statements.AddElement(body); body = sl; } foreachNode.what_do = body; convertion_data_and_alghoritms.statement_list_stack.pop(); sl2.statements.AddElement(foreachNode); return_value(sl2); }