Example #1
0
		// Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var phpIncrementrors = Collect(emiter, style, incrementors);
            var phpInitVariables = Collect(emiter, style, initVariables);

            style = style ?? new PhpEmitStyle();

            var header =
                    style.Compression == EmitStyleCompression.Beauty
                    ? "for({0}; {1}; {2})"
                    : "for({0};{1};{2})";
            header = string.Format(header, phpInitVariables, condition.GetPhpCode(style), phpIncrementrors);


            EmitHeaderStatement(emiter, writer, style, header, statement);

            /*
            if (statement == null)
                text += "{}";
            if (style.Compression == EmitStyleCompression.NearCrypto)
                writer.Write(text);
            else
                writer.WriteLn(text);
            if (statement == null) return;
            {
                var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems);
                writer.IncIndent();
                statement.Emit(emiter, writer, iStyle);
                writer.DecIndent();
            }
             */
        }
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return string.Format("{0}::{1}{2}",
         _className.NameForEmit(style),
         _isConst ? "" : "$",
         _fieldName);
 }
Example #3
0
		// Public Methods 

        public string GetPhpCode(PhpEmitStyle s)
        {
            s = s ?? new PhpEmitStyle();
            var eq = s.Compression == EmitStyleCompression.Beauty ? " = " : "=";
            var d = defaultValue != null ? (eq + defaultValue.GetPhpCode(s)) : "";
            return string.Format("${0}{1}", name, d);
        }
 public override string GetPhpCode(PhpEmitStyle style)
 {
     var form = style == null || style.Compression == EmitStyleCompression.Beauty
         ? "{0} ? {1} : {2}"
         : "{0}?{1}:{2}";
     return string.Format(form, _condition.GetPhpCode(style), _whenTrue.GetPhpCode(style), _whenFalse.GetPhpCode(style));
 }
Example #5
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var saveStyleCurrentClass = style.CurrentClass;
            var saveStyleCurrentNamespace = style.CurrentNamespace;
            try
            {
                if (IsEmpty)
                    return;
                if (style.CurrentNamespace == null)
                    style.CurrentNamespace = PhpNamespace.Root;
                if (style.CurrentNamespace != _name.Namespace)
                    throw new Exception("Unable to emit class into different namespace");
                var e = "";
                if (!_baseTypeName.IsEmpty)
                    e = " extends " + _baseTypeName.NameForEmit(style);
                writer.OpenLnF("class {0}{1} {{", Name.ShortName, e);
                style.CurrentClass = _name; // do not move this before "class XXX" is emited
                for (var orderGroup = 0; orderGroup < 3; orderGroup++)
                    foreach (var field in _fields.Where(_ => FieldOrderGroup(_) == orderGroup))
                    {
                        field.Emit(emiter, writer, style);
                    }
                foreach (var me in Methods)
                {
                    me.Emit(emiter, writer, style);
                }
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentClass = saveStyleCurrentClass;
                style.CurrentNamespace = saveStyleCurrentNamespace;
            }
        }
Example #6
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            var b = style != null && style.Compression != EmitStyleCompression.Beauty;
            var a = PhpValues.ToPhpCodeValue(Value, b);

            switch (a.Kind)
            {
            case PhpCodeValue.Kinds.Null:
                return("null");

            default:
                return(a.PhpValue);
            }
            //if (_value == null)
            //    return "null";
            //var tt = _value.GetType();
            //if (_value is string)
            //    return PhpStringEmit(_value as string, style);
            //if (_value is double)
            //    return ((double)_value).ToString(System.Globalization.CultureInfo.InvariantCulture);
            //if (_value is bool)
            //    return ((bool)_value) ? "true" : "false";
            //if (tt.IsEnum)
            //{
            //    var tmp = PhpValues.EnumToPhpCode(_value, );
            //    return tmp.Value;

            //}
            //return _value.ToString();
        }
Example #7
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpEmitStyle style, string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }

            var writer = new PhpSourceCodeWriter();
            var styleCurrentNamespace = style.CurrentNamespace;

            try
            {
                Emit(emiter, writer, style);

                {
                    var fi = new FileInfo(filename);
                    if (fi.Directory != null)
                    {
                        fi.Directory.Create();
                    }
                    var codeStr = writer.GetCode();
                    var binary  = Encoding.UTF8.GetBytes(codeStr);
                    File.WriteAllBytes(fi.FullName, binary);
                }
            }
            finally
            {
                style.CurrentNamespace = styleCurrentNamespace;
            }
        }
