Пример #1
0
        private Loop(NameDefinition label,
                     IExpression preCondition,
                     IEnumerable <IExpression> body,
                     IEnumerable <IExpression> postStep,
                     IExpression postCondition)
        {
            // each iteration is:
            // pre-condition
            // body
            // step
            // post-condition

            this.Label         = label;
            this.preCondition  = preCondition;
            this.PostStep      = (postStep ?? Enumerable.Empty <IExpression>()).StoreReadOnly();
            this.Body          = (body ?? Enumerable.Empty <IExpression>()).StoreReadOnly();
            this.postCondition = postCondition;

            this.ReadMode = ExpressionReadMode.CannotBeRead; // todo: temporary state

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreateLoop(PreCondition,
                                                                    thenPath: Body,
                                                                    postMaybes: PostStep.Concat(PostCondition)));
        }
Пример #2
0
        private TemplateTranslation overridden(IReadOnlyList <TemplateParameter> parameters, IEnumerable <IEntityInstance> arguments)
        {
            var dict = Later.Create(() => this.table.ToDictionary(it => it.Key, it => it.Value));
            int i    = -1;

            foreach (IEntityInstance arg in arguments)
            {
                ++i;

                TemplateParameter key = parameters[i];
                if (!object.ReferenceEquals(this.table[key], arg))
                {
                    dict.Value[key] = arg;
                }
            }

            if (dict.HasValue)
            {
                return(new TemplateTranslation(this.parametersProvider, dict.Value));
            }
            else
            {
                return(this);
            }
        }
Пример #3
0
        private EntityInstance(RuntimeCore core, IEntity target,
                               TemplateTranslation translation, TypeMutability overrideMutability,
                               Lifetime lifetime)
        {
            if (target == null)
            {
                throw new ArgumentNullException("Instance has to be created for existing entity");
            }

            this.core        = core;
            this.Target      = target;
            this.Translation = translation;

            this.Lifetime = lifetime;

            this.OverrideMutability = overrideMutability;

            this.nameOf = Later.Create(() => NameReference.Create(
                                           OverrideMutability,
                                           null, this.Target.Name.Name, this.TemplateArguments.Select(it => it.NameOf),
                                           target: this, isLocal: false));
            this.pureNameOf = Later.Create(() => NameReference.Create(
                                               null, this.Target.Name.Name, this.TemplateArguments.Select(it => it.NameOf),
                                               target: this, isLocal: false));
        }
Пример #4
0
        private VariableDeclaration(EntityModifier modifier, ExpressionReadMode readMode, string name,
                                    INameReference typeName,
                                    IExpression initValue,
                                    IEnumerable <LabelReference> friends = null)
            : base(readMode)
        {
            if (name == null)
            {
                throw new ArgumentNullException();
            }

            this.Modifier     = modifier ?? EntityModifier.None;
            this.Name         = NameDefinition.Create(name);
            this.TypeName     = typeName;
            this.initValue    = initValue;
            this.AccessGrants = (friends ?? Enumerable.Empty <LabelReference>()).StoreReadOnly();

            this.instancesCache = new EntityInstanceCache(this, () => GetInstance(TypeMutability.None,
                                                                                  TemplateTranslation.CreateParameterless(this), Lifetime.Timeless));

            this.closures = new List <TypeDefinition>();

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(InitValue));
        }
Пример #5
0
        private Spread(IExpression expr)
            : base(ExpressionReadMode.ReadRequired)
        {
            this.expr = expr;

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(Expr));
        }
Пример #6
0
        private Chain(IEnumerable <IExpression> instructions)
            : base(ExpressionReadMode.ReadRequired)
        {
            this.Instructions = instructions.StoreReadOnly();

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(this.Instructions));
        }
Пример #7
0
        private ReinterpretType(IExpression lhs, INameReference rhsTypeName)
            : base(ExpressionReadMode.ReadRequired)
        {
            this.Lhs         = lhs;
            this.RhsTypeName = rhsTypeName;

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(Lhs));
        }
Пример #8
0
        private IsSame(IExpression lhs, IExpression rhs)
            : base(ExpressionReadMode.ReadRequired)
        {
            this.Lhs = lhs;
            this.Rhs = rhs;

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(Lhs, Rhs));
        }
Пример #9
0
        private FunctionArgument(string nameLabel, IExpression expression)
            : base()
        {
            this.NameLabel  = nameLabel;
            this.expression = expression;

            this.closures = new List <TypeDefinition>();

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(Expression));
        }
Пример #10
0
        private FunctionCall(CallMode mode,
                             IExpression callee, IEnumerable <FunctionArgument> arguments, NameReference requestedOutcomeType)
            : base()
        {
            this.mode                     = mode;
            this.callee                   = callee;
            this.UserArguments            = (arguments ?? Enumerable.Empty <FunctionArgument>()).Indexed().StoreReadOnlyList();
            this.RequestedOutcomeTypeName = requestedOutcomeType;

            this.closures = new List <TypeDefinition>();

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(UserArguments));
        }
