From ed9c4e4440f48b53082001353f884d81587bf883 Mon Sep 17 00:00:00 2001 From: Venkatesh Potluri Date: Wed, 29 Nov 2017 09:25:20 +0530 Subject: [PATCH 1/5] added support for Python for and while loops. --- .../Languages/PythonEntityCollector.cs | 49 ++++++++++++++++--- .../Languages/PythonEntityCreationHelper.cs | 14 +++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs index 9c478a7..b1c1885 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs @@ -97,21 +97,56 @@ public override void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node) base.PostWalk(node); } - /// - /// Not realy sure of this override's purpose. may be used to override behavior when the current node being walked is an error. - /// - /// The python ast node being walked. - /// boolean value. not important for our implementation. this is handeled by a call to the base class. - public override bool Walk(ErrorExpression node) + public override bool Walk(IronPython.Compiler.Ast.ForStatement node) { + if (node == null) + { + return false; + } + var forBlock = PythonEntityCreationHelper.createForBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(forBlock); + forBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = forBlock; return true; } - public override bool Walk(Parameter node) + public override void PostWalk(IronPython.Compiler.Ast.ForStatement node) + { +if(node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.WhileStatement node) { + if(node == null) + { + return false; + } + var whileBlock = PythonEntityCreationHelper.createWhileBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(whileBlock); + whileBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = whileBlock; return true; + + } + + public override void PostWalk(IronPython.Compiler.Ast.WhileStatement node) + { + if(node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); } + /// ///This method returns the populated CodeFile. /// diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs index a49212e..a8e9e7e 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs @@ -36,8 +36,6 @@ internal static UserDefinedType createClassUDT(IronPython.Compiler.Ast.ClassDefi } /// - /// - /// /// The FunctionDefinition node /// The current CodeFile /// The parent of the current CodeFile @@ -59,5 +57,17 @@ internal static FunctionDefinition createFunction(IronPython.Compiler.Ast.Functi return fn; } + + internal static ForBlock createForBlock(IronPython.Compiler.Ast.ForStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + ForBlock forBlock = new ForBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return forBlock; + } + + internal static WhileBlock createWhileBlock(IronPython.Compiler.Ast.WhileStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + WhileBlock whileBlock = new WhileBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return whileBlock; + } } } From d2faa9773007cbfd7720b0a4d627b7e6fe64e05d Mon Sep 17 00:00:00 2001 From: Venkatesh Potluri Date: Thu, 30 Nov 2017 14:05:56 +0530 Subject: [PATCH 2/5] modified test program. --- .../Programs/PythonTests/pythonClasses.py | 17 ++++++++++- .../Languages/PythonEntityCollector.cs | 28 +++++++++++++++++-- .../Languages/PythonEntityCreationHelper.cs | 6 ++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py index 95f9665..e79341d 100644 --- a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py +++ b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py @@ -4,6 +4,7 @@ class adder: def __init__(self, a, b): self.a=a self.b=b + self.list = [1,2,3,4,5,6] def getFirstValue(self): return self.a @@ -12,7 +13,21 @@ def getSecondValue(self): return self.b def add(self): - return self.a+self.b + a=0 + for i in self.list: + self.a = self.a+ i + return self.a + + def addEvens(self): + l = len(self.list) + i=0 + sum=0 + while i in range(l): + if self.list[i]%2 == 0: + sum = sum + self.list[i] + + return sum + #method that is not in any class: def outer_method(): diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs index b1c1885..20af5e1 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs @@ -8,10 +8,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Diagnostics; -//using IronPython.Compiler.Ast; using IronPython.Compiler.Ast; -using System.Diagnostics; + namespace Microsoft.CodeTalk.LanguageService { @@ -146,6 +146,30 @@ public override void PostWalk(IronPython.Compiler.Ast.WhileStatement node) base.PostWalk(node); } + public override bool Walk(IronPython.Compiler.Ast.IfStatement node) + { + if (node == null) + { + return false; + } + var ifBlock = PythonEntityCreationHelper.createIfBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(ifBlock); + ifBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = ifBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.IfStatement node) + { + if(node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + /// ///This method returns the populated CodeFile. diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs index a8e9e7e..7084795 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs @@ -69,5 +69,11 @@ internal static WhileBlock createWhileBlock(IronPython.Compiler.Ast.WhileStateme WhileBlock whileBlock = new WhileBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); return whileBlock; } + + internal static IfBlock createIfBlock(IfStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + IfBlock ifBlock = new IfBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return ifBlock; + } } } From 81383b9ca4dcf6f00db67ffaaab800d4c575e45d Mon Sep 17 00:00:00 2001 From: Venkatesh Potluri Date: Fri, 1 Dec 2017 09:35:56 +0530 Subject: [PATCH 3/5] initial commit for test cases --- .../Programs/PythonTests/PythonFor.py.txt | 0 .../PythonTest.cs | Bin 2416 -> 4260 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/PythonFor.py.txt diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/PythonFor.py.txt b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/PythonFor.py.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs b/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs index 0becd50a34629c314413140a7d0c32909c581e43..89e1c5b02827e769cf6ae5c37b588ff48c325026 100644 GIT binary patch delta 684 zcmb7?%}N4M7=~ZdI3|T5L{^*0m4Y1#j20mgS({o^)&-b4D#%QX7P*M7AlG#kA!u8|n>ZvGCnxX;O`ajZ0|0zV2u1(^ From cbdd40872c8fa0876c3c266d9a4c5c1b40de9064 Mon Sep 17 00:00:00 2001 From: Venkatesh Potluri Date: Fri, 1 Dec 2017 10:03:33 +0530 Subject: [PATCH 4/5] final commit: added basic test cases for python. --- .../Programs/PythonTests/PythonFor.py.txt | 0 .../PythonTest.cs | Bin 4260 -> 3750 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/PythonFor.py.txt diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/PythonFor.py.txt b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/PythonFor.py.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs b/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs index 89e1c5b02827e769cf6ae5c37b588ff48c325026..c2051c066287251acb29f4ffd80ccc1303577217 100644 GIT binary patch delta 23 fcmZ3YxJ-6K72jk5F22c3972;5_}C`T6JP@XS78Qu delta 34 ocmZ1`yF_tA72jk5ZWq=xhJ1#i&G*@NGfqz6lLBJ4$qV?{0L9b_y#N3J From e03b6c8490c8f374eef058c5c0f0c24ad5d1a774 Mon Sep 17 00:00:00 2001 From: Venkatesh Potluri Date: Sun, 3 Dec 2017 20:51:41 +0530 Subject: [PATCH 5/5] added walkers for try, comprehension for, and comprehnsion if. --- .../Programs/PythonTests/pythonClasses.py | 4 +- .../Languages/PythonEntityCollector.cs | 74 ++++++++++++++++++- .../Languages/PythonEntityCreationHelper.cs | 20 ++++- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py index e79341d..c1dc8a2 100644 --- a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py +++ b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py @@ -25,10 +25,12 @@ def addEvens(self): while i in range(l): if self.list[i]%2 == 0: sum = sum + self.list[i] - return sum + def returnOdds(self): + return [odds for num in self.list if num%2 != 0] + #method that is not in any class: def outer_method(): return "bla" diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs index 20af5e1..dd24ce9 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs @@ -121,7 +121,31 @@ public override void PostWalk(IronPython.Compiler.Ast.ForStatement node) base.PostWalk(node); } - public override bool Walk(IronPython.Compiler.Ast.WhileStatement node) + public override bool Walk(IronPython.Compiler.Ast.ComprehensionFor node) + { + if (node == null) + { + return false; + } + var forBlock = PythonEntityCreationHelper.createComprehensionForBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(forBlock); + forBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = forBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.ComprehensionFor node) + { + if (node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.WhileStatement node) { if(node == null) { @@ -170,7 +194,55 @@ public override void PostWalk(IronPython.Compiler.Ast.IfStatement node) base.PostWalk(node); } + public override bool Walk(IronPython.Compiler.Ast.ComprehensionIf node) + { + if (node == null) + { + return false; + } + var ifBlock = PythonEntityCreationHelper.createComprehensionIfBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(ifBlock); + ifBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = ifBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.ComprehensionIf node) + { + if (node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.TryStatement node) + { + if (node == null) + { + return false; + } + var tryBlock = PythonEntityCreationHelper.createTryBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(tryBlock); + tryBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = tryBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.TryStatement node) + { + if (node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + /// ///This method returns the populated CodeFile. /// diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs index 7084795..da0ea34 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs @@ -64,16 +64,34 @@ internal static ForBlock createForBlock(IronPython.Compiler.Ast.ForStatement nod return forBlock; } + internal static ForBlock createComprehensionForBlock(IronPython.Compiler.Ast.ComprehensionFor node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + ForBlock forBlock = new ForBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return forBlock; + } + internal static WhileBlock createWhileBlock(IronPython.Compiler.Ast.WhileStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) { WhileBlock whileBlock = new WhileBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); return whileBlock; } - internal static IfBlock createIfBlock(IfStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + internal static IfBlock createIfBlock(IronPython.Compiler.Ast.IfStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) { IfBlock ifBlock = new IfBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); return ifBlock; } + + internal static IfBlock createComprehensionIfBlock(IronPython.Compiler.Ast.ComprehensionIf node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + IfBlock ifBlock = new IfBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return ifBlock; + } + + internal static TryBlock createTryBlock(IronPython.Compiler.Ast.TryStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + TryBlock tryBlock = new TryBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return tryBlock; + } } }