Skip to content

Commit

Permalink
some progress with 3 failures
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Dec 23, 2021
1 parent 8a72c89 commit 4dd562a
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"https://cdn.skypack.dev": false,
"https://fart.tools": false
},
"cSpell.words": ["typedefs", "typemap", "typemaps"]
"cSpell.words": ["transpiles", "typedefs", "typemap", "typemaps"]
}
54 changes: 48 additions & 6 deletions lib/transpile/cartridge/cartridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ export enum CartridgeEvent {
FileEnd = "file_end",
}

export enum ReservedType {
Omit = "_",
Number = "number",
String = "string",
Boolean = "boolean",
Default = "any",
}

export enum Modifier {
Array = "array", // Modifies anything.
Async = "async", // Modifies anything.
Dictionary = "dict", // Modifies length-2 tuples.
Function = "fn", // Modifies length-2 tuples.
}

export type CartridgeEventReturnType = (
| void
| Promise<void>
Expand Down Expand Up @@ -73,11 +88,32 @@ export interface CartridgeHandlerMap {
}

/**
* @todo @ethanthatonekid pass all tests in ./cartridge.test.ts
* @todo @ethanthatonekid refactor <https://github.com/EthanThatOneKid/fart/blob/c43f2333458b2cbc40d167610d87e2a2e3f89885/lib/gen/cart.ts>
* Returns a type composed into plain text (e.g. `number`,
* `Array<string>`, `(a: number, b: number) => number`, etc.).
*/
export type ModHandler = (...inner: string[]) => string;

/**
* The TypeMap API is designed to delegate the generation of
* syntax for various programming languages.
*/
export interface CartridgeTypeMap {
[ReservedType.Omit]?: string;
[ReservedType.Number]?: string;
[ReservedType.String]?: string;
[ReservedType.Boolean]?: string;
[ReservedType.Default]?: string;

// Modifiers are not required for all languages.
[Modifier.Array]?: ModHandler;
[Modifier.Async]?: ModHandler;
[Modifier.Dictionary]?: ModHandler;
[Modifier.Function]?: ModHandler;
}

export class Cartridge {
constructor(
private typemap: CartridgeTypeMap = {},
private handlers: CartridgeHandlerMap = {},
) {}

Expand Down Expand Up @@ -121,9 +157,7 @@ export class Cartridge {
this.handlers[name] = handler;
}

/**
* `on` is an alias for `addEventListener`
*/
/** `on` is an alias for `addEventListener` */
public on = this.addEventListener.bind(this);

public removeEventListener(name: CartridgeEvent) {
Expand All @@ -138,8 +172,16 @@ export class Cartridge {
if (handleEvent === undefined) return null;
const executionResult = handleEvent(ctx);
if (executionResult instanceof Promise) {
return await executionResult ?? null;
return (await executionResult) ?? null;
}
return executionResult ?? null;
}

public getType(type: string): string | undefined {
return this.typemap[type as ReservedType];
}

public getMod(mod: string): ModHandler | undefined {
return this.typemap[mod as Modifier];
}
}
6 changes: 4 additions & 2 deletions lib/transpile/text_builder/text_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export class TextBuilder {
): Promise<void>;
public async append(
event: CartridgeEvent.FileEnd,
tokens: Token[],
comments: Token[],
tokens?: Token[],
comments?: Token[],
): Promise<void>;
public async append(
event: CartridgeEvent,
Expand All @@ -99,6 +99,7 @@ export class TextBuilder {
CartridgeEvent.FileStart,
makeFileStartEventContext(this.currentBlock, tokens),
);
this.stash();
break;
}

Expand Down Expand Up @@ -172,6 +173,7 @@ export class TextBuilder {
}

case CartridgeEvent.FileEnd: {
this.stash();
code = await this.cartridge.dispatch(
CartridgeEvent.FileEnd,
makeFileEndEventContext(this.currentBlock, tokens),
Expand Down
111 changes: 102 additions & 9 deletions lib/transpile/transpile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,69 @@ import { assertEquals } from "../../deps/std/testing.ts";
import { TranspilationContext, transpile } from "./transpile.ts";
import { Cartridge, CartridgeEvent } from "./cartridge/mod.ts";
import type { CartridgeEventContext } from "./cartridge/mod.ts";
import { TextBuilder } from "./text_builder/mod.ts";
import { tokenize } from "./tokenize/mod.ts";

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);
const ctx = new TranspilationContext(iterator, cartridge);
assertEquals(ctx.started, false);
});

Deno.test("empty ", () => {
const iterator = tokenize("");
const cartridge = new Cartridge();
const builder = new TextBuilder(cartridge);
const ctx = new TranspilationContext(iterator, builder);
assertEquals(ctx.started, false);
Deno.test("empty input only fires file_start event and then file_end event", async () => {
const fakeCart = new Cartridge();
fakeCart.on(CartridgeEvent.FileStart, () => "ABC");
fakeCart.on(CartridgeEvent.FileEnd, () => "XYZ");
const result = await transpile("", fakeCart);
assertEquals(result, "ABC\n\nXYZ");
});

Deno.test("transpiles inline_comment event", async () => {
const fakeCart = new Cartridge();
fakeCart.on(
CartridgeEvent.InlineComment,
(event: CartridgeEventContext<CartridgeEvent.InlineComment>) => {
assertEquals(event.data.comments, ["hello world"]);
return "ABC";
},
);
const result = await transpile("; hello world", fakeCart);
assertEquals(result, "ABC");
});

Deno.test("transpiles multiline_comment event", async () => {
const fakeCart = new Cartridge();
fakeCart.on(
CartridgeEvent.MultilineComment,
(event: CartridgeEventContext<CartridgeEvent.MultilineComment>) => {
assertEquals(event.data.comments, ["example"]);
return "ABC";
},
);
const result = await transpile(
`/*
example
*/`,
fakeCart,
);
assertEquals(result, "ABC");
});

Deno.test("transpiles load event", async () => {
const fakeCart = new Cartridge();
fakeCart.on(
CartridgeEvent.Load,
(event: CartridgeEventContext<CartridgeEvent.Load>) => {
assertEquals(event.data.source, "./example.fart");
assertEquals(event.data.dependencies, ["Example1", "Example2"]);
return "ABC";
},
);
const result = await transpile(
"load './example.fart' ( Example1, Example2 )",
fakeCart,
);
assertEquals(result, "ABC");
});

Deno.test("transpiles struct_open event", async () => {
Expand All @@ -34,3 +80,50 @@ Deno.test("transpiles struct_open event", async () => {
const result = await transpile(`type Example {`, fakeCart);
assertEquals(result, "ABC");
});

Deno.test("transpiles set_property event", async () => {
const fakeCart = new Cartridge();
fakeCart.on(
CartridgeEvent.SetProperty,
(event: CartridgeEventContext<CartridgeEvent.SetProperty>) => {
assertEquals(event.data.name, "example");
assertEquals(event.data.definition.optional, false);
assertEquals(event.data.comments, []);
return "ABC";
},
);
const result = await transpile(
`type Example { example: string }`,
fakeCart,
);
assertEquals(result, "ABC");
});
// StructClose = "struct_close",
// 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");
// });
// FileEnd = "file_end",

// 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 4dd562a

@deno-deploy
Copy link

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

Please sign in to comment.