Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Nov 28, 2021
1 parent 8bafc86 commit e6617e8
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 47 deletions.
3 changes: 0 additions & 3 deletions lib/cartridge/cartridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,4 @@ export class Cartridge {
? await executionResult
: executionResult;
}

// TODO(@ethanthatonekid): add a `dispatch` method
// @see <https://github.com/EthanThatOneKid/fart/blob/c43f233345/lib/gen/cart.ts#L120>
}
23 changes: 17 additions & 6 deletions lib/cartridge/cartridge_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,30 @@ export type CartridgeEventReturnType = (
| null
);

export interface PropertyDefinition {
modifier?: string;
struct?: Record<string, PropertyDefinition>;
tuple?: Array<{
label?: string;
value: PropertyDefinition;
}>;
}

export interface CartridgeEventContext<T extends CartridgeEvent> {
type: T;
code: {
append: (code: string) => CartridgeEventReturnType;
};
code: { append: (code: string) => CartridgeEventReturnType };
tokens: Token[];
data: T extends CartridgeEvent.InlineComment ? { comments: string[] }
: T extends CartridgeEvent.MultilineComment ? { comments: string[] }
: T extends CartridgeEvent.Load ? { comments: string[] }
: T extends CartridgeEvent.Load
? { comments: string[]; dependencies: string[]; source: string }
: T extends CartridgeEvent.StructOpen
? { comments: string[]; name?: string } // undefined name implies anonymous struct
: T extends CartridgeEvent.SetProperty
? { comments: string[]; name: string; value: string }
: T extends CartridgeEvent.SetProperty ? ({
comments: string[];
name: string;
definition: PropertyDefinition;
})
: null;
}

Expand Down
1 change: 1 addition & 0 deletions lib/cartridge/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type {
CartridgeEventContext,
CartridgeHandler,
CartridgeHandlerMap,
PropertyDefinition,
} from "./cartridge_event.ts";
43 changes: 17 additions & 26 deletions lib/indent/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
assert,
bench,
BenchmarkResult,
BenchmarkTimer,
runBenchmarks,
} from "../../deps/std/testing.ts";
import { INDENT, Indent } from "./indent.ts";
import { getCachedIndent } from "./utils.ts";

const C = 0x10; // 16, cache size
const CACHE_BENCH_ID = "CACHE_TEST";
const COMPUTED_BENCH_ID = "COMPUTED_TEST";
const BENCH_RUNS = 1e3; // the higher the number, the more accurate the benchmark results
const BENCH_RUNS = 1e6; // the higher the number, the more accurate the benchmark results

