(expr: Json, wlHead?: string)
| 327 | // Handles postfix forms after a head: calls F[a, b][c]…, Part |
| 328 | // `lst[[1]]`, and derivative marks `F'[x]`. |
| 329 | private parsePostfix(expr: Json, wlHead?: string): Json { |
| 330 | for (;;) { |
| 331 | const t = this.peek(); |
| 332 | if (!t || t.kind !== 'punct') break; |
| 333 | if (t.value === "'") { |
| 334 | // F' → Derivative[1][F]; chain for F'' |
| 335 | let order = 0; |
| 336 | while (this.peek()?.kind === 'punct' && this.peek()!.value === "'") { |
| 337 | this.next(); |
| 338 | order++; |
| 339 | } |
| 340 | expr = [['Derivative', order], wlHead ? mapSymbol(wlHead) : expr]; |
| 341 | wlHead = undefined; |
| 342 | continue; |
| 343 | } |
| 344 | if (t.value !== '[' || t.spaceBefore) break; |
| 345 | this.next(); |
| 346 | // Part: lst[[i, j]] |
| 347 | if (this.peek()?.kind === 'punct' && this.peek()!.value === '[') { |
| 348 | this.next(); |
| 349 | const indices: Json[] = [this.parseExpression(0)]; |
| 350 | while (this.peek()?.kind === 'punct' && this.peek()!.value === ',') { |
| 351 | this.next(); |
| 352 | indices.push(this.parseExpression(0)); |
| 353 | } |
| 354 | this.expect(']'); |
| 355 | this.expect(']'); |
| 356 | expr = ['Part', wlHead ? mapSymbol(wlHead) : expr, ...indices]; |
| 357 | wlHead = undefined; |
| 358 | continue; |
| 359 | } |
| 360 | const args: Json[] = []; |
| 361 | if (!(this.peek()?.kind === 'punct' && this.peek()!.value === ']')) { |
| 362 | args.push(this.parseExpression(0)); |
| 363 | while (this.peek()?.kind === 'punct' && this.peek()!.value === ',') { |
| 364 | this.next(); |
| 365 | args.push(this.parseExpression(0)); |
| 366 | } |
| 367 | } |
| 368 | this.expect(']'); |
| 369 | expr = wlHead ? mapCall(wlHead, args) : ([expr, ...args] as Json[]); |
| 370 | wlHead = undefined; |
| 371 | } |
| 372 | return expr; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | function flatBinaryOp(op: string, a: Json, b: Json): Json { |
no test coverage detected