import { Import } from "../Languages/Imports/Import" ; 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 { RepeatingParameters } from "./Metadata/Parameters/RepeatingParameters" ; import { SingleParameter } from "./Metadata/Parameters/SingleParameter" ; /**
 * Declares a new lambda function.
 */ export class LambdaDeclareCommand extends Command { /**
     * Metadata on the command.
     */ private static metadata : CommandMetadata = new CommandMetadata ( CommandNames . LambdaDeclare ) . withDescription ( "The body of a lambda function" ) . withParameters ( [ new SingleParameter ( "returnType" , "Return type of the lambda" , true ) , new RepeatingParameters ( "Lambda function parameters" , [ new SingleParameter ( "parameterName" , "A named parameter for the lambda function." , true ) , new SingleParameter ( "parameterType" , "The type of the parameter." , true ) , ] ) , new SingleParameter ( "functionBody" , "The actual body of the lambda function" , true ) , ] ) ; returns public getMetadata ( ) { return LambdaDeclareCommand . metadata ; } param parameters returns public render ( parameters ) { if ( this . language . syntax . lambdas . returnTypeRequired ) { throw new Error ( "returnTypeRequired=true not implemented" ) ; } const imports = [ ] ; let lambdaBody = "" ; lambdaBody += this . language . syntax . lambdas . functionLeft ; if ( parameters . length > 3 ) { const typeLine = this . generateParameterVariable ( parameters , 2 ) ; lambdaBody += typeLine . commandResults [ 0 ] . text ; imports . push ( ... typeLine . addedImports ) ; for ( let i = 4 ; i < parameters . length - 1 ; i += 2 ) { const nextTypeLine = this . generateParameterVariable ( parameters , i ) ; lambdaBody += ", " + nextTypeLine . commandResults [ 0 ] . text ; imports . push ( ... nextTypeLine . addedImports ) ; } } lambdaBody += this . language . syntax . lambdas . functionMiddle ; if ( parameters [ 1 ] === KeywordNames . Void ) { lambdaBody += this . language . syntax . functions . defineStartRight + parameters [ parameters . length - 1 ] ; lambdaBody += this . language . syntax . style . semicolon + " " + this . language . syntax . functions . defineEnd ; } else { lambdaBody += parameters [ parameters . length - 1 ] ; } lambdaBody += this . language . syntax . lambdas . functionRight ; const output = [ new CommandResult ( lambdaBody , 0 ) ] ; return new LineResults ( output ) . withImports ( imports ) ; } param parameters param i remarks private generateParameterVariable ( parameters , i ) { if ( ! this . language . syntax . lambdas . parameterTypeRequired ) { return LineResults . newSingleLine ( parameters [ i ] ) ; } const parameterName = parameters [ i ] ; const parameterTypeLine = this . context . convertParsed ( [ CommandNames . Type , parameters [ i + 1 ] ] ) ; const parameterType = parameterTypeLine . commandResults [ 0 ] . text ; return this . context . convertParsed ( [ CommandNames . VariableInline , parameterName , parameterType ] ) . withImports ( parameterTypeLine . addedImports ) ; } }