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

Rework #1 #34

Draft
wants to merge 46 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5156a6c
Started refactoring entire internal lib from scratch
EthanThatOneKid Nov 12, 2021
7e50e1b
yeye
EthanThatOneKid Nov 12, 2021
b0540c3
made some progress but tests arent passing :/
EthanThatOneKid Nov 15, 2021
d1470f7
created base tokenzior functionality
EthanThatOneKid Nov 24, 2021
35ca584
todo: fix broken tests
EthanThatOneKid Nov 24, 2021
b68c749
progress
EthanThatOneKid Nov 24, 2021
e3bea27
progress
EthanThatOneKid Nov 24, 2021
f0102bb
woo progress
EthanThatOneKid Nov 24, 2021
0ebac47
progress
EthanThatOneKid Nov 25, 2021
e43b212
progress
EthanThatOneKid Nov 25, 2021
3690b2c
progress
EthanThatOneKid Nov 25, 2021
001672f
progress
EthanThatOneKid Nov 25, 2021
0965508
Update utils.test.ts
EthanThatOneKid Nov 25, 2021
e837ab0
progress
EthanThatOneKid Nov 26, 2021
8f21d7e
progress
EthanThatOneKid Nov 26, 2021
d9d9c1f
progress
EthanThatOneKid Nov 27, 2021
8bafc86
progress
EthanThatOneKid Nov 27, 2021
e6617e8
progress
EthanThatOneKid Nov 28, 2021
538081b
progress
EthanThatOneKid Nov 28, 2021
6fca6dc
progress
EthanThatOneKid Nov 28, 2021
7b3f2e0
progress
EthanThatOneKid Nov 28, 2021
b2bd98d
covered text_builder utils testcases
EthanThatOneKid Dec 8, 2021
0163e07
Update token.test.ts
EthanThatOneKid Dec 8, 2021
5fc87f4
Create check_lib.yaml
EthanThatOneKid Dec 8, 2021
f8be624
fixed lint errors
EthanThatOneKid Dec 8, 2021
9249a21
Update check_lib.yaml
EthanThatOneKid Dec 8, 2021
ad9a81d
Update check_lib.yaml
EthanThatOneKid Dec 8, 2021
1eaf7b2
Update check_lib.yaml
EthanThatOneKid Dec 8, 2021
d44abf8
progress
EthanThatOneKid Dec 9, 2021
86cde15
Update deno_redirect.ts
EthanThatOneKid Dec 9, 2021
3f20d43
fsdffdsfgads
EthanThatOneKid Dec 16, 2021
1b85eec
progress on fart server
EthanThatOneKid Dec 18, 2021
0a1f91d
cleaningisdfmdsf
EthanThatOneKid Dec 18, 2021
db91aa5
Create watch_serve.ts
EthanThatOneKid Dec 18, 2021
0c12660
uhhhhhhhhhhhhhhhhhhhhhhhhhhb hgvyujhbuiyxfgh
EthanThatOneKid Dec 18, 2021
b2615ad
reorganized lib lgtm
EthanThatOneKid Dec 19, 2021
7690f00
Update transpile.ts
EthanThatOneKid Dec 19, 2021
f95dfeb
started working on this coverage thing
EthanThatOneKid Dec 19, 2021
8680e26
sad bug in the transpiler
EthanThatOneKid Dec 21, 2021
b748ee4
progress
EthanThatOneKid Dec 23, 2021
4b3f831
everything is ok
EthanThatOneKid Dec 23, 2021
8a72c89
neat work
EthanThatOneKid Dec 23, 2021
4dd562a
some progress with 3 failures
EthanThatOneKid Dec 23, 2021
85beff3
added some changes
EthanThatOneKid Dec 23, 2021
c277f89
Update transpile.ts
EthanThatOneKid Dec 23, 2021
fa81791
kwik progress
EthanThatOneKid Dec 26, 2021
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
Prev Previous commit
Next Next commit
progress
  • Loading branch information
