mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-05-31 21:54:12 +00:00

We now suggest alternative table keys when code errors with "attempt to index/call 'foo' (a nil value)". For example: "redstone.getinput()", will now suggest "Did you mean: getInput". This is a bit tricky to get right! In the above example, our code reads like: 1 GETTABUP 0 0 0 ; r0 := _ENV["redstone"] 2 GETFIELD 0 0 1 ; r0 := r0["getinput"] 3 CALL 0 1 1 ; r0() Note, that when we get to the problematic line, we don't have access to the original table that we attempted to index. In order to do this, we borrow ideas from Lua's getobjname — we effectively write an evaluator that walks back over the code and tries to reconstruct the expression that resulted in nil. For example, in the above case: - We know an instruction happened at pc=3, so we try to find the expression that computed r0. - We know this was set at pc=2, so we step back one. This is a GETFIELD instruction, so we check the key (it's a constant, so worth reporting), and then try to evaluate the table. - This version of r0 was set at pc=1, so we step back again. It's a GETTABUP instruction, so we can just evaluate that directly. We then use this information (indexing _ENV.redstone with "getinput") to find alternative keys (e.g. getInput, getOutput, etc...) and then pick some likely suggestions with Damerau-Levenshtein/OSD. I'm not entirely thrilled by the implementation here. The core interpretation logic is implemented in Java. Which is *fine*, but a) feels a little cheaty and b) means we're limited to what Lua bytecode can provide (for instance, we can't inspect outer functions, or list all available names in scope). We obviously can expand the bytecode if needed, but something we'd want to be careful with. The alternative approach would be to handle all the parsing in Lua. Unfortunately, this is quite hard to get right — I think we'd need some lazy parsing strategy to avoid constructing the whole AST, while still retaining all the scope information we need. I don't know. We really could make this as complex as we like, and I don't know what the right balance is. It'd be cool to detect patterns like the following, but is it *useful*? local monitor = peripheral.wrap("left") monitor.write("Hello") -- ^ monitor is nil. Is there a peripheral to the left of the -- computer? For now, the current approach feels the easiest, and should allow us to prototype things and see what does/doesn't work.