public IInterpreter InitializingWithGetter() { 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") .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, ExpressionFactory.StackConstructor(NameReference.Create("Point"))), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)5, 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 ResultTypeInference() { 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 lambda = FunctionBuilder.CreateLambda(null, Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })).Build(); root_ns.AddBuilder(FunctionBuilder.Create("main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // f = () => x VariableDeclaration.CreateStatement("f", null, lambda), // return f() Return.Create(FunctionCall.Create(NameReference.Create("f"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
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 IInterpreter RawMethods() { 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[] { Return.Create(FunctionCall.Create(NameReference.Create(Int64Literal.Create("1"), NameFactory.AddOperator), FunctionArgument.Create(Int64Literal.Create("1")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, 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); }
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 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 StringToInt() { 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(new IExpression[] { VariableDeclaration.CreateStatement("s", null, StringLiteral.Create("2")), VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), ExpressionFactory.GetOptionValue( FunctionCall.Create(NameReference.Create(NameFactory.Int64NameReference(), NameFactory.ParseFunctionName), NameReference.Create("s")) )), Return.Create(NameReference.Create("i")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
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); }
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 OldSchoolSwapPointers() { 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")))); VariableDeclaration decl_a = VariableDeclaration.CreateStatement("a", null, ExpressionFactory.HeapConstructor(env.Options.ReassignableTypeMutability(), NameFactory.Nat8NameReference(), Nat8Literal.Create("2"))); FunctionCall swap_call = FunctionCall.Create("swap", AddressOf.CreateReference(NameReference.Create("a")), AddressOf.CreateReference(NameReference.Create("b"))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( decl_a, VariableDeclaration.CreateStatement("b", null, ExpressionFactory.HeapConstructor(env.Options.ReassignableTypeMutability(), NameFactory.Nat8NameReference(), Nat8Literal.Create("17"))), VariableDeclaration.CreateStatement("c", null, NameReference.Create("a")), swap_call, // here `c` still points to value of original `a` Return.Create(ExpressionFactory.Sub("a", "c")) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)15, result.RetValue.PlainValue); } return(interpreter); }
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 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 IInterpreter TypeIntersection() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true, DebugThrowOnError = true, AllowProtocols = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.CreateInterface("IGetPos") .With(FunctionBuilder.CreateDeclaration("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()))); root_ns.AddBuilder(TypeBuilder.CreateInterface("IGetNeg") .With(FunctionBuilder.CreateDeclaration("getMore", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()))); root_ns.AddBuilder(TypeBuilder.Create("GetAll") .Parents("IGetPos", "IGetNeg") .With(FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("3")) })) .SetModifier(EntityModifier.Override) ) .With(FunctionBuilder.Create("getMore", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("-1")) })) .SetModifier(EntityModifier.Override) )); NameReferenceIntersection intersection = NameReferenceIntersection.Create( NameFactory.PointerNameReference(NameReference.Create("IGetNeg")), NameFactory.PointerNameReference(NameReference.Create("IGetPos"))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("a", intersection, Undef.Create(), env.Options.ReassignableModifier()), VariableDeclaration.CreateStatement("b", intersection, Undef.Create(), env.Options.ReassignableModifier()), Assignment.CreateStatement(NameReference.Create("a"), ExpressionFactory.HeapConstructor("GetAll")), Assignment.CreateStatement(NameReference.Create("b"), ExpressionFactory.HeapConstructor("GetAll")), VariableDeclaration.CreateStatement("x", null, FunctionCall.Create(NameReference.Create("a", "getSome"))), VariableDeclaration.CreateStatement("y", null, FunctionCall.Create(NameReference.Create("b", "getMore"))), Return.Create(ExpressionFactory.Add(NameReference.Create("x"), NameReference.Create("y"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter OptionalNoLimitsVariadicFunction() { 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; root_ns.AddBuilder(FunctionBuilder.Create("provider", NameFactory.ChunkNameReference(NameFactory.Int64NameReference()), Block.CreateStatement( VariableDeclaration.CreateStatement("x", null, ExpressionFactory.StackConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()), FunctionArgument.Create(NatLiteral.Create("2")))), Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("x"), FunctionArgument.Create(NatLiteral.Create("0"))), Int64Literal.Create("-6")), Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("x"), FunctionArgument.Create(NatLiteral.Create("1"))), Int64Literal.Create("8")), Return.Create(NameReference.Create("x")) ))); root_ns.AddBuilder(FunctionBuilder.Create( "sum", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // let i0 = n.at(0) VariableDeclaration.CreateStatement("i0", null, FunctionCall.Create(NameReference.Create("n", NameFactory.PropertyIndexerName), FunctionArgument.Create(NatLiteral.Create("0")))), // let i1 = n.at(1) VariableDeclaration.CreateStatement("i1", null, FunctionCall.Create(NameReference.Create("n", NameFactory.PropertyIndexerName), FunctionArgument.Create(NatLiteral.Create("1")))), // return i0+i1 Return.Create(ExpressionFactory.Add("i0", "i1")) })) .Parameters(FunctionParameter.Create("n", NameFactory.Int64NameReference(), Variadic.Create(), FunctionCall.Create(NameReference.Create("provider")), isNameRequired: false))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { Return.Create(FunctionCall.Create(NameReference.Create("sum"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter CallingTraitMethodViaInterface() { 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; root_ns.AddBuilder(TypeBuilder.CreateInterface("ISay") .With(FunctionBuilder.CreateDeclaration("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()))); root_ns.AddBuilder(TypeBuilder.Create("Say") .With(FunctionBuilder.Create("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("7")) })) .SetModifier(EntityModifier.Override)) .Parents("ISay")); root_ns.AddBuilder(TypeBuilder.Create("Greeter", "T")); root_ns.AddBuilder(TypeBuilder.Create("Greeter", "X") .Constraints(ConstraintBuilder.Create("X").Inherits("ISay")) .SetModifier(EntityModifier.Trait) .Parents("ISay") .With(FunctionBuilder.Create("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement( Return.Create(Int64Literal.Create("2")) )).SetModifier(EntityModifier.Override))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement( // crucial point, we store our object as interface *ISay VariableDeclaration.CreateStatement("g", NameFactory.PointerNameReference("ISay"), ExpressionFactory.HeapConstructor(NameReference.Create("Greeter", NameReference.Create("Say")))), // we call method "say" implemented in trait Return.Create(FunctionCall.Create(NameReference.Create("g", "say"))) ))); 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 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 IInterpreter VirtualCall() { 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.Create("MyBase") .SetModifier(EntityModifier.Base) .With(FunctionBuilder.Create( "bar", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("33")) })) .SetModifier(EntityModifier.Base))); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("SomeChild") .With(FunctionBuilder.Create("bar", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })) .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase)) .Parents(NameReference.Create("MyBase"))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("MyBase")), ExpressionFactory.HeapConstructor(NameReference.Create("SomeChild"))), Return.Create(FunctionCall.Create(NameReference.Create("i", "bar"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter OverridingMethodWithIndexerGetter() { 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; root_ns.AddBuilder(TypeBuilder.CreateInterface("IProvider") .With(FunctionBuilder.CreateDeclaration(NameFactory.PropertyIndexerName, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference())))); root_ns.AddBuilder(TypeBuilder.Create("Middle") .Parents("IProvider") .SetModifier(EntityModifier.Base) .With(FunctionBuilder.Create(NameFactory.PropertyIndexerName, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(Return.Create(Int64Literal.Create("500")))) .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead)))); root_ns.AddBuilder(TypeBuilder.Create("Last") .Parents("Middle") .SetModifier(EntityModifier.Base) .With(PropertyBuilder.CreateIndexer(env.Options, NameFactory.Int64NameReference()) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead)) .With(PropertyMemberBuilder.CreateIndexerGetter(Block.CreateStatement(Return.Create(Int64Literal.Create("2")))) .Modifier(EntityModifier.Override | EntityModifier.UnchainBase)))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", NameFactory.PointerNameReference("IProvider"), ExpressionFactory.HeapConstructor("Last")), Return.Create(FunctionCall.Create(NameReference.Create("p", NameFactory.PropertyIndexerName), FunctionArgument.Create(Int64Literal.Create("18")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, 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 IInterpreter CheckingHostTraitRuntimeType() { 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; root_ns.AddBuilder(TypeBuilder.CreateInterface("ISay") .With(FunctionBuilder.CreateDeclaration("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()))); root_ns.AddBuilder(TypeBuilder.Create("NoSay")); root_ns.AddBuilder(TypeBuilder.Create("Greeter", "T")); root_ns.AddBuilder(TypeBuilder.Create("Greeter", "X") .Constraints(ConstraintBuilder.Create("X").Inherits("ISay")) .SetModifier(EntityModifier.Trait) .Parents("ISay") .With(FunctionBuilder.Create("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement( Return.Create(Int64Literal.Create("2")) )).SetModifier(EntityModifier.Override))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement( // just plain host, no trait is used VariableDeclaration.CreateStatement("g", NameFactory.PointerNameReference(NameFactory.IObjectNameReference()), ExpressionFactory.HeapConstructor(NameReference.Create("Greeter", NameReference.Create("NoSay")))), // we should have fail-test here Return.Create(ExpressionFactory.Ternary(IsType.Create(NameReference.Create("g"), NameReference.Create("ISay")), Int64Literal.Create("99"), Int64Literal.Create("2"))) ))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter HasConstraintWithValue() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true, AllowProtocols = true, DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()); root_ns.AddBuilder(FunctionBuilder.Create("proxy", TemplateParametersBuffer.Create().Add("T").Values, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(FunctionCall.Create(NameReference.Create("t", "getMe"))) })) .Constraints(ConstraintBuilder.Create("T").Has(func_constraint)) .Parameters(FunctionParameter.Create("t", NameReference.Create("T")))); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Y") .With(FunctionBuilder.Create("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })))); FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y"))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("y", null, ExpressionFactory.StackConstructor(NameReference.Create("Y"))), Return.Create(call) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter DirectRefCountings() { 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 inc_def = root_ns.AddBuilder(FunctionBuilder.Create( "inc", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // let temp *Int = n; // n is argument VariableDeclaration.CreateStatement("temp", NameFactory.PointerNameReference(NameFactory.Int64NameReference()), NameReference.Create("n")), // return temp + 1; Return.Create(FunctionCall.Create(NameReference.Create(NameReference.Create("temp"), NameFactory.AddOperator), FunctionArgument.Create(Int64Literal.Create("1")))) })) .Parameters(FunctionParameter.Create("n", NameFactory.PointerNameReference(NameFactory.Int64NameReference())))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // let p_int *Int = new *1; VariableDeclaration.CreateStatement("p_int", NameFactory.PointerNameReference(NameFactory.Int64NameReference()), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), FunctionArgument.Create(Int64Literal.Create("1")))), // let proxy *Int = p_int; VariableDeclaration.CreateStatement("proxy", NameFactory.PointerNameReference(NameFactory.Int64NameReference()), NameReference.Create("p_int")), // return inc(proxy); Return.Create(FunctionCall.Create(NameReference.Create("inc"), FunctionArgument.Create(NameReference.Create("proxy")))), }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter ImplicitClosureWithValue() { 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; root_ns.AddBuilder(TypeBuilder.Create("Beep") .SetModifier(EntityModifier.Mutable) .With(VariableDeclaration.CreateStatement("m", NameFactory.Int64NameReference(), null, EntityModifier.Public | env.Options.ReassignableModifier())) .With(FunctionBuilder.Create("getIt", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { Return.Create(NameReference.Create(NameFactory.ThisVariableName, "m")) })))); root_ns.AddBuilder(FunctionBuilder.Create("main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // b = new Beep() VariableDeclaration.CreateStatement("b", null, ExpressionFactory.StackConstructor(NameReference.Create("Beep"))), // b.m = 2 Assignment.CreateStatement(NameReference.Create("b", "m"), Int64Literal.Create("2")), // f = b.getIt // "b" value is sucked in, so we have a copy VariableDeclaration.CreateStatement("f", null, NameReference.Create("b", "getIt")), // b.m = 5 Assignment.CreateStatement(NameReference.Create("b", "m"), Int64Literal.Create("5")), // return f() Return.Create(FunctionCall.Create(NameReference.Create("f"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter DereferenceOnAssignment() { 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[] { // p_int *Int = new Int(1) VariableDeclaration.CreateStatement("p_int", NameFactory.PointerNameReference(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability())), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()), FunctionArgument.Create(Int64Literal.Create("1"))), env.Options.ReassignableModifier()), // v_int Int = *p_int // automatic dereference VariableDeclaration.CreateStatement("v_int", NameFactory.Int64NameReference(), NameReference.Create("p_int")), // z_int Int VariableDeclaration.CreateStatement("z_int", NameFactory.Int64NameReference(), null, env.Options.ReassignableModifier()), // z_int = *p_int // automatic derereference Assignment.CreateStatement(NameReference.Create("z_int"), NameReference.Create("p_int")), // p_int = new Int(77) Assignment.CreateStatement(NameReference.Create("p_int"), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()), FunctionArgument.Create(Int64Literal.Create("77")))), // return v_int + z_int Return.Create(FunctionCall.Create(NameReference.Create(NameReference.Create("v_int"), NameFactory.AddOperator), FunctionArgument.Create(NameReference.Create("z_int")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter RefCountsOnReadingFunctionCall() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { // the aim of this test is to test heap manager if it correctly handles reading function which returns pointer var env = Language.Environment.Create(new Options() { AllowInvalidMainResult = true, DebugThrowOnError = true } .SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create( "provider", ExpressionReadMode.OptionalUse, NameFactory.PointerNameReference(NameFactory.Int64NameReference()), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p_int", null, ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), FunctionArgument.Create(Int64Literal.Create("77")))), Return.Create(NameReference.Create("p_int")), }))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), FunctionCall.Create(NameReference.Create("provider"))), Return.Create(NameReference.Create("i")), }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(77L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter LocalVariablesLeakCheck() { 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; root_ns.AddBuilder(FunctionBuilder.Create( "last", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("z", null, Int64Literal.Create("2")), Return.Create(NameReference.Create("z")) }))); root_ns.AddBuilder(FunctionBuilder.Create( "pass", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("z", null, Int64Literal.Create("0")), VariableDeclaration.CreateStatement("t", null, FunctionCall.Create(NameReference.Create("last"))), Return.Create(ExpressionFactory.Add(NameReference.Create("z"), NameReference.Create("t"))) }))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { Return.Create(FunctionCall.Create(NameReference.Create("pass"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, 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); }