Ejemplo n.º 1
0
 /// <inheritdoc />
 protected override async Task <Result <Unit, IError> > Run(
     IStateMonad stateMonad,
     CancellationToken cancellationToken)
 {
     return(await Value.Run(stateMonad, cancellationToken)
            .Bind(x => stateMonad.SetVariableAsync(Variable, x, true, this)));
 }
Ejemplo n.º 2
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var variable = stateMonad.GetVariable <int>(Variable).MapError(x => x.WithLocation(this));

            if (variable.IsFailure)
            {
                return(variable.ConvertFailure <Unit>());
            }

            var amount = await Amount.Run(stateMonad, cancellationToken);

            if (amount.IsFailure)
            {
                return(amount.ConvertFailure <Unit>());
            }

            var r = await stateMonad.SetVariableAsync(
                Variable,
                variable.Value + amount.Value,
                false,
                this
                );

            return(r);
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var str = await String.Run(stateMonad, cancellationToken)
                      .Map(x => x.GetStringAsync());

            if (str.IsFailure)
            {
                return(str.ConvertFailure <Unit>());
            }

            var currentValue = stateMonad.GetVariable <StringStream>(Variable)
                               .MapError(x => x.WithLocation(this));

            if (currentValue.IsFailure)
            {
                return(currentValue.ConvertFailure <Unit>());
            }

            var newValue = await currentValue.Value.GetStringAsync() + str.Value;

            var r = await stateMonad.SetVariableAsync(
                Variable,
                new StringStream(newValue),
                false,
                this
                );

            if (r.IsFailure)
            {
                return(r.ConvertFailure <Unit>());
            }

            return(Unit.Default);
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var variableName = VariableName.Index;

            var from = await From.Run(stateMonad, cancellationToken);

            if (from.IsFailure)
            {
                return(from.ConvertFailure <Unit>());
            }

            var to = await To.Run(stateMonad, cancellationToken);

            if (to.IsFailure)
            {
                return(to.ConvertFailure <Unit>());
            }

            var increment = await Increment.Run(stateMonad, cancellationToken);

            if (increment.IsFailure)
            {
                return(increment.ConvertFailure <Unit>());
            }

            var currentValue = from.Value;

            var setResult = await stateMonad.SetVariableAsync(variableName, currentValue, false, this);

            if (setResult.IsFailure)
            {
                return(setResult.ConvertFailure <Unit>());
            }

            if (increment.Value == 0)
            {
                return(new SingleError(new ErrorLocation(this), ErrorCode.DivideByZero));
            }

            while (increment.Value > 0 ? currentValue <= to.Value : currentValue >= to.Value)
            {
                var r = await Action.Run(stateMonad, cancellationToken);

                if (r.IsFailure)
                {
                    return(r);
                }

                var currentValueResult = stateMonad.GetVariable <int>(variableName)
                                         .MapError(e => e.WithLocation(this));

                if (currentValueResult.IsFailure)
                {
                    return(currentValueResult.ConvertFailure <Unit>());
                }

                currentValue  = currentValueResult.Value;
                currentValue += increment.Value;

                var setResult2 = await stateMonad.SetVariableAsync(
                    variableName,
                    currentValue,
                    false,
                    this
                    );

                if (setResult2.IsFailure)
                {
                    return(setResult.ConvertFailure <Unit>());
                }
            }

            await stateMonad.RemoveVariableAsync(VariableName.Index, false, this);

            return(Unit.Default);
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var sclResult = await SCL.Run(stateMonad, cancellationToken).Map(x => x.GetStringAsync());

            if (sclResult.IsFailure)
            {
                return(sclResult.ConvertFailure <Unit>());
            }

            List <VariableName> variablesToExport;

            if (Export is null)
            {
                variablesToExport = new List <VariableName>();
            }
            else
            {
                var exportResult = await Export.Run(stateMonad, cancellationToken)
                                   .Bind(x => x.GetElementsAsync(cancellationToken));

                if (exportResult.IsFailure)
                {
                    return(exportResult.ConvertFailure <Unit>());
                }

                variablesToExport = exportResult.Value.Select(x => x.GetString())
                                    .Select(x => new VariableName(x))
                                    .ToList();
            }

            var stepResult = SCLParsing.TryParseStep(sclResult.Value)
                             .Bind(x => x.TryFreeze(TypeReference.Unit.Instance, stateMonad.StepFactoryStore));

            if (stepResult.IsFailure)
            {
                return(stepResult.ConvertFailure <Unit>());
            }

            await using var monad2 = new ScopedStateMonad(
                            stateMonad,
                            ImmutableDictionary <VariableName, object> .Empty
                            );

            await stepResult.Value.Run <Unit>(monad2, cancellationToken);

            foreach (var variable in variablesToExport)
            {
                var value = monad2.GetVariable <object>(variable);

                var valueV = value.IsSuccess ? value.Value : null;

                await monad2.RemoveVariableAsync(
                    variable,
                    false,
                    this
                    ); //Remove the variable to prevent it being disposed

                await stateMonad.SetVariableAsync(variable, valueV, true, this);
            }

            return(Unit.Default);
        }