Example #8
0
        public string NameForEmit(PhpEmitStyle style)
        {
            if (style == null)
            {
                return(EmitName);
            }
            if (this == style.CurrentClass)
            {
                return(ClassnameSelf);
            }
            if (style.CurrentNamespace == null)
            {
                return(FullName);
            }
            {
                if ((FullName + TokenNsSeparator).StartsWith(style.CurrentNamespace.Name + TokenNsSeparator))
                {
                    return(FullName.Substring(style.CurrentNamespace.Name.Length + 1));
                }
            }
            if (style.CurrentNamespace == Namespace)
            {
                return(ShortName);
            }
            if (style.CurrentNamespace == null)
            {
                return(EmitName);
            }
            if (Namespace == null && !FullName.StartsWith(TokenNsSeparator.ToString(CultureInfo.InvariantCulture)))
            {
                return(TokenNsSeparator + FullName);
            }

            return(EmitName);
        }
Example #9
0
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return(string.Format("{0}::{1}{2}",
                          _className.NameForEmit(style),
                          IsConst ? "" : "$",
                          _fieldName));
 }
Example #10
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            var b = style != null && style.Compression != EmitStyleCompression.Beauty;
            var a = PhpValues.ToPhpCodeValue(_value, b);
            switch (a.Kind)
            {
                case PhpCodeValue.Kinds.Null:
                    return "null";
                default:
                    return a.PhpValue;
            }
            //if (_value == null)
            //    return "null";
            //var tt = _value.GetType();
            //if (_value is string)
            //    return PhpStringEmit(_value as string, style);
            //if (_value is double)
            //    return ((double)_value).ToString(System.Globalization.CultureInfo.InvariantCulture);
            //if (_value is bool)
            //    return ((bool)_value) ? "true" : "false";
            //if (tt.IsEnum)
            //{
            //    var tmp = PhpValues.EnumToPhpCode(_value, );
            //    return tmp.Value;

            //}
            //return _value.ToString();
        }
Example #11
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = PhpEmitStyle.xClone(style);
            if (!style.AsIncrementor)
            {
                if (_expression is PhpMethodCallExpression)
                {
                    var methodCallExpression = _expression as PhpMethodCallExpression;
                    if (methodCallExpression.CallType == MethodCallStyles.Procedural &&
                        methodCallExpression.Name == "echo")
                    {
                        if (EmitInlineHtml(writer, style))
                        {
                            return;
                        }
                    }
                }
            }
            var code = _expression.GetPhpCode(style);

            if (style.AsIncrementor)
            {
                writer.Write(code);
            }
            else
            {
                writer.WriteLn(code + ";");
            }
        }
        public override string GetPhpCode(PhpEmitStyle style)
        {
            var join      = style == null || style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
            var xstyle    = PhpEmitStyle.xClone(style);
            var arguments = string.Join(join, Arguments.Select(i => i.GetPhpCode(xstyle)));

            if (IsConstructorCall)
            {
                var a = string.Format("new {0}({1})", _className.NameForEmit(style), arguments);
                return(a);
            }
            var name = _name;

            if (!_className.IsEmpty)
            {
                name = _className.NameForEmit(style) + "::" + name;
            }
            else if (TargetObject != null)
            {
                var to = TargetObject;
                if (TargetObject is PhpMethodCallExpression && (TargetObject as PhpMethodCallExpression).IsConstructorCall)
                {
                    to = new PhpParenthesizedExpression(to);
                }
                name = to.GetPhpCode(style) + "->" + name;
            }
            var code = string.Format(name == "echo" ? "{0} {1}" : "{0}({1})", name, arguments);

            return(code);
        }
