Esempio n. 1
0
        public bool IsIdentical(IEntityInstance other)
        {
            if (Object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.GetType() != other.GetType())
            {
                return(false);
            }

            EntityInstanceSet entity_set = other.Cast <EntityInstanceSet>();

            return(this.elements.SetEquals(entity_set.elements));
        }
Esempio n. 2
0
        private static async Task <Data> buildInternalDataAsync(ExecutionContext ctx, bool isNative, object value,
                                                                IEntityInstance typeInstance, bool isStatic)
        {
            EntityInstance runtime_instance = typeInstance.Cast <EntityInstance>();

            ObjectData primary_parent = null;

            if (!isStatic)
            {
                await ctx.TypeRegistry.RegisterAddAsync(ctx, runtime_instance).ConfigureAwait(false);
            }
            else
            {
                EntityInstance primary = runtime_instance.TargetType.Inheritance.GetTypeImplementationParent();
                if (primary != null)
                {
                    primary_parent = await ctx.TypeRegistry.RegisterGetAsync(ctx, primary).ConfigureAwait(false);
                }
            }

            var fields      = new Dictionary <VariableDeclaration, ObjectData>(ReferenceEqualityComparer <VariableDeclaration> .Instance);
            var translators = new List <EntityInstance>();

            IEnumerable <EntityInstance> source_types = new[] { runtime_instance };

            if (!isStatic)
            {
                source_types = source_types.Concat(runtime_instance.PrimaryAncestors(ctx.CreateBareComputation()));
            }

            foreach (EntityInstance type_instance in source_types)
            {
                translators.Add(type_instance);

                foreach (VariableDeclaration field in type_instance.TargetType.AllNestedFields
                         .Where(it => it.Modifier.HasStatic == isStatic))
                {
                    EntityInstance field_type = field.Evaluation.Components.Cast <EntityInstance>();
                    field_type = field_type.TranslateThrough((translators as IEnumerable <EntityInstance>).Reverse());
                    fields.Add(field, await ObjectData.CreateEmptyAsync(ctx, field_type).ConfigureAwait(false));
                }
            }

            Data data1 = new Data(isNative, value, runtime_instance, primary_parent, fields);

            return(data1);
        }
Esempio n. 3
0
        public void Evaluate(ComputationContext ctx)
        {
            if (this.Evaluation == null)
            {
                this.readMode = new Option <ExpressionReadMode>(this.Body.ReadMode);

                if (!this.branches.Any(it => it.IsElse))
                {
                    this.readMode = new Option <ExpressionReadMode>(ExpressionReadMode.CannotBeRead);
                }
                else if (branches.Any(it => it.ReadMode == ExpressionReadMode.ReadRequired))
                {
                    this.readMode = new Option <ExpressionReadMode>(ExpressionReadMode.ReadRequired);
                }
                else if (!this.IsElse)
                {
                    this.readMode = new Option <ExpressionReadMode>(ExpressionReadMode.OptionalUse);
                }


                if (ReadMode == ExpressionReadMode.CannotBeRead)
                {
                    this.Evaluation = ctx.Env.UnitEvaluation;
                }
                else
                {
                    IEntityInstance eval      = this.Body.Evaluation.Components;
                    IEntityInstance aggregate = this.Body.Evaluation.Aggregate;

                    if (Next != null
                        // it is legal to have some branch "unreadable", for example:
                        // if true then 5 else throw new Exception();
                        && Next.ReadMode != ExpressionReadMode.CannotBeRead &&
                        !computeLowestCommonAncestor(ctx, ref eval, ref aggregate))
                    {
                        eval      = ctx.Env.UnitType.InstanceOf;
                        aggregate = ctx.Env.UnitType.InstanceOf;
                        readMode  = new Option <ExpressionReadMode>(ExpressionReadMode.CannotBeRead);
                    }


                    this.Evaluation = new EvaluationInfo(eval, aggregate.Cast <EntityInstance>()).PromotLifetime(ctx, this);

                    this.DataTransfer(ctx, ref this.condition, ctx.Env.BoolType.InstanceOf, ignoreMutability: true);
                }
            }
        }
Esempio n. 4
0
        internal static Task <ObjectData> CreateTypeAsync(ExecutionContext ctx, IEntityInstance typeInstance)
        {
            TypeDefinition type_def = typeInstance.Cast <EntityInstance>().TargetType;

            return(constructorAsync(ctx, type_def.Modifier.HasNative, null, typeInstance, isStatic: true));
        }
Esempio n. 5
0
        private async Task <ExecValue> executeNativeFileFunctionAsync(ExecutionContext ctx, FunctionDefinition func)
        {
            if (func == ctx.Env.FileReadLines)
            {
                ObjectData filepath_obj = ctx.GetArgument(func, NameFactory.FileFilePathParameter);
                if (!filepath_obj.TryDereferenceAnyOnce(ctx.Env, out ObjectData filepath_val))
                {
                    throw new Exception($"{ExceptionCode.SourceInfo()}");
                }
                string filepath = filepath_val.PlainValue.Cast <string>();

                string[] lines = null;
                try
                {
                    // todo: change it to ReadLines once we have deferred execution
                    lines = System.IO.File.ReadAllLines(filepath);
                }
#pragma warning disable 0168
                catch (Exception ex) // we would use it when debugging
#pragma warning restore 0168
                {
                }

                Option <ObjectData> opt_lines_obj;
                ObjectData          chunk_ptr;

                if (lines == null)
                {
                    chunk_ptr     = null;
                    opt_lines_obj = new Option <ObjectData>();
                }
                else
                {
                    IEntityInstance string_ptr_instance;
                    {
                        IEntityInstance ptr_iterable_instance     = func.ResultTypeName.Evaluation.Components.Cast <EntityInstance>().TemplateArguments.Single();
                        IEntityInstance iterable_str_ptr_instance = ptr_iterable_instance.Cast <EntityInstance>().TemplateArguments.Single();
                        string_ptr_instance = iterable_str_ptr_instance.Cast <EntityInstance>().TemplateArguments.Single();
                    }

                    var lines_obj = new ObjectData[lines.Length];
                    int i         = 0;
                    foreach (string s in lines)
                    {
                        ObjectData s_ptr = await createStringAsync(ctx, s).ConfigureAwait(false);

                        if (!ctx.Heap.TryInc(ctx, s_ptr, RefCountIncReason.FileLine, $"{filepath}"))
                        {
                            throw new Exception($"{ExceptionCode.SourceInfo()}");
                        }

                        lines_obj[i] = s_ptr;
                        ++i;
                    }

                    chunk_ptr = await createChunkOnHeap(ctx, string_ptr_instance, lines_obj).ConfigureAwait(false);

                    opt_lines_obj = new Option <ObjectData>(chunk_ptr);
                }


                ExecValue opt_exec = await createOption(ctx, func.ResultTypeName.Evaluation.Components, opt_lines_obj).ConfigureAwait(false);

                if (chunk_ptr != null)
                {
                    ctx.Heap.TryRelease(ctx, chunk_ptr, null, false, RefCountDecReason.DroppingLocalPointer, "");
                }

                if (opt_exec.IsThrow)
                {
                    return(opt_exec);
                }
                ObjectData result = opt_exec.ExprValue;
                return(ExecValue.CreateReturn(result));
            }
            else if (func == ctx.Env.FileExists)
            {
                ObjectData filepath_obj = ctx.GetArgument(func, NameFactory.FileFilePathParameter);
                if (!filepath_obj.TryDereferenceAnyOnce(ctx.Env, out ObjectData filepath_val))
                {
                    throw new Exception($"{ExceptionCode.SourceInfo()}");
                }
                string filepath = filepath_val.PlainValue.Cast <string>();

                bool exists = System.IO.File.Exists(filepath);

                ExecValue result = ExecValue.CreateReturn(await ObjectData.CreateInstanceAsync(ctx, func.ResultTypeName.Evaluation.Components,
                                                                                               exists).ConfigureAwait(false));
                return(result);
            }
            else
            {
                throw new NotImplementedException($"{ExceptionCode.SourceInfo()}");
            }
        }