Пример #1
0
        public override void VisitEnumDeclaration(EnumDeclarationSyntax node)
        {
            var name = node.Identifier.Text;

            cb.Append("enum class ").AppendLine(name)
            .Scope(() =>
            {
                base.VisitEnumDeclaration(node);
            });

            // now let's generate ostream& operator <<
            cb.AppendLine("std::ostream& operator<<(std::ostream& os, const " + name + " obj)")
            .Scope(() =>
            {
                cb.AppendLineWithIndent("switch (obj)")
                .Scope(() =>
                {
                    foreach (var enumCase in node.Members)
                    {
                        string caseName = enumCase.Identifier.Text;
                        // todo: string output needs to parse camelCase => "camel case"
                        cb.AppendWithIndent("case " + name + "::" + caseName + ": ")
                        .AppendLine("os << \"" + Regex.Replace(caseName, @"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))", " $1").ToLowerInvariant() + "\"; break;");
                    }
                });
                cb.AppendLineWithIndent("return os;");
            });
        }
Пример #2
0
        public override void VisitExpressionStatement(ExpressionStatementSyntax node)
        {
            // hack: Console.WriteLine
            var s = node.Expression.GetText().ToString().Trim();

            if (s.StartsWith("Console.WriteLine"))
            {
                var ise  = node.Expression as InvocationExpressionSyntax;
                var args = ise.ArgumentList;
                cb.AppendWithIndent("cout << ")
                .Append(args.GetText().ToString().Trim('(', ')').Replace(" + ", " << ").Replace("+", " << "))
                .AppendLine(" << endl;");
            }
            else if (s.StartsWith("Environment.Exit"))
            {
                var ise  = node.Expression as InvocationExpressionSyntax;
                var args = ise.ArgumentList;
                cb.AppendWithIndent("exit(")
                .Append(args.GetText().ToString().Trim('(', ')'))
                .AppendLine(");");
            }
            else
            {
                base.VisitExpressionStatement(node);
            }
        }
Пример #3
0
        public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            // todo: ensure this is same visibility
            var z = model.GetDeclaredSymbol(node);

            var builder = tcb.GetBuilderFor(z.DeclaredAccessibility);

            builder.AppendWithIndent("__declspec(property(");
            CodeBuilder getBuilder = null, setBuilder = null;

            if (z.GetMethod != null)
            {
                builder.Append("get=Get").Append(z.Name);

                getBuilder = new CodeBuilder(builder.IndentValue);
                getBuilder.AppendWithIndent(z.Type.ToCppType())
                .Append(" ") // return by value
                .Append("Get")
                .Append(z.Name)
                .AppendLine("() const;");
            }
            if (z.SetMethod != null)
            {
                if (z.GetMethod != null)
                {
                    builder.Append(",");
                }
                builder.Append("put=Set").Append(z.Name);

                setBuilder = new CodeBuilder(builder.IndentValue);
                setBuilder.AppendWithIndent("void ")
                .Append("Set")
                .Append(z.Name)
                .Append("(")
                .Append(z.Type.ToCppType())
                .AppendLine(");");
            }

            builder.AppendLine(")) ")
            .AppendWithIndent(z.Type.ToCppType())
            .Append(" ")
            .Append(z.Name)
            .AppendLine(";");

            if (getBuilder != null)
            {
                builder.Append(getBuilder.ToString());
            }

            if (setBuilder != null)
            {
                builder.Append(setBuilder.ToString());
            }
        }
Пример #4
0
        public override void VisitAccessorDeclaration(AccessorDeclarationSyntax node)
        {
            if (node.Body != null && node.Parent is BasePropertyDeclarationSyntax)
            {
                var z = model.GetDeclaredSymbol(node.Parent as PropertyDeclarationSyntax);

                cb.AppendWithIndent(z.Type.ToCppType())
                .Append(" ")
                .Append(settings.GetPrefix)
                .Append(z.Name)
                .AppendLine("() const");
            }
            base.VisitAccessorDeclaration(node);
        }
Пример #5
0
        public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            // todo: ensure this is same visibility
              var z = model.GetDeclaredSymbol(node);

              var builder = tcb.GetBuilderFor(z.DeclaredAccessibility);

              builder.AppendWithIndent("__declspec(property(");
              CodeBuilder getBuilder = null;
              if (z.GetMethod != null)
              {
            builder.Append("get=Get").Append(z.Name);

            getBuilder = new CodeBuilder(builder.IndentValue);
            getBuilder.AppendWithIndent(z.Type.ToCppType())
              .Append("& ") // this is rather opinionated :) might rethink later
              .Append("Get")
              .Append(z.Name)
              .AppendLine("() const;");
              }
              if (z.SetMethod != null)
              {
            if (z.GetMethod != null) builder.Append(",");
            builder.Append("put=Set").Append(z.Name);
              }

              builder.Append(")) ")
            .Append(z.Type.ToCppType())
            .Append(" ")
            .Append(z.Name)
            .AppendLine(";");

              if (getBuilder != null)
            builder.Append(getBuilder.ToString());
        }