src/app/www/js/functions.js
/**
 * Preprocesses code to make it standard and make processing it faster.
 * Basically:
 *  * Trim all lines
 *  * Translate commands and keys that have multiple names to one standard
 *    name.
 *  * If no command is found, replace command as 'KEYS', as it is a
 *    sequence of keys to press
 *  * Remove REM lines (comment) and whichever line doesn't have any
 *    command or the command is not recognized.
 *  * Swap the sequence of the REPEAT command: normally, in Duckyscript,
 *    REPEAT refers to the command before it. To make processing it easier,
 *    REPEAT is swapped with the line above and now refers to the command
 *    after it.
 * @param {String} content 
 */
Preprocesses code to make it standard and make processing it faster.
Basically:
Trim all lines
Translate commands and keys that have multiple names to one standard
name.
If no command is found, replace command as 'KEYS', as it is a
sequence of keys to press
Remove REM lines (comment) and whichever line doesn't have any
command or the command is not recognized.
Swap the sequence of the REPEAT command: normally, in Duckyscript,
REPEAT refers to the command before it. To make processing it easier,
REPEAT is swapped with the line above and now refers to the command
after it.
@param {String} content
Preprocesses code to make it standard and make processing it faster . Basically : Trim all lines Translate commands and keys that have multiple names to one standard name . If no command is found replace command as ' KEYS ' as it is a sequence of keys to press Remove REM lines ( comment ) and whichever line doesn ' t have any command or the command is not recognized . Swap the sequence of the REPEAT command : normally in Duckyscript REPEAT refers to the command before it . To make processing it easier REPEAT is swapped with the line above and now refers to the command after it . @param { String } content
function preProcessCode(content) {
    let lines = content.split('\n');
    lines = lines.map((el) => {
        return el.trim();
    }).filter((el, idx) => {
        let parts = el.split(' ');
        let command = parts[0];
        parts = parts.filter((el) => el != "");
        if(translate[command]) {
            command = translate[command];
        }
        else if(keys.find((el) => el == command)) {
            parts = parts.map((el) => {
                if(translateKey[el]) {
                    return translateKey[el];
                }
                return el;
            });
            command = "KEYS";
            parts.unshift(command);
        }
        lines[idx] = parts.join(' ');
        return commands.find((el) => el == command);
    });
    lines.forEach((el, idx) => {
        const command = el.split(' ')[0];
        if(command == "REPEAT") {
            const temp = lines[idx - 1];
            lines[idx - 1] = lines[idx];
            lines[idx] = temp;
        }
    });
    return lines.join("\n");
}
function preProcessCode ( content ) { let lines = content . split ( '\n' ) ; lines = lines . map ( ( el ) => { return el . trim ( ) ; } ) . filter ( ( el , idx ) => { let parts = el . split ( ' ' ) ; let command = parts [ 0 ] ; parts = parts . filter ( ( el ) => el != "" ) ; if ( translate [ command ] ) { command = translate [ command ] ; } else if ( keys . find ( ( el ) => el == command ) ) { parts = parts . map ( ( el ) => { if ( translateKey [ el ] ) { return translateKey [ el ] ; } return el ; } ) ; command = "KEYS" ; parts . unshift ( command ) ; } lines [ idx ] = parts . join ( ' ' ) ; return commands . find ( ( el ) => el == command ) ; } ) ; lines . forEach ( ( el , idx ) => { const command = el . split ( ' ' ) [ 0 ] ; if ( command == "REPEAT" ) { const temp = lines [ idx - 1 ] ; lines [ idx - 1 ] = lines [ idx ] ; lines [ idx ] = temp ; } } ) ; return lines . join ( "\n" ) ; }
Preprocesses code to make it standard and make processing it faster.
Preprocesses code to make it standard and make processing it faster .