Skip to content

Commit 9b188f1

Browse files
committed
add nodejs examples
1 parent 384ec86 commit 9b188f1

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Interpreter } from "../../dist/miniscript-ts.mjs"
2+
3+
const interp = new Interpreter();
4+
let code = `print "Hello " * 3
5+
for i in range(1,10); print i; end for`;
6+
interp.runSrcCode(code);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Interpreter } from "../../dist/miniscript-ts.mjs"
2+
3+
const interp = new Interpreter();
4+
let code = `print "Defining function ..."
5+
sumTwo = function(a,b)
6+
return a + b
7+
end function`;
8+
interp.runSrcCode(code);
9+
// Now let's _use_ the function
10+
code = `print sumTwo(3,2)`;
11+
// Run new code, but in the same context
12+
interp.runSrcCode(code);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Interpreter } from "../../dist/miniscript-ts.mjs"
2+
3+
const interp = new Interpreter();
4+
let code = `s = "Hello " * 3`;
5+
interp.runSrcCode(code);
6+
// Access the globalContext property
7+
const context = interp.globalContext;
8+
// Ask for a value by key. Result might be _undefined_.
9+
const value = context.getOpt("s");
10+
if (value !== undefined) {
11+
console.log("We got from the global context that 's' is:", value);
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Interpreter } from "../../dist/miniscript-ts.mjs"
2+
3+
const interp = new Interpreter();
4+
// Use a MiniScript function signature. Default values are supported.
5+
// Then provide a JavaScript anonymous function.
6+
interp.addIntrinsic("mySum(a=0,b=0)", (a, b) => {
7+
return a + b;
8+
});
9+
// Let's call the intrinsic
10+
let code = `
11+
print mySum(8,4)
12+
print mySum(2)`;
13+
interp.runSrcCode(code);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Interpreter, newMap } from "../../dist/miniscript-ts.mjs"
2+
3+
const interp = new Interpreter();
4+
// A typical pattern is to have a map which represents either a "super-type"
5+
// or a singleton object. In order for this to be accessible one needs to
6+
// define in turn an intrinsic function that returns such value.
7+
8+
// First we need the map / object.
9+
// The module exposes a `newMap` function for that!
10+
const myType = newMap();
11+
12+
// Then we create the intrinsic function to get ahold of it.
13+
interp.addIntrinsic("MyType", () => {
14+
return myType;
15+
});
16+
17+
// At this point you can do `new MyType` on MiniScript.
18+
19+
// Now let's augment the type with an intrinsic map-function.
20+
interp.addMapIntrinsic(myType, "someMethod", () => {
21+
console.log("Method called!");
22+
});
23+
24+
// Let's use what we have
25+
let code = `
26+
m = new MyType
27+
m.someMethod`;
28+
interp.runSrcCode(code);

examples/nodejs/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# Examples for Node.js
3+
4+
At the time of this writing MiniScript.TS is not yet published as an npm package.
5+
6+
In order to be able to load the examples:
7+
8+
* Build the project using node.js locally
9+
* Execute the example from the command line (e.g. `nodejs myExample.mjs`, replace with `node` if it applies to you).

0 commit comments

Comments
 (0)