Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Dec 23, 2021
1 parent 8680e26 commit b748ee4
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 71 deletions.
5 changes: 0 additions & 5 deletions lib/repl/repl.ts

This file was deleted.

1 change: 1 addition & 0 deletions lib/transpile/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { transpile } from "./transpile.ts";
92 changes: 91 additions & 1 deletion lib/transpile/text_builder/text_builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,99 @@
import { assertEquals } from "../../../deps/std/testing.ts";
import { TextBuilder } from "./text_builder.ts";
import { Cartridge } from "../cartridge/mod.ts";
import { Cartridge, CartridgeEvent } from "../cartridge/mod.ts";
import { T } from "../tokenize/mod.ts";

Deno.test("text builder exports an empty string when nothing is appended", () => {
const cartridge = new Cartridge();
const builder = new TextBuilder(cartridge);
assertEquals(builder.export(), "");
});

Deno.test("text builder appends file_start event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.FileStart, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(CartridgeEvent.FileStart);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends inline_comment event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.InlineComment, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(
CartridgeEvent.InlineComment,
[T.comment("; Example", 1, 1)],
[],
);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends multiline_comment event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.MultilineComment, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(
CartridgeEvent.MultilineComment,
[T.multiline_comment(
`/*
This is a multiline comment!
*/`,
1,
1,
)],
[],
);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends load event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.Load, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(
CartridgeEvent.Load,
/* tokens=*/ [],
/* comments=*/ [],
/* always undefined=*/ undefined,
/* src=*/ "",
/* dep1=*/ "",
);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends struct_open event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.StructOpen, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(CartridgeEvent.StructOpen, [], []);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends set_property event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.SetProperty, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(CartridgeEvent.SetProperty, [], []);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends struct_close event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.StructClose, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(
CartridgeEvent.StructClose,
[T.denest(1, 1)],
[],
);
assertEquals(builder.export(), "ABC");
});

