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 IInterpreter FileExists() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = 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( VariableDeclaration.CreateStatement("e", null, FunctionCall.Create(NameReference.Create(NameFactory.FileNameReference(), NameFactory.FileExists), StringLiteral.Create(randomTextFilePath))), IfBranch.CreateIf(NameReference.Create("e"), new[] { Return.Create(Int64Literal.Create("2")) }, IfBranch.CreateElse(new[] { Return.Create(Int64Literal.Create("-5")) })) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
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); }
public IInterpreter IfBranches() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.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.Int64NameReference(), Block.CreateStatement(new IExpression[] { IfBranch.CreateIf(BoolLiteral.CreateFalse(), new[] { Return.Create(Int64Literal.Create("5")) }, IfBranch.CreateElse(new[] { Return.Create(Int64Literal.Create("2")) })) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter InitializationWithOptionalAssignment() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { DebugThrowOnError = true, DiscardingAnyExpressionDuringTests = true, }.SetMutability(mutability)); var root_ns = env.Root; // this test is a bit tougher than regular opt.assignment, because variables will be // initialized for the first time with this assigment root_ns.AddBuilder(FunctionBuilder.Create("main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("acc", null, Nat8Literal.Create("0"), env.Options.ReassignableModifier()), VariableDeclaration.CreateStatement("x", null, ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("3"))), VariableDeclaration.CreateStatement("z", null, ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("5"))), VariableDeclaration.CreateStatement("a", NameFactory.Nat8NameReference(), null, env.Options.ReassignableModifier()), VariableDeclaration.CreateStatement("b", NameFactory.Nat8NameReference(), null, env.Options.ReassignableModifier()), IfBranch.CreateIf(ExpressionFactory.OptionalAssignment( new[] { NameReference.Create("a"), NameReference.Create("b") }, new[] { NameReference.Create("x"), NameReference.Create("z") }), new[] { // assign tracker should recognize the variable is initialized ExpressionFactory.IncBy("acc", NameReference.Create("a")), }, // making else branch a dead one IfBranch.CreateElse(ExpressionFactory.GenericThrow())), // assign tracker should recognize the variable is initialized (because `else` branch of above `if` is dead) ExpressionFactory.IncBy("acc", NameReference.Create("b")), Return.Create(NameReference.Create("acc")) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)8, result.RetValue.PlainValue); } return(interpreter); }
public static IExpression DownCast(IExpression lhs, INameReference rhsTypeName) { // if the expression is not of the given type we get null // if it is the runtime type IS PRESERVED // say you have statically types object // x *Object // and in runtime x is Orange // if you cast it to Vehicle you will get null, when you cast it to Fruit you will get Orange (sic!) IExpression condition = IsType.Create(lhs, rhsTypeName); IExpression success = ExpressionFactory.StackConstructor(NameFactory.OptionNameReference(rhsTypeName), FunctionArgument.Create(ReinterpretType.Create(lhs, rhsTypeName))); IExpression failure = ExpressionFactory.StackConstructor(NameFactory.OptionNameReference(rhsTypeName)); return(IfBranch.CreateIf(condition, new[] { success }, IfBranch.CreateElse(new[] { failure }))); }
public static TypeBuilder WithComparableCompare(this TypeBuilder builder, EntityModifier modifier = null) { return(builder.With(FunctionBuilder.Create(NameFactory.ComparableCompare, ExpressionReadMode.ReadRequired, NameFactory.OrderingNameReference(), Block.CreateStatement( IfBranch.CreateIf(IsSame.Create(NameReference.CreateThised(), NameReference.Create("cmp")), new[] { Return.Create(NameFactory.OrderingEqualReference()) }), // let obj = cmp cast? Self VariableDeclaration.CreateStatement("obj", null, ExpressionFactory.CheckedSelfCast("cmp", NameFactory.ReferenceNameReference(builder.CreateTypeNameReference(TypeMutability.ReadOnly)))), // return this.compare(obj.value) Return.Create(FunctionCall.Create(NameReference.CreateThised(NameFactory.ComparableCompare), NameReference.Create("obj"))))) .SetModifier(EntityModifier.Override | modifier) .Parameters(FunctionParameter.Create("cmp", NameFactory.ReferenceNameReference(NameFactory.IComparableNameReference(TypeMutability.ReadOnly)))))); }
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 TestingSamePointers() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { AllowInvalidMainResult = true, DebugThrowOnError = true, DiscardingAnyExpressionDuringTests = 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("x", null, ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2"))), VariableDeclaration.CreateStatement("y", null, NameReference.Create("x")), VariableDeclaration.CreateStatement("z", null, ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2"))), VariableDeclaration.CreateStatement("acc", null, Int64Literal.Create("0"), env.Options.ReassignableModifier()), IfBranch.CreateIf(IsSame.Create(NameReference.Create("x"), NameReference.Create("y")), new[] { Assignment.CreateStatement(NameReference.Create("acc"), ExpressionFactory.Add(NameReference.Create("acc"), Int64Literal.Create("2"))) }), IfBranch.CreateIf(IsSame.Create(NameReference.Create("x"), NameReference.Create("z")), new[] { Assignment.CreateStatement(NameReference.Create("acc"), ExpressionFactory.Add(NameReference.Create("acc"), Int64Literal.Create("7"))) }), Return.Create(NameReference.Create("acc")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter RecursiveCall() { 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; IExpression i_eq_2 = ExpressionFactory.IsEqual(NameReference.Create("i"), Int64Literal.Create("2")); IExpression i_add_1 = ExpressionFactory.Add(NameReference.Create("i"), Int64Literal.Create("1")); root_ns.AddBuilder(FunctionBuilder.Create("foo", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { // if i==2 then return i IfBranch.CreateIf(i_eq_2, new[] { Return.Create(NameReference.Create("i")) }, // else return self(i+1) IfBranch.CreateElse(new[] { Return.Create(FunctionCall.Create(NameReference.Create(NameFactory.RecurFunctionName), FunctionArgument.Create(i_add_1))) })) })) .Parameters(FunctionParameter.Create("i", NameFactory.Int64NameReference()))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { Return.Create(FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(Int64Literal.Create("0")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter ResolvingGenericArgumentInRuntime() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create("oracle", "O", VarianceMode.None, NameFactory.BoolNameReference(), Block.CreateStatement( Return.Create(IsType.Create(NameReference.Create("thing"), NameReference.Create("O"))) )) .Parameters(FunctionParameter.Create("thing", NameFactory.ReferenceNameReference(NameFactory.IObjectNameReference())))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("acc", null, Nat8Literal.Create("0"), env.Options.ReassignableModifier()), VariableDeclaration.CreateStatement("i", null, ExpressionFactory.HeapConstructor(NameFactory.IntNameReference(), IntLiteral.Create("7"))), VariableDeclaration.CreateStatement("d", null, ExpressionFactory.HeapConstructor(NameFactory.RealNameReference(), RealLiteral.Create("3.3"))), IfBranch.CreateIf(FunctionCall.Create(NameReference.Create("oracle", NameFactory.IntNameReference()), NameReference.Create("i")), new[] { ExpressionFactory.IncBy("acc", Nat8Literal.Create("2")) }), IfBranch.CreateIf(FunctionCall.Create(NameReference.Create("oracle", NameFactory.IntNameReference()), NameReference.Create("d")), new[] { ExpressionFactory.IncBy("acc", Nat8Literal.Create("88")) }), Return.Create(NameReference.Create("acc")) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)2, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter ClosureRecursiveCall() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; IExpression i_eq_jack = ExpressionFactory.IsEqual(NameReference.Create("i"), NameReference.Create("jack")); IExpression i_add_1 = ExpressionFactory.Add(NameReference.Create("i"), Nat8Literal.Create("1")); FunctionDefinition lambda = FunctionBuilder.CreateLambda(NameFactory.Nat8NameReference(), Block.CreateStatement(new[] { // if i==jack then return i IfBranch.CreateIf(i_eq_jack, new[] { Return.Create(NameReference.Create("i")) }, // else return self(i+1) IfBranch.CreateElse(new[] { Return.Create(FunctionCall.Create(NameReference.Create(NameFactory.RecurFunctionName), FunctionArgument.Create(i_add_1))) })) })) .Parameters(FunctionParameter.Create("i", NameFactory.Nat8NameReference())); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("jack", null, Nat8Literal.Create("50")), // Immediately-Invoked Function Expression (IIEFE) in Javascript world Return.Create(FunctionCall.Create(lambda, FunctionArgument.Create(Nat8Literal.Create("0")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)50, result.RetValue.PlainValue); } return(interpreter); }
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 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 IInterpreter ParallelOptionalDeclaration() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { DebugThrowOnError = true, }.SetMutability(mutability)); var root_ns = env.Root; IExpression opt_declaration = ExpressionFactory.OptionalDeclaration(new[] { VariablePrototype.Create("x", NameFactory.Nat8NameReference()), VariablePrototype.Create("y", NameFactory.Nat8NameReference()) }, new[] { ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("2")), ExpressionFactory.OptionOf(NameFactory.Nat8NameReference(), Nat8Literal.Create("7")), }); root_ns.AddBuilder(FunctionBuilder.Create("main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( IfBranch.CreateIf(opt_declaration, Return.Create(ExpressionFactory.Add("x", "y")), IfBranch.CreateElse( ExpressionFactory.GenericThrow() )) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)9, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter TraitFunctionCall() { 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; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // e &IEquatable = 3 VariableDeclaration.CreateStatement("e", NameFactory.ReferenceNameReference(NameFactory.IEquatableNameReference()), Int64Literal.Create("3")), // i Int = 7 VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Int64Literal.Create("7")), // if e!=i and i!=e then return 2 IfBranch.CreateIf(ExpressionFactory.And(ExpressionFactory.IsNotEqual("e", "i"), ExpressionFactory.IsNotEqual("e", "i")), new[] { Return.Create(Int64Literal.Create("2")) }), // return 15 Return.Create(Int64Literal.Create("15")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public static IExpression AssertTrue(IExpression condition) { return(IfBranch.CreateIf(ExpressionFactory.Not(condition), new[] { GenericThrow() })); }
public IInterpreter Indexer() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { AllowInvalidMainResult = true, DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; IEnumerable <FunctionParameter> property_parameters = new[] { FunctionParameter.Create("idx", NameFactory.Int64NameReference()) }; NameReference property_typename = NameFactory.Int64NameReference(); var point_type = root_ns.AddBuilder(TypeBuilder.Create("Point") .SetModifier(EntityModifier.Mutable) .With(Property.CreateIndexer(env.Options, property_typename, new[] { VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(), Int64Literal.Create("1"), env.Options.ReassignableModifier()) }, new[] { Property.CreateIndexerGetter(property_typename, property_parameters, Block.CreateStatement(IfBranch.CreateIf(ExpressionFactory.IsEqual(NameReference.Create("idx"), Int64Literal.Create("17")), new[] { Return.Create(NameReference.CreateThised("x")) }, IfBranch.CreateElse(new[] { Return.Create(Int64Literal.Create("300")) })))) }, new[] { Property.CreateIndexerSetter(property_typename, property_parameters, Block.CreateStatement(IfBranch.CreateIf(ExpressionFactory.IsEqual(NameReference.Create("idx"), Int64Literal.Create("17")), new[] { Assignment.CreateStatement(NameReference.CreateThised("x"), NameReference.Create(NameFactory.PropertySetterValueParameter)) }))) } ))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // p = Point() // p.x is initialized with 1 VariableDeclaration.CreateStatement("p", null, ExpressionFactory.StackConstructor(NameReference.Create("Point"))), // p[17] = 1+p[17] Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("p"), FunctionArgument.Create(Int64Literal.Create("17"))), FunctionCall.Create(NameReference.Create(Int64Literal.Create("1"), NameFactory.AddOperator), FunctionArgument.Create(FunctionCall.Indexer(NameReference.Create("p"), FunctionArgument.Create(Int64Literal.Create("17")))))), // return p[17] Return.Create(FunctionCall.Indexer(NameReference.Create("p"), FunctionArgument.Create(Int64Literal.Create("17")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public static IExpression Ternary(IExpression condition, IExpression then, IExpression otherwise) { return(IfBranch.CreateIf(condition, then, IfBranch.CreateElse(otherwise))); }
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 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); }