public IInterpreter ChannelDeadLockOnReceive() { 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( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("ch", null, ExpressionFactory.HeapConstructor(NameFactory.ChannelNameReference(NameFactory.Int64NameReference()))), ExpressionFactory.Readout(FunctionCall.Create(NameReference.Create("ch", NameFactory.ChannelReceive))), Return.Create(Int64Literal.Create("0")) }))); int task_id = Task.WaitAny(Task.Delay(TimeSpan.FromSeconds(Interpreter.Interpreter.TimeoutSeconds)), interpreter.TestRunAsync(env)); Assert.AreEqual(0, task_id); } return(interpreter); }
public IErrorReporter ErrorReassigningFixedVariable() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; IExpression assignment = Assignment.CreateStatement(NameReference.Create("x"), Int64Literal.Create("5")); root_ns.AddBuilder(FunctionBuilder.Create( "notimportant", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new[] { VariableDeclaration.CreateStatement("x", null, Int64Literal.Create("3")), assignment, ExpressionFactory.Readout("x") }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotReassignReadOnlyVariable, assignment)); } return(resolver); }
public IErrorReporter ScopeShadowing() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { ScopeShadowing = true, DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create( "anything", null, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("x",null,Int64Literal.Create("2")), Block.CreateStatement(new IExpression[]{ // shadowing VariableDeclaration.CreateStatement("x", null, BoolLiteral.CreateFalse()), VariableDeclaration.CreateStatement("a",NameFactory.BoolNameReference(),NameReference.Create("x")), ExpressionFactory.Readout("a"), }), VariableDeclaration.CreateStatement("b",NameFactory.Int64NameReference(),NameReference.Create("x")), ExpressionFactory.Readout("b"), }))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return resolver; }
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 IErrorReporter ErrorAddressingRValue() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; Int64Literal int_literal = Int64Literal.Create("1"); var func_def = root_ns.AddBuilder(FunctionBuilder.Create( "foo", null, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new[] { VariableDeclaration.CreateStatement("x", null, AddressOf.CreatePointer(int_literal)), VariableDeclaration.CreateStatement("y", null, AddressOf.CreateReference(Int64Literal.Create("2"))), ExpressionFactory.Readout("x"), ExpressionFactory.Readout("y"), }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AddressingRValue, int_literal)); } return(resolver); }
public IErrorReporter ErrorReturning() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; Return empty_return = Return.Create(); var func_def_int = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { empty_return }))); Int64Literal return_value = Int64Literal.Create("5"); var func_def_void = root_ns.AddBuilder(FunctionBuilder.Create( "foox", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new[] { Return.Create(return_value) }))); resolver = NameResolver.Create(env); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EmptyReturn, empty_return)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, return_value)); } return(resolver); }
public IErrorReporter ConflictingAdditionalOptionalOverload() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; var func_def1 = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.RealNameReference(), Block.CreateStatement(new[] { Return.Create(RealLiteral.Create("3.3")) }))); var func_def2 = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.RealNameReference(), Block.CreateStatement(new[] { Return.Create(RealLiteral.Create("3.3")) })).Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), Variadic.None, Int64Literal.Create("3"), false, usageMode: ExpressionReadMode.CannotBeRead))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count()); Assert.AreEqual(ErrorCode.OverloadingDuplicateFunctionDefinition, resolver.ErrorManager.Errors.Single().Code); } return(resolver); }
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 IErrorReporter ErrorIsSameOnValues() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; Int64Literal value = Int64Literal.Create("3"); root_ns.AddBuilder(FunctionBuilder.Create("foo", NameFactory.BoolNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("x", null, ExpressionFactory.HeapConstructor(NameFactory.Int64NameReference(), Int64Literal.Create("2"))), Return.Create(IsSame.Create(NameReference.Create("x"), value)) ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotUseValueExpression, value)); } return(resolver); }
public IErrorReporter ErrorDereferencingValue() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowDereference = true }.SetMutability(mutability)); var root_ns = env.Root; Int64Literal value = Int64Literal.Create("3"); root_ns.AddBuilder(FunctionBuilder.Create( "foo", null, ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Dereference.Create(value)), }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.DereferencingValue, value)); } return(resolver); }
public IErrorReporter ReadingIfAsExpression() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { GlobalVariables = true, RelaxedMode = true }.SetMutability(mutability)); var root_ns = env.Root; var if_ctrl = IfBranch.CreateIf(BoolLiteral.CreateTrue(), new IExpression[] { Int64Literal.Create("5") }, IfBranch.CreateElse(new[] { Int64Literal.Create("7") })); root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(), if_ctrl, EntityModifier.Public)); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter ErrorNothingToOverride() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; FunctionDefinition function = FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("3")) })) .SetModifier(EntityModifier.Override); root_ns.AddBuilder(TypeBuilder.Create("GetPos") .With(function)); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NothingToOverride, function)); } return(resolver); }
public IErrorReporter ErrorDiscardingNonFunctionCall() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; IExpression discard = ExpressionFactory.Readout("c"); root_ns.AddBuilder(FunctionBuilder.Create( "foo", null, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new[] { VariableDeclaration.CreateStatement("c", null, Int64Literal.Create("3")), discard, }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.DiscardingNonFunctionCall, discard)); } return(resolver); }
private IErrorReporter errorDuckTypingValues(Options options) { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(options.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("IX") .SetModifier(env.Options.InterfaceDuckTyping ? EntityModifier.Interface : EntityModifier.Protocol)); root_ns.AddBuilder(TypeBuilder.Create("X")); IExpression init_value = ExpressionFactory.StackConstructor(NameReference.Create("X")); // even with duck typing we cannot make the assigment because slicing is forbidden in all cases VariableDeclaration decl = VariableDeclaration.CreateStatement("i", NameReference.Create("IX"), init_value); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { decl, ExpressionFactory.Readout("i"), Return.Create(Int64Literal.Create("2")) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, init_value)); } return(resolver); }
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 IErrorReporter ErrorIncorrectMethodDerivation() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("IX") .With(FunctionBuilder.CreateDeclaration( "foo", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference())) .With(FunctionBuilder.Create("fin", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement())) .With(FunctionBuilder.CreateDeclaration("bar", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference())) .SetModifier(EntityModifier.Interface)); FunctionDefinition bar_impl = FunctionBuilder.Create( "bar", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })); FunctionDefinition fin_impl = FunctionBuilder.Create( "fin", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement()) .SetModifier(EntityModifier.Override | EntityModifier.UnchainBase); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("X") .With(bar_impl) .With(fin_impl) .Parents(NameReference.Create("IX"))); resolver = NameResolver.Create(env); Assert.AreEqual(3, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BaseFunctionMissingImplementation, type_impl)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MissingOverrideModifier, bar_impl)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotOverrideSealedMethod, fin_impl)); } return(resolver); }
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 MinLimitVariadicFunctionWithSpread() { 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( "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(2), null, isNameRequired: false))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("x", null, ExpressionFactory.HeapConstructor(NameFactory.ChunkNameReference(NameFactory.Int64NameReference()), FunctionArgument.Create(NatLiteral.Create("2"))), env.Options.ReassignableModifier()), 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(FunctionCall.Create(NameReference.Create("sum"), FunctionArgument.Create(Spread.Create(NameReference.Create("x"))))) }))); 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 IErrorReporter ErrorMatchingIntersection() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowProtocols = true, DiscardingAnyExpressionDuringTests = 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") .With(FunctionBuilder.Create("getSome", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("3")) }))) .With(FunctionBuilder.Create("getMore", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("-1")) })))); NameReferenceIntersection intersection = NameReferenceIntersection.Create( NameFactory.PointerNameReference(NameReference.Create("IGetNeg")), NameFactory.PointerNameReference(NameReference.Create("IGetPos"))); IExpression init_value = ExpressionFactory.HeapConstructor("GetAll"); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.CannotBeRead, NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("a", intersection, init_value), ExpressionFactory.Readout("a") }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, init_value)); } return(resolver); }
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 IErrorReporter ErrorCallingTraitMethodOnHost() { NameResolver resolver = null; 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.CreateInterface("ISay") .With(FunctionBuilder.CreateDeclaration("say", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()))); root_ns.AddBuilder(TypeBuilder.Create("NoSay")); // this function is located in trait, thus unavailable FunctionCall int_call = FunctionCall.Create(NameReference.CreateThised("hello")); root_ns.AddBuilder(TypeBuilder.Create("Greeter", "T") .With(FunctionBuilder.Create("reaching", NameFactory.UnitNameReference(), Block.CreateStatement(int_call)))); root_ns.AddBuilder(TypeBuilder.Create("Greeter", "X") .SetModifier(EntityModifier.Trait) .Constraints(ConstraintBuilder.Create("X").Inherits("ISay")) .With(FunctionBuilder.Create("hello", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement( Return.Create(Int64Literal.Create("7")) )))); FunctionCall ext_call = FunctionCall.Create(NameReference.Create("g", "hello")); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("g", null, ExpressionFactory.StackConstructor(NameReference.Create("Greeter", NameReference.Create("NoSay")))), Return.Create(ext_call) ))); resolver = NameResolver.Create(env); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceNotFound, ext_call.Name)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceNotFound, int_call.Name)); } return(resolver); }
public IErrorReporter ErrorHasConstraint() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true, AllowProtocols = true }.SetMutability(mutability)); var root_ns = env.Root; FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()); // this function accepts any parameter where parameter type has function "getMe" FunctionDefinition constrained_func = 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", NameFactory.PointerNameReference("T")))); // this type does NOT have function "getMe" TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("YMan") .With(FunctionBuilder.Create("missing", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })))); FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y_man"))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("y_man", null, ExpressionFactory.HeapConstructor(NameReference.Create("YMan"))), Return.Create(call) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ViolatedHasFunctionConstraint, call.Callee)); } return(resolver); }
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 IErrorReporter PassingTraitAsIncorrectInterface() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = 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))); IExpression init_value = ExpressionFactory.HeapConstructor(NameReference.Create("Greeter", NameReference.Create("NoSay"))); root_ns.AddBuilder(FunctionBuilder.Create("major", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("g", NameFactory.PointerNameReference("ISay"), init_value), ExpressionFactory.Readout("g") ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, init_value)); } return(resolver); }
public IErrorReporter ErrorVariableNotUsed() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true }.SetMutability(mutability)); var root_ns = env.Root; VariableDeclaration member = VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(), Int64Literal.Create("5")); root_ns.AddBuilder(TypeBuilder.Create("Thing") .With(member)); var decl1 = VariableDeclaration.CreateStatement("s", NameFactory.Int64NameReference(), null); var decl2 = VariableDeclaration.CreateStatement("t", NameFactory.Int64NameReference(), null); NameDefinition loop_label = NameDefinition.Create("here"); var loop = Loop.CreateFor(loop_label, init: null, condition: null, step: null, body: new IExpression[] { decl1, decl2 }); var func_def = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { loop, }))); resolver = NameResolver.Create(env); Assert.AreEqual(4, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BindableNotUsed, decl1.Name)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BindableNotUsed, decl2.Name)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BindableNotUsed, loop_label)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BindableNotUsed, member.Name)); } return(resolver); }
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); }