wduquette / molt

Embeddable TCL Interpreter for Rust applications
BSD 3-Clause "New" or "Revised" License
103 stars 12 forks source link

Turn Datum into an enum #105

Open Twisol opened 2 years ago

Twisol commented 2 years ago

Hi Will -- it's Jonathan! This weekend, I'm making good on my years-old promise to check out Molt. Feel free to take or leave the changes in this PR; I had an idea to try out and it seems to have worked fairly nicely, but it's up to your discretion in the end.

This PR turns the existing struct Datum into a more Rust-y enum Datum, and propagates those changes throughout expr.rs. In the process, I ended up unifying the type coercion sections for each operator with their respective evaluation sections. I especially noticed an inconsistency between the assumptions made by execution for AND and OR and what the respective coercion was doing; I think the approach in this PR scopes the relevant assumptions much more tightly, so it'll be much more difficult for things to get out of sync like that.

This PR also contains minor contributions for the shift-right operator and absolute value function, which can both be simplified by using core Rust functionality. (Yes, as an arithmetic shift, >> is guaranteed to preserve sign!)

Lastly, I ran rustfmt on my changes to keep things consistent. I find some of the formatting choices it made to be less readable, but I defer to convention :)