Skip to content

Commit

Permalink
[alg] Add a way to serialize to string using LGN.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Oct 7, 2024
1 parent 8363c40 commit 26f0d38
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 64 deletions.
9 changes: 6 additions & 3 deletions src/cubing/alg/Alg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AlgCommon, type Comparable } from "./common";
import { experimentalIs, experimentalIsAlgNode } from "./is";
import { IterationDirection, direct, reverse } from "./iteration";
import { parseAlg } from "./parseAlg";
import type { ExperimentalSerializationOptions } from "./SerializationOptions";
import { simplify, type SimplifyOptions } from "./simplify";
import { warnOnce } from "./warnOnce";

Expand Down Expand Up @@ -247,7 +248,9 @@ export class Alg extends AlgCommon<Alg> {
* // R U2 L
* console.log(alg.toString())
*/
toString(): string {
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
let output = "";
let previousVisibleAlgNode: AlgNode | null = null;
for (const algNode of this.#algNodes) {
Expand All @@ -259,11 +262,11 @@ export class Alg extends AlgCommon<Alg> {
if (nissGrouping.amount !== -1) {
throw new Error("Invalid NISS Grouping amount!");
}
output += `^(${nissGrouping.alg.toString()})`;
output += `^(${nissGrouping.alg.toString(experimentalSerializationOptions)})`;
} else if (algNode.as(Grouping)?.experimentalNISSPlaceholder) {
// do not serialize (rely on the placeholder instead)
} else {
output += algNode.toString();
output += algNode.toString(experimentalSerializationOptions);
}
previousVisibleAlgNode = algNode;
}
Expand Down
22 changes: 0 additions & 22 deletions src/cubing/alg/Serializable.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/cubing/alg/SerializationOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type notation = "auto" | "LGN";

export interface ExperimentalSerializationOptions {
// TODO: this will still serialize caret NISS notation as normal.
notation?: notation;
}
7 changes: 5 additions & 2 deletions src/cubing/alg/alg-nodes/containers/Commutator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import {
experimentalEnsureAlg,
type Alg,
Expand Down Expand Up @@ -84,7 +85,9 @@ export class Commutator extends AlgCommon<Commutator> {
}
}

toString(): string {
return `[${this.#A.toString()}, ${this.#B.toString()}]`;
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
return `[${this.#A.toString(experimentalSerializationOptions)}, ${this.#B.toString(experimentalSerializationOptions)}]`;
}
}
7 changes: 5 additions & 2 deletions src/cubing/alg/alg-nodes/containers/Conjugate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import {
experimentalEnsureAlg,
type Alg,
Expand Down Expand Up @@ -52,7 +53,9 @@ export class Conjugate extends AlgCommon<Conjugate> {
}
}

toString(): string {
return `[${this.A}: ${this.B}]`;
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
return `[${this.A.toString(experimentalSerializationOptions)}: ${this.B.toString(experimentalSerializationOptions)}]`;
}
}
29 changes: 23 additions & 6 deletions src/cubing/alg/alg-nodes/containers/Grouping.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import { Commutator, Conjugate } from "..";
import { Alg, experimentalEnsureAlg, type FlexibleAlgSource } from "../../Alg";
import { AlgCommon, type Comparable } from "../../common";
Expand All @@ -13,7 +14,14 @@ class Square1TupleFormatter {
quantumU_SQ_: QuantumMove | null = null;
quantumD_SQ_: QuantumMove | null = null;

format(grouping: Grouping): string | null {
format(
grouping: Grouping,
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string | null {
if (experimentalSerializationOptions?.notation === "LGN") {
return null;
}

if (grouping.amount !== 1) {
return null;
}
Expand Down Expand Up @@ -107,8 +115,12 @@ export class Grouping extends AlgCommon<Grouping> {
throw new Error("unimplemented");
}

#unrepeatedString(): string | null {
const insideString = this.#quantumWithAmount.quantum.toString();
#unrepeatedString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string | null {
const insideString = this.#quantumWithAmount.quantum.toString(
experimentalSerializationOptions,
);
const iter = this.alg.childAlgNodes();
const { value } = iter.next() as {
value: AlgNode;
Expand All @@ -120,10 +132,15 @@ export class Grouping extends AlgCommon<Grouping> {
return `(${insideString})`;
}

toString(): string {
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
return (
square1TupleFormatterInstance.format(this) ??
`${this.#unrepeatedString()}${this.#quantumWithAmount.suffix()}`
square1TupleFormatterInstance.format(
this,
experimentalSerializationOptions,
) ??
`${this.#unrepeatedString(experimentalSerializationOptions)}${this.#quantumWithAmount.suffix()}`
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/cubing/alg/alg-nodes/leaves/LineComment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import { AlgCommon, type Comparable } from "../../common";
import { IterationDirection } from "../../iteration";
import type { AlgLeaf } from "../AlgNode";
Expand Down Expand Up @@ -36,7 +37,9 @@ export class LineComment extends AlgCommon<LineComment> {
yield this;
}

toString(): string {
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
return `//${this.#text}`;
}
// toJSON(): LineCommentJSON {
Expand Down
63 changes: 39 additions & 24 deletions src/cubing/alg/alg-nodes/leaves/Move.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import { AlgCommon, Comparable } from "../../common";
import { IterationDirection } from "../../iteration";
import { MAX_INT, MAX_INT_DESCRIPTION } from "../../limits";
import { parseMove, parseQuantumMove, transferCharIndex } from "../../parseAlg";
import { warnOnce } from "../../warnOnce";
import { QuantumWithAmount } from "../QuantumWithAmount";
import type { AlgLeaf } from "../AlgNode";
import { QuantumWithAmount } from "../QuantumWithAmount";

interface QuantumMoveModifications {
outerLayer?: number;
Expand Down Expand Up @@ -114,7 +115,9 @@ export class QuantumMove extends Comparable {
);
}

override toString(): string {
override toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
let s = this.#family;
if (this.#innerLayer !== null) {
s = String(this.#innerLayer) + s;
Expand Down Expand Up @@ -171,7 +174,10 @@ export class Move extends AlgCommon<Move> {
// TODO: handle char indices more consistently among alg nodes.
return transferCharIndex(
this,
new Move(this.#quantumWithAmount.quantum, -this.amount),
new Move(
this.#quantumWithAmount.quantum,
this.#isSlash() ? this.amount : -this.amount,
),
);
}

Expand Down Expand Up @@ -229,29 +235,38 @@ export class Move extends AlgCommon<Move> {
return this.#quantumWithAmount.quantum.innerLayer ?? undefined;
}

toString(): string {
if (this.family === "_SLASH_") {
return "/"; // TODO: validate no amount
}
if (this.family.endsWith("_PLUS_")) {
return (
this.#quantumWithAmount.quantum.toString().slice(0, -6) +
Math.abs(this.amount) +
(this.amount < 0 ? "-" : "+")
); // TODO
}
if (this.family.endsWith("_PLUSPLUS_")) {
const absAmount = Math.abs(this.amount);
return (
this.#quantumWithAmount.quantum.toString().slice(0, -10) +
(absAmount === 1 ? "" : absAmount) +
(this.amount < 0 ? "--" : "++")
); // TODO
}
#cachedSlashMove: Move | undefined;
#isSlash(): boolean {
return this.isIdentical((this.#cachedSlashMove ??= new Move("_SLASH_")));
}

toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
if (experimentalSerializationOptions?.notation !== "LGN") {
if (this.#isSlash()) {
return "/"; // TODO: validate no amount
}
if (this.family.endsWith("_PLUS_")) {
return (
this.#quantumWithAmount.quantum.toString().slice(0, -6) +
Math.abs(this.amount) +
(this.amount < 0 ? "-" : "+")
); // TODO
}
if (this.family.endsWith("_PLUSPLUS_")) {
const absAmount = Math.abs(this.amount);
return (
this.#quantumWithAmount.quantum.toString().slice(0, -10) +
(absAmount === 1 ? "" : absAmount) +
(this.amount < 0 ? "--" : "++")
); // TODO
}
}
return (
this.#quantumWithAmount.quantum.toString() +
this.#quantumWithAmount.suffix()
this.#quantumWithAmount.quantum.toString(
experimentalSerializationOptions,
) + this.#quantumWithAmount.suffix()
);
}
// // TODO: Serialize as a string?
Expand Down
5 changes: 4 additions & 1 deletion src/cubing/alg/alg-nodes/leaves/Newline.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import { AlgCommon, type Comparable } from "../../common";
import { IterationDirection } from "../../iteration";
import type { AlgLeaf } from "../AlgNode";

/** @category Alg Nodes */
export class Newline extends AlgCommon<Newline> {
toString(): string {
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
return "\n";
}

Expand Down
5 changes: 4 additions & 1 deletion src/cubing/alg/alg-nodes/leaves/Pause.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExperimentalSerializationOptions } from "cubing/alg/SerializationOptions";
import { AlgCommon, type Comparable } from "../../common";
import { IterationDirection } from "../../iteration";
import type { AlgLeaf } from "../AlgNode";
Expand All @@ -7,7 +8,9 @@ import type { Grouping } from "../containers/Grouping";
export class Pause extends AlgCommon<Pause> {
experimentalNISSGrouping?: Grouping; // TODO: tie this to the alg

toString(): string {
toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string {
return ".";
}

Expand Down
7 changes: 5 additions & 2 deletions src/cubing/alg/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Alg } from "./Alg";
import type { IterationDirection } from "./iteration";
import type { AlgLeaf, AlgNode } from "./alg-nodes/AlgNode";
import type { IterationDirection } from "./iteration";
import type { ExperimentalSerializationOptions } from "./SerializationOptions";

let writeAlgDebugField = false;
export function setAlgDebugField(debug: boolean): void {
Expand Down Expand Up @@ -50,7 +51,9 @@ export abstract class AlgCommon<T extends Alg | AlgNode>
) => void;
}

abstract override toString(): string;
abstract override toString(
experimentalSerializationOptions?: ExperimentalSerializationOptions,
): string;

abstract invert(): T;

Expand Down

0 comments on commit 26f0d38

Please sign in to comment.