/**
* @see https://deno.land/[email protected]/testing#benching
Expand All @@ -22,12 +20,12 @@ bench({
func: (timer: BenchmarkTimer): void => {
const store: string[] = [];
timer.start();
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Tab1, i));
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Tab2, i));
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space1, i));
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space2, i));
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space3, i));
for (let i = 1; i <= C; i++) store.push(getCachedIndent(Indent.Space4, i));
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Tab1, i));
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Tab2, i));
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space1, i));
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space2, i));
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space3, i));
for (let i = 1; i <= 16; i++) store.push(getCachedIndent(Indent.Space4, i));
timer.stop();
},
});
Expand All @@ -38,30 +36,23 @@ bench({
func: (timer: BenchmarkTimer): void => {
const store: string[] = [];
timer.start();
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Tab1].repeat(i));
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Tab2].repeat(i));
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space1].repeat(i));
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space2].repeat(i));
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space3].repeat(i));
for (let i = 1; i <= C; i++) store.push(INDENT[Indent.Space4].repeat(i));
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Tab1].repeat(i));
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Tab2].repeat(i));
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space1].repeat(i));
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space2].repeat(i));
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space3].repeat(i));
for (let i = 1; i <= 16; i++) store.push(INDENT[Indent.Space4].repeat(i));
timer.stop();
},
});

Deno.test("benchmarking the cache algorithm vs the `repeat` method", async () => {
const { results } = await runBenchmarks({ silent: true });
const cacheResults = results.find(({ name }: BenchmarkResult) =>
name === CACHE_BENCH_ID
) as BenchmarkResult;
const computedResults = results.find(({ name }: BenchmarkResult) =>
name === COMPUTED_BENCH_ID
) as BenchmarkResult;
const speedBoost = computedResults.measuredRunsAvgMs /
cacheResults.measuredRunsAvgMs;
const speedBoostPercentage = (speedBoost - 1) * 100;
const { results: [cache, computed] } = await runBenchmarks({ silent: true });
const speedBoostPercentage = 100 * computed.measuredRunsAvgMs /
cache.measuredRunsAvgMs;
const finalMessage = `the cache algorithm is ${
speedBoostPercentage.toFixed(2)
}% the speed of the \`repeat\` algorithm`;
console.log(finalMessage);
assert(speedBoost > 1);
assert(speedBoostPercentage > 100);
});
123 changes: 113 additions & 10 deletions lib/text_builder/text_builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { CodeBlock } from "../code_block/mod.ts";
import { Indent, IndentOption } from "../indent/mod.ts";
import { Cartridge, CartridgeEvent } from "../cartridge/mod.ts";
import { Token } from "../tokenize/mod.ts";
import { makeFileStartEventContext } from "./utils.ts";
import {
Cartridge,
CartridgeEvent,
PropertyDefinition,
} from "../cartridge/mod.ts";
import { Lexicon, Token } from "../tokenize/mod.ts";
import {
makeFileEndEventContext,
makeFileStartEventContext,
makeInlineCommentEventContext,
makeLoadEventContext,
makeMultilineCommentEventContext,
makeSetPropertyEventContext,
makeStructCloseEventContext,
makeStructOpenEventContext,
} from "./utils.ts";

export class TextBuilder {
private blocks: CodeBlock[];
Expand All @@ -15,42 +28,132 @@ export class TextBuilder {
this.indentLevel = 0;
}

/**
* @todo @ethanthatonekid complete this method
* @see https://github.com/EthanThatOneKid/fart/blob/c43f233345/lib/gen/builder.ts#L20
*/
async append(event: CartridgeEvent, tokens: Token[]): Promise<void> {
public async append(
event: CartridgeEvent.FileStart,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.InlineComment,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.MultilineComment,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.Load,
tokens: Token[],
comments: Token[],
value: undefined,
source: string,
...dependencies: string[]
): Promise<void>;
public async append(
event: CartridgeEvent.StructOpen,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.SetProperty,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.StructClose,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.FileEnd,
tokens: Token[],
comments: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent,
tokens: Token[],
comments: Token[] = [],
value?: PropertyDefinition,
...rest: string[]
): Promise<void> {
let code: string | void | null;
switch (event) {
case CartridgeEvent.FileStart: {
const code = await this.cartridge.dispatch(
code = await this.cartridge.dispatch(
CartridgeEvent.FileStart,
makeFileStartEventContext(this.currentBlock, tokens),
);
if (typeof code === "string") this.currentBlock.append(code);
break;
}
case CartridgeEvent.InlineComment: {
code = await this.cartridge.dispatch(
CartridgeEvent.InlineComment,
makeInlineCommentEventContext(this.currentBlock, tokens, comments),
);
break;
}
case CartridgeEvent.MultilineComment: {
code = await this.cartridge.dispatch(
CartridgeEvent.MultilineComment,
makeMultilineCommentEventContext(this.currentBlock, tokens, comments),
);
break;
}
case CartridgeEvent.Load: {
const [source, ...dependencies] = rest;
code = await this.cartridge.dispatch(
CartridgeEvent.Load,
makeLoadEventContext(
this.currentBlock,
tokens,
comments,
source,
dependencies,
),
);
break;
}
case CartridgeEvent.StructOpen: {
code = await this.cartridge.dispatch(
CartridgeEvent.StructOpen,
makeStructOpenEventContext(this.currentBlock, tokens, comments),
);
break;
}
case CartridgeEvent.SetProperty: {
const [name] = rest;
code = await this.cartridge.dispatch(
CartridgeEvent.SetProperty,
makeSetPropertyEventContext(
this.currentBlock,
tokens,
comments,
name,
value as PropertyDefinition,
),
);
break;
}
case CartridgeEvent.StructClose: {
code = await this.cartridge.dispatch(
CartridgeEvent.StructOpen,
makeStructCloseEventContext(this.currentBlock, tokens),
);
break;
}
case CartridgeEvent.FileEnd: {
code = await this.cartridge.dispatch(
CartridgeEvent.FileEnd,
makeFileEndEventContext(this.currentBlock, tokens),
);
break;
}
}
if (typeof code === "string") {
this.currentBlock.append(code);
}
}

export(indent: IndentOption = Indent.Space2): string {
Expand Down
Loading

1 comment on commit e6617e8

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on e6617e8 Nov 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

failed to fetch 'https://raw.githubusercontent.com/EthanThatOneKid/fart/e6617e8fe80d360644e0ec7e14a22ddad77e0da2/std/server/worker.ts': HTTP status client error (404 Not Found) for url (https://raw.githubusercontent.com/EthanThatOneKid/fart/e6617e8fe80d360644e0ec7e14a22ddad77e0da2/std/server/worker.ts)

Please sign in to comment.