Shopify / liquid-c

Liquid performance extension in C.
MIT License
120 stars 25 forks source link

Branches Proof Of Concept #174

Open damnMeddlingKid opened 2 years ago

damnMeddlingKid commented 2 years ago

What

This PR implements a proof of concept for handling branches in the Liquid VM.

How

OP Codes

To implement branches i've added 3 OP codes:

Process

When an if tag is encountered we add an OP_EVAL_CONDITION for the if statement and add an OP_BRANCH_UNLESS operation that jumps to the next clause (or endif)

Parsing then resumes as normal by calling internal_block_body_parse, when internal_block_body_parse returns with an unknown tag we check if its one of the clauses we can handle like else if so we add an unconditional OP_BRANCH which exits to the next endif and continue parsing the tokens for the else block.

Nesting is handled through recursion.

Heres an example of a liquid if block and its corresponding bytecode.

image

Theres a more complete example in test.rb with elsif and nesting.

Limitations of the POC

I'm sure theres a lot thats missing, heres the stuff im aware of: