branch-lang
v0.0.3
Published
Branch ======
Downloads
0
Readme
Branch
Branch is a state-based language.
Installation:
- via npm:
npm install branch-lang
Usage:
- Include the javascript file from dist/js in the html file
<script type="text/javascript" src="node_modules/branch-lang/dist/js/branch-lang.js"></script>
- Initialize an engine with a url to the initial branch file
new BranchLangEngine(entryPointUrl, opts);
Params:
- entryPointUrl: url to the initial branch file
- opts:
- output: method used to output text
- after: method run after input is processed (see below)
- miss: method run after input is processed when the input misses all the matches
Example:
var BranchEngine = new BranchLangEngine('/branches/main.branch', {
output: console.log,
after: function() {
console.log('END');
},
miss: function(input) {
console.log('miss on input ' + input);
}
});
Language
Each branch contains a list of events.
enter
and exit
blocks are run when the engine changes to and from the branch respectively.
The paths
block maps input matches (regular expressions or strings) to instruction blocks when the engine processes input.
Instructions:
- branch: change to new branch. Parameter can be a branch, or a url to a branch file
- print: output using method specified by the engine
Example:
{
enter: {
print ("You are at a fork in the road."),
print ('It reads: "pond left, forest right", what do you do?')
},
paths: {
/^(go )?left$/gi: {
print ("You go left."),
branch ({
enter: {
print ("The water is still")
},
paths: {
"back": {
branch ("/branches/main.branch")
}
},
exit: {
print ("You go back to the fork in the road.")
}
})
},
/^(go )?right$/gi: {
print ("You go right."),
branch ("/branches/forest.branch")
}
}
}