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 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 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 void ListTest() { var fibListExpression = _expressions.List(new[] { 1, 1 }); for (var i = 0; i < 20; i++) { fibListExpression = _expressions.List(_expressions.ConditionalList(_expressions.LessThan(_expressions.Last(fibListExpression), _expressions.Int(100)), _expressions.Append(fibListExpression, _expressions.Add(_expressions.First(_expressions.TakeLast(fibListExpression, _expressions.Int(2))), _expressions.Last(_expressions.TakeLast(fibListExpression, _expressions.Int(2))))), fibListExpression).Interpret(_context)); } var sut = fibListExpression.Interpret(_context); Assert.Equal(13, sut[6]); Assert.Equal(21, sut[7]); Assert.Equal(34, sut[8]); }
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 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 PointerArgumentAutoDereference() { 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[] { Return.Create(ExpressionFactory.Add(NameReference.Create("n"), Int64Literal.Create("1"))) })) .Parameters(FunctionParameter.Create("n", NameFactory.Int64NameReference()))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p_int", NameFactory.PointerNameReference(NameFactory.Int64NameReference()), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), FunctionArgument.Create(Int64Literal.Create("1")))), Return.Create(FunctionCall.Create(NameReference.Create("inc"), FunctionArgument.Create(NameReference.Create("p_int")))), }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter StackChunkWithBasicPointers() { 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(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("chicken", null, ExpressionFactory.StackConstructor(NameFactory.ChunkNameReference(NameFactory.PointerNameReference(NameFactory.Int64NameReference())), FunctionArgument.Create(NatLiteral.Create("2")))), Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("0")), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("-6"))), Assignment.CreateStatement(FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("1")), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("8"))), Return.Create(ExpressionFactory.Add(FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("0")), FunctionCall.Indexer(NameReference.Create("chicken"), NatLiteral.Create("1")))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public static IntHandle operator +(IntHandle left, IntHandle right) { return((IntHandle)ExpressionFactory.Add(left.Expression, right.Expression)); }
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 VirtualCallAtBase() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { ReferencingBase = true, AllowInvalidMainResult = true, DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.CreateInterface("IBase") .With(FunctionBuilder.CreateDeclaration("getA", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference())) .With(FunctionBuilder.CreateDeclaration("getB", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()))); root_ns.AddBuilder(TypeBuilder.Create("Middle") .Parents("IBase") .SetModifier(EntityModifier.Base) .With(FunctionBuilder.Create( "getA", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("-50")) })) .SetModifier(EntityModifier.Override)) .With(FunctionBuilder.Create( "getB", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("51")) })) .SetModifier(EntityModifier.Override))); root_ns.AddBuilder(TypeBuilder.Create("End") .Parents("Middle") .With(FunctionBuilder.Create( "getA", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("-1000")) })) .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase)) .With(FunctionBuilder.Create( "getB", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { // return 1+super()+base.getA() Return.Create(ExpressionFactory.Add(Int64Literal.Create("1"), ExpressionFactory.Add(FunctionCall.Create(NameReference.Create(NameFactory.SuperFunctionName)), FunctionCall.Create(NameReference.Create(NameFactory.BaseVariableName, "getA"))))) })) .SetModifier(EntityModifier.Override))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // i *IBase VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("IBase")), null, env.Options.ReassignableModifier()), // i = new End() Assignment.CreateStatement(NameReference.Create("i"), ExpressionFactory.HeapConstructor(NameReference.Create("End"))), // return i.getB() Return.Create(FunctionCall.Create(NameReference.Create("i", "getB"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IInterpreter ExplicitDereferencing() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { AllowInvalidMainResult = true, DebugThrowOnError = true, AllowDereference = true }.SetMutability(mutability)); var root_ns = env.Root; VariableDeclaration decl = VariableDeclaration.CreateStatement("x", NameFactory.PointerNameReference(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability())), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()), FunctionArgument.Create(Int64Literal.Create("4"))), env.Options.ReassignableModifier()); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { // x *Int = new Int(4) decl, // y *Int = x VariableDeclaration.CreateStatement("y", NameFactory.PointerNameReference(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability())), NameReference.Create("x")), // *x = 7 // y <- 7 Assignment.CreateStatement(Dereference.Create(NameReference.Create("x")), Int64Literal.Create("7")), // z *Int VariableDeclaration.CreateStatement("z", NameFactory.PointerNameReference(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability())), null, env.Options.ReassignableModifier()), // z = x Assignment.CreateStatement(NameReference.Create("z"), NameReference.Create("x")), // v = y + z // 14 VariableDeclaration.CreateStatement("v", null, ExpressionFactory.Add("y", "z"), env.Options.ReassignableModifier()), // x = -12 Assignment.CreateStatement(NameReference.Create("x"), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()), FunctionArgument.Create(Int64Literal.Create("-12")))), // *z = *x // z <- -12 Assignment.CreateStatement(Dereference.Create(NameReference.Create("z")), Dereference.Create(NameReference.Create("x"))), // x = -1000 Assignment.CreateStatement(NameReference.Create("x"), ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(env.Options.ReassignableTypeMutability()), FunctionArgument.Create(Int64Literal.Create("-1000")))), // v = v+z // v = 14 + (-12) Assignment.CreateStatement(NameReference.Create("v"), ExpressionFactory.Add("v", "z")), Return.Create(NameReference.Create("v")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }