public IInterpreter RealDividingByZeroWithoutNaNs() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DebugThrowOnError = true, DiscardingAnyExpressionDuringTests = true } .SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("a", null, Real64Literal.Create(5.0)), VariableDeclaration.CreateStatement("b", null, Real64Literal.Create(0.0)), ExpressionFactory.Readout(ExpressionFactory.Divide("a", "b")), Return.Create(Nat8Literal.Create("2")) ))); ExecValue result = interpreter.TestRun(env); Assert.IsTrue(result.IsThrow); } return(interpreter); }
public IErrorReporter ErrorInitializingWithGetter() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("Point") .With(PropertyBuilder.CreateAutoGetter(env.Options, "x", NameFactory.Nat8NameReference())) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement( Assignment.CreateStatement(NameReference.CreateThised("x"), Nat8Literal.Create("5")) )))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", null, ConstructorCall.StackConstructor(NameReference.Create("Point")) // cannot use getter in post-initialization .Init("x", Nat8Literal.Create("17"), out Assignment post_init) .Build()), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) })));
public IInterpreter NoMutability() { var env = Environment.Create(new Options() { DebugThrowOnError = true } .SetMutability(MutabilityModeOption.OnlyAssignability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("a", NameFactory.Nat8NameReference(TypeMutability.ForceMutable), Nat8Literal.Create("2"), env.Options.ReassignableModifier()), VariableDeclaration.CreateStatement("b", NameFactory.Nat8NameReference(TypeMutability.ForceConst), Nat8Literal.Create("13")), Assignment.CreateStatement(NameReference.Create("a"), NameReference.Create("b")), Return.Create(NameReference.Create("a")) ))); var interpreter = new Interpreter.Interpreter(); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)13, result.RetValue.PlainValue); return(interpreter); }
public IInterpreter InstanceCallStaticDispatch() { 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; Extension ext = root_ns.AddNode(Extension.Create()); ext.AddBuilder(FunctionBuilder.Create("paf", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("x", "x")))) .Parameters(FunctionParameter.Create("x", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), EntityModifier.This))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("i", null, Nat8Literal.Create("5")), Return.Create(FunctionCall.Create(NameReference.Create("i", "paf"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)25, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter NoExtrasWithCopyConstructor() { // nothing is written in stone, but for now let's treat assignment in declaration as assignment // not copy constructor (as in C++) 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; root_ns.AddBuilder(TypeBuilder.Create("Point") .SetModifier(EntityModifier.Mutable) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement())) // copy-constructor .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement( Assignment.CreateStatement(NameReference.CreateThised("x"), Nat8Literal.Create("66")) )).Parameters(FunctionParameter.Create("p", NameFactory.ReferenceNameReference("Point"), ExpressionReadMode.CannotBeRead))) .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference()) .With(VariableDeclaration.CreateStatement("f", NameFactory.Nat8NameReference(), null, env.Options.ReassignableModifier())) .WithGetter(Block.CreateStatement(Return.Create(NameReference.CreateThised("f")))) .WithSetter(Block.CreateStatement(Assignment.CreateStatement(NameReference.CreateThised("f"), ExpressionFactory.Mul(NameFactory.PropertySetterValueReference(), Nat8Literal.Create("2"))))))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", null, ConstructorCall.StackConstructor(NameReference.Create("Point")) .Init("x", Nat8Literal.Create("7")) .Build()), // bit-copy of the object, there is no calling copy-constructor here VariableDeclaration.CreateStatement("r", null, NameReference.Create("p")), Return.Create(NameReference.Create(NameReference.Create("r"), "x")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)14, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter ErrorTransitiveMutabilityTypePassing() { NameResolver resolver = null; foreach (var mutability in Options.StrictMutabilityModes) { var env = Language.Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("Alien") .SetModifier(EntityModifier.Mutable)); // Option is a type which mutability is not decided until concrete type is given // here we pass mutable type so it should evaluate into mutable one // and next it should create an error when passing to const type VariableDeclaration decl_bad = VariableDeclaration.CreateStatement("x", NameFactory.PointerNameReference(NameFactory.IObjectNameReference(TypeMutability.ForceConst)), ExpressionFactory.OptionEmpty("Alien", Memory.Heap)); root_ns.AddBuilder(TypeBuilder.Create("Cool")); VariableDeclaration decl_ok = VariableDeclaration.CreateStatement("y", NameFactory.PointerNameReference(NameFactory.IObjectNameReference(TypeMutability.ForceConst)), ExpressionFactory.OptionEmpty("Cool", Memory.Heap)); root_ns.AddBuilder(FunctionBuilder.Create(NameFactory.MainFunctionName, NameFactory.Nat8NameReference(), Block.CreateStatement( decl_bad, ExpressionFactory.Readout("x"), decl_ok, ExpressionFactory.Readout("y"), Return.Create(Nat8Literal.Create("0")) ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, decl_bad.InitValue)); } return(resolver); }
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 IInterpreter InitializingWithCustomSetter() { 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; root_ns.AddBuilder(TypeBuilder.Create("Point") .SetModifier(EntityModifier.Mutable) .With(PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference()) .With(VariableDeclaration.CreateStatement("f", NameFactory.Nat8NameReference(), null, env.Options.ReassignableModifier())) .WithGetter(Block.CreateStatement(Return.Create(NameReference.CreateThised("f")))) .WithSetter(Block.CreateStatement(Assignment.CreateStatement(NameReference.CreateThised("f"), ExpressionFactory.Mul(NameFactory.PropertySetterValueReference(), Nat8Literal.Create("2"))))))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", null, ConstructorCall.StackConstructor(NameReference.Create("Point")) .Init("x", Nat8Literal.Create("7")) .Build()), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)14, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter ErrorCustomGetterWithInitialization() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; // error: custom getter (with no setter) + post initialization Property property = PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference()) .With(PropertyMemberBuilder.CreateGetter(Block.CreateStatement(Return.Create(Nat8Literal.Create("3"))))) .SetModifier(EntityModifier.PostInitialization); root_ns.AddBuilder(TypeBuilder.Create("Point") .With(property)); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", null, ConstructorCall.StackConstructor(NameReference.Create("Point")) // this cannot be executed but do not report an error for it, because the type has to be fixed first .Init("x", Nat8Literal.Create("17")) .Build()), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.PostInitializedCustomGetter, property)); } 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 CommandLine() { 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; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( ExpressionFactory.AssertEqual(StringLiteral.Create(Interpreter.Interpreter.CommandLineTestProgramPath), NameReference.Create(NameFactory.CommandLineProgramPath)), ExpressionFactory.AssertEqual(StringLiteral.Create(Interpreter.Interpreter.CommandLineTestArgument), FunctionCall.Create(NameReference.Create(NameFactory.CommandLineArguments, NameFactory.AtFunctionName), NatLiteral.Create("0"))), Return.Create(Nat8Literal.Create("0")) )). Parameters(FunctionParameter.Create(NameFactory.CommandLineProgramPath, NameFactory.StringPointerNameReference(TypeMutability.ReadOnly)), FunctionParameter.Create(NameFactory.CommandLineArguments, NameFactory.StringPointerNameReference(TypeMutability.ReadOnly), Variadic.Create(), null, isNameRequired: false))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)0, 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 GenericTypeAliasing() { 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; root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point", "V", VarianceMode.None)) .With(Alias.Create("VType", NameReference.Create("V"), EntityModifier.Public))); VariableDeclaration decl = VariableDeclaration.CreateStatement("x", NameReference.Create("p", "VType"), Undef.Create(), env.Options.ReassignableModifier()); root_ns.AddBuilder(FunctionBuilder.Create("main", NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("p", null, ExpressionFactory.StackConstructor(NameReference.Create("Point", NameFactory.Nat8NameReference()))), decl, Assignment.CreateStatement(NameReference.Create("x"), Nat8Literal.Create("5")), Return.Create(NameReference.Create("x")) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)5, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter AutoPropertiesWithPointers() { 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; var point_type = root_ns.AddBuilder(TypeBuilder.Create("Point") .SetModifier(EntityModifier.Mutable) .With(Property.Create(env.Options, "x", NameFactory.PointerNameReference(NameFactory.Nat8NameReference()), new[] { Property.CreateAutoField(NameFactory.PropertyAutoField, NameFactory.PointerNameReference(NameFactory.Nat8NameReference()), ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(), Nat8Literal.Create("1")), env.Options.ReassignableModifier()) }, new[] { Property.CreateAutoGetter(NameFactory.PropertyAutoField, NameFactory.PointerNameReference(NameFactory.Nat8NameReference())) }, new[] { Property.CreateAutoSetter(NameFactory.PropertyAutoField, NameFactory.PointerNameReference(NameFactory.Nat8NameReference())) } ))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { // p = Point() // p.x is initialized with 1 VariableDeclaration.CreateStatement("p", null, ExpressionFactory.StackConstructor(NameReference.Create("Point"))), // p.x = 1+p.x Assignment.CreateStatement(NameReference.Create(NameReference.Create("p"), "x"), ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(), FunctionCall.Create(NameReference.Create(Nat8Literal.Create("1"), NameFactory.AddOperator), FunctionArgument.Create(NameReference.Create("p", "x"))))), // return p.x Return.Create(NameReference.Create("p", "x")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)2, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter ErrorInvalidDefinitions() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; Extension ext = root_ns.AddNode(Extension.Create()); // `this` parameter as the second one --> error FunctionParameter second_this_param = FunctionParameter.Create("y", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), EntityModifier.This); ext.AddBuilder(FunctionBuilder.Create("second_this", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("x", "y")))) .Parameters(FunctionParameter.Create("x", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference())), second_this_param)); // `this` parameter as optional one --> error FunctionParameter opt_this_param = FunctionParameter.Create("a", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), Variadic.None, Nat8Literal.Create("0"), false, EntityModifier.This); ext.AddBuilder(FunctionBuilder.Create("opt_this", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("a", "a")))) .Parameters(opt_this_param)); // variadic `this` parameter --> error FunctionParameter variadic_this_param = FunctionParameter.Create("b", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), Variadic.Create(2, 3), null, false, EntityModifier.This); FunctionCall b_count = FunctionCall.Create(NameReference.Create("b", NameFactory.IIterableCount)); ext.AddBuilder(FunctionBuilder.Create("variadic_this", NameFactory.SizeNameReference(), Block.CreateStatement( // this is invalid as well, because it would mean we array of references and we try to make // a template function (`count`) executed with reference as its template argument // return b.count() Return.Create(b_count))) .Parameters(variadic_this_param) .Include(NameFactory.LinqExtensionReference())); // `this` parameter as value (no reference) --> error FunctionParameter value_this_param = FunctionParameter.Create("c", NameFactory.Nat8NameReference(), EntityModifier.This); ext.AddBuilder(FunctionBuilder.Create("value_this", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("c", "c")))) .Parameters(value_this_param)); resolver = NameResolver.Create(env); Assert.AreEqual(5, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NonPrimaryThisParameter, second_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.OptionalThisParameter, opt_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VariadicThisParameter, variadic_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NonReferenceThisParameter, value_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceAsTypeArgument, b_count.Name.TemplateArguments.Single())); } return(resolver); }
public IInterpreter StoringSequenceAsSequence() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { // todo: add analogous test with storing iterable as sequence // yes, this test seems like pointless thing but it is important nevertheless because // we have to check if conversion does NOT happen var env = Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("x", null, ExpressionFactory.HeapConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()), FunctionArgument.Create(NatLiteral.Create("2"))), env.Options.ReassignableModifier()), ExpressionFactory.InitializeIndexable("x", Int64Literal.Create("-6"), Int64Literal.Create("8")), // conversion should NOT happen VariableDeclaration.CreateStatement("stored", null, FunctionCall.Create(NameFactory.StoreFunctionReference(), NameReference.Create("x"))), Return.Create(ExpressionFactory.Ternary(IsSame.Create("x", "stored"), Nat8Literal.Create("2"), Nat8Literal.Create("7"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)2, result.RetValue.PlainValue); } return(interpreter); }
public static IExpression Inc(Func <IExpression> lhs) { return(IncBy(lhs, Nat8Literal.Create("1"))); }
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 IErrorReporter ErrorSwapNonReassignableValues() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowDereference = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create("swap", "T", VarianceMode.None, NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("t", NameReference.Create("T"), NameReference.Create("a")), Assignment.CreateStatement(Dereference.Create(NameReference.Create("a")), NameReference.Create("b")), Assignment.CreateStatement(Dereference.Create(NameReference.Create("b")), NameReference.Create("t")) )) .Constraints(ConstraintBuilder.Create("T") .SetModifier(env.Options.ReassignableModifier())) .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T")), FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T")))); VariableDeclaration decl = VariableDeclaration.CreateStatement("a", null, Nat8Literal.Create("2")); FunctionCall swap_call = FunctionCall.Create("swap", NameReference.Create("a"), NameReference.Create("b")); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( decl, VariableDeclaration.CreateStatement("b", null, Nat8Literal.Create("17")), // error: both values are const swap_call, Return.Create(ExpressionFactory.Sub("a", "b")) ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ViolatedMutabilityConstraint, swap_call.Name)); } return(resolver); }
public IInterpreter RealNotANumber() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { // at this point Skila adheres to the standard but maybe should raise an exception for every NaN // and this way remove them from the language (similarly to null pointers) // https://stackoverflow.com/questions/5394424/causes-for-nan-in-c-application-that-do-no-raise-a-floating-point-exception // https://stackoverflow.com/questions/2941611/can-i-make-gcc-tell-me-when-a-calculation-results-in-nan-or-inf-at-runtime var env = Environment.Create(new Options() { DebugThrowOnError = true, AllowRealMagic = true }.SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("a", null, Real64Literal.Create(double.NaN)), VariableDeclaration.CreateStatement("b", null, Real64Literal.Create(double.NaN)), Return.Create(ExpressionFactory.Ternary(ExpressionFactory.IsEqual("a", "b"), Nat8Literal.Create("15"), Nat8Literal.Create("2"))) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)2, result.RetValue.PlainValue); } return(interpreter); }
internal static IExpression Dec(Func <IExpression> lhs) { return(DecBy(lhs, Nat8Literal.Create("1"))); }
public IInterpreter OldSchoolSwapValuesViaPointers() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DebugThrowOnError = true, AllowDereference = true } .SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create("swap", "T", VarianceMode.None, NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("t", NameReference.Create("T"), NameReference.Create("a")), Assignment.CreateStatement(Dereference.Create(NameReference.Create("a")), NameReference.Create("b")), Assignment.CreateStatement(Dereference.Create(NameReference.Create("b")), NameReference.Create("t")) )) .Constraints(ConstraintBuilder.Create("T") .SetModifier(env.Options.ReassignableModifier())) .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T")), FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T")))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("a", null, ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(env.Options.ReassignableTypeMutability()), Nat8Literal.Create("2"))), VariableDeclaration.CreateStatement("b", null, ExpressionFactory.HeapConstructor(NameFactory.Nat8NameReference(env.Options.ReassignableTypeMutability()), Nat8Literal.Create("17"))), FunctionCall.Create("swap", NameReference.Create("a"), NameReference.Create("b")), Return.Create(ExpressionFactory.Sub("a", "b")) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)15, result.RetValue.PlainValue); } return(interpreter); }
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 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); }