Skip to content

Commit

Permalink
Removed concept of wildcard.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Sep 19, 2023
1 parent 70a2848 commit 545b4f6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 42 deletions.
18 changes: 9 additions & 9 deletions src/basis/NumberBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default function bootstrapNumber(locales: Locale[]) {
createBinaryOp(
(locale) => locale.basis.Number.function.multiply,
// The operand's type can be any unitless measurement
NumberType.wildcard(),
NumberType.make(),
// The output's type is is the unit's product
NumberType.make((left, right) =>
right ? left.product(right) : left
Expand All @@ -273,7 +273,7 @@ export default function bootstrapNumber(locales: Locale[]) {
),
createBinaryOp(
(locale) => locale.basis.Number.function.divide,
NumberType.wildcard(),
NumberType.make(),
NumberType.make((left, right) =>
right ? left.quotient(right) : left
),
Expand All @@ -282,35 +282,35 @@ export default function bootstrapNumber(locales: Locale[]) {
),
createBinaryOp(
(locale) => locale.basis.Number.function.remainder,
NumberType.wildcard(),
NumberType.make(),
NumberType.make((left) => left),
(requestor, left, right) =>
left.remainder(requestor, right),
false
),
createUnaryOp(
(locale) => locale.basis.Number.function.roundDown,
NumberType.wildcard(),
NumberType.make(),
(requestor, left) => left.roundDown(requestor)
),
createUnaryOp(
(locale) => locale.basis.Number.function.roundUp,
NumberType.wildcard(),
NumberType.make(),
(requestor, left) => left.roundUp(requestor)
),
createUnaryOp(
(locale) => locale.basis.Number.function.positive,
NumberType.wildcard(),
NumberType.make(),
(requestor, left) => left.absolute(requestor)
),
createUnaryOp(
(locale) => locale.basis.Number.function.round,
NumberType.wildcard(),
NumberType.make(),
(requestor, left) => left.round(requestor)
),
createBinaryOp(
(locale) => locale.basis.Number.function.power,
NumberType.wildcard(),
NumberType.make(),
NumberType.make((left, right, constant) => {
right;
return constant === undefined
Expand All @@ -322,7 +322,7 @@ export default function bootstrapNumber(locales: Locale[]) {
),
createBinaryOp(
(locale) => locale.basis.Number.function.root,
NumberType.wildcard(),
NumberType.make(),
NumberType.make((left, right, constant) => {
right;
return constant === undefined
Expand Down
6 changes: 1 addition & 5 deletions src/input/Random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type Locale from '../locale/Locale';
import { createBasisFunction } from '../basis/Basis';
import type Evaluation from '../runtime/Evaluation';
import type Expression from '../nodes/Expression';
import Unit from '../nodes/Unit';
import NoneValue from '../values/NoneValue';
import TypeVariables from '../nodes/TypeVariables';
import TypeVariable from '../nodes/TypeVariable';
Expand Down Expand Up @@ -69,10 +68,7 @@ function toPrecisionRange(

export function createRandomFunction(locales: Locale[]) {
/** The type variable can be any number type, but nothing else */
const NumberTypeVariable = TypeVariable.make(
['Number'],
NumberType.make(Unit.Wildcard)
);
const NumberTypeVariable = TypeVariable.make(['Number'], NumberType.make());

return createBasisFunction(
locales,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/Is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import evaluateCode from '../runtime/evaluate';

test.each([
['1•#', '⊤'],
['1s•#', ''],
['1s•#', ''],
['1s•#s', '⊤'],
['1s•#m', '⊥'],
["'hi'•#", '⊥'],
Expand Down
12 changes: 3 additions & 9 deletions src/nodes/NumberType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,9 @@ export default class NumberType extends BasisType {
return [NumberType.make()];
}

static wildcard() {
return NumberType.make(Unit.Wildcard);
}

generalize() {
// Remove the wildcard if it is one.
return this.unit instanceof Unit && this.unit.isWildcard()
? NumberType.make()
: NumberType.make(this.unit);
// Remove the specific number, if there is one, but preserve the unit.
return NumberType.make(this.unit);
}

getGrammar(): Grammar {
Expand Down Expand Up @@ -130,7 +124,7 @@ export default class NumberType extends BasisType {

// If the units aren't compatible, then the the types aren't compatible.
if (
!(this.unit instanceof Function || this.unit.isWildcard()) &&
!(this.unit instanceof Function || this.unit.isUnitless()) &&
!thisUnit.accepts(thatUnit)
)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/UnionType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test.each([
["'hi'/en|'hello'/en", "''/en"],
["'hi'/en|'hi'/fr", "''/en|''/fr"],
['1|2|3', '#'],
['1m|2|3', '#m|#'],
['1m|2|3', '#'],
['1m|2m|3m', '#m'],
['[1|2|3]', '[#]'],
['{1}', '{#}'],
Expand Down
20 changes: 3 additions & 17 deletions src/nodes/Unit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
QUESTION_SYMBOL,
EXPONENT_SYMBOL,
LANGUAGE_SYMBOL,
} from '@parser/Symbols';
import { EXPONENT_SYMBOL, LANGUAGE_SYMBOL } from '@parser/Symbols';
import { PRODUCT_SYMBOL } from '@parser/Symbols';
import Dimension from './Dimension';
import Token from './Token';
Expand Down Expand Up @@ -153,11 +149,6 @@ export default class Unit extends Type {
}

static Empty = new Unit();
static Wildcard = (() => {
const exp = new Map();
exp.set(QUESTION_SYMBOL, 1);
return new Unit(exp);
})();

getGrammar(): Grammar {
return [
Expand Down Expand Up @@ -218,9 +209,6 @@ export default class Unit extends Type {
return newUnit;
}

isWildcard() {
return this.exponents.get('?') === 1;
}
isUnitless() {
return this.exponents.size === 0;
}
Expand Down Expand Up @@ -278,10 +266,8 @@ export default class Unit extends Type {
// And...
// The units are equal
(this.isEqualTo(unit) ||
// Or this is a wildcard
this.isWildcard() ||
// Or this is unitless and the unit is a wildcard
(this.isUnitless() && unit.isWildcard()))
// Or this is unitless
this.isUnitless())
);
}

Expand Down

0 comments on commit 545b4f6

Please sign in to comment.