public IInterpreter DateDayOfWeek() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true }.SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.NatNameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("d", null, ExpressionFactory.StackConstructor(NameFactory.DateNameReference(), // it is Friday Int16Literal.Create("2017"), Nat8Literal.Create("12"), Nat8Literal.Create("29"))), VariableDeclaration.CreateStatement("i", null, FunctionCall.ConvCall(NameReference.Create("d", NameFactory.DateDayOfWeekProperty), NameFactory.NatNameReference())), Return.Create(NameReference.Create("i")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(5UL, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter InheritingEnums() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true, DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.CreateEnum("Weekend") .With(EnumCaseBuilder.Create("Sat", "Sun")) .SetModifier(EntityModifier.Base)); root_ns.AddBuilder(TypeBuilder.CreateEnum("First") .With(EnumCaseBuilder.Create("Mon")) .Parents("Weekend")); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.NatNameReference(), Block.CreateStatement(new IExpression[] { // let a Weekend = First.Sat VariableDeclaration.CreateStatement("a", NameReference.Create("Weekend"), // please note we only refer to "Sat" through "First", the type is still "Weekend" NameReference.Create("First", "Sat")), // var b First = Weekend.Sun VariableDeclaration.CreateStatement("b", NameReference.Create("First"), NameReference.Create("Weekend", "Sun"), env.Options.ReassignableModifier()), // b = First.Mon Assignment.CreateStatement(NameReference.Create("b"), NameReference.Create("First", "Mon")), // let x = a to Nat; // 0 VariableDeclaration.CreateStatement("x", null, FunctionCall.ConvCall(NameReference.Create("a"), NameFactory.NatNameReference())), // let y = b to Nat; // 2 VariableDeclaration.CreateStatement("y", null, FunctionCall.ConvCall(NameReference.Create("b"), NameFactory.NatNameReference())), // return x + y Return.Create(ExpressionFactory.Add(NameReference.Create("x"), NameReference.Create("y"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2UL, result.RetValue.PlainValue); } return(interpreter); }
public static bool DataTransfer(this IEvaluable @this, ComputationContext ctx, ref IExpression source, IEntityInstance targetTypeName, bool ignoreMutability = false) { if (source == null) { return(true); } IEntityInstance src_type = source.Evaluation.Components; TypeMatch match = src_type.MatchesTarget(ctx, targetTypeName, TypeMatching.Create(ctx.Env.Options.InterfaceDuckTyping, allowSlicing: false) .WithIgnoredMutability(ignoreMutability) .AllowedLifetimeChecking(true)); if (match.HasFlag(TypeMatch.Attachment)) { match ^= TypeMatch.Attachment; } if (match == TypeMatch.No) { ctx.ErrorManager.AddError(ErrorCode.TypeMismatch, source); return(false); } else if (match == TypeMatch.Lifetime) { ctx.ErrorManager.AddError(ErrorCode.EscapingReference, source); return(false); } else if (match == TypeMatch.InConversion) { source.DetachFrom(@this); source = ExpressionFactory.StackConstructor((targetTypeName as EntityInstance).NameOf, FunctionArgument.Create(source)); source.AttachTo(@this); TypeMatch m = source.Evaluated(ctx, EvaluationCall.AdHocCrossJump).MatchesTarget(ctx, targetTypeName, TypeMatching.Create(ctx.Env.Options.InterfaceDuckTyping, allowSlicing: false)); if (m != TypeMatch.Same && m != TypeMatch.Substitute) { throw new Exception("Internal error"); } } else if (match.HasFlag(TypeMatch.ImplicitReference)) { match ^= TypeMatch.ImplicitReference; if (match != TypeMatch.Substitute && match != TypeMatch.Same) { throw new NotImplementedException(); } source.DetachFrom(@this); source = AddressOf.CreateReference(source); source.AttachTo(@this); IEntityInstance source_eval = source.Evaluated(ctx, EvaluationCall.AdHocCrossJump); TypeMatch m = source_eval.MatchesTarget(ctx, targetTypeName, TypeMatching.Create(ctx.Env.Options.InterfaceDuckTyping, allowSlicing: true)); if (m != TypeMatch.Same && m != TypeMatch.Substitute) { throw new Exception($"Internal error: matching result {m}"); } } else if (match == TypeMatch.OutConversion) { source.DetachFrom(@this); source = FunctionCall.ConvCall(source, (targetTypeName as EntityInstance).NameOf); source.AttachTo(@this); TypeMatch m = source.Evaluated(ctx, EvaluationCall.AdHocCrossJump).MatchesTarget(ctx, targetTypeName, TypeMatching.Create(ctx.Env.Options.InterfaceDuckTyping, allowSlicing: false)); if (m != TypeMatch.Same && m != TypeMatch.Substitute) { throw new Exception("Internal error"); } } else if (match.HasFlag(TypeMatch.AutoDereference)) { source.DereferencedCount_LEGACY = match.Dereferences; @this.Cast <IExpression>().DereferencingCount = match.Dereferences; match ^= TypeMatch.AutoDereference; if (match != TypeMatch.Substitute && match != TypeMatch.Same) { throw new NotImplementedException(); } } else if (match != TypeMatch.Same && match != TypeMatch.Substitute) { throw new NotImplementedException(); } return(true); }