Example #13
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            if (initializers == null || initializers.Length == 0)
            {
                return("array()");
            }
            style = style ?? new PhpEmitStyle();
            var przecinek = style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
            var www       = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";


            var list = new List <string>();

            foreach (var initializeValue in initializers)
            {
                if (initializeValue is PhpAssignExpression)
                {
                    var assignExpression = initializeValue as PhpAssignExpression;
                    if (!string.IsNullOrEmpty(assignExpression.OptionalOperator))
                    {
                        throw new NotSupportedException();
                    }

                    if (assignExpression.Left is PhpArrayAccessExpression)
                    {
                        var left = assignExpression.Left as PhpArrayAccessExpression;
                        if (left.PhpArray is PhpThisExpression)
                        {
                            var o = left.Index + www + assignExpression.Right;
                            list.Add(o);
                            continue;
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else if (assignExpression.Left is PhpInstanceFieldAccessExpression)
                    {
                        var l1 = assignExpression.Left as PhpInstanceFieldAccessExpression;
                        var fn = new PhpConstValue(l1.FieldName);
                        var o  = fn.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                    else
                    {
                        var o = assignExpression.Left.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                }
                else
                {
                    list.Add(initializeValue.GetPhpCode(style));
                }
            }
            return("array(" + string.Join(przecinek, list) + ")");
        }
Example #14
0
        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var nsManager = new PhpModuleNamespaceManager();

            style.CurrentNamespace = null;
            if (!string.IsNullOrEmpty(_topComments))
            {
                writer.WriteLn("/*\r\n" + _topComments.Trim() + "\r\n*/");
            }
            var module = this;
            {
                // var noBracketStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.Never);

                {
                    // top code
                    var collectedTopCodeBlock = new PhpCodeBlock();
                    collectedTopCodeBlock.Statements.AddRange(ConvertRequestedToCode());
                    collectedTopCodeBlock.Statements.AddRange(ConvertDefinedConstToCode());
                    if (TopCode != null)
                    {
                        collectedTopCodeBlock.Statements.AddRange(TopCode.Statements);
                    }
                    nsManager.Add(collectedTopCodeBlock.Statements);
                }

                {
                    var classesGbNamespace = module.Classes.GroupBy(u => u.Name.Namespace);
                    foreach (var classesInNamespace in classesGbNamespace.OrderBy(i => !i.Key.IsRoot))
                    {
                        foreach (var c in classesInNamespace)
                        {
                            nsManager.Add(c);
                        }
                    }
                }
                if (BottomCode != null)
                {
                    nsManager.Add(BottomCode.Statements);
                }
                if (!nsManager.Container.Any())
                {
                    return;
                }
                if (nsManager.OnlyOneRootStatement)
                {
                    foreach (var cl in nsManager.Container[0].Items)
                    {
                        cl.Emit(emiter, writer, style);
                    }
                }
                else
                {
                    foreach (var ns in nsManager.Container)
                    {
                        EmitWithNamespace(ns.Name, emiter, writer, style, ns.Items);
                    }
                }
            }
        }
        // Public Methods 

        public override string GetPhpCode(PhpEmitStyle style)
        {
            var o = increment ? "++" : "--";
            return string.Format("{0}{1}{2}",
                pre ? o : "",
                operand.GetPhpCode(style),
                pre ? "" : o);
        }
Example #16
0
 public override string GetPhpCode(PhpEmitStyle style)
 {
     if (style == null || style.Compression == EmitStyleCompression.Beauty)
     {
         return(string.Format("{0} {1} {2}", Left.GetPhpCode(style), Operator, Right.GetPhpCode(style)));
     }
     return(string.Format("{0}{1}{2}", Left.GetPhpCode(style), Operator, Right.GetPhpCode(style)));
 }
Example #17
0
		// Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var s = style == null ? EmitStyleCompression.Beauty : style.Compression;
            if (s == EmitStyleCompression.NearCrypto)
                writer.Write("continue;");
            else
                writer.WriteLn("continue;");
        }
Example #18
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (_returnValue == null)
                writer.WriteLn("return;");
            else
                writer.WriteLnF("return {0};", _returnValue.GetPhpCode(style));

        }
Example #19
0
        // Public Methods 

        public string GetPhpCode(PhpEmitStyle s)
        {
            s = s ?? new PhpEmitStyle();
            var eq = s.Compression == EmitStyleCompression.Beauty ? " = " : "=";
            var d  = defaultValue != null ? (eq + defaultValue.GetPhpCode(s)) : "";

            return(string.Format("${0}{1}", name, d));
        }
        public override string GetPhpCode(PhpEmitStyle style)
        {
            var form = style == null || style.Compression == EmitStyleCompression.Beauty
                ? "{0} ? {1} : {2}"
                : "{0}?{1}:{2}";

            return(string.Format(form, _condition.GetPhpCode(style), _whenTrue.GetPhpCode(style), _whenFalse.GetPhpCode(style)));
        }
