Skip to content

Commit

Permalink
Fixed selection of locale in evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Jul 13, 2024
1 parent 5904dce commit a8f6e30
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:
- [#514](https://github.com/wordplaydev/wordplay/issues/514) Fixed cursor position on hidden language tags.
- Fixed parsing bug that prevented complete parsing of the program.
- Fixed reactivity dependency bug that included evaluates in branch dependencies.
- Fixed selection of locale in evaluation.

## 0.10.4 2024-07-08

Expand Down
2 changes: 1 addition & 1 deletion src/components/app/PlayView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let evaluator: Evaluator;
let latest: Value | undefined = undefined;
$: {
evaluator = new Evaluator(project, DB, $locales);
evaluator = new Evaluator(project, DB, $locales.getLocales());
if (evaluator) {
evaluator.stop();
evaluator.ignore(update);
Expand Down
7 changes: 6 additions & 1 deletion src/components/app/ProjectPreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
$: path = link ?? project.getLink(true);
function updatePreview() {
const evaluator = new Evaluator(project, DB, $locales, false);
const evaluator = new Evaluator(
project,
DB,
$locales.getLocales(),
false,
);
const value = evaluator.getInitialValue();
evaluator.stop();
const stage = value ? toStage(evaluator, value) : undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/components/concepts/ExampleUI.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
if (evaluator) evaluator.ignore(update);
if (evaluated) {
evaluator = new Evaluator(project, DB, $locales);
evaluator = new Evaluator(project, DB, $locales.getLocales());
evaluator.observe(update);
evaluator.start();
} else {
Expand Down
11 changes: 9 additions & 2 deletions src/components/project/ProjectView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@
import { AnimationFactorIcons } from '@db/AnimationFactorSetting';
import { COPY_SYMBOL } from '@parser/Symbols';
import CopyButton from './CopyButton.svelte';
import { toLocaleString } from '@locale/Locale';
import { toLocaleString, type Locale } from '@locale/Locale';
import { default as ModeChooser } from '@components/widgets/Mode.svelte';
import DefaultLocale from '@locale/DefaultLocale';
export let project: Project;
export let original: Project | undefined = undefined;
Expand Down Expand Up @@ -227,6 +228,9 @@
});
setContext(KeyModfifierSymbol, keyModifiers);
/** Keep a currently selected output locale to send to the Evaluator for evaluation and rendering */
let evaluationLocale: Locale | undefined;
// When keyboard edit idle changes to true, set a timeout
// to reset it to false after a delay.
$: {
Expand Down Expand Up @@ -324,7 +328,10 @@
const newEvaluator = new Evaluator(
newProject,
DB,
newProject.getLocales(),
// Choose the selected evaluation locale or if not selected, currently selected IDE locale
evaluationLocale
? [evaluationLocale, DefaultLocale]
: $locales.getLocales(),
true,
replayInputs ? $evaluator : undefined,
);
Expand Down
2 changes: 1 addition & 1 deletion src/edit/OutputExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class OutputExpression {
expression instanceof Expression ? expression : undefined,
value:
expression instanceof Literal
? expression.getValue(this.locales)
? expression.getValue(this.locales.getLocales())
: undefined,
};
}
Expand Down
42 changes: 21 additions & 21 deletions src/edit/OutputProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getPoseProperty(project: Project, name: NameAndDoc): OutputProperty {
expr instanceof Evaluate &&
(expr.is(project.shares.output.Pose, context) ||
expr.is(project.shares.output.Sequence, context)),
(locales) => createPoseLiteral(project, locales)
(locales) => createPoseLiteral(project, locales),
);
}

Expand All @@ -38,7 +38,7 @@ export function getDurationProperty(locales: Locales): OutputProperty {
false,
false,
(expr) => expr instanceof NumberLiteral,
() => NumberLiteral.make(0.25, Unit.create(['s']))
() => NumberLiteral.make(0.25, Unit.create(['s'])),
);
}

Expand All @@ -51,26 +51,26 @@ export function getStyleProperty(locales: Locales): OutputProperty {
...all,
...(Array.isArray(next) ? next : [next]),
],
[]
[],
),
true,
(text: string) => TextLiteral.make(text),
(expression: Expression | undefined) =>
expression instanceof TextLiteral
? expression.getValue(locales).text
: undefined
? expression.getValue(locales.getLocales()).text
: undefined,
),
false,
false,
(expr) => expr instanceof TextLiteral,
() => TextLiteral.make(DefaultStyle)
() => TextLiteral.make(DefaultStyle),
);
}

