Skip to content

Commit

Permalink
fix to missing RiTa issue in grammar transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Jan 20, 2024
1 parent 588946f commit 6d9b7d1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class RiGrammar {
* Creates an instance of RiGrammar.
* @param {Object<string, string>|string} [rules] - an object (or JSON string) containing the rules
* @param {Object<string, any>} [context] - the context (or world-state)
* @param {Object<string, any>} [options] - options for the grammar
*/
constructor(rules = {}, context = {}) {
constructor(rules = {}, context = {}, options = {}) {

/** @type {Object<string, string>} */ this.rules = {};
/** @type {RiScript} */ this.scripting = new RiScript();
/** @type {Object<string, any>} */ this.context = context;
/** @type {RiScript} */ this.scripting = new RiScript(options);


if (typeof rules === 'string') {
rules = parseJSON(rules);
Expand Down Expand Up @@ -57,6 +59,7 @@ class RiGrammar {
this.scripting.addTransform(name, def);
return this;
}

/**
* Removes a transform from the Grammar instance
* @param {string} name
Expand Down
18 changes: 15 additions & 3 deletions src/riscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ class RiScript {
* Create a RiTa grammar instance
* @param {object} [rules] - the rules of the grammar
* @param {object} [context] - the context of the grammar
* @param {object} [options] - options for the evaluation
* @returns {RiGrammar} - a new RiGrammar instance
*/
static grammar(rules, context) {
return new RiGrammar(rules, context);
static grammar(rules, context, options) {
return new RiGrammar(rules, context, options);
}

/**
Expand Down Expand Up @@ -115,7 +116,7 @@ class RiScript {

/** @type {Object<string, any>} */ this.RiTa = options.RiTa || {
VERSION: 0,
randi: (k) => Math.floor(Math.random() * k),
randi: (k) => Math.floor(Math.random() * k)
}

/** @type {Object.<string, Function>} */
Expand Down Expand Up @@ -569,6 +570,17 @@ class RiScript {
}
}

// let _randi = (a,b) => {
// let crand = Math.random();
// if (typeof a === ' ') return crand;
// if (Array.isArray(arguments[0])) {
// let arr = arguments[0];
// return arr[Math.floor(crand * arr.length)];
// }
// return arguments.length === 1 ? crand * arguments[0] :
// crand * (arguments[1] - arguments[0]) + arguments[0];
// }

// Class ref hack for testing
RiScript.Visitor = RiScriptVisitor;
RiScript.Util = Util;
Expand Down
1 change: 1 addition & 0 deletions test/riscript.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe(title, function () {
LTR && describe('OneOff', function () {
it('Be a single problematic test', function () { });
});

describe('Markdown', function () {
it('Handle basic markdown', function () {
let test = 'Some *italic* and **bold** and _other_ markdown';
Expand Down
12 changes: 12 additions & 0 deletions test/targets/grammar-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RiScript, RiGrammar } from '../../dist/riscript.js';

console.log('RiScript v' + RiScript.VERSION + '\n\n');

let rg = new RiGrammar();
rg.addTransform('rhyme', () => "log");

rg.setRules({
start: 'dog rhymes with [dog].rhyme()'
});
//, { rhyme }
console.log(rg.expand());

0 comments on commit 6d9b7d1

Please sign in to comment.