public IErrorReporter ErrorInitializingWithGetter() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create("Point") .With(PropertyBuilder.CreateAutoGetter(env.Options, "x", NameFactory.Nat8NameReference())) .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement( Assignment.CreateStatement(NameReference.CreateThised("x"), Nat8Literal.Create("5")) )))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", null, ConstructorCall.StackConstructor(NameReference.Create("Point")) // cannot use getter in post-initialization .Init("x", Nat8Literal.Create("17"), out Assignment post_init) .Build()), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) })));
/// <summary> /// Recognizes expressions that start with the <b>new</b> keyword. /// </summary> /// <returns> /// An <see cref="ObjectInitializer"/> or a <see cref="ConstructorCall"/> /// </returns> protected Expression AtomStartingWithNew() { Expression expr; Token first = Match(TokenID.KW_New), last = null; SkipComments(); switch (token.TokenID) { case TokenID.LeftBrace: { Consume(1); PropertyInitializer[] fields = List <PropertyInitializer>( PropertyInitializer, true, Resources.DuplicatedProperty); last = Match(TokenID.RightBrace); expr = new ObjectInitializer(fields); } break; case TokenID.Identifier: { QualifiedName className = QualifiedName(ref first, ref last); var args = new Expression[0]; if (token.TokenID == TokenID.LeftParenthesis) { Consume(1); args = List <Expression>(Expression, false, null); last = Match(TokenID.RightParenthesis); } if (TryMatch(TokenID.LeftBrace)) { Consume(1); PropertyInitializer[] fields = List <PropertyInitializer>( PropertyInitializer, true, Resources.DuplicatedProperty); last = Match(TokenID.RightBrace); expr = new ConstructorCall(className, args, fields); } else { if (last == null) { throw new ParseException(FileName, token); } expr = new ConstructorCall(className, args, null); } } break; default: throw new ParseException(FileName, token, Resources.InvalidNewUsage); } expr.SetLocation(first.Start, last.End); return(expr); }
public override void AcceptVisitor(StatementVisitor visitor) { visitor.VisitNewObjectExpression(this); if (ConstructorCall != null) { ConstructorCall.AcceptVisitor(visitor); } }
public void ShouldBeAbleToAddItemsToDependencyMap() { var ctor = typeof(Vehicle).GetConstructor(new Type[0]); var dependency = new Dependency(typeof(IVehicle), string.Empty); var constructorImplementation = new ConstructorCall(ctor); var dependencyMap = new DependencyMap(); dependencyMap.AddService(dependency, constructorImplementation); Assert.IsTrue(dependencyMap.Contains(dependency)); }
public void CompileConstructorCall(ConstructorCall ctorCall) { textWriter.Write("new {0}", SafeName(ctorCall.Name)); DumpExpressionsList(ctorCall.Arguments); if (ctorCall.PropertyInitializers == null) { return; } textWriter.Write(" "); DumpPropertyInitializersList(ctorCall.PropertyInitializers); }
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 void CompileConstructorCall(ConstructorCall constCall) { XmlElement previousElement = currentElement; XmlElement tmpElement = document.CreateElement("ConstructorCall"); tmpElement.SetAttribute("ClassName", constCall.Name.ToString()); currentElement = document.CreateElement("Arguments"); foreach (Expression argument in constCall.Arguments) { argument.AcceptCompiler(this); } tmpElement.AppendChild(currentElement); previousElement.AppendChild(tmpElement); currentElement = previousElement; }
public void ShouldBeAbleToCompileContainerWithParameterlessConstructor() { var targetConstructor = typeof(Vehicle).GetConstructor(new System.Type[0]); var dependency = new Dependency(typeof(IVehicle), string.Empty); var implementation = new ConstructorCall(targetConstructor); var map = new DependencyMap(); map.AddService(dependency, implementation); var container = Compile(map); Assert.IsTrue(container.Contains(typeof(IVehicle), string.Empty)); var result = container.GetInstance(typeof(IVehicle), string.Empty); Assert.IsNotNull(result); }
public IInterpreter InitializingWithCustomSetter() { 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(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()), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual((byte)14, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter ErrorCustomGetterWithInitialization() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; // error: custom getter (with no setter) + post initialization Property property = PropertyBuilder.Create(env.Options, "x", () => NameFactory.Nat8NameReference()) .With(PropertyMemberBuilder.CreateGetter(Block.CreateStatement(Return.Create(Nat8Literal.Create("3"))))) .SetModifier(EntityModifier.PostInitialization); root_ns.AddBuilder(TypeBuilder.Create("Point") .With(property)); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Nat8NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("p", null, ConstructorCall.StackConstructor(NameReference.Create("Point")) // this cannot be executed but do not report an error for it, because the type has to be fixed first .Init("x", Nat8Literal.Create("17")) .Build()), Return.Create(NameReference.Create(NameReference.Create("p"), "x")) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.PostInitializedCustomGetter, property)); } return(resolver); }
public static IExpression OptionEmpty(INameReference typeName, Memory memory = Memory.Stack) { return(ConstructorCall.Constructor(NameFactory.OptionNameReference(typeName), memory).Build()); }
public static IExpression HeapConstructor(NameReference innerTypeName) { return(ConstructorCall.HeapConstructor(innerTypeName).Build()); }
public static IExpression HeapConstructor(NameReference innerTypeName, params IExpression[] arguments) { return(ConstructorCall.HeapConstructor(TypeMutability.None, innerTypeName, arguments).Build()); }
public static IExpression HeapConstructor(string innerTypeName, params IExpression[] arguments) { return(ConstructorCall.HeapConstructor(innerTypeName, arguments).Build()); }
public static IExpression HeapConstructor(NameReference innerTypeName, TypeMutability mutability, params FunctionArgument[] arguments) { return(ConstructorCall.HeapConstructor(mutability, innerTypeName, arguments).Build()); }
public static IExpression StackConstructor(NameReference typeName, out NameReference constructorReference, params FunctionArgument[] arguments) { return(ConstructorCall.StackConstructor(typeName, out constructorReference, arguments).Build()); }
public static IExpression StackConstructor(NameReference typeName, params IExpression[] arguments) { return(ConstructorCall.StackConstructor(typeName, arguments).Build()); }
public static IExpression StackConstructor(string typeName) { return(ConstructorCall.StackConstructor(typeName).Build()); }
public static IExpression StackConstructor(string typeName, params FunctionArgument[] arguments) { return(ConstructorCall.StackConstructor(typeName, arguments).Build()); }
public static IExpression StackConstructor(NameReference typeName) { return(ConstructorCall.StackConstructor(typeName).Build()); }