|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { ArraySpecifier, Expression, ExpressionStatement, parse } from 'shaderkit' |
| 3 | + |
| 4 | +function format(node: ArraySpecifier | Expression | null, forceParens = false): string { |
| 5 | + if (!node) return '' |
| 6 | + |
| 7 | + switch (node.type) { |
| 8 | + case 'Identifier': |
| 9 | + return node.name |
| 10 | + case 'Literal': |
| 11 | + return node.value |
| 12 | + case 'ArraySpecifier': |
| 13 | + return `${node.typeSpecifier.name}${node.dimensions.map((d) => `[${format(d)}]`).join('')}` |
| 14 | + case 'ArrayExpression': |
| 15 | + return `${format(node.typeSpecifier)}(${node.elements.map((e) => format(e)).join(', ')})` |
| 16 | + case 'UnaryExpression': |
| 17 | + case 'UpdateExpression': { |
| 18 | + const inner = node.prefix |
| 19 | + ? `${node.operator}${format(node.argument, true)}` |
| 20 | + : `${format(node.argument, true)}${node.operator}` |
| 21 | + return forceParens ? `(${inner})` : inner |
| 22 | + } |
| 23 | + case 'BinaryExpression': |
| 24 | + case 'LogicalExpression': { |
| 25 | + const inner = `${format(node.left, true)} ${node.operator} ${format(node.right, true)}` |
| 26 | + return forceParens ? `(${inner})` : inner |
| 27 | + } |
| 28 | + case 'AssignmentExpression': { |
| 29 | + const inner = `${format(node.left, true)} ${node.operator} ${format(node.right, true)}` |
| 30 | + return forceParens ? `(${inner})` : inner |
| 31 | + } |
| 32 | + case 'MemberExpression': { |
| 33 | + const obj = format(node.object, true) |
| 34 | + const prop = node.computed ? `[${format(node.property)}]` : `.${format(node.property)}` |
| 35 | + return `${obj}${prop}` |
| 36 | + } |
| 37 | + case 'CallExpression': { |
| 38 | + const callee = format(node.callee, true) |
| 39 | + return `${callee}(${node.arguments.map((a) => format(a)).join(', ')})` |
| 40 | + } |
| 41 | + case 'ConditionalExpression': { |
| 42 | + const inner = `${format(node.test, true)} ? ${format(node.alternate, true)} : ${format(node.consequent, true)}` |
| 43 | + return forceParens ? `(${inner})` : inner |
| 44 | + } |
| 45 | + default: |
| 46 | + return node satisfies never |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const ungrouped = /* glsl */ ` |
| 51 | +arith1 = a + b * c; |
| 52 | +arith2 = (a + b) * c; |
| 53 | +arith3 = a / b * c; |
| 54 | +arith4 = a / (b * c); |
| 55 | +arith5 = a - b - c; |
| 56 | +arith6 = a - (b - c); |
| 57 | +rel1 = a < b || b < c; |
| 58 | +rel2 = a < b && b < c; |
| 59 | +rel3 = a == b || c != d; |
| 60 | +log1 = x || y && z; |
| 61 | +log2 = (x || y) && z; |
| 62 | +log3 = !(x && y); |
| 63 | +tern1 = x ? a + b : c * d; |
| 64 | +tern2 = y ? a - b : c / d; |
| 65 | +bit1 = ia | ib & ic; |
| 66 | +bit2 = ia ^ ib & ic; |
| 67 | +bit3 = ia << ib >> ic; |
| 68 | +colArith = vec3((arith1 + arith2 + arith3 + arith4 + arith5 + arith6) / 100.0, 0.0, 0.0); |
| 69 | +colRel = vec3(float(rel1), float(rel2), float(rel3)); |
| 70 | +colLog = vec3(float(log1), float(log2), float(log3)); |
| 71 | +colTern = vec3((tern1 + tern2) / 20.0, 0.5, 0.0); |
| 72 | +colBits = vec3(float(bit1 & 1), float(bit2 & 1), float(bit3 & 1)); |
| 73 | +`.trim() |
| 74 | + |
| 75 | +const grouped = /* glsl */ ` |
| 76 | +arith1 = (a + (b * c)); |
| 77 | +arith2 = ((a + b) * c); |
| 78 | +arith3 = ((a / b) * c); |
| 79 | +arith4 = (a / (b * c)); |
| 80 | +arith5 = ((a - b) - c); |
| 81 | +arith6 = (a - (b - c)); |
| 82 | +rel1 = ((a < b) || (b < c)); |
| 83 | +rel2 = ((a < b) && (b < c)); |
| 84 | +rel3 = ((a == b) || (c != d)); |
| 85 | +log1 = (x || (y && z)); |
| 86 | +log2 = ((x || y) && z); |
| 87 | +log3 = (!(x && y)); |
| 88 | +tern1 = (x ? (a + b) : (c * d)); |
| 89 | +tern2 = (y ? (a - b) : (c / d)); |
| 90 | +bit1 = (ia | (ib & ic)); |
| 91 | +bit2 = (ia ^ (ib & ic)); |
| 92 | +bit3 = ((ia << ib) >> ic); |
| 93 | +colArith = vec3((((((arith1 + arith2) + arith3) + arith4) + arith5) + arith6) / 100.0, 0.0, 0.0); |
| 94 | +colRel = vec3(float(rel1), float(rel2), float(rel3)); |
| 95 | +colLog = vec3(float(log1), float(log2), float(log3)); |
| 96 | +colTern = vec3((tern1 + tern2) / 20.0, 0.5, 0.0); |
| 97 | +colBits = vec3(float(bit1 & 1), float(bit2 & 1), float(bit3 & 1)); |
| 98 | +`.trim() |
| 99 | + |
| 100 | +describe('parser', () => { |
| 101 | + it('can handle precedence', () => { |
| 102 | + const expressions = |
| 103 | + parse(ungrouped) |
| 104 | + .body.map((n) => format((n as ExpressionStatement).expression)) |
| 105 | + .join(';\n') + ';' |
| 106 | + expect(expressions).toBe(grouped) |
| 107 | + }) |
| 108 | +}) |
0 commit comments