Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 43x 43x 43x 43x 41x 1x 40x 40x 40x 40x 174x 72x 72x 72x 102x 8x 8x 8x 8x 94x 5x 5x 5x 5x 5x 89x 40x 3x 3x 3x 40x 40x 40x 49x 26x 1x 25x 1x 24x 24x 24x 24x 23x 23x 7x 7x 7x 23x 1x 22x 8x 8x 8x 8x 8x 5x 5x 8x 8x 22x 37x 32x 2x 30x 30x 30x 35x 35x 35x 115x 68x 47x 8x 8x 39x 39x 39x 115x 4x 31x 1x 30x 43x | import { assertUnreachable } from "shared/lib/scriptValue/format"; import { getAssociativity, getPrecedence, getArgsLen } from "./helpers"; import { Associativity, RPNToken, Token, isRPNToken, getOperatorArgsLen, } from "./types"; const shuntingYard = (input: Token[]): RPNToken[] => { if (input.length === 0) { // Input was empty return [{ type: "VAL", value: 0 }]; } const output: RPNToken[] = []; const operatorStack: Token[] = []; const functionStack: Token[] = []; let prevToken: Token | undefined; for (const token of input) { // If the current Token is a value or a variable, put them into the output stream. if ( token.type === "VAL" || token.type === "VAR" || token.type === "CONST" ) { output.push(token); prevToken = token; continue; } // If the current Token is a function, put it onto the operator stack. if (token.type === "FUN") { operatorStack.push(token); functionStack.push(token); prevToken = token; continue; } /* * If the current Token is a function argument separator, pop operators * to output stream until a left brace is encountered. */ if (token.type === "SEPERATOR") { while ( operatorStack.length > 0 && operatorStack[operatorStack.length - 1].type !== "LBRACE" ) { const stackTail = operatorStack.pop(); Iif (stackTail && isRPNToken(stackTail)) { output.push(stackTail); } } // If no left brace is encountered, separator was misplaced or parenthesis mismatch Iif ( operatorStack.length > 0 && operatorStack[operatorStack.length - 1].type !== "LBRACE" ) { // TODO never reached, check this. throw new Error("Misplaced separator or mismatched parenthesis."); } functionStack.push(token); prevToken = token; continue; } // /* if the current Tokens type is MINUS and the previous Token is an operator or type LBRACE // * or we're at the beginning of the expression (prevToken == null) the current Token is // * an unary minus, so the tokentype has to be changed. // */ // if ( // token.type === "OP" && // token.operator === "-" && // (prevToken === undefined || // prevToken.type === "OP" || // prevToken.type === "LBRACE") // ) { // const newToken: Token = { // type: "OP", // operator: "u", // }; // operatorStack.push(newToken); // prevToken = newToken; // continue; // } /* * If the current token is an operator and it's priority is lower than the priority of the last * operator in the operator buffer, than put the operators from the operator buffer into the output * stream until you find an operator with a priority lower or equal as the current tokens. * Then add the current Token to the operator buffer. */ if (token.type === "OP") { while ( operatorStack.length > 0 && // Left Associative ((getAssociativity(token) === Associativity.Left && getPrecedence(token) <= getPrecedence(operatorStack[operatorStack.length - 1])) || // Right Associative (getAssociativity(token) === Associativity.Right && getPrecedence(token) < getPrecedence(operatorStack[operatorStack.length - 1]))) ) { const stackTail = operatorStack.pop(); if (stackTail && isRPNToken(stackTail)) { output.push(stackTail); } } operatorStack.push(token); prevToken = token; continue; } // If the current Token is a left brace, put it on the operator buffer. if (token.type === "LBRACE") { if (prevToken?.type === "VAR") { throw new Error(`${prevToken.symbol}() is not a function`); } if (prevToken?.type === "VAL") { throw new Error(`${prevToken.value}() is not a function`); } Iif (prevToken?.type === "CONST") { throw new Error(`Const ${prevToken.symbol}() is not a function`); } operatorStack.push(token); prevToken = token; continue; } // If the current Token is a right brace, empty the operator buffer until you find a left brace. if (token.type === "RBRACE") { while ( operatorStack.length > 0 && operatorStack[operatorStack.length - 1].type !== "LBRACE" ) { const stackTail = operatorStack.pop(); if (stackTail && isRPNToken(stackTail)) { output.push(stackTail); } } // Expect next token on stack to be left parenthesis and pop it if ( operatorStack.length === 0 || operatorStack.pop()?.type !== "LBRACE" ) { throw new Error("Mismatched parenthesis."); } // If the token at the top of the stack is a function token, pop it onto the output queue. if ( operatorStack.length > 0 && operatorStack[operatorStack.length - 1].type === "FUN" ) { const stackTail = operatorStack.pop(); if (stackTail && isRPNToken(stackTail)) { output.push(stackTail); } // Check number of args is correct let numArgs = 1; while ( functionStack.length > 0 && functionStack[functionStack.length - 1].type !== "FUN" ) { functionStack.pop(); numArgs++; } Iif ( stackTail?.type === "FUN" && numArgs !== getArgsLen(stackTail.function) ) { throw new Error( `Unexpected number of args passed to function "${ stackTail.function }". Found ${numArgs} but expected ${getArgsLen(stackTail.function)}` ); } functionStack.pop(); } } prevToken = token; } /* * When the algorithm reaches the end of the input stream, we add the * tokens in the operatorBuffer to the outputStream. If the operator * on top of the stack is a parenthesis, there are mismatched parenthesis. */ while (operatorStack.length > 0) { if ( operatorStack[operatorStack.length - 1].type === "LBRACE" || operatorStack[operatorStack.length - 1].type === "RBRACE" ) { throw new Error("Mismatched parenthesis."); } const stackTail = operatorStack.pop(); if (stackTail && isRPNToken(stackTail)) { output.push(stackTail); } } // Validate output let stackCount = 0; let lastOp = ""; for (const token of output) { if ( token.type === "VAL" || token.type === "VAR" || token.type === "CONST" ) { stackCount++; } else if (token.type === "FUN") { lastOp = token.function; stackCount -= getArgsLen(token.function) - 1; } else if (token.type === "OP") { lastOp = token.operator; stackCount -= getOperatorArgsLen(token.operator) - 1; } else E{ /* istanbul ignore next */ assertUnreachable(token); } if (stackCount <= 0) { throw new Error(`Not enough operands for ${lastOp}.`); } } if (stackCount > 1) { throw new Error(`Invalid expression.`); } return output; }; export default shuntingYard; |