Пример #11
0
        private FunctionDefinition(EntityModifier modifier,
                                   NameDefinition name,
                                   NameDefinition label,
                                   IEnumerable <TemplateConstraint> constraints,
                                   IEnumerable <FunctionParameter> parameters,
                                   ExpressionReadMode callMode,
                                   INameReference result,
                                   FunctionCall constructorChainCall,
                                   Block body,
                                   IEnumerable <NameReference> includes,
                                   IEnumerable <LabelReference> friends)
            : base(modifier | (body == null ? EntityModifier.Abstract : EntityModifier.None), name, constraints, includes)
        {
            parameters = parameters ?? Enumerable.Empty <FunctionParameter>();

            this.Label        = label ?? NameDefinition.Create(name.Name);
            this.AccessGrants = (friends ?? Enumerable.Empty <LabelReference>()).StoreReadOnly();
            this.Parameters   = parameters.Indexed().StoreReadOnlyList();
            setResultParameter(result);
            this.IsResultTypeNameInfered = result == null;
            if (this.IsResultTypeNameInfered)
            {
                this.resultTypeCandidates = new List <IEntityInstance>();
            }
            this.UserBody = body;
            this.CallMode = callMode;

            // attaching zero-constructor call to the body of the function will be done when attaching entire function to a type
            if (constructorChainCall != null)
            {
                this.UserBody.SetConstructorChainCall(constructorChainCall);
            }

            if (this.IsLambdaInvoker)
            {
                this.LambdaTrap = new LambdaTrap();
            }

            this.attachPostConstructor();

            this.flow = Later.Create(() => ExecutionFlow.CreatePath(UserBody));
            this.constructionCompleted = true;

            if (!IsValidMutableName(this.Name.Name, this.Modifier))
            {
                throw new ArgumentException($"Mutable function has to be marked with name as well {this.Name}");
            }
        }
Пример #12
0
        private IfBranch(IExpression condition, IEnumerable <IExpression> body, IfBranch next)
        {
            this.condition = condition;
            // we have to postpone calculating read-mode because the last instruction can be function call
            // and it is resolved only after finding its target
            if (!body.Any())
            {
                body = new[] { UnitLiteral.Create() }
            }
            ;
            this.Body = Block.Create((block) => block.Instructions.Last().ReadMode, body);
            this.Next = next;

            this.attachPostConstructor();

            this.flow = Later.Create(() => this.IsElse
                ? ExecutionFlow.CreateElse(Body, Next) : ExecutionFlow.CreateFork(Condition, Body, Next));
        }
Пример #13
0
        public static TemplateTranslation Translated(TemplateTranslation baseTranslation,
                                                     TemplateTranslation through, ref bool translated)
        {
            if (baseTranslation == null || through == null)
            {
                throw new ArgumentNullException();
            }

            if (baseTranslation.table.Count == 0 || through.table.Count == 0)
            {
                return(baseTranslation);
            }

            var dict = Later.Create(() => baseTranslation.table.ToDictionary(it => it.Key, it => it.Value));

            foreach (KeyValuePair <TemplateParameter, IEntityInstance> entry in baseTranslation.table)
            {
                if (entry.Value != null)
                {
                    bool            trans          = false;
                    IEntityInstance entityInstance = entry.Value.TranslateThrough(ref trans, through);

                    if (trans)
                    {
                        dict.Value[entry.Key] = entityInstance;
                    }
                }
                else if (through.Translate(entry.Key, out IEntityInstance value))
                {
                    dict.Value[entry.Key] = value;
                }
            }

            if (dict.HasValue)
            {
                translated = true;
                return(new TemplateTranslation(baseTranslation.parametersProvider, dict.Value));
            }
            else
            {
                return(baseTranslation);
            }
        }
Пример #14
0
        private BoolOperator(OpMode mode, IExpression lhs, IExpression rhs)
            : base(ExpressionReadMode.ReadRequired)
        {
            this.Mode = mode;
            this.lhs  = lhs;
            this.rhs  = rhs;

            this.attachPostConstructor();

            this.flow = Later.Create(() =>
            {
                switch (this.Mode)
                {
                case OpMode.And: return(ExecutionFlow.CreateFork(Lhs, Rhs, null));

                case OpMode.Or: return(ExecutionFlow.CreateFork(Lhs, null, Rhs));

                default: throw new InvalidOperationException();
                }
            });
        }
Пример #15
0
 protected Expression(Option <ExpressionReadMode> readMode)
 {
     this.readMode = readMode;
     this.flow     = Later.Create(() => ExecutionFlow.CreatePath(ChildrenNodes.WhereType <IExpression>()));
 }