~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonLocalVariableAssignment.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using IronPython.Compiler.Ast;
 
6
 
 
7
namespace ICSharpCode.PythonBinding
 
8
{
 
9
        public class PythonLocalVariableAssignment
 
10
        {
 
11
                AssignmentStatement assignment;
 
12
                string variableName = String.Empty;
 
13
                string typeName = String.Empty;
 
14
                
 
15
                public PythonLocalVariableAssignment(AssignmentStatement assignment)
 
16
                {
 
17
                        this.assignment = assignment;
 
18
                        ParseAssignment();
 
19
                }
 
20
                
 
21
                public string TypeName {
 
22
                        get { return typeName; }
 
23
                }
 
24
                
 
25
                public string VariableName {
 
26
                        get { return variableName; }
 
27
                }
 
28
                
 
29
                public bool IsLocalVariableAssignment()
 
30
                {
 
31
                        return !String.IsNullOrEmpty(variableName);
 
32
                }
 
33
                
 
34
                void ParseAssignment()
 
35
                {
 
36
                        NameExpression nameExpression = assignment.Left[0] as NameExpression;
 
37
                        CallExpression callExpression = assignment.Right as CallExpression;
 
38
                        if ((nameExpression != null) && (callExpression != null)) {
 
39
                                variableName = nameExpression.Name;
 
40
                                typeName = GetTypeName(callExpression.Target);
 
41
                        }
 
42
                }
 
43
                
 
44
                /// <summary>
 
45
                /// Gets the fully qualified name of the type from the expression.
 
46
                /// </summary>
 
47
                /// <remarks>
 
48
                /// The expression is the first target of a call expression.
 
49
                /// 
 
50
                /// A call expression is a method or constructor call (right hand side of expression below):
 
51
                /// 
 
52
                /// a = Root.Test.Class1()
 
53
                /// 
 
54
                /// So the expression passed to this method will be a field expression in the
 
55
                /// above example which refers to Class1. The next target will be a field
 
56
                /// expression referring to Test. The The last target will be a name expression
 
57
                /// referring to Root.
 
58
                /// 
 
59
                /// If we have 
 
60
                /// 
 
61
                /// a = Class1()
 
62
                /// 
 
63
                /// then the expression will be a name expression referring to Class1.
 
64
                /// </remarks>
 
65
                string GetTypeName(Expression expression)
 
66
                {
 
67
                        NameExpression nameExpression = expression as NameExpression;
 
68
                        if (nameExpression != null) {
 
69
                                return nameExpression.Name;
 
70
                        }
 
71
                        return PythonControlFieldExpression.GetMemberName(expression as MemberExpression);
 
72
                }
 
73
        }
 
74
}