Deno.test("text builder appends file_end event", async () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.FileEnd, () => "ABC");
const builder = new TextBuilder(cartridge);
await builder.append(CartridgeEvent.FileEnd, [], []);
assertEquals(builder.export(), "ABC");
});
44 changes: 36 additions & 8 deletions lib/transpile/text_builder/text_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CartridgeEvent,
PropertyDefinition,
} from "../cartridge/mod.ts";
import { Lexicon } from "../tokenize/mod.ts";
import type { Token } from "../tokenize/mod.ts";
import {
makeFileEndEventContext,
Expand All @@ -17,22 +16,34 @@ import {
makeStructCloseEventContext,
makeStructOpenEventContext,
} from "./utils.ts";
import { assertKind } from "../utils.ts";
// import { assertKind } from "../utils.ts";

export class TextBuilder {
private blocks: CodeBlock[];
private currentBlock: CodeBlock;
private indentLevel: number;

constructor(private cartridge: Cartridge) {
this.blocks = [];
this.currentBlock = new CodeBlock();
this.indentLevel = 0;
}

/**
* _stash_ away the current code block into the list of
* code blocks ready to be exported.
*/
private stash() {
if (this.currentBlock.code.length > 0) {
this.blocks.push(this.currentBlock);
this.currentBlock = new CodeBlock();
}
}

public async append(
event: CartridgeEvent.FileStart,
tokens: Token[],
comments: Token[],
tokens?: Token[],
comments?: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent.InlineComment,
Expand All @@ -56,6 +67,7 @@ export class TextBuilder {
event: CartridgeEvent.StructOpen,
tokens: Token[],
comments: Token[],
value?: PropertyDefinition,
): Promise<void>;
public async append(
event: CartridgeEvent.SetProperty,
Expand All @@ -74,12 +86,13 @@ export class TextBuilder {
): Promise<void>;
public async append(
event: CartridgeEvent,
tokens: Token[],
tokens: Token[] = [],
comments: Token[] = [],
value?: PropertyDefinition,
...rest: string[]
): Promise<void> {
let code: string | void | null;

switch (event) {
case CartridgeEvent.FileStart: {
code = await this.cartridge.dispatch(
Expand All @@ -88,20 +101,23 @@ export class TextBuilder {
);
break;
}

case CartridgeEvent.InlineComment: {
code = await this.cartridge.dispatch(
CartridgeEvent.InlineComment,
makeInlineCommentEventContext(this.currentBlock, tokens),
);
break;
}

case CartridgeEvent.MultilineComment: {
code = await this.cartridge.dispatch(
CartridgeEvent.MultilineComment,
makeMultilineCommentEventContext(this.currentBlock, tokens),
);
break;
}

case CartridgeEvent.Load: {
const [source, ...dependencies] = rest;
code = await this.cartridge.dispatch(
Expand All @@ -116,14 +132,21 @@ export class TextBuilder {
);
break;
}

case CartridgeEvent.StructOpen: {
const { value: name } = assertKind(tokens[1], Lexicon.Identifier);
code = await this.cartridge.dispatch(
CartridgeEvent.StructOpen,
makeStructOpenEventContext(this.currentBlock, tokens, comments, name),
makeStructOpenEventContext(
this.currentBlock,
tokens,
comments,
value?.value,
),
);
this.indentLevel++;
break;
}

case CartridgeEvent.SetProperty: {
const [name] = rest;
code = await this.cartridge.dispatch(
Expand All @@ -138,13 +161,16 @@ export class TextBuilder {
);
break;
}

case CartridgeEvent.StructClose: {
if (--this.indentLevel === 0) this.stash();
code = await this.cartridge.dispatch(
CartridgeEvent.StructOpen,
CartridgeEvent.StructClose,
makeStructCloseEventContext(this.currentBlock, tokens),
);
break;
}

case CartridgeEvent.FileEnd: {
code = await this.cartridge.dispatch(
CartridgeEvent.FileEnd,
Expand All @@ -153,12 +179,14 @@ export class TextBuilder {
break;
}
}

if (typeof code === "string") {
this.currentBlock.append(code);
}
}

export(indent: IndentOption = Indent.Space2): string {
this.stash();
return CodeBlock.join(indent, ...this.blocks);
}
}
37 changes: 23 additions & 14 deletions lib/transpile/transpile.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { transpile } from "./transpile.ts";
import { TranspilationContext, transpile } from "./transpile.ts";
import { Cartridge, CartridgeEvent } from "./cartridge/mod.ts";
import type { CartridgeEventContext } from "./cartridge/mod.ts";
// import type { FartOptions } from "./transpile.ts";
import { TextBuilder } from "./text_builder/mod.ts";
import { tokenize } from "./tokenize/mod.ts";

Deno.test("empty string results in empty string", async () => {
const fakeCart = new Cartridge();
fakeCart.on(
CartridgeEvent.StructOpen,
(event: CartridgeEventContext<CartridgeEvent.StructOpen>) => {
assertEquals(event.data.name, "Example");
console.log({ event });
return "";
},
);
const result = await transpile(`type Example {`, fakeCart);
assertEquals(result, "");
Deno.test("create transpilation context without crashing", () => {
const iterator = tokenize("");
const cartridge = new Cartridge();
const builder = new TextBuilder(cartridge);
const ctx = new TranspilationContext(iterator, builder);
assertEquals(ctx.started, false);
});

// Deno.test("transpiles struct_open event", async () => {
// const fakeCart = new Cartridge();
// fakeCart.on(
// CartridgeEvent.StructOpen,
// (event: CartridgeEventContext<CartridgeEvent.StructOpen>) => {
// assertEquals(event.data.name, "Example");
// assertEquals(event.data.comments, []);
// return "ABC";
// },
// );
// const result = await transpile(`type Example {`, fakeCart);
// assertEquals(result, "ABC");
// });
Loading

1 comment on commit b748ee4

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on b748ee4 Dec 23, 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/b748ee4fc00904d52798d256c4d91eaae4311a55/fart_server/handle_request.ts': HTTP status client error (404 Not Found) for url (https://raw.githubusercontent.com/EthanThatOneKid/fart/b748ee4fc00904d52798d256c4d91eaae4311a55/fart_server/handle_request.ts)

Please sign in to comment.