public static FunctionBuilder CreateInitConstructor(Block body, FunctionCall constructorChainCall = null) { FunctionBuilder builder = Create(NameFactory.InitConstructorName, NameFactory.UnitNameReference(), body); builder.ChainCall(constructorChainCall); return(builder); }
public Settler.Settler GenerateParent(bool isMale) { int age = (int)(Math.Pow(new Random().NextDouble(), 0.2) * Varsk.VarskElderYears + Varsk.VarskAdultYears) * Date.DaysInYear; NameFactory name = isMale ? VarskNameFactories.Male() : VarskNameFactories.Female(); var settler = new Varsk(new Date(-1 * age), name.Generate(), $"{VarskNameFactories.Male().Generate()}sson", isMale ? BinaryGender.Male : BinaryGender.Female); TraitGenerator.GenerateTraits(settler); return(settler); }
internal ProfileData(IProfile profile, ApiData apiData) { _name = new Lazy <string>(() => NameFactory.GetProfileName(this)); _controllers = new List <ControllerData>(); ProfileType = profile.GetTypeData(); ControllerType = profile.ControllerType; ApiData = apiData; }
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 static FunctionDefinition CreateSetter(INameReference typeName, Block body, EntityModifier modifier = null) { return(FunctionDefinition.CreateFunction(EntityModifier.Accessor | modifier | EntityModifier.Mutable, NameDefinition.Create(NameFactory.PropertySetter), null, new[] { FunctionParameter.Create(NameFactory.PropertySetterValueParameter, typeName) }, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), body)); }
/* Method that converts a Name Service to a Name Service */ public void NameServiceToNameFactory(WorldObject wObji, WorldObject wObjf) { if (wObji.ContainService(typeof(NameService))) { NameService name = (NameService)wObji.GetService(typeof(NameService)); NameFactory nameFact = new NameFactory(); nameFact.objectName = name.name; /* Save the factory in the object builder list */ wObjf.objectBuilder.Add(nameFact); } }
internal ControllerData(TypeData controllerType, ApiData apiData, bool deprecated) { _apiVersion = new Lazy <ApiVersion>(() => ApiVersionFactory.GetControllerApiVersion(this)); _headers = new Lazy <IReadOnlyList <RequestParameterData> >(() => HeaderFactory.GetControllerHeaders(this)); _name = new Lazy <string>(() => NameFactory.GetControllerName(this)); _endpoints = new Lazy <IReadOnlyList <EndpointData> >(() => EndpointFactory.GetControllerEndpoints(this).ToArray()); ControllerType = controllerType; ApiData = apiData; Deprecated = deprecated; }
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 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); }
private NameReference getTransferValueTypeName() { if (this.referential) { return(NameFactory.ReferenceNameReference(this.ValueTypeName)); } else { return(this.ValueTypeName); } }
public void CreateMan(Guid manId, Enums.ManTypes manType) { ManInstanceData ManData = new ManInstanceData(); ManData.ManId = manId; ManData.ManType = manType; ManData.ManFirstName = NameFactory.GetNewFirstName(); ManData.ManLastName = NameFactory.GetNewLastName(); CreateMan(ManData); }
public static FunctionDefinition CreateZeroConstructor(EntityModifier modifier, Block body) { return(new FunctionDefinition(modifier | EntityModifier.Private, NameFactory.ZeroConstructorNameDefinition(), (NameDefinition)null, null, null, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), constructorChainCall: null, body: body, includes: null, friends: null)); }
public override void Evaluate(ComputationContext ctx) { if (this.Evaluation == null) { this.Evaluation = ctx.Env.UnitEvaluation; NameReference req_typename = NameFactory.PointerNameReference(NameFactory.ExceptionNameReference()); IEntityInstance eval_typename = req_typename.Evaluated(ctx, EvaluationCall.AdHocCrossJump); this.DataTransfer(ctx, ref this.expr, eval_typename); } }
// we use textual value to preserve user formatting like separator in "140_000" private Nat64Literal(string inputValue) : base(inputValue, NameFactory.Nat64NameReference()) { if (UInt64.TryParse(inputValue, out UInt64 result)) { this.value = result; } else { throw new System.Exception("Internal error"); } }
/// <summary>Creates a try-catch block around this block. </summary> /// <param name="exceptionType">The exception type to catch. </param> /// <returns><c>true</c> if the try-catch block could be created; otherwise, <c>false</c>. </returns> public bool SurroundWithTryBlock(IDeclaredType exceptionType) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var exceptionVariableName = NameFactory.CatchVariableName(Node, exceptionType); var tryStatement = codeElementFactory.CreateTryStatement(exceptionType, exceptionVariableName, Node); var block = codeElementFactory.CreateBlock(Node); tryStatement.SetTry(block); Node.ReplaceBy(tryStatement); return(true); }
// we use textual value to preserve user formatting like separator in "140_000" private Int16Literal(string inputValue) : base(inputValue, NameFactory.Int16NameReference()) { if (Int16.TryParse(inputValue, out Int16 result)) { this.value = result; } else { throw new System.Exception("Internal error"); } }
// we use textual value to preserve user formatting like separator in "140_000" private Nat8Literal(string inputValue) : base(inputValue, NameFactory.Nat8NameReference(TypeMutability.DualConstMutable)) { if (byte.TryParse(inputValue, out byte result)) { this.value = result; } else { throw new System.Exception("Internal error"); } }
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 Settler.Settler GenerateChild(Varsk father, Varsk mother) { int minParentAge = Math.Min(-1 * father.Birthday.DaysSinceSettlement, -1 * mother.Birthday.DaysSinceSettlement); int age = new Random().Next(minParentAge - Varsk.VarskAdultYears * Date.DaysInYear); bool isMale = new Random().Next(2) == 0; NameFactory name = isMale ? VarskNameFactories.Male() : VarskNameFactories.Female(); var child = new Varsk(new Date(-1 * age), name.Generate(), $"{(father.PrestigeLevel >= mother.PrestigeLevel ? father.GivenName : mother.GivenName)}sson", isMale ? BinaryGender.Male : BinaryGender.Female); ParentChildRelationship.Make(_manager, 0, father, child); ParentChildRelationship.Make(_manager, 0, mother, child); Inheritor.GenerateChildTraits(child); return(child); }
internal static bool IsValidMutableName(string name, EntityModifier modifier) { modifier = modifier ?? EntityModifier.None; if (modifier.HasAccessor) { return(!NameFactory.IsMutableName(name)); } else { return(modifier.HasMutable == NameFactory.IsMutableName(name)); } }
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 IErrorReporter ErrorEscapingReferenceWithAttachmentObject() { 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; root_ns.AddBuilder(TypeBuilder.Create("Keeper") .With(VariableDeclaration.CreateStatement("world", NameFactory.PointerNameReference(NameFactory.IntNameReference()), Undef.Create(), EntityModifier.Public)) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement( Assignment.CreateStatement(NameReference.CreateThised("world"), NameReference.Create("in_value")) )) .Parameters(FunctionParameter.Create("in_value", NameFactory.PointerNameReference(NameFactory.IntNameReference()))))); Assignment assign = Assignment.CreateStatement(NameReference.Create("attach"), ExpressionFactory.StackConstructor("Keeper", NameReference.Create("i"))).Cast <Assignment>(); FunctionDefinition func = root_ns.AddBuilder(FunctionBuilder.Create("notimportant", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("attach", NameReference.Create("Keeper"), Undef.Create(), EntityModifier.Reassignable), Block.CreateStatement( VariableDeclaration.CreateStatement("i", NameFactory.ReferenceNameReference(NameFactory.IntNameReference()), IntLiteral.Create("0")), // cannot execute this assignment because the reference would move from here to outer scope // just wrapped in `Keeper` instance // please note this `Keeper` instance is attached to `i` // (triggered by conversion reference->pointer in constructor), so it cannot outlive it assign, ExpressionFactory.Readout("attach") ) ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, assign.RhsValue)); } return(resolver); }
public IErrorReporter ErrorAbusingForcedConst() { 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; root_ns.AddBuilder(TypeBuilder.Create("Stone")); root_ns.AddBuilder(TypeBuilder.Create("Mutator", "M") .With(FunctionBuilder.Create("violate", NameFactory.UnitNameReference(), Block.CreateStatement()) .SetModifier(EntityModifier.Mutable)) .SetModifier(EntityModifier.Mutable)); root_ns.AddBuilder(TypeBuilder.Create("Mangler") .SetModifier(EntityModifier.Mutable) .With(VariableDeclaration.CreateStatement("m", NameFactory.PointerNameReference(NameReference.Create("Mutator", NameReference.Create("Stone"))), Undef.Create(), EntityModifier.Public | env.Options.ReassignableModifier())) ); FunctionCall mut_call = FunctionCall.Create(NameReference.CreateThised("f", "m", NameFactory.MutableName("violate"))); IExpression assignment = Assignment.CreateStatement(NameReference.CreateThised("f", "m"), Undef.Create()); root_ns.AddBuilder(TypeBuilder.Create("Keeper") .With(VariableDeclaration.CreateStatement("f", NameFactory.PointerNameReference(NameReference.Create(TypeMutability.ForceConst, "Mangler")), Undef.Create(), EntityModifier.Public)) .With(FunctionBuilder.Create("testing", NameFactory.UnitNameReference(), Block.CreateStatement( mut_call, assignment )))); resolver = NameResolver.Create(env); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AlteringNonMutableInstance, mut_call)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.AlteringNonMutableInstance, assignment)); } return(resolver); }
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 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 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 IErrorReporter ErrorEscapingReceivedReferenceFromFunction() { 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; root_ns.AddBuilder(FunctionBuilder.Create("selector", ExpressionReadMode.ReadRequired, NameFactory.ReferenceNameReference(NameFactory.IntNameReference()), Block.CreateStatement( ExpressionFactory.Readout("b"), Return.Create(NameReference.Create("a")) )) .Parameters( FunctionParameter.Create("a", NameFactory.ReferenceNameReference(NameFactory.IntNameReference())), FunctionParameter.Create("b", NameFactory.ReferenceNameReference(NameFactory.IntNameReference())))); FunctionCall call = FunctionCall.Create("selector", IntLiteral.Create("2"), IntLiteral.Create("3")); FunctionDefinition func = root_ns.AddBuilder(FunctionBuilder.Create("notimportant", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("h", NameFactory.ReferenceNameReference(NameFactory.IntNameReference()), IntLiteral.Create("0"), EntityModifier.Reassignable), Block.CreateStatement( // error: the most alive reference the function can return is limited to this scope // so it cannot be assigned to outer-scope variable Assignment.CreateStatement(NameReference.Create("h"), call) ), ExpressionFactory.Readout("h") ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, call)); } return(resolver); }