import { BudgieUtilities } from "../../BudgieUtilities" ; import { StringToNumberStartConversionType } from "../Languages/Properties/Syntax/StringToNumberSyntax" ; import { LineResults } from "../LineResults" ; import { CommandNames } from "../Names/CommandNames" ; import { KeywordNames } from "../Names/KeywordNames" ; import { Command } from "./Command" ; import { CommandResult } from "./CommandResult" ; import { CommandMetadata } from "./Metadata/CommandMetadata" ; import { SingleParameter } from "./Metadata/Parameters/SingleParameter" ; import { addLineEnder } from "./Utilities" ; /**
 * Starts a block to be executed if a string can be converted to a double.
 */ export class IfStringToDoubleStartCommand extends Command { /**
     * Metadata on the command.
     */ private static metadata : CommandMetadata = new CommandMetadata ( CommandNames . IfStringToDoubleStart ) . withDescription ( "Starts a block to be executed if a string can be converted to a double" ) . withIndentation ( [ 1 ] ) . withParameters ( [ new SingleParameter ( "stringName" , "Name of a string to try converting." , true ) , new SingleParameter ( "doubleName" , "What to call the string in double form." , true ) , ] ) ; returns public getMetadata ( ) { return IfStringToDoubleStartCommand . metadata ; } param parameters returns public render ( parameters ) { const conversionType = this . language . syntax . strings . toDouble . conversionType ; const lines = [ ] ; if ( conversionType === StringToNumberStartConversionType . PredeclareConvertAndValidate ) { this . predeclareVariables ( parameters , lines ) ; } if ( conversionType !== StringToNumberStartConversionType . ValidateDirectly ) { this . convertStrings ( parameters , lines ) ; } this . validateDoubles ( parameters , lines ) ; const results = new LineResults ( lines ) ; results . withImports ( this . language . syntax . strings . toDouble . requiredImports ) ; results . commandResults [ results . commandResults . length - 1 ] . indentation = 1 ; return results ; } param parameters param lines private predeclareVariables ( parameters , lines ) { const initialValue = this . language . syntax . strings . toDouble . initialVariableValues ; for ( let i = 1 ; i < parameters . length ; i += 2 ) { const declarationParameters = [ CommandNames . Variable , parameters [ i + 1 ] , KeywordNames . Double ] ; if ( initialValue !== "" ) { declarationParameters . push ( initialValue ) ; } const declaration = this . context . convertParsed ( declarationParameters ) . commandResults [ 0 ] ; declaration . text += this . language . syntax . style . semicolon ; lines . push ( declaration ) ; } addLineEnder ( lines , this . language . syntax . strings . toDouble . initializeVariablesEnd , 0 ) ; } param parameters param lines private convertStrings ( parameters , lines ) { if ( lines . length === 0 ) { lines . push ( new CommandResult ( "" , 0 ) ) ; } for ( let i = 1 ; i < parameters . length ; i += 2 ) { const stringName = parameters [ i ] ; const doubleName = parameters [ i + 1 ] ; addLineEnder ( lines , this . language . syntax . strings . toDouble . perVariableConversionStartLeft , 0 ) ; addLineEnder ( lines , doubleName , 0 ) ; addLineEnder ( lines , this . language . syntax . strings . toDouble . perVariableConversionStartMiddle , 0 ) ; addLineEnder ( lines , stringName , 0 ) ; addLineEnder ( lines , this . language . syntax . strings . toDouble . perVariableConversionStartRight , 0 ) ; } } param parameters param lines private validateDoubles ( parameters , lines ) { if ( lines . length === 0 ) { lines . push ( new CommandResult ( "" , 0 ) ) ; } addLineEnder ( lines , this . language . syntax . strings . toDouble . validationBlockLeft , 0 ) ; for ( let i = 1 ; i < parameters . length ; i += 2 ) { const stringName = parameters [ i ] ; const doubleName = parameters [ i + 1 ] ; let comparison = this . language . syntax . strings . toDouble . validationBlockComparison ; comparison = BudgieUtilities . stringReplaceAll ( comparison , "{0}" , stringName ) ; comparison = BudgieUtilities . stringReplaceAll ( comparison , "{1}" , doubleName ) ; addLineEnder ( lines , comparison , 0 ) ; if ( i < parameters . length - 2 ) { addLineEnder ( lines , this . language . syntax . strings . toDouble . validationBlockMiddle , 0 ) ; } } addLineEnder ( lines , this . language . syntax . strings . toDouble . validationBlockRight , 0 ) ; } }