public IErrorReporter ErrorUnusedExpression() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; var system_ns = env.SystemNamespace; var var_decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(), Undef.Create()); var var_ref = NameReference.Create("x"); FunctionDefinition func_def = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.BoolNameReference(), Block.CreateStatement(new IExpression[] { var_decl, var_ref, Return.Create(BoolLiteral.CreateTrue()) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.AreEqual(ErrorCode.ExpressionValueNotUsed, resolver.ErrorManager.Errors.Single().Code); Assert.AreEqual(var_ref, resolver.ErrorManager.Errors.Single().Node); } return(resolver); }
public IErrorReporter ReadingIfAsExpression() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { GlobalVariables = true, RelaxedMode = true }.SetMutability(mutability)); var root_ns = env.Root; var if_ctrl = IfBranch.CreateIf(BoolLiteral.CreateTrue(), new IExpression[] { Int64Literal.Create("5") }, IfBranch.CreateElse(new[] { Int64Literal.Create("7") })); root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(), if_ctrl, EntityModifier.Public)); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter ErrorCannotInferResultType() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; IExpression lambda = FunctionBuilder.CreateLambda(null, Block.CreateStatement(new IExpression[] { IfBranch.CreateIf(BoolLiteral.CreateFalse(), new[] { Return.Create(BoolLiteral.CreateTrue()) }), Return.Create(Int64Literal.Create("2")) })).Build(); root_ns.AddBuilder(FunctionBuilder.Create("me", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { // f = () => x VariableDeclaration.CreateStatement("f", null, lambda), ExpressionFactory.Readout("f") }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotInferResultType, lambda)); } return(resolver); }
public IErrorReporter ErrorReadingFunctionVoidResult() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { GlobalVariables = true, RelaxedMode = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.CannotBeRead, NameFactory.BoolNameReference(), Block.CreateStatement(new[] { Return.Create(BoolLiteral.CreateTrue()) })) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), usageMode: ExpressionReadMode.CannotBeRead))); root_ns.AddNode(VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Undef.Create())); var call = FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(NameReference.Create("i"))); VariableDeclaration decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(), call, EntityModifier.Public); root_ns.AddNode(decl); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotReadExpression, call)); } return(resolver); }
public IErrorReporter ErrorIfScope() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true, }.SetMutability(mutability)); var root_ns = env.Root; NameReference bad_ref = NameReference.Create("x"); root_ns.AddBuilder(FunctionBuilder.Create( "testing", NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("b", NameFactory.BoolNameReference(), Undef.Create(), env.Options.ReassignableModifier()), IfBranch.CreateIf(VariableDeclaration.CreateExpression("x", null, BoolLiteral.CreateTrue()), // x is in scope Assignment.CreateStatement("b", "x"), IfBranch.CreateElse( // x is in scope as well Assignment.CreateStatement("b", "x"))), // here x is not in the scope (is already removed) Assignment.CreateStatement(NameReference.Create("b"), bad_ref), ExpressionFactory.Readout("b") ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceNotFound, bad_ref)); } return(resolver); }
public IErrorReporter TranslationTableOfInferredCommonTypes() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; TemplateParameter template_param = TemplateParametersBuffer.Create("T").Values.Single(); root_ns.AddBuilder(FunctionBuilder.Create("getMe", new[] { template_param }, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement()) .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead), FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead))); FunctionCall call = FunctionCall.Create(NameReference.Create("getMe"), NameReference.Create("x"), NameReference.Create("y")); root_ns.AddBuilder(FunctionBuilder.Create("common", NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("x", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Int64Literal.Create("3")), VariableDeclaration.CreateStatement("y", NameFactory.ReferenceNameReference(NameFactory.BoolNameReference()), BoolLiteral.CreateTrue()), call ))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); // the actual point of this test are those two lines checking if we get correct translation table for entire // instance of the called function Assert.IsTrue(call.Resolution.TargetFunctionInstance.Translation.Translate(template_param, out IEntityInstance common_instance)); Assert.AreEqual(resolver.Context.Env.IEquatableType.InstanceOf, common_instance); } return(resolver); }
public static TypeBuilder WithEquatableEquals(this TypeBuilder builder, EntityModifier modifier = null) { return(builder.With(FunctionBuilder.Create(NameFactory.EqualOperator, ExpressionReadMode.ReadRequired, NameFactory.BoolNameReference(), Block.CreateStatement( IfBranch.CreateIf(IsSame.Create(NameReference.CreateThised(), NameReference.Create("cmp")), new[] { Return.Create(BoolLiteral.CreateTrue()) }), // let obj = cmp cast? Self VariableDeclaration.CreateStatement("obj", null, ExpressionFactory.CheckedSelfCast("cmp", NameFactory.ReferenceNameReference(builder.CreateTypeNameReference(TypeMutability.ReadOnly)))), // return this==obj.value Return.Create(ExpressionFactory.IsEqual(NameReference.Create(NameFactory.ThisVariableName), NameReference.Create("obj"))))) .SetModifier(EntityModifier.Override | modifier) .Parameters(FunctionParameter.Create("cmp", NameFactory.ReferenceNameReference(NameFactory.IEquatableNameReference(TypeMutability.ReadOnly)))))); }
public IInterpreter ShortcutComputationInOptionalDeclaration() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { // purpose: check if RHS of the optional declaration is computed only when it is needed // here we count RHS computations and since we declare two variables // let (x,y) =? (None,Some) // Some should not be executed, because `x` assigment fails first var env = Language.Environment.Create(new Options() { DebugThrowOnError = true, DiscardingAnyExpressionDuringTests = true, }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("Mutator") .SetModifier(EntityModifier.Mutable) .With(VariableDeclaration.CreateStatement("c", NameFactory.IntNameReference(), null, env.Options.ReassignableModifier() | EntityModifier.Public))); // return Some or None depending on the `f` parameter, and also increments the count of option evaluations root_ns.AddBuilder(FunctionBuilder.Create("give", NameFactory.OptionNameReference(NameFactory.Nat8NameReference()), Block.CreateStatement( ExpressionFactory.Inc(() => NameReference.Create("m", "c")), Return.Create(ExpressionFactory.Ternary(NameReference.Create("f"), ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("11")), ExpressionFactory.OptionEmpty(NameFactory.Nat8NameReference()))) )) .Parameters(FunctionParameter.Create("f", NameFactory.BoolNameReference()), FunctionParameter.Create("m", NameFactory.ReferenceNameReference(NameReference.Create("Mutator"))))); IExpression opt_declaration = ExpressionFactory.OptionalDeclaration(new[] { VariablePrototype.Create("x", NameFactory.Nat8NameReference()), VariablePrototype.Create("y", NameFactory.Nat8NameReference()) }, new[] { FunctionCall.Create("give", BoolLiteral.CreateFalse(), NameReference.Create("mut")), FunctionCall.Create("give", BoolLiteral.CreateTrue(), NameReference.Create("mut")), }); root_ns.AddBuilder(FunctionBuilder.Create("main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("mut", null, ExpressionFactory.StackConstructor(NameReference.Create("Mutator"))), IfBranch.CreateIf(opt_declaration, new[] { ExpressionFactory.Readout("x"), ExpressionFactory.Readout("y"), ExpressionFactory.GenericThrow(), }, IfBranch.CreateElse( // crucial check -- we should not evaluate the second option ExpressionFactory.AssertEqual(IntLiteral.Create("1"), NameReference.Create("mut", "c")) )), Return.Create(Nat8Literal.Create("0")) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)0, result.RetValue.PlainValue); } return(interpreter); }
public static IExpression OptionalAssignment(IEnumerable <IExpression> lhsExpressions, IEnumerable <IExpression> rhsExpressions) { // todo: add support for spread if (lhsExpressions.Count() != rhsExpressions.Count()) { throw new NotImplementedException(); } // please note we could have dummy assignments in form // _ ?= x // in such case we are not interested in the assigment but the fact it was sucessful or not lhsExpressions = lhsExpressions.Select(lhs => lhs is NameReference lhs_name && lhs_name.IsSink ? null : lhs); var temp_names = new List <string>(); IExpression condition = null; foreach (Tuple <IExpression, IExpression> pair in rhsExpressions.SyncZip(lhsExpressions)) { IExpression rhs = pair.Item1; IExpression lhs = pair.Item2; IExpression opt; if (lhs == null) { temp_names.Add(null); opt = rhs; } else { string temp = AutoName.Instance.CreateNew("optassign"); temp_names.Add(temp); opt = VariableDeclaration.CreateExpression(temp, null, rhs); } IExpression curr = NameReference.Create(opt, BrowseMode.Decompose, NameFactory.OptionHasValue); if (condition == null) { condition = curr; } else { condition = And(condition, curr); } } var success_body = new List <IExpression>(); { foreach (Tuple <IExpression, string> pair in lhsExpressions.SyncZip(temp_names)) { IExpression lhs = pair.Item1; string temp = pair.Item2; if (lhs != null) { success_body.Add(Assignment.CreateStatement(lhs, NameReference.Create(NameReference.Create(temp), BrowseMode.Decompose, NameFactory.OptionValue))); } } success_body.Add(BoolLiteral.CreateTrue()); } IfBranch result = IfBranch.CreateIf(condition, success_body, IfBranch.CreateElse(BoolLiteral.CreateFalse())); return(result); }
public IInterpreter DereferenceOnIfCondition() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { DebugThrowOnError = true, AllowInvalidMainResult = true }.SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("ptr", NameFactory.PointerNameReference(NameFactory.BoolNameReference()), ExpressionFactory.HeapConstructor(NameFactory.BoolNameReference(), FunctionArgument.Create(BoolLiteral.CreateTrue()))), Return.Create(IfBranch.CreateIf(NameReference.Create("ptr"), new[] { Int64Literal.Create("2") }, IfBranch.CreateElse(new[] { Int64Literal.Create("5") }))), }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }