Beispiel #1
0
        protected override IPyStatement[] VisitLocalDeclarationStatement(LocalDeclarationStatement src)
        {
            var s = new List <IPyStatement>();

            foreach (var i in src.Declaration.Declarators)
            {
                // to jest przypadek z c# 'int x;', dla Py można to pominąć
                if (i.Value == null)
                {
                    continue;
                }
                if (i.Value is UnknownIdentifierValue)
                {
                    throw new NotImplementedException();
                }
                var l  = new PyVariableExpression(i.Name, PyVariableKind.Local);
                var r  = TransValue(i.Value);
                var tt = new PyAssignExpression(l, r);
                s.Add(new PyExpressionStatement(tt));

                //var r = new PyAssignVariable( PyVariableExpression.AddDollar(i.Name), false );
                //// r.Name = "$" + i.Name;
                //r.Value = TV(i.Value);
                //s.Add(r);
            }

            return(s.ToArray());
        }
Beispiel #2
0
 public static IPyValue FindIncrement(PyVariableExpression controlVariable, IPyStatement statement)
 {
     if (statement is PyExpressionStatement expressionStatement)
     {
         return(FindIncrement(controlVariable, expressionStatement.Expression));
     }
     return(null);
 }
Beispiel #3
0
 protected virtual T VisitPyVariableExpression(PyVariableExpression node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPyVariableExpression", this.GetType().FullName));
     }
     return(default(T));
 }
Beispiel #4
0
        protected override IPyValue VisitLocalVariableExpression(LocalVariableExpression src)
        {
            if (_state.Principles.CurrentMethod == null)
            {
                return(PyVariableExpression.MakeLocal(src.Name, false));
            }
            var isArgument = _state.Principles.CurrentMethod.GetParameters().Any(u => u.Name == src.Name);

            return(PyVariableExpression.MakeLocal(src.Name, isArgument));
        }
Beispiel #5
0
        public static IPyValue FindIncrement(PyVariableExpression controlVariable, IPyValue expression)
        {
            switch (expression)
            {
            case PyIncrementDecrementExpression incDecExpression:
                if (incDecExpression.Operand is PyVariableExpression opv)
                {
                    if (controlVariable.Equals(opv))
                    {
                        return(new PyConstValue(incDecExpression.Increment ? 1 : -1));
                    }
                }
                break;

            case PyAssignExpression assignExpression:
                if (!controlVariable.Equals(assignExpression.Left))
                {
                    return(null);
                }
                switch (assignExpression.OptionalOperator)
                {
                case "+": return(assignExpression.Right);

                case "-": return(Minus(assignExpression.Right));

                case "":
                {
                    if (assignExpression.Right is PyBinaryOperatorExpression b)
                    {
                        var isMinus = b.Operator == "-";
                        if (b.Operator == "+" || isMinus)
                        {
                            if (controlVariable.Equals(b.Left))
                            {
                                return(isMinus ? Minus(b.Right) : b.Right);
                            }
                            if (controlVariable.Equals(b.Right))
                            {
                                if (!isMinus)
                                {
                                    return(b.Left);
                                }
                            }
                        }
                    }
                }
                break;
                }

                break;
            }

            return(null);
        }
Beispiel #6
0
        public void T05_Should_Find_Increment()
        {
            var variable = new PyVariableExpression("x", PyVariableKind.Local);

            void TestExpr(IPyValue e, object expected)
            {
                var value = ForTranslator.FindIncrement(variable, e);

                Assert.NotNull(value);
                var result = value as PyConstValue;

                Assert.NotNull(result);
                Assert.Equal(result.Value, expected);
            }

            TestExpr(new PyIncrementDecrementExpression(variable, true, false), 1);
            TestExpr(new PyIncrementDecrementExpression(variable, false, false), -1);

            TestExpr(new PyAssignExpression(variable, new PyConstValue(3), "+"), 3);
            TestExpr(new PyAssignExpression(variable, new PyConstValue(3.7), "+"), 3.7);
            TestExpr(new PyAssignExpression(variable, new PyConstValue(3), "-"), -3);
            TestExpr(new PyAssignExpression(variable, new PyConstValue(3.7), "-"), -3.7);

            TestExpr(
                new PyAssignExpression(variable, new PyBinaryOperatorExpression("+", variable, new PyConstValue(3))),
                3);
            TestExpr(
                new PyAssignExpression(variable, new PyBinaryOperatorExpression("+", variable, new PyConstValue(3.7))),
                3.7);
            TestExpr(
                new PyAssignExpression(variable, new PyBinaryOperatorExpression("+", new PyConstValue(3), variable)),
                3);
            TestExpr(
                new PyAssignExpression(variable, new PyBinaryOperatorExpression("+", new PyConstValue(3.7), variable)),
                3.7);

            TestExpr(
                new PyAssignExpression(variable, new PyBinaryOperatorExpression("-", variable, new PyConstValue(3))),
                -3);
            TestExpr(
                new PyAssignExpression(variable, new PyBinaryOperatorExpression("-", variable, new PyConstValue(3.7))),
                -3.7);
        }
Beispiel #7
0
        protected override IPyStatement[] VisitVariableDeclaration(VariableDeclaration src)
        {
            //throw new Exception("DELETE THIS ??????");
            var s = new List <IPyStatement>();

            foreach (var i in src.Declarators)
            {
                var l  = new PyVariableExpression(i.Name, PyVariableKind.Local);
                var r  = TransValue(i.Value);
                var tt = new PyAssignExpression(l, r);
                s.Add(new PyExpressionStatement(tt));

                //var r = new PyAssignVariable(PyVariableExpression.AddDollar(i.Name), false);
                //r.Value = TV(i.Value);
                //s.Add(r);
            }

            return(s.ToArray());
        }
Beispiel #8
0
        private void TranslateField(PyCodeModule module, PyClassDefinition pyClass, FieldDeclaration field)
        {
            PyValueTranslator pyValueTranslator = null;

            foreach (var item in field.Items)
            {
                if (item.OptionalFieldInfo == null)
                {
                    continue;
                }
                var fti = Info.GetOrMakeTranslationInfo(item.OptionalFieldInfo);
                switch (fti.Destination)
                {
                case FieldTranslationDestionations.DefinedConst:
                    if (item.Value == null)
                    {
                        throw new NotSupportedException();
                    }
                    if (pyValueTranslator == null)
                    {
                        pyValueTranslator = new PyValueTranslator(_state);
                    }
                    var definedValue = pyValueTranslator.TransValue(item.Value);
                    {
                        if (fti.IncludeModule != module.ModuleName)
                        {
                            module = GetOrMakeModuleByName(fti.IncludeModule);
                        }
                    }
                    module.DefinedConsts.Add(new KeyValuePair <string, IPyValue>(fti.ScriptName, definedValue));
                    break;

                case FieldTranslationDestionations.GlobalVariable:
                    if (item.Value != null)
                    {
                        IPyValue value;
                        // muszę na chwilę wyłączyć current type, bo to jes poza klasą generowane
                        {
                            var saveCurrentType = _state.Principles.CurrentType;
                            _state.Principles.CurrentType = null;
                            try
                            {
                                if (pyValueTranslator == null)
                                {
                                    pyValueTranslator = new PyValueTranslator(_state);
                                }
                                value = pyValueTranslator.TransValue(item.Value);
                            }
                            finally
                            {
                                _state.Principles.CurrentType = saveCurrentType;
                            }
                        }

                        var assign = new PyAssignExpression(PyVariableExpression.MakeGlobal(fti.ScriptName),
                                                            value);
                        module.TopCode.Statements.Add(new PyExpressionStatement(assign));
                    }

                    break;

                case FieldTranslationDestionations.JustValue:
                    continue;     // don't define

                case FieldTranslationDestionations.NormalField:
                case FieldTranslationDestionations.ClassConst:
                {
                    var def = new PyClassFieldDefinition(fti.ScriptName, field.Type.DotnetType);
                    var cti = _state.Principles.GetTi(_state.Principles.CurrentType, true);
                    if (cti.IsArray)
                    {
                        continue;
                    }
                    if (field.Modifiers.Has("const") ^
                        (fti.Destination == FieldTranslationDestionations.ClassConst))
                    {
                        throw new Exception("beige lion");
                    }

                    def.IsConst =
                        fti.Destination ==
                        FieldTranslationDestionations.ClassConst;     // field.Modifiers.Has("const");

                    def.IsStatic = def.IsConst || field.Modifiers.Has("static");
                    if (field.Modifiers.Has("public"))
                    {
                        def.Visibility = Visibility.Public;
                    }
                    else if (field.Modifiers.Has("protected"))
                    {
                        def.Visibility = Visibility.Protected;
                    }
                    else
                    {
                        def.Visibility = Visibility.Private;
                    }

                    if (item.Value != null)
                    {
                        if (pyValueTranslator == null)
                        {
                            pyValueTranslator = new PyValueTranslator(_state);
                        }

                        var value = pyValueTranslator.TransValue(item.Value);

                        /*
                         * if (!(value is PyConstValue))
                         * {
                         *  // converts to value
                         *  value = ExpressionEvaluator.Evaluate(value);
                         *  // dificult to translate-move values to additional class
                         *  // var t = new RefactorByMovingToAnotherClass();
                         *  //value = t.ConvertAndRefactor(value);
                         * }
                         *
                         */
                        def.ConstValue = value;
                    }
                    pyClass.Fields.Add(def);
                    break;
                }

                default:
                    throw new NotSupportedException();
                }
            }
        }
