Skip to content

[JsLIGO] Add `for( ... ; ... ; ... ) { ... }` loop

Melwyn Saldanha requested to merge melwyn95@jsligo_for_loop into dev

Motivation and Context

The C-style for loops were missing in JsLIGO.

Description

This MR adds for loop to JsLIGO.

  • Frontend
    • Update grammar
    • Add error messages
    • Do other frontend stuff (cst fold, fuzz, etc.)
  • Nanopasses
    • Compile for to while
    • Add unit tests for compile
    • Decompile for
    • Add unit tests for decompile
    • Add comments documenting the nanopass
  • Expect tests
    • Add tests for ligo run test
    • Add tests for ligo compile contract
  • Docs
    • Add a section in the webpage related to interation for JsLIGO

Component

  • compiler
  • website
  • webide
  • vscode-plugin
  • debugger

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement (non-breaking change that improves performance)
  • None (change with no changelog)

Changelog

JsLIGO now supports for loops

Example

const getChar = (s: string, idx: nat): string => String.sub(idx, 1 as nat, s)

const isPalindrome = (s: string): bool => {
  let length = String.length(s);
  let isP = true;
  for (let i = 0, j = length - 1 ;  i <= j ; i++, j--) {
    isP = isP && getChar(s, abs(i)) == getChar(s, abs(j))
  }
  return isP;
}

const testPalindrome = (() => {
  Test.assert(isPalindrome("abba"));
  Test.assert(isPalindrome("ababa"));
  Test.assert(!isPalindrome("abcd"));
  Test.assert(!isPalindrome("abcde"));
})();

Result

$ ligo run test x.jsligo 
Everything at the top-level was executed.
- testPalindrome exited with value ().

Checklist:

  • Changes follow the existing coding style (use dune @fmt to check).
  • Tests for the changes have been added (for bug fixes / feature).
  • Documentation has been updated.
  • Changelog description has been added (if appropriate).
  • Start titles under ## Changelog section with #### (if appropriate).
  • There is no image or uploaded file in changelog
  • Examples in changed behaviour have been added to the changelog (for breaking change / feature).
Edited by Melwyn Saldanha

Merge request reports