Example #21
0
        // Protected Methods 

        protected void EmitHeaderStatement(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style, string header, IPhpStatement statement)
        {
            style = style ?? new PhpEmitStyle();
            var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems);

            if (style.UseBracketsEvenIfNotNecessary)
            {
                iStyle.Brackets = ShowBracketsEnum.Always;
            }

            var statementToEmit = PhpCodeBlock.Reduce(statement);
            var emptyStatement  = !PhpCodeBlock.HasAny(statementToEmit);


            if (emptyStatement)
            {
                header += "{}";
            }
            if (style.Compression == EmitStyleCompression.NearCrypto)
            {
                writer.Write(header);
            }
            else
            {
                writer.WriteLn(header);
            }
            if (emptyStatement)
            {
                return;
            }


            var myBracket = style.UseBracketsEvenIfNotNecessary;

            if (!myBracket)
            {
                var gf = statementToEmit.GetStatementEmitInfo(iStyle);
                if (gf != StatementEmitInfo.NormalSingleStatement)
                {
                    myBracket = true;
                }
            }
            writer.IncIndent();
            if (myBracket)
            {
                iStyle.Brackets = ShowBracketsEnum.Never;
                writer.OpenLn("{");
            }


            statementToEmit.Emit(emiter, writer, iStyle);
            if (myBracket)
            {
                writer.CloseLn("}");
            }

            writer.DecIndent();
        }
Example #22
0
 public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     if (name.IsRoot)
         writer.OpenLn("namespace {");
     else
         writer.OpenLnF("namespace {0} {{", name);
     code.Emit(emiter, writer, style);
     writer.CloseLn("}");
 }
Example #23
0
        // Public Methods 

        public override string GetPhpCode(PhpEmitStyle style)
        {
            var o = Increment ? "++" : "--";

            return(string.Format("{0}{1}{2}",
                                 Pre ? o : "",
                                 Operand.GetPhpCode(style),
                                 Pre ? "" : o));
        }
Example #24
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var isBeauty = style == null || style.Compression == EmitStyleCompression.Beauty;

            var ifTrueAny = PhpCodeBlock.HasAny(_ifTrue);
            var ifFalseAny = PhpCodeBlock.HasAny(_ifFalse);
            if (!ifTrueAny && !ifFalseAny) return;

            writer.OpenLnF("if{1}({0}){2}",
                _condition.GetPhpCode(style),
                isBeauty ? " " : "",
                ifTrueAny ? "" : "{}");
            if (ifTrueAny)
            {
                var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems_OR_IfStatement);
                if (style != null && style.UseBracketsEvenIfNotNecessary)
                    iStyle.Brackets = ShowBracketsEnum.Always;
                var bound = PhpCodeBlock.Bound(_ifTrue);
                bound.Emit(emiter, writer, iStyle);
            }
            writer.DecIndent();
            if (!ifFalseAny) return;
            var oneLine = _ifFalse is PhpIfStatement;
            var oldIndent = writer.Intent;
            {
                if (oneLine)
                {
                    writer.Write("else ");
                    writer.SkipIndent = true;
                }
                else
                    writer.OpenLn("else");
                var myBracket = style != null && style.UseBracketsEvenIfNotNecessary;

                var iStyle = PhpEmitStyle.xClone(style,
                    myBracket
                        ? ShowBracketsEnum.Never
                        : ShowBracketsEnum.IfManyItems_OR_IfStatement);

                if (!myBracket)
                {
                    var gf = _ifFalse.GetStatementEmitInfo(iStyle);
                    if (gf != StatementEmitInfo.NormalSingleStatement)
                        myBracket = true;
                }
                if (myBracket)
                {
                    iStyle.Brackets = ShowBracketsEnum.Never;
                    writer.OpenLn("{");
                }                   
                _ifFalse.Emit(emiter, writer, iStyle);
                if (myBracket)
                    writer.CloseLn("}");
            }
            writer.Intent = oldIndent;
        }
Example #25
0
 public override string GetPhpCode(PhpEmitStyle style)
 {
     if (expression == (object)null)
         throw new Exception("Unable to get code from empty expression");
     var ex = PhpParenthesizedExpression.Strip(expression);
     var a = expression.GetPhpCode(style);
     if (byRef)
         a = "&" + a;
     return a;
 }
