From b4f1cedc20514ffe4d3473b4dba9ae70988d1e5c Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 17 May 2024 20:44:44 +0200 Subject: [PATCH] update readme after making it sync --- packages/hs2js/README.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/hs2js/README.md b/packages/hs2js/README.md index f4059ff0..24d9c774 100644 --- a/packages/hs2js/README.md +++ b/packages/hs2js/README.md @@ -15,10 +15,11 @@ You can load the library directly from a script tag via unpkg: ``` @@ -66,11 +67,11 @@ Example: ```js // simple -hs2js.evaluate(`2 + 2`).then(console.log) // log 4 +hs2js.evaluate(`2 + 2`) // = 4 // 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 -hs2js.evaluate(`2 |* 3`, {}, { '|*': (l, r) => l * r }).then(console.log); // logs 6 +hs2js.evaluate(`2 |* 3`, {}, { '|*': (l, r) => l * r }) // = 6 ``` ### parse @@ -80,7 +81,8 @@ hs2js.evaluate(`2 |* 3`, {}, { '|*': (l, r) => l * r }).then(console.log); // lo Example: ```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)))))) ``` @@ -95,21 +97,18 @@ Evaluates `rootNode` of haskell AST (used by evaluate internally). Example: ```js -async function main() { - const ast = await hs2js.parse(`2 + 3`); - const res = hs2js.run(ast.rootNode); - console.log(res); -} -main(); // logs 5 +const ast = hs2js.parse(`2 + 3`); +const res = hs2js.run(ast.rootNode); +console.log(res); // = 5 ``` ### loadParser 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 -hs2js.loadParser().then(() => console.log('parser loaded')) +hs2js.loadParser().then(() => hs2js.evaluate('alert "ready"')) ``` ### setBase