import { ScriptIfDescriptor, Condition } from "../types/script/ScriptIfDescriptor"; import { ScriptLineDescriptor } from "../types/script/ScriptLineDescriptor"; import { ScriptReturnType } from "./types/ScriptReturnType"; import { ScriptDescriptor } from "../types/script/ScriptDescriptor"; import { ScriptConditionType } from "../types/script/ScriptConditionType"; import { TokenGroup, Token } from "../types/Token"; import { Scope } from "./types/Scope"; import { ScriptPromptDescriptor } from "../types/script/ScriptPromptDescriptor"; import { createVariableName } from "./variables"; import { ScriptAlertDescriptor } from "../types/script/ScriptAlertDescriptor"; import { ScriptVariableDescriptor } from "../types/script/ScriptVariableDescriptor"; import { VariableAssignment } from "./types/VariableAssignment"; /** * Goes through each token in a provided set of script lines, replacing the template string with an actual name * @param lines The lines of code to process * @param reservedNames Names that have already been used in parent scope */ export function replaceVariableNames( script: ScriptDescriptor, scope: Scope ) { let lastVariable: VariableAssignment | null = null; //TODO: need data type for( let i = 0; i < script.lines.length; i++ ) { script.lines[i] = handleLine( script, script.lines[i], scope, lastVariable ); const varType = getVariableType( script.lines[i] ); if( varType !== ScriptReturnType.Unknown && script.lines[i].variables ) { const variables = script.lines[i].variables as string[]; for( let l = 0; l < variables.length; l++ ) { scope.variables.push( variables[l] ); lastVariable = { name: variables[l], type: varType }; } script.lines[i].variables = variables; } } return script; } /** * Moves through the individual tokens of a script line and replaces the variable template string, if present * @param line A descriptor to the line of code * @param reservedNames Names that are already in use within scope */ function handleLine( script: ScriptDescriptor, line: ScriptLineDescriptor, scope: Scope, lastVariable: VariableAssignment | null ): ScriptLineDescriptor { switch( line.type ) { case 'store': return handleStore( script, line as ScriptVariableDescriptor, scope ); case 'if': return handleIf( script, line as ScriptIfDescriptor, scope, lastVariable ); case 'prompt': return handlePrompt( script, line as ScriptPromptDescriptor, scope ); case 'alert': return handleAlert( script, line as ScriptAlertDescriptor, scope ); } return line; } export function isAsyncCode( script: ScriptDescriptor ) { function doesAsyncExist( lines: ScriptLineDescriptor[] ) { for( let i = 0; i < lines.length; i++ ) { if( lines[i].children ) { if( doesAsyncExist( lines[i].children as ScriptLineDescriptor[] ) ) { return true; } } else if( lines[i].isAsync ) { return true; } } return false; } return doesAsyncExist( script.lines ); } function getVariableType( line: ScriptLineDescriptor ): ScriptReturnType { switch( line.type ) { case 'prompt': return ScriptReturnType.String; case 'store': return ScriptReturnType.Object; //TODO: Actually derive type from right hand side. Temporarily assume object default: return ScriptReturnType.Unknown; } } function handleIf( script: ScriptDescriptor, line: ScriptIfDescriptor, scope: Scope, lastVariable: VariableAssignment | null ) { for( let i = 0; i < line.conditions.items.length; i++ ) { const item = line.conditions.items[i]; if( typeof( item['length'] ) === 'undefined' ) { } else { const conditions: Condition[] = item as Condition[]; for( let l = 0; l < conditions.length; l++ ) { const condition = conditions[ l ]; const hints: Token[] = line.conditions.items[ i ][ l ].tokens .filter( (x: Token) => x.group === TokenGroup.Generic && x.section === 'hint' ); if( condition.leftSide === '{{variable}}' ) { if( lastVariable === null && script.inputs.length === 1 ) { line.conditions.items[ i ][ l ].leftSide = script.inputs[ 0 ]; } else if( lastVariable !== null ) { line.conditions.items[ i ][ l ].leftSide = lastVariable.name; } if( hints.length > 0 ) { const isLength = hints.some( x => x.key === 'length' ); if( isLength ) { line.conditions.items[ i ][ l ].leftSide += '.length'; } } } if( condition.rightSide === '{{variable}}' ) { if( lastVariable === null && script.inputs.length === 1 ) { line.conditions.items[ i ][ l ].rightSide = script.inputs[ 0 ]; } else if( lastVariable !== null ) { line.conditions.items[ i ][ l ].rightSide = lastVariable.name; } if( hints.length > 0 ) { const isLength = hints.some( x => x.key === 'length' ); if( isLength ) { line.conditions.items[ i ][ l ].rightSide += '.length'; } } } //Check to see if this is an if statement that calls a function for truthiness if( condition.leftSide === '' && condition.rightSide === 'true' && condition.type === ScriptConditionType.Equal ) { const customText = condition.tokens.filter( x => x.group === TokenGroup.Custom && !x.section ).map( x => x.key ).join('').toLowerCase(); const foundFunction = scope.functions.find( ( { name } ) => customText.indexOf( name.toLowerCase() ) !== -1 ); if( foundFunction ) { line.conditions.items[ i ][ l ].leftSide = `${foundFunction.name}(${foundFunction.inputs.map( ( name: string, idx: number ) => scope.variables[ scope.variables.length - idx - 1 ] )})`; } } } } } return line; } function handlePrompt( script: ScriptDescriptor, line: ScriptPromptDescriptor, scope: Scope ) { const varName = createVariableName( line.text.split(' ') ); //TODO: Check collision if( line.variables ) { line.variables[ 0 ] = varName as string; } return line; } function handleAlert( script: ScriptDescriptor, line: ScriptAlertDescriptor, scope: Scope ) { const words = line.text.split(' '); for( let i = 0; i < words.length; i++ ) { const word = words[ i ]; if( word.indexOf('{{variable') !== -1 ) { if( word.indexOf('{{variable:') !== -1 ) { const hint = word.split('{{variable:')[ 1 ].split('}}')[ 0 ]; const variableName = scope.variables .reverse() .find( name => name.toLowerCase().indexOf( hint.toLowerCase() ) !== -1 ); if( variableName ) { words[ i ] = variableName; } else { words[ i ] = `${scope.variables.reverse()[ scope.variables.length - 1 ]}.${hint}`; } } else { words[ i ] = scope.variables[ scope.variables.length - 1 ]; } } } line.text = words.join(' '); return line; } function handleStore( script: ScriptDescriptor, line: ScriptVariableDescriptor, scope: Scope ) { const calledFunction = scope.functions.find( x => x.name === line.rightSide ); if( calledFunction ) { line.rightSide += `(${calledFunction.inputs.length > 0 ? calledFunction.inputs.map( ( name: string, idx: number) => scope.variables[ scope.variables.length - idx - 1]) : ''})` if( isAsyncCode( calledFunction ) ) { line.isAsync = true; line.rightSide = line.rightSide; } } return line; }