Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to do branching via gotos, labels and conditionals #9

Open
Philogy opened this issue Jun 9, 2024 · 2 comments
Open

Add ability to do branching via gotos, labels and conditionals #9

Philogy opened this issue Jun 9, 2024 · 2 comments
Labels
feature New functionality that didn't exist

Comments

@Philogy
Copy link
Owner

Philogy commented Jun 9, 2024

Currently BALLS only supports continuous code to avoid having to write as much Huff it'd be useful to support some basic constructs that make branching possible:

  • labels: labels allow marking a certain part of the code as a possible control flow arrival point, separating logic into before/after (EVM: JUMPDEST)
  • gotos: jumps to a label unconditionally (EVM: JUMP)
  • conditional gotos: just a conditional goto, accepts a value and jumps if the value is non-zero (EVM: JUMPI)
@Philogy Philogy added the feature New functionality that didn't exist label Jun 9, 2024
@eugenioclrc
Copy link
Contributor

Quick idea about this, what if you just add a simple modifier?, example:

// simple example
fn ACTION(condition, param0, param1) -> () SKIP_IF(condition) {
  ...
}
// more complex example
fn MINT(account, amount) -> () SKIP_IF(or(iszero(account), iszero(amount)) {
  ...
}

This will be transpiled as:

#define macro ACTION() = takes(3) returns(0) {
    // takes:                      [param1, param0, condition]
    // will skip if condition
    dup1 actionSkipIf jumpi

    // stuff
    
    actionSkipIf:
    pop
    pop
    pop
}

#define macro MINT() = takes(2) returns(0) {
    // takes:                      [amount, account]
    // will skip if or(iszero(account), iszero(amount)
    // solve condition first
    dup2 iszero // [amount, account, iszero(amount)]
    dup2 iszero // [amount, account, iszero(amount), iszero(account)]
    or
   mintSkitIf jumpi
    // stuff
    mintSkitIf:
    pop
    pop
}

@Philogy
Copy link
Owner Author

Philogy commented Jun 10, 2024

Seems a bit limited with similar effort to implement, no? Doesn't let you do direct jumps which would be required to simulate else blocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality that didn't exist
Projects
None yet
Development

No branches or pull requests

2 participants