Example #26
0
        // Public Methods 

        //LangType ItemType, string VarName, IValue Collection, IStatement Statement
        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = style ?? new PhpEmitStyle();
            var arrayOperator = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";
            var header = OneVariable ? "foreach({0} as {3})" : "foreach({0} as {1}{2}{3})";
            header = string.Format(header,
                collection.GetPhpCode(style),
                keyVarname, arrayOperator, valueVarname);
            EmitHeaderStatement(emiter, writer, style, header, statement);
        }
 public override string GetPhpCode(PhpEmitStyle style)
 {
     if (translationInfo == null)
         throw new ArgumentNullException("translationInfo");
     if (translationInfo.IsStatic)
         throw new NotSupportedException();
     // jeśli nic nie podano to zakładam odczyt własności
     var a = MakeGetValueExpression();
     return a.GetPhpCode(style);
 }
Example #28
0
 private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpValue[] collection)
 {
     var list = new List<string>();
     var xStyle = PhpEmitStyle.xClone(style);
     xStyle.AsIncrementor = true;
     foreach (var item in collection)
     {
         list.Add(item.GetPhpCode(xStyle));
     }
     return string.Join(", ", list);
 }
Example #29
0
        private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpValue[] collection)
        {
            var list   = new List <string>();
            var xStyle = PhpEmitStyle.xClone(style);

            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                list.Add(item.GetPhpCode(xStyle));
            }
            return(string.Join(", ", list));
        }
 public override StatementEmitInfo GetStatementEmitInfo(PhpEmitStyle style)
 {
     if (!IsEcho)
         return StatementEmitInfo.NormalSingleStatement;
     var a = GetEchoItems(null);
     if (a.Length == 0)
         return StatementEmitInfo.Empty;
     if (a.Length != 1)
         return StatementEmitInfo.ManyItemsOrPlainHtml;
     return a[0].PlainHtml
         ? StatementEmitInfo.ManyItemsOrPlainHtml
         : StatementEmitInfo.NormalSingleStatement;
 }
 public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     if (name.IsRoot)
     {
         writer.OpenLn("namespace {");
     }
     else
     {
         writer.OpenLnF("namespace {0} {{", name);
     }
     code.Emit(emiter, writer, style);
     writer.CloseLn("}");
 }
        public override string GetPhpCode(PhpEmitStyle style)
        {
            if (initializers == null || initializers.Length == 0)
                return "array()";
            style = style ?? new PhpEmitStyle();
            var przecinek = style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
            var www = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";


            var list = new List<string>();
            foreach (var initializeValue in initializers)
            {
                if (initializeValue is PhpAssignExpression)
                {
                    var assignExpression = initializeValue as PhpAssignExpression;
                    if (!string.IsNullOrEmpty(assignExpression.OptionalOperator))
                        throw new NotSupportedException();

                    if (assignExpression.Left is PhpArrayAccessExpression)
                    {
                        var left = assignExpression.Left as PhpArrayAccessExpression;
                        if (left.PhpArray is PhpThisExpression)
                        {
                            var o = left.Index + www + assignExpression.Right;
                            list.Add(o);
                            continue;
                        }
                        else
                            throw new NotSupportedException();
                    }
                    else if (assignExpression.Left is PhpInstanceFieldAccessExpression)
                    {
                        var l1 = assignExpression.Left as PhpInstanceFieldAccessExpression;
                        var fn = new PhpConstValue(l1.FieldName);
                        var o = fn.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                    else
                    {
                        var o = assignExpression.Left.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                }
                else
                    list.Add(initializeValue.GetPhpCode(style));
            }
            return "array(" + string.Join(przecinek, list) + ")";
        }
Example #33
0
		// Private Methods 

        private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpStatement[] collection)
        {
            var list = new List<string>();
            var xStyle = PhpEmitStyle.xClone(style);
            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                var writer = new PhpSourceCodeWriter();
                writer.Clear();
                item.Emit(emiter, writer, xStyle);
                list.Add(writer.GetCode(true).Trim());
            }
            return string.Join(", ", list);
        }
Example #34
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            if (expression == (object)null)
            {
                throw new Exception("Unable to get code from empty expression");
            }
            var ex = PhpParenthesizedExpression.Strip(expression);
            var a  = expression.GetPhpCode(style);

            if (byRef)
            {
                a = "&" + a;
            }
            return(a);
        }
