Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stats generation #1089

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Story as ParsedStory } from "./Parser/ParsedHierarchy/Story";
import { DebugMetadata } from "../engine/DebugMetadata";
import { StringValue } from "../engine/Value";
import { asOrNull } from "../engine/TypeAssertion";
import { GenerateStoryStats, Stats } from "./Stats";

export { CompilerOptions } from "./CompilerOptions";
export { InkParser } from "./Parser/InkParser";
Expand Down Expand Up @@ -113,6 +114,13 @@ export class Compiler {
}
};

public readonly GenerateStats = (): Stats | null => {
if (this._parsedStory === null) {
return null;
}
return GenerateStoryStats(this._parsedStory);
};

public readonly DebugMetadataForContentAtOffset = (
offset: number
): DebugMetadata | null => {
Expand Down
52 changes: 52 additions & 0 deletions src/compiler/Stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Choice } from "./Parser/ParsedHierarchy/Choice";
import { Divert } from "./Parser/ParsedHierarchy/Divert/Divert";
import { Gather } from "./Parser/ParsedHierarchy/Gather/Gather";
import { Knot } from "./Parser/ParsedHierarchy/Knot";
import { Stitch } from "./Parser/ParsedHierarchy/Stitch";
import { Story } from "./Parser/ParsedHierarchy/Story";
import { Text } from "./Parser/ParsedHierarchy/Text";

export interface Stats {
words: number;
knots: number;
stitches: number;
functions: number;
choices: number;
gathers: number;
diverts: number;
}

export function GenerateStoryStats(story: Story): Stats {
let allText = story.FindAll(Text)();
let words = 0;
for (const text of allText) {
let wordsInThisStr = 0;
let wasWhiteSpace = true;
for (const c of text.text) {
if (c == " " || c == "\t" || c == "\n" || c == "\r") {
wasWhiteSpace = true;
} else if (wasWhiteSpace) {
wordsInThisStr++;
wasWhiteSpace = false;
}
}

words += wordsInThisStr;
}

const knots = story.FindAll(Knot)();
const stitches = story.FindAll(Stitch)();
const choices = story.FindAll(Choice)();
const gathers = story.FindAll(Gather)((g) => g.debugMetadata != null);
const diverts = story.FindAll(Divert)();

return {
words,
knots: knots.length,
functions: knots.filter((k) => k.isFunction).length,
stitches: stitches.length,
gathers: gathers.length,
diverts: diverts.length - 1,
choices: choices.length,
};
}
43 changes: 43 additions & 0 deletions src/tests/specs/inkjs/compiler/Stats.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Stats } from "../../../../compiler/Stats";
import { Compiler } from "../../../../ink";

function getStats(inkSource: string): Stats {
const compiler = new Compiler(inkSource);
compiler.Compile();
const stats = compiler.GenerateStats();
expect(stats).not.toBeNull();
return stats!;
}

describe("Stat Generation", () => {
it("basic word count", () => {
const stats = getStats("this is an ink story.");
expect(stats.words).toBe(5);
});
it("word count doesn't include divert or variable names", () => {
const stats = getStats(
"VAR MyVariable = 3\n->start\n=== start\nthis is an ink story.\n->END"
);
expect(stats.words).toBe(5);
});
it("count functions, knots, and stitches", () => {
const stats = getStats(
"->start\n=== function myFunc()\n~ return 0\n=== start\n= stitch\nHello world!"
);
expect(stats.functions).toBe(1);
expect(stats.knots).toBe(2);
expect(stats.stitches).toBe(1);
});
it("count diverts", () => {
const stats = getStats("->go\n- (go)\n->next\n-(next)");
expect(stats.diverts).toBe(2);
});
it("end counts as a divert", () => {
const stats = getStats("->go\n- (go)\n->next\n-(next)\n->END");
expect(stats.diverts).toBe(3);
});
it("count gathers", () => {
const stats = getStats("->go\n- (go)\n->next\n-(next)");
expect(stats.gathers).toBe(2);
});
});