public IErrorReporter ErrorUsingFunctionAsProperty() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { // when writing this test compiler crashed when the function was called like a property var env = Skila.Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; NameReference func_name = NameReference.Create("b", NameFactory.IIterableCount); root_ns.AddBuilder(FunctionBuilder.Create("bad_call", NameFactory.SizeNameReference(), Block.CreateStatement( // using function like a property (error) // todo: however it should be another error, because this reference should create functor and the error should // say about type mismatch between returning value and result type Return.Create(func_name))) .Parameters(FunctionParameter.Create("b", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), Variadic.Create(2, 3), null, false)) .Include(NameFactory.LinqExtensionReference())); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.UndefinedTemplateArguments, func_name)); } return(resolver); }
public IErrorReporter ErrorViolatingAssociatedReference() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; FunctionDefinition first_constructor = FunctionBuilder.CreateInitConstructor(Block.CreateStatement()) .SetModifier(EntityModifier.UnchainBase) .Parameters(FunctionParameter.Create("y", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead), FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead)); FunctionDefinition second_constructor = FunctionBuilder.CreateInitConstructor(Block.CreateStatement()) .SetModifier(EntityModifier.UnchainBase) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), ExpressionReadMode.CannotBeRead)); VariableDeclaration first_field = VariableDeclaration.CreateStatement("a", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Undef.Create(), env.Options.ReassignableModifier() | EntityModifier.Public); VariableDeclaration second_field = VariableDeclaration.CreateStatement("b", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Undef.Create(), EntityModifier.Public); TypeDefinition type = root_ns.AddBuilder(TypeBuilder.Create("Hi") .SetModifier(EntityModifier.Base | EntityModifier.AssociatedReference | EntityModifier.Mutable) .With(first_field) .With(second_field) .With(first_constructor) .With(second_constructor)); NameReference parameter_typename = NameFactory.Int64NameReference(); root_ns.AddBuilder(TypeBuilder.Create("HelloValue") .SetModifier(EntityModifier.AssociatedReference | EntityModifier.Mutable) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement()) .SetModifier(EntityModifier.UnchainBase) .Parameters(FunctionParameter.Create("x", parameter_typename, ExpressionReadMode.CannotBeRead)))); FunctionParameter variadic_param = FunctionParameter.Create("x", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Variadic.Create(2, 6), null, isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead); root_ns.AddBuilder(TypeBuilder.Create("HelloVariadic") .SetModifier(EntityModifier.AssociatedReference | EntityModifier.Mutable) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement()) .SetModifier(EntityModifier.UnchainBase) .Parameters(variadic_param))); FunctionParameter optional_param = FunctionParameter.Create("x", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Variadic.None, Int64Literal.Create("0"), isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead); root_ns.AddBuilder(TypeBuilder.Create("HelloOptional") .SetModifier(EntityModifier.AssociatedReference | EntityModifier.Mutable) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement()) .SetModifier(EntityModifier.UnchainBase) .Parameters(optional_param))); VariableDeclaration value_decl = VariableDeclaration.CreateStatement("v", null, ExpressionFactory.StackConstructor("Hi", Int64Literal.Create("3"))); root_ns.AddBuilder(FunctionBuilder.Create("notimportant", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement( value_decl, ExpressionFactory.Readout("v") ))); resolver = NameResolver.Create(env); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresSealedType, type.Modifier)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresSingleParameter, first_constructor)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresSingleConstructor, second_constructor)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceFieldCannotBeReassignable, first_field)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresSingleReferenceField, second_field)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresReferenceParameter, parameter_typename)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresNonVariadicParameter, variadic_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresNonOptionalParameter, optional_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AssociatedReferenceRequiresPassingByReference, value_decl)); Assert.AreEqual(9, resolver.ErrorManager.Errors.Count); } return(resolver); }
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 IErrorReporter ErrorInvalidDefinitions() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; Extension ext = root_ns.AddNode(Extension.Create()); // `this` parameter as the second one --> error FunctionParameter second_this_param = FunctionParameter.Create("y", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), EntityModifier.This); ext.AddBuilder(FunctionBuilder.Create("second_this", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("x", "y")))) .Parameters(FunctionParameter.Create("x", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference())), second_this_param)); // `this` parameter as optional one --> error FunctionParameter opt_this_param = FunctionParameter.Create("a", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), Variadic.None, Nat8Literal.Create("0"), false, EntityModifier.This); ext.AddBuilder(FunctionBuilder.Create("opt_this", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("a", "a")))) .Parameters(opt_this_param)); // variadic `this` parameter --> error FunctionParameter variadic_this_param = FunctionParameter.Create("b", NameFactory.ReferenceNameReference(NameFactory.Nat8NameReference()), Variadic.Create(2, 3), null, false, EntityModifier.This); FunctionCall b_count = FunctionCall.Create(NameReference.Create("b", NameFactory.IIterableCount)); ext.AddBuilder(FunctionBuilder.Create("variadic_this", NameFactory.SizeNameReference(), Block.CreateStatement( // this is invalid as well, because it would mean we array of references and we try to make // a template function (`count`) executed with reference as its template argument // return b.count() Return.Create(b_count))) .Parameters(variadic_this_param) .Include(NameFactory.LinqExtensionReference())); // `this` parameter as value (no reference) --> error FunctionParameter value_this_param = FunctionParameter.Create("c", NameFactory.Nat8NameReference(), EntityModifier.This); ext.AddBuilder(FunctionBuilder.Create("value_this", NameFactory.Nat8NameReference(), Block.CreateStatement( Return.Create(ExpressionFactory.Mul("c", "c")))) .Parameters(value_this_param)); resolver = NameResolver.Create(env); Assert.AreEqual(5, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NonPrimaryThisParameter, second_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.OptionalThisParameter, opt_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VariadicThisParameter, variadic_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NonReferenceThisParameter, value_this_param)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceAsTypeArgument, b_count.Name.TemplateArguments.Single())); } return(resolver); }
public IInterpreter CommandLine() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement( ExpressionFactory.AssertEqual(StringLiteral.Create(Interpreter.Interpreter.CommandLineTestProgramPath), NameReference.Create(NameFactory.CommandLineProgramPath)), ExpressionFactory.AssertEqual(StringLiteral.Create(Interpreter.Interpreter.CommandLineTestArgument), FunctionCall.Create(NameReference.Create(NameFactory.CommandLineArguments, NameFactory.AtFunctionName), NatLiteral.Create("0"))), Return.Create(Nat8Literal.Create("0")) )). Parameters(FunctionParameter.Create(NameFactory.CommandLineProgramPath, NameFactory.StringPointerNameReference(TypeMutability.ReadOnly)), FunctionParameter.Create(NameFactory.CommandLineArguments, NameFactory.StringPointerNameReference(TypeMutability.ReadOnly), Variadic.Create(), null, isNameRequired: false))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)0, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter PreferringNonVariadicFunction() { 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 system_ns = env.SystemNamespace; FunctionDefinition func_def1 = 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.Create(), null, false, usageMode: ExpressionReadMode.CannotBeRead))); FunctionDefinition 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(), usageMode: ExpressionReadMode.CannotBeRead))); root_ns.AddNode(VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Undef.Create())); var call = FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(NameReference.Create("i"))); root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameFactory.RealNameReference(), call, modifier: EntityModifier.Public)); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); Assert.AreEqual(func_def2, call.Resolution.TargetFunctionInstance.Target); } return(resolver); }
public IErrorReporter ConflictingVariadicOverload() { 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(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.RealNameReference(), Block.CreateStatement(new[] { Return.Create(RealLiteral.Create("3.3")) })) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), Variadic.Create(1, 3), null, isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead))); 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.Create(3, 5), null, isNameRequired: 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 IErrorReporter ErrorVariadicParametersInvalidLimits() { 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 param1 = FunctionParameter.Create("x", NameFactory.Int64NameReference(), Variadic.Create(4, 3), null, isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead); var func_def = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.RealNameReference(), Block.CreateStatement(new[] { Return.Create(RealLiteral.Create("3.3")) })) .Parameters(param1)); root_ns.AddNode(VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), null, EntityModifier.Public)); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.InvalidVariadicLimits, param1)); } return(resolver); }