Created
September 20, 2024 20:37
-
-
Save tabjy/030382443eb6381e59c3f4c5fe2b52ef to your computer and use it in GitHub Desktop.
For Hotspot C2 Idea Graph IR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rlp = require('node:readline/promises'); | |
const input = ` | |
5 3 Start === 3 0 [[ 3 5 6 7 8 9 10 ]] #{0:control, 1:abIO, 2:memory, 3:rawptr:BotPTR, 4:return_address, 5:int} | |
4 22 ConI === 0 [[ 23 ]] #int:7 | |
4 10 Parm === 3 [[ 4 18 21 23 ]] Parm0: int !jvms: TestImageReader::test @ bci:-1 (line 39) | |
3 23 AndI === _ 10 22 [[ 25 ]] !jvms: TestImageReader::test @ bci:3 (line 39) | |
3 0 Root === 0 [[ 0 1 3 22 24 26 29 ]] | |
2 25 AddI === _ 23 24 [[ 21 21 27 ]] !jvms: TestImageReader::test @ bci:5 (line 39) | |
2 24 ConI === 0 [[ 25 27 31 ]] #int:1 | |
1 27 LShiftI === _ 24 25 [[ 21 28 30 31 32 33 ]] !jvms: TestImageReader::test @ bci:10 (line 40) | |
1 31 LShiftI === _ 27 24 [[ 32 ]] !jvms: TestImageReader::test @ bci:11 (line 40) | |
0 32 AddI === _ 31 27 [[ ]] !jvms: TestImageReader::test @ bci:11 (line 40) | |
` | |
const nodes = input.split('\n') | |
.filter(Boolean) | |
.map(l => l.trim().split(/\s+/)) | |
.map(cols => { | |
const [_, id, opcode, __] = cols | |
let flag = true | |
return { | |
id, opcode, inputs: cols.slice(4).filter(col => flag = (flag && col !== '[[')).filter(input => input !== '_') | |
} | |
}) | |
.reduce((acc, cur) => Object.assign(acc, { | |
[cur.id]: cur | |
}), {}) | |
function toString(node) { | |
let infixOp = '' | |
if (/^Add.?/.test(node.opcode)) { | |
infixOp = '+' | |
} | |
if (/^Sub.?/.test(node.opcode)) { | |
infixOp = '+' | |
} | |
if (/^Mul.?/.test(node.opcode)) { | |
infixOp = '*' | |
} | |
if (/^.?Div.?/.test(node.opcode)) { | |
infixOp = '/' | |
} | |
if (/^LShift.?/.test(node.opcode)) { | |
infixOp = '<<' | |
} | |
if (infixOp) { | |
return `(#${node.inputs[0]} ${infixOp} #${node.inputs[1]})` | |
} | |
return `#${node.id}` | |
} | |
const rl = rlp.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
;(async() => { | |
console.log(input) | |
let formula = '#' + await rl.question('Term to solve: #'); | |
while (true) { | |
console.log(formula) | |
const expansion = await rl.question('Expand: #'); | |
formula = formula.replaceAll(`#${expansion}`, toString(nodes[expansion + ''])) | |
} | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment