Skip to content

added support for python blocks #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,8 +13,24 @@ 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

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"
Expand Down
Binary file modified src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -97,21 +97,152 @@ public override void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node)
base.PostWalk(node);
}

/// <summary>
/// Not realy sure of this override's purpose. may be used to override behavior when the current node being walked is an error.
/// </summary>
/// <param name="node"> The python ast node being walked.</param>
/// <returns> boolean value. not important for our implementation. this is handeled by a call to the base class.</returns>
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.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)
{
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);
}

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);
}

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);
}


///<summary>
///This method returns the populated CodeFile.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ internal static UserDefinedType createClassUDT(IronPython.Compiler.Ast.ClassDefi
}

/// <summary>
/// <summary>
/// </summary>
/// <param name="node">The FunctionDefinition node</param>
/// <param name="currentCodeFile">The current CodeFile</param>
/// <param name="parent">The parent of the current CodeFile</param>
Expand All @@ -59,5 +57,41 @@ 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 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(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;
}
}
}