Example #35
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            if (translationInfo == null)
            {
                throw new ArgumentNullException("translationInfo");
            }
            if (translationInfo.IsStatic)
            {
                throw new NotSupportedException();
            }
            // jeśli nic nie podano to zakładam odczyt własności
            var a = MakeGetValueExpression();

            return(a.GetPhpCode(style));
        }
Example #36
0
        // Private Methods 

        private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpStatement[] collection)
        {
            var list   = new List <string>();
            var xStyle = PhpEmitStyle.xClone(style);

            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                var writer = new PhpSourceCodeWriter();
                writer.Clear();
                item.Emit(emiter, writer, xStyle);
                list.Add(writer.GetCode(true).Trim());
            }
            return(string.Join(", ", list));
        }
Example #37
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            writer.OpenLnF("switch ({0}) {{", expression.GetPhpCode(style));
            foreach (var sec in sections)
            {
                foreach (var l in sec.Labels)
                    writer.WriteLnF("{0}{1}:",
                        l.IsDefault ? "" : "case ",
                        l.IsDefault ? "default" : l.Value.GetPhpCode(style));
                writer.IncIndent();
                sec.Statement.Emit(emiter, writer, style);
                writer.DecIndent();
            }
            writer.CloseLn("}");
        }
Example #38
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (statements.Count == 0)
            {
                return;
            }
            var bracketStyle = style == null ? ShowBracketsEnum.IfManyItems : style.Brackets;
            var brack        =
                bracketStyle == ShowBracketsEnum.Never
                ? false
                : bracketStyle == ShowBracketsEnum.Always
                ? true
                : statements == null || !(statements.Count == 1);

            if (statements != null && statements.Count == 1 && bracketStyle == ShowBracketsEnum.IfManyItems_OR_IfStatement)
            {
                if (statements[0] is PhpIfStatement)
                {
                    brack = true;
                }
            }


            var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.Never);

            if (!brack && bracketStyle != ShowBracketsEnum.Never && statements.Count == 1)
            {
                var tmp = statements[0];
                var gf  = tmp.GetStatementEmitInfo(iStyle);
                if (gf != StatementEmitInfo.NormalSingleStatement)
                {
                    brack = true;
                }
            }
            if (brack)
            {
                writer.OpenLn("{");
            }
            foreach (var i in statements)
            {
                i.Emit(emiter, writer, iStyle);
            }
            if (brack)
            {
                writer.CloseLn("}");
            }
            return;
        }
Example #39
0
        public string GetPhpCode(PhpEmitStyle style)
        {
            /*
             echo preg_replace_callback('~-([a-z])~', function ($match) {
    return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
             */
            var s = PhpEmitStyle.xClone(style);
            s.AsIncrementor = true;
            var e = new PhpSourceCodeEmiter();
            var wde = new PhpSourceCodeWriter();
            wde.Clear();
            methodDefinition.Emit(e, wde, s);
            var code = wde.GetCode(true).Trim();
            return code;
        }
Example #40
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var saveStyleCurrentClass     = style.CurrentClass;
            var saveStyleCurrentNamespace = style.CurrentNamespace;

            try
            {
                if (IsEmpty)
                {
                    return;
                }
                if (style.CurrentNamespace == null)
                {
                    style.CurrentNamespace = PhpNamespace.Root;
                }
                if (style.CurrentNamespace != Name.Namespace)
                {
                    throw new Exception("Unable to emit class into different namespace");
                }
                var e = "";
                if (!_baseTypeName.IsEmpty)
                {
                    e = " extends " + _baseTypeName.NameForEmit(style);
                }
                writer.OpenLnF("class {0}{1} {{", Name.ShortName, e);
                style.CurrentClass = Name; // do not move this before "class XXX" is emited
                for (var orderGroup = 0; orderGroup < 3; orderGroup++)
                {
                    foreach (var field in Fields.Where(_ => FieldOrderGroup(_) == orderGroup))
                    {
                        field.Emit(emiter, writer, style);
                    }
                }
                foreach (var me in Methods)
                {
                    me.Emit(emiter, writer, style);
                }
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentClass     = saveStyleCurrentClass;
                style.CurrentNamespace = saveStyleCurrentNamespace;
            }
        }
        // Public Methods 
        

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (_isConst)
            {
                //  const CONSTANT = 'constant value';
                writer.WriteLnF("const {0} = {1};", Name, _constValue.GetPhpCode(style));
                return;
            }

            var a = string.Format("{0}{1} ${2}",
                _visibility.ToString().ToLower(),
                _isStatic ? " static" : "",
                Name
                );
            if (_constValue != null)
                a += " = " + _constValue.GetPhpCode(style);
            writer.WriteLn(a + ";");
        }