EthanThatOneKid committed Nov 27, 2021
commit 8bafc86a379c5aa9df7a2da4de5e701ae81e1f8c
3 changes: 3 additions & 0 deletions lib/cartridge/cartridge.test.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,9 @@ import { CartridgeEvent } from "./cartridge_event.ts";
*/
Deno.test("hello world", () => {
const cartridge = new Cartridge();
cartridge.on(CartridgeEvent.Load, (event) => {
event.data;
});
cartridge.on(CartridgeEvent.FileStart, console.log);
assertEquals(1, 1);
});
34 changes: 23 additions & 11 deletions lib/cartridge/cartridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CartridgeEvent } from "./cartridge_event.ts";
import { CartridgeEvent, CartridgeEventReturnType } from "./cartridge_event.ts";
import type {
CartridgeEventContext,
CartridgeHandler,
@@ -14,35 +14,35 @@ export class Cartridge {
private handlers: CartridgeHandlerMap = {},
) {}

addEventListener(
public addEventListener(
name: CartridgeEvent.FileStart,
handler: CartridgeHandler<CartridgeEvent.FileStart>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent.InlineComment,
handler: CartridgeHandler<CartridgeEvent.InlineComment>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent.MultilineComment,
handler: CartridgeHandler<CartridgeEvent.MultilineComment>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent.Load,
handler: CartridgeHandler<CartridgeEvent.Load>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent.StructOpen,
handler: CartridgeHandler<CartridgeEvent.StructOpen>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent.SetProperty,
handler: CartridgeHandler<CartridgeEvent.StructClose>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent.FileEnd,
handler: CartridgeHandler<CartridgeEvent.FileEnd>,
): void;
addEventListener(
public addEventListener(
name: CartridgeEvent,
// deno-lint-ignore no-explicit-any
handler: any,
@@ -53,12 +53,24 @@ export class Cartridge {
/**
* `on` is an alias for `addEventListener`
*/
on = this.addEventListener.bind(this);
public on = this.addEventListener.bind(this);

removeEventListener(name: CartridgeEvent) {
public removeEventListener(name: CartridgeEvent) {
delete this.handlers[name];
}

public async dispatch<T extends CartridgeEvent>(
name: CartridgeEvent,
ctx: CartridgeEventContext<T>,
): Promise<string | null | void> {
const handleEvent = this.handlers[name] as CartridgeHandler<T>;
if (handleEvent === undefined) return null;
const executionResult = handleEvent(ctx);
return executionResult instanceof Promise
? await executionResult
: executionResult;
}

// TODO(@ethanthatonekid): add a `dispatch` method
// @see <https://github.com/EthanThatOneKid/fart/blob/c43f233345/lib/gen/cart.ts#L120>
}
14 changes: 12 additions & 2 deletions lib/cartridge/cartridge_event.ts
Original file line number Diff line number Diff line change
@@ -11,9 +11,19 @@ export enum CartridgeEvent {
FileEnd = "file_end",
}

export type CartridgeEventReturnType = (
| void
| Promise<void>
| string
| Promise<string>
| null
);

export interface CartridgeEventContext<T extends CartridgeEvent> {
type: T;
code: { append: (code: string) => void };
code: {
append: (code: string) => CartridgeEventReturnType;
};
tokens: Token[];
data: T extends CartridgeEvent.InlineComment ? { comments: string[] }
: T extends CartridgeEvent.MultilineComment ? { comments: string[] }
@@ -32,7 +42,7 @@ export interface CartridgeEventContext<T extends CartridgeEvent> {
*/
export type CartridgeHandler<T extends CartridgeEvent> = (
event: CartridgeEventContext<T>,
) => void | Promise<void>;
) => CartridgeEventReturnType;

export interface CartridgeHandlerMap {
[CartridgeEvent.FileStart]?: CartridgeHandler<CartridgeEvent.FileStart>;
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
import { assertEquals } from "../../../deps/std/testing.ts";
import { CodeBlock } from "./code_block.ts";
Deno.test("new code block is empty", () => {
assertEquals(new CodeBlock().export(), "");
});
Deno.test("add 3 lines of code to the block", () => {
const block = new CodeBlock();
block.append("a");
block.append("b");
block.append("c");
const expectation = "a\nb\nc";
const reality = block.export();
assertEquals(expectation, reality);
});
Deno.test("add 3 lines of code to the block (indented)", () => {
const block = new CodeBlock();
block.append("a", 0);
block.append("b", 1);
block.append("c", 2);
const expectation = "a\n b\n c";
const reality = block.export();
assertEquals(expectation, reality);
});
Deno.test("join 3 code blocks", () => {
const block1 = new CodeBlock();
block1.append("a", 0);
block1.append("b", 1);
block1.append("c", 2);
const block2 = new CodeBlock();
block2.append("d", 1);
block2.append("e", 0);
block2.append("f", 1);
const block3 = new CodeBlock();
block3.append("g", 2);
block3.append("h", 1);
block3.append("i", 0);
const expectation = `a
b
c
d
e
f
g
h
i`;
const reality = CodeBlock.join(block1, block2, block3);
assertEquals(expectation, reality);
});
import { assertEquals } from "../../deps/std/testing.ts";
import { CodeBlock } from "./code_block.ts";

Deno.test("new code block is empty", () => {
assertEquals(new CodeBlock().export(), "");
});

Deno.test("add 3 lines of code to the block", () => {
const block = new CodeBlock();
block.append("a");
block.append("b");
block.append("c");
const expectation = "a\nb\nc";
const reality = block.export();
assertEquals(expectation, reality);
});

Deno.test("add 3 lines of code to the block (indented)", () => {
const block = new CodeBlock();
block.append("a", 0);
block.append("b", 1);
block.append("c", 2);
const expectation = "a\n b\n c";
const reality = block.export();
assertEquals(expectation, reality);
});

Deno.test("join 3 code blocks", () => {
const block1 = new CodeBlock();
block1.append("a", 0);
block1.append("b", 1);
block1.append("c", 2);
const block2 = new CodeBlock();
block2.append("d", 1);
block2.append("e", 0);
block2.append("f", 1);
const block3 = new CodeBlock();
block3.append("g", 2);
block3.append("h", 1);
block3.append("i", 0);
const expectation = `a
b
c

d
e
f

g
h
i`;
const reality = CodeBlock.join(block1, block2, block3);
assertEquals(expectation, reality);
});
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
import { getIndent, Indent, IndentOption } from "../../indent/mod.ts";

export interface LineOfCode {
content: string;
indentLevel: number;
}

/**
* Represents a block of code.
*/
export class CodeBlock {
public code: LineOfCode[] = [];

append(content: string, indentLevel = 0): void {
this.code.push(
...content.split("\n").map((line) => ({ content: line, indentLevel })),
);
}

export(indent: IndentOption = Indent.Space2): string {
return this.code
.map(({ content, indentLevel }) =>
getIndent(indent, indentLevel) + content
)
.join("\n");
}

/**
* `toString` is an alias for `CodeBlock.export`.
*/
toString = this.export.bind(this);

static join(
indentOrFirstBlock: IndentOption | CodeBlock,
...blocks: CodeBlock[]
): string {
const blockPadding = 2; // lines between each code block
const blockSeparator = "\n".repeat(blockPadding);
const indentSpecified = !(indentOrFirstBlock instanceof CodeBlock);
if (!indentSpecified) blocks = [indentOrFirstBlock, ...blocks];
return blocks
.filter((block) => block !== null)
.reduce(
(file, block, i) => {
const exportedCode = indentSpecified
? block.export(indentOrFirstBlock)
: block.export();
return file + exportedCode +
(blocks.length - 1 > i ? blockSeparator : "");
},
"",
);
}
}
import { getIndent, Indent, IndentOption } from "../indent/mod.ts";

export interface LineOfCode {
content: string;
indentLevel: number;
}

/**
* Represents a block of code.
*/
export class CodeBlock {
public code: LineOfCode[] = [];

/**
* @param content string that is split up by line break
* @param indentLevel depth of nesting; defaults to 0
*/
append(content: string, indentLevel = 0): void {
this.code.push(
...content.split("\n").map((line) => ({ content: line, indentLevel })),
);
}

export(indent: IndentOption = Indent.Space2): string {
return this.code
.map(({ content, indentLevel }) =>
getIndent(indent, indentLevel) + content
)
.join("\n");
}

/**
* `toString` is an alias for `CodeBlock.export`.
*/
toString = this.export.bind(this);

static join(
indentOrFirstBlock: IndentOption | CodeBlock,
...blocks: CodeBlock[]
): string {
const blockPadding = 2; // lines between each code block
const blockSeparator = "\n".repeat(blockPadding);
const indentSpecified = !(indentOrFirstBlock instanceof CodeBlock);
if (!indentSpecified) blocks = [indentOrFirstBlock, ...blocks];
return blocks
.filter((block) => block !== null)
.reduce(
(file, block, i) => {
const exportedCode = indentSpecified
? block.export(indentOrFirstBlock)
: block.export();
const isLast = blocks.length - 1 <= i;
return file + exportedCode + (isLast ? "" : blockSeparator);
},
"",
);
}
}
File renamed without changes.
1 change: 0 additions & 1 deletion lib/text_builder/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { TextBuilder } from "./text_builder.ts";
export { CodeBlock } from "./code_block/mod.ts";
11 changes: 8 additions & 3 deletions lib/text_builder/text_builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CodeBlock } from "./code_block/mod.ts";
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";

export class TextBuilder {
private blocks: CodeBlock[];
@@ -18,10 +19,14 @@ export class TextBuilder {
* @todo @ethanthatonekid complete this method
* @see https://github.com/EthanThatOneKid/fart/blob/c43f233345/lib/gen/builder.ts#L20
*/
// deno-lint-ignore no-unused-vars
append(event: CartridgeEvent, tokens: Token[]): void {
async append(event: CartridgeEvent, tokens: Token[]): Promise<void> {
switch (event) {
case CartridgeEvent.FileStart: {
const code = await this.cartridge.dispatch(
CartridgeEvent.FileStart,
makeFileStartEventContext(this.currentBlock, tokens),
);
if (typeof code === "string") this.currentBlock.append(code);
break;
}
case CartridgeEvent.InlineComment: {
8 changes: 8 additions & 0 deletions lib/text_builder/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { CodeBlock } from "../code_block/mod.ts";
import type { Token } from "../tokenize/mod.ts";
import { CartridgeEvent } from "../cartridge/mod.ts";

export const makeFileStartEventContext = (
code: CodeBlock,
tokens: Token[],
) => ({ type: CartridgeEvent.FileStart, code, tokens, data: null });