pascalabcnet/SyntaxVisitors/VisitorsForLambdas/StandOutExprWithLambdaInForeachSequenceVisitor.cs
Mikhalkovich Stanislav 07df31f762 fix #2401
2021-01-10 07:54:31 +03:00

60 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using PascalABCCompiler.SyntaxTree;
namespace PascalABCCompiler.SyntaxTreeConverters
{
// Первое предназначение - вынести последовательность из заголовка в foreach до foreach как отдельное присваивание
// Второе предназначение - переименовать все переменные, совпадающие по имени с типом T обобщенного класса, в котором находится метод, содержащий лямбду
public class StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor : BaseChangeVisitor
{
public static StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor New
{
get
{
return new StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor();
}
}
private int GenIdNum = 0;
//private ident_list ClassTemplateArgsOrNull = null; // если мы - в обобщенном классе, то это - его обобщенные параметры
public ident GenIdentName()
{
GenIdNum++;
return new ident("$GenContFE" + GenIdNum.ToString());
}
public override void visit(foreach_stmt fe)
{
//if (fe.DescendantNodes().OfType<function_lambda_definition>().Count() > 0) // из-за #1984 убрал вообще условие. Пусть будет всегда
{
var id = GenIdentName();
id.Parent = fe;
var ass = new var_statement(id, fe.in_what, fe.in_what.source_context);
id.source_context = fe.in_what.source_context;
fe.in_what = id;
var l = new List<statement> { ass, fe };
//ReplaceStatement(fe, l);
ReplaceStatementUsingParent(fe, l);
}
base.visit(fe);
}
private int countNestedLambdas = 0;
public override void visit(function_lambda_definition fld)
{
countNestedLambdas += 1;
if (countNestedLambdas>10)
throw new SyntaxVisitors.SyntaxVisitorError("NESTED_LAMBDAS_MAXIMUM_10", fld.source_context);
ProcessNode(fld.proc_body);
countNestedLambdas -= 1;
}
}
}