Beispiel #9
0
        protected override IPyValue VisitClassFieldAccessExpression(ClassFieldAccessExpression src)
        {
            var tmp = _state.Principles.NodeTranslators.Translate(_state, src);

            if (tmp != null)
            {
                return(SimplifyPyExpression(tmp));
            }

            var isStatic            = src.IsStatic;
            var member              = src.Member;
            var memberName          = member.Name;
            var memberDeclaringType = member.DeclaringType;

            {
                var tInfo = _state.Principles.GetOrMakeTranslationInfo(src.Member);
                if (tInfo.IsDefinedInNonincludableModule)
                {
                    var b = _state.Principles.GetTi(_state.Principles.CurrentType, true);
                    if (tInfo.IncludeModule != b.ModuleName)
                    {
                        throw new Exception(
                                  string.Format(
                                      "Unable to reference to field {1}.{0} from {2}.{3}. Containing module is page and cannot be included.",
                                      memberName,
                                      memberDeclaringType == null
                                    ? "?"
                                    : (memberDeclaringType.FullName ?? memberDeclaringType.Name),
                                      _state.Principles.CurrentType.FullName,
                                      _state.Principles.CurrentMethod
                                      ));
                    }
                }

                var fieldDeclaringType = memberDeclaringType;
                if (fieldDeclaringType == null)
                {
                    throw new Exception("fieldDeclaringType");
                }
                _state.Principles.GetTi(fieldDeclaringType, false);
                {
                    if (fieldDeclaringType.IsEnum)
                    {
                        if (!isStatic)
                        {
                            throw new NotSupportedException();
                        }
                        var asDefinedConstAttribute = member.GetCustomAttribute <AsDefinedConstAttribute>();
                        if (asDefinedConstAttribute != null)
                        {
                            var definedExpression =
                                new PyDefinedConstExpression(asDefinedConstAttribute.DefinedConstName,
                                                             tInfo.IncludeModule);
                            return(SimplifyPyExpression(definedExpression));
                        }

                        var renderValueAttribute = member.GetCustomAttribute <RenderValueAttribute>();
                        if (renderValueAttribute != null)
                        {
                            if (PyValues.TryGetPyStringValue(renderValueAttribute.Name, out var strCandidate))
                            {
                                var valueExpression = new PyConstValue(strCandidate);
#if DEBUG
                                {
                                    var a1 = renderValueAttribute.Name.Trim();
                                    var a2 = valueExpression.ToString();
                                    if (a1 != a2)
                                    {
                                        throw new InvalidOperationException();
                                    }
                                }
#endif
                                return(SimplifyPyExpression(valueExpression));
                            }
                            else
                            {
                                var valueExpression = new PyFreeExpression(renderValueAttribute.Name);
                                return(SimplifyPyExpression(valueExpression));
                            }
                        }

                        {
                            // object v1 = ReadEnumValueAndProcessForPy(member);
                            var v1 = member.GetValue(null);
                            var g  = new PyConstValue(v1);
                            return(SimplifyPyExpression(g));
                        }
                        //throw new NotSupportedException();
                    }
                }

                var principles = _state.Principles;
                switch (tInfo.Destination)
                {
                case FieldTranslationDestionations.DefinedConst:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException("Unable to convert instance field into Py defined const");
                    }
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    var definedExpression = new PyDefinedConstExpression(tInfo.ScriptName, tInfo.IncludeModule);
                    return(SimplifyPyExpression(definedExpression));

                case FieldTranslationDestionations.GlobalVariable:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException(
                                  "Unable to convert instance field into Py global variable");
                    }
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    var globalVariable = PyVariableExpression.MakeGlobal(tInfo.ScriptName);
                    return(SimplifyPyExpression(globalVariable));

                case FieldTranslationDestionations.JustValue:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException("Unable to convert instance field into compile-time value");
                    }
                    var constValue   = member.GetValue(null);
                    var pyConstValue = new PyConstValue(constValue, tInfo.UsGlueForValue);
                    return(SimplifyPyExpression(pyConstValue));

                case FieldTranslationDestionations.NormalField:
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    var rr = new PyClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = tInfo.Destination == FieldTranslationDestionations.ClassConst
                    };
                    rr.SetClassName(
                        principles.GetPyType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType)
                        );
                    return(SimplifyPyExpression(rr));

                case FieldTranslationDestionations.ClassConst:
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    rr = new PyClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = true
                    };
                    rr.SetClassName(
                        principles.GetPyType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType));

                    return(SimplifyPyExpression(rr));

                default:
                    throw new NotSupportedException(string.Format(
                                                        "Unable to translate class field with destination option equal {0}", tInfo.Destination));
                }
            }
        }
Beispiel #10
0
        // Protected Methods 

        protected override IPyValue VisitArgumentExpression(ArgumentExpression src)
        {
            return(PyVariableExpression.MakeLocal(src.Name, true));
        }