Skip to content

Commit

Permalink
Support for ? symbol and questioned equal operator (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
swarty authored Jan 17, 2024
1 parent 9da1cf9 commit ba1aece
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {flatten, isOneOf, join, repeat, unique, words} from '@mathigon/core';
import {evaluate, interval, Interval} from './eval';
import {collapseTerm} from './parser';
import {BRACKETS, escape, isSpecialFunction, VOICE_STRINGS} from './symbols';
import {CustomFunction, ExprElement, ExprMap, ExprNumber, MathMLMap, VarMap} from './elements';
import {ExprElement, ExprMap, ExprNumber, MathMLMap, VarMap} from './elements';
import {ExprError} from './errors';


Expand Down Expand Up @@ -119,7 +119,7 @@ export class ExprFunction extends ExprElement {
if (this.fn === 'sub') return args.join('_');
if (this.fn === 'subsup') return `${args[0]}_${args[1]}^${args[2]}`;

if (words('+ * × · / = < > ≤ ≥ ≈').includes(this.fn)) {
if (words('+ * × · / = < > ≤ ≥ ≈ ≟ ≠').includes(this.fn)) {
return args.join(` ${this.fn} `);
}

Expand Down Expand Up @@ -149,7 +149,7 @@ export class ExprFunction extends ExprElement {
return argsF.length > 1 ? argsF.join('<mo value="−">−</mo>') : `<mo rspace="0" value="−">−</mo>${argsF[0]}`;
}

if (isOneOf(this.fn, '+', '=', '<', '>', '≤', '≥', '≈')) {
if (isOneOf(this.fn, '+', '=', '<', '>', '≤', '≥', '≈', '≟', '≠')) {
const fn = escape(this.fn);
return argsF.join(`<mo value="${fn}">${fn}</mo>`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export function collapseTerm(tokens: ExprElement[]): ExprElement {
if (!tokens.length) throw ExprError.invalidExpression();

// Match comparison operators first
const comp = tokens.findIndex(t => isOperator(t, '= < > ≤ ≥'));
const comp = tokens.findIndex(t => isOperator(t, '= < > ≤ ≥ ≟ ≠'));
if (comp === 0) throw ExprError.startOperator(tokens[0]);
if (comp === tokens.length - 1) throw ExprError.endOperator(tokens[0]);
if (comp > 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const SPECIAL_OPERATORS: Obj<string> = {
'\'': '’',

'!=': '≠',
'?=': '≟',
'<=': '≤',
'>=': '≥',
'in': '∈',
Expand Down Expand Up @@ -112,7 +113,7 @@ const UPPERCASE = ALPHABET.toUpperCase().split('');
const GREEK = Object.values(SPECIAL_IDENTIFIERS);
export const IDENTIFIER_SYMBOLS = [...LOWERCASE, ...UPPERCASE, ...GREEK, '$'];

const SIMPLE_SYMBOLS = '|()[]{}÷,!<>=*/+-–−~^_…°•∥⊥\'∠:%∼△';
const SIMPLE_SYMBOLS = '|()[]{}÷,!?<>=*/+-–−~^_…°•∥⊥\'∠:%∼△';
const COMPLEX_SYMBOLS = Object.values(SPECIAL_OPERATORS);
export const OPERATOR_SYMBOLS = [...SIMPLE_SYMBOLS, ...COMPLEX_SYMBOLS];

Expand Down Expand Up @@ -154,6 +155,7 @@ export const VOICE_STRINGS: Obj<string> = {
'±': 'plus-minus',
'=': 'equals',
'≠': 'does not equal',
'≟': 'is it equal?',
'<': 'is less than',
'>': 'is greater than',
'≤': 'is less than or equal to',
Expand Down
1 change: 1 addition & 0 deletions test/parsing-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ tape('symbols', (test) => {
test.equal(str('x cup y'), 'x ∪ y');
test.equal(str('x ∩ y'), 'x ∩ y');
test.equal(str('a != b'), 'a ≠ b');
test.equal(str('a ?= b'), 'a ≟ b');
test.end();
});

Expand Down

0 comments on commit ba1aece

Please sign in to comment.