private static void ToCode(this CSharp.Class @class, CsBuilder builder)
        {
            builder.StartClass(@class.AccessModifiers, @class.ClassModifiers, @class.Keyword, @class.Name);

            foreach (var field in @class.Fields)
            {
                builder.StartField(field.AccessModifiers, field.FieldModifiers, field.TypeName, field.Name);

                var initExpr = field.InitExpression;
                if (!String.IsNullOrEmpty(initExpr))
                {
                    builder.Append(initExpr);
                }
            }

            foreach (var property in @class.Properties)
            {
                builder.Property(property.AccessModifiers, property.TypeName, property.Name);
            }

            foreach (var method in @class.Methods)
            {
                var parameters = method.Parameters
                                 .Select(p => (name: p.Name, type: p.TypeName))
                                 .ToArray();
                builder.WriteIndent();
                builder.StartMethod(method.AccessModifiers, method.MethodModifiers, method.TypeName, method.Name, parameters);
                builder.WriteIndent();
                builder.AppendLine(method.GetBody(0).ToString());
                builder.EndScope();
            }

            foreach (var @enum in @class.Enums)
            {
                builder.StartEnum(@enum.AccessModifiers, @enum.Name, @enum.BaseTypeName);

                foreach (var option in @enum.Options)
                {
                    builder.WriteIndent();
                    builder.Append($"{option.Name}");
                    if (!String.IsNullOrEmpty(option.Value))
                    {
                        builder.Append($" = {option.Value}");
                    }
                    builder.AppendLine(",");
                }

                builder.EndScope();
            }

            foreach (var nested in @class.Classes)
            {
                nested.ToCode(builder);
            }

            builder.EndScope();
        }
        public static string ToCode(this AstExpression?expression)
        {
            if (expression is null)
            {
                return(String.Empty);
            }

            var builder = new CsBuilder();

            new EmitExpression(builder).VisitExpression(expression);
            return(builder.ToString());
        }
        public static string ToCode(this CSharp.Namespace ns)
        {
            var builder = new CsBuilder();

            builder.StartNamespace(ns.Name);

            foreach (var @using in ns.Usings)
            {
                builder.Using(@using);
            }

            foreach (var @class in ns.Classes)
            {
                @class.ToCode(builder);
            }

            builder.EndScope();
            return(builder.ToString());
        }
Beispiel #4
0
 internal CodeBuilder(CsBuilder csBuilder)
 {
     _csBuilder = csBuilder;
 }
Beispiel #5
0
 internal EmitExpression(CsBuilder builder)
 {
     _builder = builder;
 }
            public override void Build(CsBuilder builder, AstFunctionDefinition functionDef)
            {
                var conversion = "Conversion";

                builder.Append($"Zsharp.Runtime.{conversion}.{functionDef.Identifier.SymbolName.CanonicalName.FullName}");
            }
 public virtual void Build(CsBuilder builder, AstFunctionDefinition functionDef)
 => throw new ZsharpException("Not Implemented.");
 public virtual void Build(CsBuilder builder)
 => throw new ZsharpException("Not Implemented.");