update readme after making it sync

This commit is contained in:
Felix Roos 2024-05-17 20:44:44 +02:00
parent 5b89500acd
commit b4f1cedc20

View File

@ -15,10 +15,11 @@ You can load the library directly from a script tag via unpkg:
<button id="hello">hello</button> <button id="hello">hello</button>
<script> <script>
hs2js.setBase('https://unpkg.com/hs2js@0.0.3/dist/'); hs2js.setBase('https://unpkg.com/hs2js@0.0.3/dist/');
hs2js.loadParser(); hs2js.loadParser().then(()=>{
document.getElementById('hello').addEventListener('click', () => { document.getElementById('hello').addEventListener('click', () => {
hs2js.evaluate('alert "hello from haskell!"'); hs2js.evaluate('alert "hello from haskell!"');
}); });
})
</script> </script>
``` ```
@ -66,11 +67,11 @@ Example:
```js ```js
// simple // simple
hs2js.evaluate(`2 + 2`).then(console.log) // log 4 hs2js.evaluate(`2 + 2`) // = 4
// passing variables via scope: // passing variables via scope:
hs2js.evaluate(`a + b`, { a: 1, b: 2 }).then(console.log); // logs 3 hs2js.evaluate(`a + b`, { a: 1, b: 2 }) // = 3
// custom operator // custom operator
hs2js.evaluate(`2 |* 3`, {}, { '|*': (l, r) => l * r }).then(console.log); // logs 6 hs2js.evaluate(`2 |* 3`, {}, { '|*': (l, r) => l * r }) // = 6
``` ```
### parse ### parse
@ -80,7 +81,8 @@ hs2js.evaluate(`2 |* 3`, {}, { '|*': (l, r) => l * r }).then(console.log); // lo
Example: Example:
```js ```js
hs2js.parse(`2 + 2`).then(ast=>console.log(ast.toString())) const ast = hs2js.parse(`2 + 2`)
console.log(ast.toString())
// (haskell declarations: (declarations (top_splice (apply function: (variable) argument: (literal (integer)))))) // (haskell declarations: (declarations (top_splice (apply function: (variable) argument: (literal (integer))))))
``` ```
@ -95,21 +97,18 @@ Evaluates `rootNode` of haskell AST (used by evaluate internally).
Example: Example:
```js ```js
async function main() { const ast = hs2js.parse(`2 + 3`);
const ast = await hs2js.parse(`2 + 3`); const res = hs2js.run(ast.rootNode);
const res = hs2js.run(ast.rootNode); console.log(res); // = 5
console.log(res);
}
main(); // logs 5
``` ```
### loadParser ### loadParser
Loads and caches the parser by fetching `tree-sitter.wasm` and `tree-sitter-haskell.wasm`. Loads and caches the parser by fetching `tree-sitter.wasm` and `tree-sitter-haskell.wasm`.
This function is called internally by `parse` but calling it yourself might make the parser be ready faster, depending on your application. Make sure to call and await this function before calling `parse` or `evaluate`.
```js ```js
hs2js.loadParser().then(() => console.log('parser loaded')) hs2js.loadParser().then(() => hs2js.evaluate('alert "ready"'))
``` ```
### setBase ### setBase