Encapsulates the program context relevent to a field.
Inheritance: IdContext
コード例 #1
0
ファイル: ContextBuilder.cs プロジェクト: abb-iss/Swum.NET
        /// <summary>
        /// Builds a FieldContext object based on the given field element.
        /// </summary>
        /// <param name="declElement">An XElement representing the decl element of the field.</param>
        /// <returns>A FieldContext object based on the given field.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException">Thrown if the passed XElement does not represent a decl element.</exception>
        public static FieldContext BuildFieldContext(XElement declElement)
        {
            if (declElement == null)
            {
                throw new ArgumentNullException("declElement");
            }
            if (declElement.Name != SRC.Declaration)
            {
                throw new ArgumentException(string.Format("The passed XElement must represent a <decl> element. Received a <{0}> element.", declElement.Name.ToString()), "declElement");
            }

            FieldContext fc = new FieldContext();
            //Type of the field
            var typeElement = declElement.Element(SRC.Type);
            if (typeElement != null)
            {
                bool isPrimitive;
                fc.IdType = ConstructTypeName(typeElement, out isPrimitive);
                fc.IdTypeIsPrimitive = isPrimitive;
            }

            //Determine declaring class
            XElement classElement = FindEnclosingClassElement(declElement);
            if (classElement != null && classElement.Element(SRC.Name) != null)
            {
                fc.DeclaringClass = GetNameFromNameElement(classElement.Element(SRC.Name));
            }

            return fc;
        }
コード例 #2
0
ファイル: SUnitTranslator.cs プロジェクト: herbertkb/Swummary
    private static SUnit TranslateAssignment(Statement statement)
    {
        // action = "Assign"
        // define left-hand-side (lhs)
        // theme = right hand side

        var fieldRule = SetupFieldRule();

        //        var equalsSign = statement.GetDescendants<OperatorUse>()
        //                                .Where(o => o.Text.Equals("=")).First();

        //        var lhs = equalsSign.GetSiblingsBeforeSelf<VariableUse>().First();

        var assignExpression = (VariableDeclaration) statement.GetExpressions().First();
        var lhs = assignExpression.Name;

        var lhsFieldContext = new FieldContext(assignExpression.VariableType.ToString(), false, "");
        var lhsDecNode = new FieldDeclarationNode(lhs.ToString(), lhsFieldContext);
        fieldRule.InClass(lhsDecNode);
        fieldRule.ConstructSwum(lhsDecNode);

        var rhsString = "";
        var rhsAction = "";
        var rhsTheme = "";
        Expression rhs = new Expression();
        if (assignExpression.Initializer != null)
        {
            rhs = assignExpression.Initializer;
        }

        if (rhs is VariableUse)
        {
            var rhsFieldContext = new FieldContext(rhs.ResolveType().First().ToString(), false, "");
            var rhsDecNode = new FieldDeclarationNode(rhs.ToString(), lhsFieldContext);
            fieldRule.InClass(rhsDecNode);
            fieldRule.ConstructSwum(rhsDecNode);
            rhsAction = "Assign";
            rhsString = rhsDecNode.ToPlainString();

        }
        else if (rhs is MethodCall)
        {

            string type = rhs.ResolveType().ToString();

            MethodContext mc = new MethodContext(type);
            MethodDeclarationNode mdn = new MethodDeclarationNode(rhs.ToString(), mc);

            var swumRule = SetupBaseVerbRule();
            swumRule.InClass(mdn);
            swumRule.ConstructSwum(mdn);

            rhsAction = mdn.Action.ToPlainString();
            rhsTheme = mdn.Action.ToPlainString();
            rhsString = mdn.ToPlainString();
        }
        else
        {
            rhsString = rhs.ToString();
        }

        var sunit = new SUnit();
        sunit.type = SUnitType.Assignment;
        sunit.action = rhsString;
        //sunit.lhs = lhsDecNode.ToPlainString();
        sunit.lhs = lhs.ToString();
        sunit.theme = rhsString;

        return sunit;
    }
コード例 #3
0
        public void TestBuildFieldContext_Struct() {
            string testSrcML = "<struct>struct <name>foo</name> <block>{<public type=\"default\"><decl_stmt><decl><type><name>int</name></type> <name>a</name></decl>;</decl_stmt></public>}</block>;</struct>";
            XElement xml = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);

            FieldContext fc1 = new FieldContext("int", true, "foo");
            FieldContext fc2 = ContextBuilder.BuildFieldContext(xml.Descendants(SRC.Declaration).First());
            Assert.IsTrue(FieldContextsAreEqual(fc1, fc2));
        }
コード例 #4
0
        public void TestBuildFieldContext_Global() {
            string testSrcML = "<decl_stmt><decl><type><name>int</name></type> <name>a</name> =<init> <expr><lit:literal type=\"number\">42</lit:literal></expr></init></decl>;</decl_stmt>";
            XElement xml = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);

            FieldContext fc1 = new FieldContext("int", true, "");
            FieldContext fc2 = ContextBuilder.BuildFieldContext(xml.Descendants(SRC.Declaration).First());
            Assert.IsTrue(FieldContextsAreEqual(fc1, fc2));
        }
コード例 #5
0
 private bool FieldContextsAreEqual(FieldContext fc1, FieldContext fc2) {
     if(fc1 == null && fc2 == null) {
         return true;
     }
     if((fc1 == null) ^ (fc2 == null)) {
         return false;
     }
     if(fc1.DeclaringClass != fc2.DeclaringClass
        || fc1.IdType != fc2.IdType
        || fc1.IdTypeIsPrimitive != fc2.IdTypeIsPrimitive) {
         return false;
     }
     return true;
 }