From 44a5bee28ceba824beb85abc2041cf00450b6763 Mon Sep 17 00:00:00 2001 From: Mikhalkovich Stanislav Date: Sun, 21 Jun 2020 17:12:43 +0300 Subject: [PATCH] FIX #2265 --- Configuration/GlobalAssemblyInfo.cs | 2 +- Configuration/Version.defs | 2 +- Localization/DefaultLang.resources | Bin 94258 -> 94365 bytes Release/pabcversion.txt | 2 +- ReleaseGenerators/PascalABCNET_version.nsh | 2 +- .../PatternsDesugaringVisitor.cs | 48 ++++++++++++++ SyntaxVisitors/SyntaxVisitors.csproj | 1 + .../YieldVisitors/DeleteAllLocalDefs.cs | 6 ++ .../YieldVisitors/IsVarRenameVisitor.cs | 60 ++++++++++++++++++ .../ProcessYieldsCapturedVars.cs | 5 ++ TestSuite/isvar2.pas | 18 ++++++ TestSuite/isvar3.pas | 19 ++++++ TestSuite/isvar4.pas | 19 ++++++ bin/Lng/Eng/SemanticErrors_ms.dat | 1 + 14 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 SyntaxVisitors/YieldVisitors/IsVarRenameVisitor.cs create mode 100644 TestSuite/isvar2.pas create mode 100644 TestSuite/isvar3.pas create mode 100644 TestSuite/isvar4.pas diff --git a/Configuration/GlobalAssemblyInfo.cs b/Configuration/GlobalAssemblyInfo.cs index b073e89b0..5a99a445c 100644 --- a/Configuration/GlobalAssemblyInfo.cs +++ b/Configuration/GlobalAssemblyInfo.cs @@ -15,7 +15,7 @@ internal static class RevisionClass public const string Major = "3"; public const string Minor = "6"; public const string Build = "3"; - public const string Revision = "2514"; + public const string Revision = "2518"; public const string MainVersion = Major + "." + Minor; public const string FullVersion = Major + "." + Minor + "." + Build + "." + Revision; diff --git a/Configuration/Version.defs b/Configuration/Version.defs index 86b20346d..78d147a63 100644 --- a/Configuration/Version.defs +++ b/Configuration/Version.defs @@ -1,4 +1,4 @@ %COREVERSION%=3 -%REVISION%=2514 +%REVISION%=2518 %MINOR%=6 %MAJOR%=3 diff --git a/Localization/DefaultLang.resources b/Localization/DefaultLang.resources index b6020d20093187546478ac563b0f21c58b2f043d..67f5ba17c2bec4f4a80aa960eedcb44e028b9a7e 100644 GIT binary patch delta 130 zcmV-|0Db?m;02xG1(4tYxv}A%s%b@7P)tcfNmO4$K~q#yUq(+-Ura$wLPSAdMOaW$ zMN?BrPfk-`K~hCuMo&^gNkl|NPCeh^?fc~*@9^m2<>ck<=>H({=0^=^*Fe@9^dM(isPatternExpr) != null, "Couldn't find statement list in upper nodes"); + var l = new List(); + //l.Add(stat); + var pp = isPatternExpr.right?.parameters; + if (pp != null) + { + foreach (var p in pp) + { + if (p is var_deconstructor_parameter vdp) + { + var idClone = vdp.identifier.TypedClone(); + var idNew = vdp.identifier.TypedClone(); + idNew.name = GenerateNewName(idNew.name); + var vs = new var_statement(idClone, idNew, idClone.source_context); + vdp.identifier.name = idNew.name; + l.Add(vs); + } + } + } + + var stat = GetAscendant(isPatternExpr); + if (stat is if_node ifn) + { + //l.Add(ifn.then_body); + if (!(ifn.then_body is statement_list)) + { + ifn.then_body = new statement_list(ifn.then_body); + } + (ifn.then_body as statement_list).list.InsertRange(0, l); + } + else + { + l.Insert(0, stat); + foreach (var x in l) + x.Parent = stat.Parent; + var stl = stat.Parent as statement_list; + stl.ReplaceInList(stat, l); + } + + //ReplaceStatement(stat, l); // тут нельзя. + DesugarIsExpression(isPatternExpr); } diff --git a/SyntaxVisitors/SyntaxVisitors.csproj b/SyntaxVisitors/SyntaxVisitors.csproj index 459191d04..70c553eb2 100644 --- a/SyntaxVisitors/SyntaxVisitors.csproj +++ b/SyntaxVisitors/SyntaxVisitors.csproj @@ -65,6 +65,7 @@ + diff --git a/SyntaxVisitors/YieldVisitors/DeleteAllLocalDefs.cs b/SyntaxVisitors/YieldVisitors/DeleteAllLocalDefs.cs index 6d190a2b6..fad1a1bcc 100644 --- a/SyntaxVisitors/YieldVisitors/DeleteAllLocalDefs.cs +++ b/SyntaxVisitors/YieldVisitors/DeleteAllLocalDefs.cs @@ -85,5 +85,11 @@ namespace SyntaxVisitors // еще - не заходить в лямбды } + + public override void visit(desugared_deconstruction dd) + { + DefaultVisit(dd); + } + } } diff --git a/SyntaxVisitors/YieldVisitors/IsVarRenameVisitor.cs b/SyntaxVisitors/YieldVisitors/IsVarRenameVisitor.cs new file mode 100644 index 000000000..9b7c86b9f --- /dev/null +++ b/SyntaxVisitors/YieldVisitors/IsVarRenameVisitor.cs @@ -0,0 +1,60 @@ +// 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 System.Text; +using System.Threading.Tasks; + +using PascalABCCompiler; +using PascalABCCompiler.SyntaxTree; + +namespace SyntaxVisitors +{ + public class IsVarRenameVisitor : BaseChangeVisitor + { + + public IsVarRenameVisitor() + { + } + + public static IsVarRenameVisitor New + { + get { return new IsVarRenameVisitor(); } + } + + public static void Accept(procedure_definition pd) + { + New.ProcessNode(pd); + } + + public override void visit(procedure_header proc_header) + { + // DO THIS THING! + } + /* public override void visit(desugared_deconstruction dd) + { + var l = new List(); + l.Add(dd); + foreach (var d in dd.variables.definitions) + { + foreach (var id in d.vars.idents) + { + // добавить после неё кучу объявлений с присваиваниями + //ReplaceStatement(dd,) + var newvar = new ident(NewVariableName(id.name),id.source_context); + var vs = new var_statement(new ident(id.name, id.source_context), newvar, id.source_context); + l.Add(vs); + id.name = newvar.name; + } + } + ReplaceStatement(dd, l); + }*/ + + private string NewVariableName(string name) + { + name = name.ToLower(); + return "$RenIsVarYield$" + name; + } + } +} diff --git a/SyntaxVisitors/YieldVisitors/ProcessYieldsCapturedVars.cs b/SyntaxVisitors/YieldVisitors/ProcessYieldsCapturedVars.cs index 2913a37a2..a2e7238ca 100644 --- a/SyntaxVisitors/YieldVisitors/ProcessYieldsCapturedVars.cs +++ b/SyntaxVisitors/YieldVisitors/ProcessYieldsCapturedVars.cs @@ -971,6 +971,11 @@ namespace SyntaxVisitors pd.visit(checkVarRedefVisitor); */ + // Если есть в pd узлы is_pattern_expr, то запустить визитор, добавляющий присваивание временной переменной и заменяющий + // везде далее имя исходной переменной на имя временной + // SSM 20/06 - не очень так получается. Сложно + //IsVarRenameVisitor.Accept(pd); + // Выносим выражение из yield в отдельную переменную ReplaceYieldExprByVarVisitor.Accept(pd); diff --git a/TestSuite/isvar2.pas b/TestSuite/isvar2.pas new file mode 100644 index 000000000..4808ebbc9 --- /dev/null +++ b/TestSuite/isvar2.pas @@ -0,0 +1,18 @@ +function f1(o: object): sequence of object; +begin + + //var c := o is object(var a); + if o is object(var a) then + begin + // var b := a; + // Writeln(a); // object + yield a; + Assert(a <> nil); // nil + // Writeln(b); // object + // b := b; + end; +end; + +begin + foreach var o in f1(new object) do +end. \ No newline at end of file diff --git a/TestSuite/isvar3.pas b/TestSuite/isvar3.pas new file mode 100644 index 000000000..4f10e0e48 --- /dev/null +++ b/TestSuite/isvar3.pas @@ -0,0 +1,19 @@ +function f1(o: object): sequence of object; +begin + + //var c := o is object(var a); + if o is object(var a) then + //begin + // var b := a; + // Writeln(a); // object + yield a; + //Assert(a <> nil); // nil + // Writeln(b); // object + // b := b; + //end; +end; + +begin + foreach var o in f1(new object) do + Assert(o<>nil) +end. \ No newline at end of file diff --git a/TestSuite/isvar4.pas b/TestSuite/isvar4.pas new file mode 100644 index 000000000..3d54646af --- /dev/null +++ b/TestSuite/isvar4.pas @@ -0,0 +1,19 @@ +function f1(o: object): sequence of object; +begin + + var c := o is object(var a); + //if o is object(var a) then + //begin + // var b := a; + // Writeln(a); // object + yield a; + Assert(a <> nil); // nil + // Writeln(b); // object + // b := b; + //end; +end; + +begin + foreach var o in f1(new object) do + +end. \ No newline at end of file diff --git a/bin/Lng/Eng/SemanticErrors_ms.dat b/bin/Lng/Eng/SemanticErrors_ms.dat index 8a1f15388..35a546dfc 100644 --- a/bin/Lng/Eng/SemanticErrors_ms.dat +++ b/bin/Lng/Eng/SemanticErrors_ms.dat @@ -39,3 +39,4 @@ ONE_DIM_ARRAY_OR_LIST_EXPECTED=One-dimensional array or list expected TUPLE_TYPE_ELEMENTS_COUNT_MUST_BE_LESSEQUAL_7=Number of elements in tuple type must be less than 8 OVERRIDE_METHODS_CANNOT_CONTAIN_WHERE_SECTION=Methods with override modifier can't contain where section Ìåòîäû ñ ìîäèôèêàòîðîì override íå ìîãóò ñîäåðæàòü ñîáñòâåííóþ ñåêöèþ where +EXPLICIT_CASTS_FOR_LAMBDA_EXPRESSIONS_ARE_FORBIDDEN=Explicit type casts for lambda expressions are forbidden \ No newline at end of file