Example #42
0
        private IEnumerable <IPhpStatement> ConvertRequestedToCode()
        {
            var result         = new List <IPhpStatement>();
            var alreadyDefined = new List <string>();
            var style          = new PhpEmitStyle();

            foreach (var item in _requiredFiles.Distinct())
            {
                var code = item.GetPhpCode(style); //rozróżniam je po wygenerowanym kodzie
                if (alreadyDefined.Contains(code))
                {
                    continue;
                }
                alreadyDefined.Add(code);
                var req = new PhpMethodCallExpression("require_once", item);
                result.Add(new PhpExpressionStatement(req));
            }
            return(result.ToArray());
        }
Example #43
0
        public override StatementEmitInfo GetStatementEmitInfo(PhpEmitStyle style)
        {
            if (!IsEcho)
            {
                return(StatementEmitInfo.NormalSingleStatement);
            }
            var a = GetEchoItems(null);

            if (a.Length == 0)
            {
                return(StatementEmitInfo.Empty);
            }
            if (a.Length != 1)
            {
                return(StatementEmitInfo.ManyItemsOrPlainHtml);
            }
            return(a[0].PlainHtml
                ? StatementEmitInfo.ManyItemsOrPlainHtml
                : StatementEmitInfo.NormalSingleStatement);
        }
Example #44
0
        public string GetPhpCode(PhpEmitStyle style)
        {
            /*
             * echo preg_replace_callback('~-([a-z])~', function ($match) {
             * return strtoupper($match[1]);
             * }, 'hello-world');
             * // outputs helloWorld
             */
            var s = PhpEmitStyle.xClone(style);

            s.AsIncrementor = true;
            var e   = new PhpSourceCodeEmiter();
            var wde = new PhpSourceCodeWriter();

            wde.Clear();
            MethodDefinition.Emit(e, wde, s);
            var code = wde.GetCode(true).Trim();

            return(code);
        }
Example #45
0
        // Public Methods 

        public virtual void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter code, PhpEmitStyle style)
        {

            // public function addFunction($function, $namespace = '') 
            var accessModifiers = GetAccessModifiers();
            var param = Arguments == null ? "" : string.Join(", ", Arguments.Select(u => u.GetPhpCode(style)));
            code.OpenLnF("{0} function {1}({2}) {{", accessModifiers, Name, param);
            {
                var g = GetGlobals();
                if (!string.IsNullOrEmpty(g))
                    code.WriteLnF("global {0};", g);
            }
            foreach (var statement in Statements)
            {
                var g = PhpEmitStyle.xClone(style);
                g.Brackets = ShowBracketsEnum.Never;
                statement.Emit(emiter, code, g);
            }
            code.CloseLn("}");
        }
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = PhpEmitStyle.xClone(style);
            if (!style.AsIncrementor)
                if (_expression is PhpMethodCallExpression)
                {
                    var methodCallExpression = _expression as PhpMethodCallExpression;
                    if (methodCallExpression.CallType == MethodCallStyles.Procedural
                        && methodCallExpression.Name == "echo")
                        if (EmitInlineHtml(writer, style))
                            return;
                }
            var code = _expression.GetPhpCode(style);
            if (style.AsIncrementor)
            {
                writer.Write(code);
            }
            else
                writer.WriteLn(code + ";");

        }
        // Public Methods 


        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (IsConst)
            {
                //  const CONSTANT = 'constant value';
                writer.WriteLnF("const {0} = {1};", Name, ConstValue.GetPhpCode(style));
                return;
            }

            var a = string.Format("{0}{1} ${2}",
                                  Visibility.ToString().ToLower(),
                                  IsStatic ? " static" : "",
                                  Name
                                  );

            if (ConstValue != null)
            {
                a += " = " + ConstValue.GetPhpCode(style);
            }
            writer.WriteLn(a + ";");
        }
