fe-lang / sonatina

Apache License 2.0
47 stars 6 forks source link

New parser (pest) #45

Closed sbillig closed 5 months ago

sbillig commented 6 months ago

This replaces the old parser with a pest-derived parser. It currently implements all text-format functionality required for the tests to pass, meaning that there may be syntax that the old parser supported that the new one doesn't (I'm not sure if there is, actually). The goal is to extend this parser to support some new functionality required for sonatina to be a useful evm backend ("objects", contract constructor functions, etc), but that'll happen in future PRs.

Some minimal error checking is performed (duplicate values definitions, referring to undefined identifiers, etc).

I also made some minor syntax changes:

Note that there are a couple small changes to the optimization passes, to make them work with alias values, though I made no attempt to ensure they function correctly in all situations involving alias values, just the particular situations that arose during testing. (I haven't thought about a more general solution to this problem, but perhaps we could discuss this.) The parser introduces an alias when a value is used before it's defined (in the textual form of the program, not the actual execution flow of course). For example:

v0.i8 = phi (v1 block1) (10.i8 block2)
...
v1.i8 = ...

In this case, the name v1 will first be marked as undefined, and given a new value id with a dummy value, say Value(5). When the definition of v1 is reached, we create a new value id for the definition, say Value(6), associate the name v1 with Value(6), and change Value(5) to be an alias of Value(6). (Note that unlike in the old parser, the name "v1" isn't guaranteed to be associated with ir::Value(0))

TODO:

sbillig commented 5 months ago

Thanks for the cleanup hints. I have some followup work in mind, but I'll open a new PR.