function interpret(insts, limit, rng) {
    let stack = [];
    let ip = 0;
    while (ip < insts.length) {
        const inst = insts[ip++];
        switch (inst) {
            case "+": {
                const right = stack.pop();
                const left = stack.pop();

                stack.push(left + right);
                break;
            }
            case "-": {
                const right = stack.pop();
                const left = stack.pop();

                stack.push(left - right);
                break;
            }
            case "*": {
                const right = stack.pop();
                const left = stack.pop();

                stack.push(left * right);
                break;
            }
            case "/": {
                const right = stack.pop();
                const left = stack.pop();

                stack.push(left / right);
                break;
            }
            case "d": {
                const right = stack.pop();
                const left = stack.pop();

                stack.push(rollDice(left, right, limit, rng));
                break;
            }
            case "~": {
                let value = stack.pop();

                stack.push(-value);
                break;
            }
            default: {
                stack.push(inst);
                break;
            }
        }
    }
    return Math.round(stack.pop());
}
/**
 * This is a stack-based interpreter, which evaluates mathematical expressions in
 * postfix notation (https://en.wikipedia.org/wiki/Reverse_Polish_notation).
 *
 * @param {string[]} insts Instructions in reverse polish notation
 * @param {number} limit max number of rolls in a single evaluation
 * @param {(min: number, max: number) => number} rng Random number generation function
 */
This is a stack-based interpreter, which evaluates mathematical expressions in
postfix notation .

@param {string[]} insts Instructions in reverse polish notation
@param {number} limit max number of rolls in a single evaluation
@param {(min: number, max: number) => number} rng Random number generation function
This is a stack - based interpreter which evaluates mathematical expressions in postfix notation . @param { string [] } insts Instructions in reverse polish notation @param { number } limit max number of rolls in a single evaluation @param { ( min : number max : number ) = > number } rng Random number generation function
function interpret(insts, limit, rng) {
    let stack = [];
    let ip = 0;
    while (ip < insts.length) {
        const inst = insts[ip++];
        switch (inst) {
            case "+": {
                const right = stack.pop();
                const left = stack.pop();
                stack.push(left + right);
                break;
            }
            case "-": {
                const right = stack.pop();
                const left = stack.pop();
                stack.push(left - right);
                break;
            }
            case "*": {
                const right = stack.pop();
                const left = stack.pop();
                stack.push(left * right);
                break;
            }
            case "/": {
                const right = stack.pop();
                const left = stack.pop();
                stack.push(left / right);
                break;
            }
            case "d": {
                const right = stack.pop();
                const left = stack.pop();
                stack.push(rollDice(left, right, limit, rng));
                break;
            }
            case "~": {
                let value = stack.pop();
                stack.push(-value);
                break;
            }
            default: {
                stack.push(inst);
                break;
            }
        }
    }
    return Math.round(stack.pop());
}
function interpret ( insts , limit , rng ) { let stack = [ ] ; let ip = 0 ; while ( ip < insts . length ) { const inst = insts [ ip ++ ] ; switch ( inst ) { case "+" : { const right = stack . pop ( ) ; const left = stack . pop ( ) ; stack . push ( left + right ) ; break ; } case "-" : { const right = stack . pop ( ) ; const left = stack . pop ( ) ; stack . push ( left - right ) ; break ; } case "*" : { const right = stack . pop ( ) ; const left = stack . pop ( ) ; stack . push ( left * right ) ; break ; } case "/" : { const right = stack . pop ( ) ; const left = stack . pop ( ) ; stack . push ( left / right ) ; break ; } case "d" : { const right = stack . pop ( ) ; const left = stack . pop ( ) ; stack . push ( rollDice ( left , right , limit , rng ) ) ; break ; } case "~" : { let value = stack . pop ( ) ; stack . push ( - value ) ; break ; } default : { stack . push ( inst ) ; break ; } } } return Math . round ( stack . pop ( ) ) ; }
This is a stack-based interpreter, which evaluates mathematical expressions in
 postfix notation (https://en.wikipedia.org/wiki/Reverse_Polish_notation).
This is a stack - based interpreter which evaluates mathematical expressions in postfix notation ( https : // en . wikipedia . org / wiki / Reverse_Polish_notation ) .