Example #48
0
        // Public Methods 

        public virtual void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter code, PhpEmitStyle style)
        {
            // public function addFunction($function, $namespace = '')
            var accessModifiers = GetAccessModifiers();
            var param           = Arguments == null ? "" : string.Join(", ", Arguments.Select(u => u.GetPhpCode(style)));

            code.OpenLnF("{0} function {1}({2}) {{", accessModifiers, Name, param);
            {
                var g = GetGlobals();
                if (!string.IsNullOrEmpty(g))
                {
                    code.WriteLnF("global {0};", g);
                }
            }
            foreach (var statement in Statements)
            {
                var g = PhpEmitStyle.xClone(style);
                g.Brackets = ShowBracketsEnum.Never;
                statement.Emit(emiter, code, g);
            }
            code.CloseLn("}");
        }
Example #49
0
        // Private Methods 

        private static void EmitWithNamespace(PhpNamespace ns, PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style, IEnumerable<IEmitable> classesInNamespace)
        {
            if (classesInNamespace == null)
                return;
            var inNamespace = classesInNamespace as IEmitable[] ?? classesInNamespace.ToArray();
            if (!inNamespace.Any())
                return;
            style.CurrentNamespace = ns;
            try
            {
                if (ns.IsRoot)
                    writer.OpenLn("namespace {");
                else
                    writer.OpenLnF("namespace {0} {{", ns.Name.Substring(1));
                foreach (var cl in inNamespace)
                    cl.Emit(emiter, writer, style);
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentNamespace = null;
            }
        }
Example #50
0
        // Private Methods 

        private static void EmitWithNamespace(PhpNamespace ns, PhpSourceCodeEmiter emiter,
                                              PhpSourceCodeWriter writer,
                                              PhpEmitStyle style, IEnumerable <IEmitable> classesInNamespace)
        {
            if (classesInNamespace == null)
            {
                return;
            }
            var inNamespace = classesInNamespace as IEmitable[] ?? classesInNamespace.ToArray();

            if (!inNamespace.Any())
            {
                return;
            }
            style.CurrentNamespace = ns;
            try
            {
                if (ns.IsRoot)
                {
                    writer.OpenLn("namespace {");
                }
                else
                {
                    writer.OpenLnF("namespace {0} {{", ns.Name.Substring(1));
                }
                foreach (var cl in inNamespace)
                {
                    cl.Emit(emiter, writer, style);
                }
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentNamespace = null;
            }
        }
Example #51
0
 public abstract string GetPhpCode(PhpEmitStyle style);
Example #52
0
 public abstract string GetPhpCode(PhpEmitStyle style);
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return string.Format("{0}{1}", _operator, _operand.GetPhpCode(style));
 }
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return(string.Format("({0})", expression.GetPhpCode(style)));
 }
 public StatementEmitInfo GetStatementEmitInfo(PhpEmitStyle style)
 {
     return(StatementEmitInfo.NormalSingleStatement);
 }
Example #56
0
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return(expression);
 }
Example #57
0
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return _variableName;
 }
Example #58
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = style ?? new PhpEmitStyle();
            var header = string.Format("while({0})", _condition.GetPhpCode(style));
            EmitHeaderStatement(emiter, writer, style, header, _statement);
        }
Example #59
0
 public override string GetPhpCode(PhpEmitStyle style)
 {
     return expression;
 }
 public override string GetPhpCode(PhpEmitStyle style)
 {
     var join = style == null || style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
     var xstyle = PhpEmitStyle.xClone(style);
     var arguments = string.Join(join, _arguments.Select(i => i.GetPhpCode(xstyle)));
     if (IsConstructorCall)
     {
         var a = string.Format("new {0}({1})", _className.NameForEmit(style), arguments);
         return a;
     }
     var name = _name;
     if (!_className.IsEmpty)
         name = _className.NameForEmit(style) + "::" + name;
     else if (_targetObject != null)
     {
         var to = _targetObject;
         if (_targetObject is PhpMethodCallExpression && (_targetObject as PhpMethodCallExpression).IsConstructorCall)
             to = new PhpParenthesizedExpression(to);
         name = to.GetPhpCode(style) + "->" + name;
     }
     var code = string.Format(name == "echo" ? "{0} {1}" : "{0}({1})", name, arguments);
     return code;
 }