Yesterday, I watched a bunch of presentations about compiler architectures. None of it is directly applicable to my design, but it was inspiring anyway.
So now I have code that can translate the legacy scripts from Oolite 1.70 to something quite close to usable JavaScript. Not bad for a mere five hours of frenzied hacking…
There are a number of limitations, of course:
- It only supports selectors that are used in the script I was testing on. Fixing this is mostly drudgery, not very complicated.
- It only supports selectors that have JavaScript equivalents (call() doesn’t count).
- It doesn’t support all of the syntax of the legacy script engine. In particular, it would break on a condition with more than two spaces in it, like something_string equal two words.
- In its current state, it would do the wrong thing for non-player ship scripts.
- The legacy scripting engine’s text expansion thing is pretty wacky. In particular, it rewrites all of every statement except the leftmost word before execution. When text expansion is used in a sane way, the translator can handle it – for instance, foo [local_a] bar %R [local_b] baz translates to "foo " + this.local_a + expandDescription(" bar %R ") + this.local_b + " baz". What it can’t deal with is variables being used to specify multiple parameters at once:I’d consider that pretty bad style, but if anyone is actually doing it I’d like to know. :-)
Code: Select all
("set: local_thing foo 1", "addShips: my_[local_thing]") // Equivalent to: ("addShips: my_foo 1")
- There’s room for optimization, in particular turning STATUS_EXITING_WITCHSPACE, STATUS_LAUNCHING and STATUS_DOCKING conditions into event handlers, and adding separate tickleDocked and tickleInFlight handlers for STATUS_DOCKED/STATUS_IN_FLIGHT conditions. Manually rewriting scripts to use appropriate event handlers will always be better, though.