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); }
private static async Task <ExecValue> executeNativeBoolFunctionAsync(ExecutionContext ctx, FunctionDefinition func, ObjectData this_value) { if (func.IsDefaultInitConstructor()) { this_value.Assign(await ObjectData.CreateInstanceAsync(ctx, this_value.RunTimeTypeInstance, false).ConfigureAwait(false)); return(ExecValue.CreateReturn(null)); } else if (func.IsCopyInitConstructor(ctx.CreateBareComputation())) { this_value.Assign(ctx.FunctionArguments.Single()); return(ExecValue.CreateReturn(null)); } else if (func.Name.Name == NameFactory.NotOperator) { return(ExecValue.CreateReturn(await ObjectData.CreateInstanceAsync(ctx, this_value.RunTimeTypeInstance, !this_value.PlainValue.Cast <bool>()).ConfigureAwait(false))); } else { throw new NotImplementedException($"{ExceptionCode.SourceInfo()}"); } }
private async Task <ExecValue> executeNativeNat8FunctionAsync(ExecutionContext ctx, FunctionDefinition func, ObjectData thisValue) { if (func == ctx.Env.Nat8ParseStringFunction) { ObjectData arg_ptr = ctx.FunctionArguments.Single(); ObjectData arg_val = arg_ptr.DereferencedOnce(); string input_str = arg_val.NativeString; Option <ObjectData> int_obj; if (byte.TryParse(input_str, out byte int_val)) { int_obj = new Option <ObjectData>(await ObjectData.CreateInstanceAsync(ctx, ctx.Env.Nat8Type.InstanceOf, int_val) .ConfigureAwait(false)); } else { int_obj = new Option <ObjectData>(); } ExecValue opt_exec = await createOption(ctx, func.ResultTypeName.Evaluation.Components, int_obj).ConfigureAwait(false); if (opt_exec.IsThrow) { return(opt_exec); } ObjectData result = opt_exec.ExprValue; return(ExecValue.CreateReturn(result)); } else if (func.Name.Name == NameFactory.AddOperator) { var this_int = thisValue.NativeNat8; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeNat8; byte value = (byte)checked (this_int + arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.AddOverflowOperator) { var this_int = thisValue.NativeNat8; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeNat8; byte value = (byte)(this_int + arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.MulOperator) { var this_int = thisValue.NativeNat8; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeNat8; byte value = (byte)checked (this_int * arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.SubOperator) { ObjectData arg = ctx.FunctionArguments.Single(); var this_int = thisValue.NativeNat8; var arg_int = arg.NativeNat8; byte value = (byte)checked (this_int - arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.IsDefaultInitConstructor()) { thisValue.Assign(await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, (byte)0).ConfigureAwait(false)); return(ExecValue.CreateReturn(null)); } else if (func.IsCopyInitConstructor(ctx.CreateBareComputation())) { thisValue.Assign(ctx.FunctionArguments.Single()); return(ExecValue.CreateReturn(null)); } else if (func.Name.Name == NameFactory.ComparableCompare) { ObjectData arg = ctx.FunctionArguments.Single(); var this_int = thisValue.NativeNat8; var arg_int = arg.NativeNat8; ObjectData ordering_type = await ctx.TypeRegistry.RegisterGetAsync(ctx, ctx.Env.OrderingType.InstanceOf).ConfigureAwait(false); ObjectData ordering_value; if (this_int < arg_int) { ordering_value = ordering_type.GetField(ctx.Env.OrderingLess); } else if (this_int > arg_int) { ordering_value = ordering_type.GetField(ctx.Env.OrderingGreater); } else { ordering_value = ordering_type.GetField(ctx.Env.OrderingEqual); } ExecValue result = ExecValue.CreateReturn(ordering_value); return(result); } else { ExecValue?result = await numComparisonAsync <byte>(ctx, func, thisValue).ConfigureAwait(false); if (result.HasValue) { return(result.Value); } else { throw new NotImplementedException($"Function {func} is not implemented"); } } }
private async Task <ExecValue> executeNativeReal64FunctionAsync(ExecutionContext ctx, FunctionDefinition func, ObjectData thisValue) { if (func == ctx.Env.Real64ParseStringFunction) { ObjectData arg_ptr = ctx.FunctionArguments.Single(); ObjectData arg_val = arg_ptr.DereferencedOnce(); string input_str = arg_val.NativeString; Option <ObjectData> int_obj; if (Double.TryParse(input_str, out Double int_val)) { int_obj = new Option <ObjectData>(await ObjectData.CreateInstanceAsync(ctx, ctx.Env.Real64Type.InstanceOf, int_val) .ConfigureAwait(false)); } else { int_obj = new Option <ObjectData>(); } ExecValue opt_exec = await createOption(ctx, func.ResultTypeName.Evaluation.Components, int_obj).ConfigureAwait(false); if (opt_exec.IsThrow) { return(opt_exec); } ObjectData result = opt_exec.ExprValue; return(ExecValue.CreateReturn(result)); } else if (func.Name.Name == NameFactory.AddOperator) { var this_int = thisValue.NativeReal64; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeReal64; double value = checked (this_int + arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.AddOverflowOperator) { var this_int = thisValue.NativeReal64; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeReal64; double value1 = this_int + arg_int; ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value1) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.MulOperator) { var this_int = thisValue.NativeReal64; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeReal64; double value2 = checked (this_int * arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value2) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.SubOperator) { ObjectData arg = ctx.FunctionArguments.Single(); var this_int = thisValue.NativeReal64; var arg_int = arg.NativeReal64; double value3 = checked (this_int - arg_int); ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value3) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } else if (func.Name.Name == NameFactory.DivideOperator) { ObjectData arg = ctx.FunctionArguments.Single(); var this_int = thisValue.NativeReal64; var arg_int = arg.NativeReal64; double value = checked (this_int / arg_int); if (!ctx.Env.Options.AllowRealMagic && (double.IsNaN(value) || double.IsInfinity(value))) { ExecValue exec_cons = await createObject(ctx, ctx.Env.ExceptionType.Modifier.HasHeapOnly, ctx.Env.ExceptionType.InstanceOf, ctx.Env.ExceptionType.DefaultConstructor(), null).ConfigureAwait(false); if (exec_cons.IsThrow) { return(exec_cons); } if (ctx.Env.ExceptionType.Modifier.HasHeapOnly) { ctx.Heap.TryInc(ctx, exec_cons.ExprValue, RefCountIncReason.ThrowingException, ""); } return(ExecValue.CreateThrow(exec_cons.ExprValue)); } ObjectData res_value = await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, value) .ConfigureAwait(false); ExecValue result = ExecValue.CreateReturn(res_value); return(result); } // keep it for NaNs (as long as they part of the language) else if (func.Name.Name == NameFactory.EqualOperator) { var this_int = thisValue.NativeReal64; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeReal64; ExecValue result = ExecValue.CreateReturn(await ObjectData.CreateInstanceAsync(ctx, func.ResultTypeName.Evaluation.Components, this_int == arg_int).ConfigureAwait(false)); return(result); } else if (func.Name.Name == NameFactory.NotEqualOperator) { var this_int = thisValue.NativeReal64; ObjectData arg = ctx.FunctionArguments.Single(); var arg_int = arg.NativeReal64; ExecValue result = ExecValue.CreateReturn(await ObjectData.CreateInstanceAsync(ctx, func.ResultTypeName.Evaluation.Components, this_int != arg_int).ConfigureAwait(false)); return(result); } else if (func.IsDefaultInitConstructor()) { thisValue.Assign(await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, (double)0).ConfigureAwait(false)); return(ExecValue.CreateReturn(null)); } else if (func.IsCopyInitConstructor(ctx.CreateBareComputation())) { thisValue.Assign(ctx.FunctionArguments.Single()); return(ExecValue.CreateReturn(null)); } else if (func == ctx.Env.Real64FromNat8Constructor) { ObjectData arg_obj = ctx.FunctionArguments.Single(); var arg_val = arg_obj.NativeNat8; thisValue.Assign(await ObjectData.CreateInstanceAsync(ctx, thisValue.RunTimeTypeInstance, (Double)arg_val).ConfigureAwait(false)); return(ExecValue.CreateReturn(null)); } else { ExecValue?result = await numComparisonAsync <double>(ctx, func, thisValue).ConfigureAwait(false); if (result.HasValue) { return(result.Value); } else { throw new NotImplementedException($"Function {func} is not implemented"); } } }