/// <inheritdoc /> protected override Result <ICompoundStep, IError> TryCreateInstance( TypeReference expectedTypeReference, FreezableStepData freezeData, TypeResolver typeResolver) { var genericTypeParameter = GetGenericTypeParameter( expectedTypeReference, freezeData, typeResolver ); if (genericTypeParameter.IsFailure) { var firstError = genericTypeParameter.Error.GetAllErrors().First(); if (firstError.ErrorBuilder.ErrorCode == ErrorCode.CannotInferType) //Get a more specific error { return(ErrorCode.WrongOutputType.ToErrorBuilder( TypeName, OutputTypeExplanation, expectedTypeReference.Name ) .WithLocationSingle(firstError.Location)); } return(genericTypeParameter.ConvertFailure <ICompoundStep>()); } var result = genericTypeParameter.Value.TryGetType(typeResolver) .Bind(x => TryCreateGeneric(StepType, x)) .MapError(e => e.WithLocation(freezeData)); return(result); }
/// <summary> /// Create a new Freezable Sequence /// </summary> public static IFreezableStep CreateFreezableSequence( IEnumerable <IFreezableStep> steps, IFreezableStep finalStep, TextLocation?location) { var dict = new StepParameterDict { { new StepParameterReference.Named(nameof(Sequence <object> .InitialSteps)), new FreezableStepProperty.StepList(steps.ToImmutableList(), location) }, { new StepParameterReference.Named(nameof(Sequence <object> .FinalStep)), new FreezableStepProperty.Step(finalStep, location) }, }; var fpd = new FreezableStepData(dict, location); return(new CompoundFreezableStep( "Sequence", fpd, location )); }
/// <inheritdoc /> public override Result <TypeReference, IError> TryGetOutputTypeReference( TypeReference expectedTypeReference, FreezableStepData freezableStepData, TypeResolver typeResolver) => GetGenericTypeParameter( expectedTypeReference, freezableStepData, typeResolver ) .Map(GetOutputTypeReference);
/// <inheritdoc /> public override Result <TypeReference, IError> TryGetOutputTypeReference( TypeReference expectedTypeReference, FreezableStepData freezableStepData, TypeResolver typeResolver) { var reference = TypeReference.Create(typeof(TOutput)); return(expectedTypeReference .CheckAllows(reference, StepType, typeResolver) .MapError(x => x.WithLocation(freezableStepData)) .Map(_ => reference)); }
/// <inheritdoc /> protected override Result <ICompoundStep, IError> TryCreateInstance( TypeReference expectedTypeReference, FreezableStepData freezableStepData, TypeResolver typeResolver) { var r = TryGetOutputTypeReference(expectedTypeReference, freezableStepData, typeResolver); if (r.IsFailure) { return(r.ConvertFailure <ICompoundStep>()); } return(new TStep()); }
/// <summary> /// Create a new Freezable EntityGetValue /// </summary> public static IFreezableStep CreateFreezableArrayAccess( IFreezableStep entityOrArray, IFreezableStep indexer, TextLocation?location) { var entityGetValueDict = new StepParameterDict { { new StepParameterReference.Named(nameof(EntityGetValue <int> .Entity)), new FreezableStepProperty.Step(entityOrArray, location) }, { new StepParameterReference.Named(nameof(EntityGetValue <int> .Property)), new FreezableStepProperty.Step(indexer, location) }, }; var entityGetValueData = new FreezableStepData(entityGetValueDict, location); var entityGetValueStep = new CompoundFreezableStep( "EntityGetValue", entityGetValueData, location ); var elementAtIndexDict = new StepParameterDict { { new StepParameterReference.Named(nameof(ElementAtIndex <object> .Array)), new FreezableStepProperty.Step(entityOrArray, location) }, { new StepParameterReference.Named(nameof(ElementAtIndex <object> .Index)), new FreezableStepProperty.Step(indexer, location) }, }; var elementAtData = new FreezableStepData(elementAtIndexDict, location); var elementAtStep = new CompoundFreezableStep( "ElementAtIndex", elementAtData, location ); var result = new OptionFreezableStep(new[] { entityGetValueStep, elementAtStep }, location); return(result); }
/// <summary> /// Create a freezable Not step. /// </summary> public static IFreezableStep CreateFreezableNot(IFreezableStep boolean, TextLocation location) { var dict = new StepParameterDict { { new StepParameterReference.Named(nameof(Not.Boolean)), new FreezableStepProperty.Step(boolean, location) }, }; var fpd = new FreezableStepData(dict, location); var step = new CompoundFreezableStep(nameof(Not), fpd, location); return(step); }
/// <summary> /// Create a new Freezable Interpolated string /// </summary> public static IFreezableStep CreateFreezableInterpolatedString( IEnumerable <IFreezableStep> steps, TextLocation?location) { var dict = new StepParameterDict { { new StepParameterReference.Named(nameof(StringInterpolate.Strings)), new FreezableStepProperty.StepList(steps.ToImmutableList(), location) }, }; var fpd = new FreezableStepData(dict, location); return(new CompoundFreezableStep( nameof(StringInterpolate), fpd, location )); }
/// <summary> /// Create a new Freezable Array /// </summary> public static IFreezableStep CreateFreezableList( ImmutableList <IFreezableStep> elements, TextLocation?location) { var dict = new StepParameterDict { { new StepParameterReference.Named(nameof(ArrayNew <object> .Elements)), new FreezableStepProperty.StepList(elements, location) } }; var fpd = new FreezableStepData(dict, location); return(new CompoundFreezableStep( "ArrayNew", fpd, location )); }
/// <summary> /// Create a freezable GetVariable step. /// </summary> public static IFreezableStep CreateFreezableGetVariable( VariableName variableName, TextLocation?location) { var dict = new StepParameterDict { { new StepParameterReference.Named(nameof(GetVariable <object> .Variable)), new FreezableStepProperty.Variable(variableName, location) } }; var fpd = new FreezableStepData(dict, location); var step = new CompoundFreezableStep( "GetVariable", fpd, location ); return(step); }
/// <summary> /// Create a freezable GetVariable step. /// </summary> public static IFreezableStep CreateFreezableSetVariable( FreezableStepProperty variableName, FreezableStepProperty value, TextLocation location) { var dict = new StepParameterDict { { new StepParameterReference.Named(nameof(SetVariable <object> .Variable)), variableName }, { new StepParameterReference.Named(nameof(SetVariable <object> .Value)), value }, }; var fpd = new FreezableStepData(dict, location); var step = new CompoundFreezableStep( "SetVariable", fpd, location ); return(step); }
/// <inheritdoc /> protected override Result <TypeReference, IError> GetGenericTypeParameter( TypeReference expectedTypeReference, FreezableStepData freezableStepData, TypeResolver typeResolver) { var expectedMemberTypeReference = GetExpectedArrayTypeReference(expectedTypeReference) .MapError(x => x.WithLocation(freezableStepData)); if (expectedMemberTypeReference.IsFailure) { return(expectedMemberTypeReference.ConvertFailure <TypeReference>()); } var step = freezableStepData.TryGetStep(ArrayPropertyName, StepType); if (step.IsFailure) { return(step.ConvertFailure <TypeReference>()); } var outputTypeReference = step.Value.TryGetOutputTypeReference( expectedMemberTypeReference.Value, typeResolver ); if (outputTypeReference.IsFailure) { return(outputTypeReference.ConvertFailure <TypeReference>()); } var arrayMemberType = outputTypeReference.Value .TryGetArrayMemberTypeReference(typeResolver) .MapError(e => e.WithLocation(freezableStepData)); return(arrayMemberType); }
/// <summary> /// Gets the type /// </summary> protected abstract Result <TypeReference, IError> GetGenericTypeParameter( TypeReference expectedTypeReference, FreezableStepData freezableStepData, TypeResolver typeResolver);
/// <summary> /// Try to create an infix step /// </summary> public static Result <FreezableStepProperty, IError> TryCreateStep( TextLocation textLocation, string op, IReadOnlyList <Result <FreezableStepProperty, IError> > terms) { List <IError> errors = new(); List <FreezableStepProperty> properties = new(); foreach (var result in terms) { if (result.IsFailure) { errors.Add(result.Error); } else { properties.Add(result.Value); } } if (errors.Any()) { return(Result.Failure <FreezableStepProperty, IError>(ErrorList.Combine(errors))); } var operatorData = OperatorLookup[op].ToList(); if (!operatorData.Any()) { return(new SingleError( textLocation, ErrorCode.CouldNotParse, op, "Operator" )); } List <IFreezableStep> freezableSteps = new(); foreach (var(_, stepName, termsName) in operatorData) { var stepParameterDict = new StepParameterDict() { { new StepParameterReference.Named(termsName), new FreezableStepProperty.StepList( properties.Select(x => x.ConvertToStep()).ToImmutableList(), textLocation ) } }; var data = new FreezableStepData( stepParameterDict, textLocation ); var step = new CompoundFreezableStep(stepName, data, textLocation); freezableSteps.Add(step); } if (freezableSteps.Count == 1) { return(new FreezableStepProperty.Step(freezableSteps.Single(), textLocation)); } var alt = new OptionFreezableStep(freezableSteps, textLocation); return(new FreezableStepProperty.Step(alt, textLocation)); }