From 869c5d094b54ae481228ad1777598fed3728fe2e Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Tue, 5 Oct 2021 13:39:51 +0200 Subject: [PATCH] Implement categoryProbability, functionProbability, functionIsConstructor, functionsByCategory --- src/runtime/javascript/index.ts | 45 ++++++++++++++++++++++ src/runtime/javascript/tests/basic.test.ts | 45 ++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/runtime/javascript/index.ts b/src/runtime/javascript/index.ts index 702b666c3..724ea5963 100644 --- a/src/runtime/javascript/index.ts +++ b/src/runtime/javascript/index.ts @@ -204,6 +204,51 @@ export class PGFGrammar { return funs } + categoryProbability (cat: string): number | undefined { + const catname = PgfText_FromString(cat) + const err = new PgfExn + const prob = runtime.pgf_category_prob(this.db, this.revision.deref(), catname, err.ref()) + handleError(err) + if (prob === Infinity) { + return undefined + } else { + return prob + } + } + + functionProbability (fun: string): number | undefined { + const funname = PgfText_FromString(fun) + const err = new PgfExn + const prob = runtime.pgf_function_prob(this.db, this.revision.deref(), funname, err.ref()) + handleError(err) + if (prob === Infinity) { + return undefined + } else { + return prob + } + } + + functionIsConstructor (fun: string): boolean { + const funname = PgfText_FromString(fun) + const err = new PgfExn + const isCon = runtime.pgf_function_is_constructor(this.db, this.revision.deref(), funname, err.ref()) + handleError(err) + return Boolean(isCon) + } + + functionsByCategory (cat: string): string[] { + const catname = PgfText_FromString(cat) + const funs: string[] = [] + const callback = ffi.Callback(ref.types.void, [ PgfItorPtr, PgfTextPtr, voidPtr, PgfExnPtr ], + function (self: Pointer, key: Pointer, value: Pointer, err: Pointer) { // eslint-disable-line @typescript-eslint/no-unused-vars + const k = PgfText_AsString(key) + funs.push(k) + }) + const err = new PgfExn + runtime.pgf_iter_functions_by_cat(this.db, this.revision.deref(), catname, callback.ref() as Pointer, err.ref()) + handleError(err) + return funs + } } // ---------------------------------------------------------------------------- diff --git a/src/runtime/javascript/tests/basic.test.ts b/src/runtime/javascript/tests/basic.test.ts index f975b1d24..497419ede 100644 --- a/src/runtime/javascript/tests/basic.test.ts +++ b/src/runtime/javascript/tests/basic.test.ts @@ -160,4 +160,49 @@ describe('abstract syntax', () => { test('functions', () => { expect(gr.getFunctions()).toEqual(['c','ind','s','z']) }) + + describe('function is constructor', () => { + test('s', () => { + expect(gr.functionIsConstructor('s')).toBe(true) + }) + test('z', () => { + expect(gr.functionIsConstructor('z')).toBe(true) + }) + test('c', () => { + expect(gr.functionIsConstructor('c')).toBe(true) + }) + test('ind', () => { + expect(gr.functionIsConstructor('ind')).toBe(false) + }) + }) + + describe('functions by category', () => { + test('N', () => { + expect(gr.functionsByCategory('N')).toEqual(['s','z']) + }) + test('S', () => { + expect(gr.functionsByCategory('S')).toEqual(['c']) + }) + test('X', () => { + expect(gr.functionsByCategory('X')).toEqual([]) + }) + }) + + describe('probabilities', () => { + test.skip('category existing', () => { + expect(gr.categoryProbability('S')).toBeDefined() + }) + + test('category non-existent', () => { + expect(gr.categoryProbability('NP')).toBeUndefined() + }) + + test('function existing', () => { + expect(gr.functionProbability('c')).toBeDefined() // returns -0 (!) + }) + + test('function non-existent', () => { + expect(gr.functionProbability('mkC')).toBeUndefined() + }) + }) })