// All type output has these properties.
export function getTypeOutputProperties(
project: Project,
locales: Locales
locales: Locales,
): OutputProperty[] {
return [
new OutputProperty(
Expand All @@ -79,7 +79,7 @@ export function getTypeOutputProperties(
false,
true,
(expr) => expr instanceof NumberLiteral,
() => NumberLiteral.make(1, Unit.meters())
() => NumberLiteral.make(1, Unit.meters()),
),
new OutputProperty(
locales.get((l) => l.output.Phrase.face),
Expand All @@ -89,13 +89,13 @@ export function getTypeOutputProperties(
(text: string) => TextLiteral.make(text),
(expression: Expression | undefined) =>
expression instanceof TextLiteral
? expression.getValue(locales).text
: undefined
? expression.getValue(locales.getLocales()).text
: undefined,
),
false,
true,
(expr) => expr instanceof TextLiteral,
() => TextLiteral.make(locales.get((l) => l.ui.font.app))
() => TextLiteral.make(locales.get((l) => l.ui.font.app)),
),
new OutputProperty(
locales.get((l) => l.output.Phrase.place),
Expand All @@ -111,14 +111,14 @@ export function getTypeOutputProperties(
Evaluate.make(
Reference.make(
locales.getName(project.shares.output.Place.names),
project.shares.output.Place
project.shares.output.Place,
),
[
NumberLiteral.make(0, Unit.meters()),
NumberLiteral.make(0, Unit.meters()),
NumberLiteral.make(0, Unit.meters()),
]
)
],
),
),
...getOutputProperties(project, locales),
];
Expand All @@ -128,7 +128,7 @@ export function getTypeOutputProperties(
// All type output has these properties, in this order.
export function getOutputProperties(
project: Project,
locales: Locales
locales: Locales,
): OutputProperty[] {
return [
new OutputProperty(
Expand All @@ -137,32 +137,32 @@ export function getOutputProperties(
false,
false,
(expr) => expr instanceof TextLiteral,
() => TextLiteral.make('')
() => TextLiteral.make(''),
),
new OutputProperty(
locales.get((l) => l.output.Phrase.selectable),
'bool',
false,
false,
(expr) => expr instanceof BooleanLiteral,
() => BooleanLiteral.make(false)
() => BooleanLiteral.make(false),
),
...getPoseProperties(project, locales, true),
getPoseProperty(
project,
locales.get((l) => l.output.Phrase.entering)
locales.get((l) => l.output.Phrase.entering),
),
getPoseProperty(
project,
locales.get((l) => l.output.Phrase.resting)
locales.get((l) => l.output.Phrase.resting),
),
getPoseProperty(
project,
locales.get((l) => l.output.Phrase.moving)
locales.get((l) => l.output.Phrase.moving),
),
getPoseProperty(
project,
locales.get((l) => l.output.Phrase.exiting)
locales.get((l) => l.output.Phrase.exiting),
),
getDurationProperty(locales),
getStyleProperty(locales),
Expand Down
7 changes: 6 additions & 1 deletion src/examples/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ test.each([...projects])(
`Ensure $name doesn't evaluate to exception`,
async (example: SerializedProject) => {
const project = await Project.deserialize(Locales, example);
const evaluator = new Evaluator(project, DB, DefaultLocales, false);
const evaluator = new Evaluator(
project,
DB,
DefaultLocales.getLocales(),
false,
);
const value = evaluator.getInitialValue();
evaluator.stop();
expect(value).not.toBeInstanceOf(ExceptionValue);
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/FormattedLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export default class FormattedLiteral extends Literal {
return Glyphs.Formatted;
}

getValue(locales: Locales): Value {
const preferred = this.getPreferredText(locales.getLocales());
getValue(locales: Locale[]): Value {
const preferred = this.getPreferredText(locales);
return new MarkupValue(this, preferred.markup);
}

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SimpleExpression from './SimpleExpression';
import type Expression from './Expression';
import Purpose from '../concepts/Purpose';
import type Context from './Context';
import type Locales from '../locale/Locales';
import type Locale from '@locale/Locale';

export default abstract class Literal extends SimpleExpression {
constructor() {
Expand All @@ -29,8 +29,8 @@ export default abstract class Literal extends SimpleExpression {
evaluate(evaluator: Evaluator, prior: Value | undefined): Value {
if (prior) return prior;

return this.getValue(evaluator.locales);
return this.getValue(evaluator.getLocales());
}

abstract getValue(locales: Locales): Value;
abstract getValue(locales: Locale[]): Value;
}
13 changes: 5 additions & 8 deletions src/nodes/Program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Project from '../models/Project';
import type Value from '../values/Value';
import { DB } from '../db/Database';
import DefaultLocale from '../locale/DefaultLocale';
import Locales from '../locale/Locales';

test.each([
// A single source with 1 should evaluate to 1
Expand All @@ -23,14 +22,12 @@ test.each([
code
.slice(1)
.map((code, index) => new Source(`sup${index + 1}`, code)),
DefaultLocale
DefaultLocale,
);
const value = new Evaluator(
project,
DB,
new Locales([DefaultLocale], DefaultLocale)
).getInitialValue();
const value = new Evaluator(project, DB, [
DefaultLocale,
]).getInitialValue();
expect(value).toBeDefined();
expect((value as Value).constructor).toBe(valueType);
}
},
);
11 changes: 3 additions & 8 deletions src/nodes/Reaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Reaction from './Reaction';
import ExpectedStream from '../conflicts/ExpectedStream';
import { DB } from '../db/Database';
import DefaultLocale from '../locale/DefaultLocale';
import Locales from '../locale/Locales';

const makeOne = (creator: Expression) => Time.make(creator, 1);

Expand Down Expand Up @@ -46,16 +45,12 @@ test.each([
code: string,
value: (expression: Expression) => Value,
expectedInitial: string,
expectedNext: string
expectedNext: string,
) => {
// Make the project
const source = new Source('test', code);
const project = Project.make(null, 'test', source, [], DefaultLocale);
const evaluator = new Evaluator(
project,
DB,
new Locales([DefaultLocale], DefaultLocale)
);
const evaluator = new Evaluator(project, DB, [DefaultLocale]);

evaluator.start();

Expand All @@ -75,7 +70,7 @@ test.each([
const actualNext = evaluator.getLatestSourceValue(source);
expect(actualNext?.toString()).toBe(expectedNext);
evaluator.stop();
}
},
);

testConflict('1 … ∆ Time() … 1 + .', '1 … ⊤ … 1 + .', Reaction, ExpectedStream);
4 changes: 2 additions & 2 deletions src/nodes/TextLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ export default class TextLiteral extends Literal {
: getPreferred(locales, this.texts);
}

getValue(locales: Locales): TextValue {
getValue(locales: Locale[]): TextValue {
// Get the alternatives
const best = this.getLocaleText(locales.getLocales());
const best = this.getLocaleText(locales);
return new TextValue(
this,
best.getText(),
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/Evaluator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test.each([0, 1, 10, 15])('Step back %i', (steps: number) => {

const source = new Source('test', fib);
const project = Project.make(null, 'test', source, [], DefaultLocale);
const evaluator = new Evaluator(project, DB, DefaultLocales);
const evaluator = new Evaluator(project, DB, DefaultLocales.getLocales());
evaluator.start();
const stepIndex = evaluator.getStepIndex();

Expand All @@ -38,7 +38,7 @@ test('Too many steps', () => {

const source = new Source('test', fib);
const project = Project.make(null, 'test', source, [], DefaultLocale);
const evaluator = new Evaluator(project, DB, DefaultLocales);
const evaluator = new Evaluator(project, DB, DefaultLocales.getLocales());
const value = evaluator.getInitialValue();
expect(value).toBeInstanceOf(StepLimitException);
});
Expand All @@ -51,7 +51,7 @@ test('Too many evaluations', () => {

const source = new Source('test', fib);
const project = Project.make(null, 'test', source, [], DefaultLocale);
const evaluator = new Evaluator(project, DB, DefaultLocales);
const evaluator = new Evaluator(project, DB, DefaultLocales.getLocales());
const value = evaluator.getInitialValue();
expect(value).toBeInstanceOf(EvaluationLimitException);
});
Loading

0 comments on commit a8f6e30

Please sign in to comment.