Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Latest commit

 

History

History
45 lines (28 loc) · 1.19 KB

Grain.md

File metadata and controls

45 lines (28 loc) · 1.19 KB

WebAssembly with Grain

  1. Setting up the environment

    To install Grain on your local system, follow the instructions given here, and to install Wasmtime to run our .wasm file, you can take a look at their website.

  2. Code

    Open your preferred text editor and make a file with .gr extension. Grain recommends VS Code to work with .gr files. Installation of grain extension for VS code is highly recommended.

    The Fibonacci code in Grain is as follows :

    let rec fibonacci = (n) => {
      if (n == 0 || n == 1) {
        n
      } else {
        fibonacci(n - 1) + fibonacci(n - 2)
      }
    }
    
    print(fibonacci(7))
    

    We need to mention that a function is recursive with the keyword recin Grain.

  3. Compiling the code

    To compile your Grain code, simply run :

    grain fibonacci.gr
    

    This would print 13 on your terminal and generate a fibonacci.gr.wasm file.

  4. Running .wasm file in Wasmtime

    To run our .wasm file in wasmtime, run the following command

    wasmtime fibonacci.gr.wasm