Пример #1
0
        IPyValue IExternalTranslationContext.TranslateValue(IValue srcValue)
        {
            //var aa = new ExpressionSimplifier(new OptimizeOptions());
            //srcValue = aa.Simplify(srcValue);

            var t = new PyValueTranslator(this);
            var g = t.TransValue(srcValue);

            return(g);
        }
Пример #2
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();
                }
            }
        }