Skip to content

Commit

Permalink
Update transpile.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Dec 19, 2021
1 parent b2615ad commit 7690f00
Showing 1 changed file with 53 additions and 13 deletions.
66 changes: 53 additions & 13 deletions lib/transpile/transpile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Token, tokenize } from "./tokenize/mod.ts";
import { Cartridge } from "./cartridge/mod.ts";
import { Lexicon, Token, tokenize } from "./tokenize/mod.ts";
import { Cartridge, CartridgeEvent } from "./cartridge/mod.ts";
import { TextBuilder } from "./text_builder/mod.ts";
import type { FartTokenGenerator } from "./tokenize/mod.ts";

Expand All @@ -11,11 +11,18 @@ export interface FartOptions {
preserveComments: boolean;
}

const assertKind = (token: Token | null, lexeme: Lexicon): Token => {
if (token === null || token.kind !== lexeme) {
throw new Error(`Expected token kind ${lexeme}, got ${token}`);
}
return token;
};

class TranspilationContext {
constructor(
public tokenizer?: FartTokenGenerator,
public builder?: TextBuilder,
public currentToken: Token | null = null,
public currentToken: IteratorResult<Token, Token> | null = null,
public prevTokens: Token[] = [],
) {}

Expand All @@ -26,11 +33,12 @@ class TranspilationContext {
this.prevTokens !== undefined &&
this.currentToken !== undefined
) {
this.prevTokens.push(this.currentToken);
this.prevTokens.push(this.currentToken.value);
}
const token = this.tokenizer.next().value;
if (token !== undefined) {
return this.currentToken = token;
const curr = this.tokenizer.next();
if (curr.value !== undefined) {
this.currentToken = curr;
return curr.value;
}
}
return null;
Expand All @@ -53,10 +61,42 @@ export const transpile = (
code: string,
// options: FartOptions,
): string => {
const ctx = new TranspilationContext(
tokenize(code),
new TextBuilder(new Cartridge()),
);
console.log({ ctx });
return "";
const builder = new TextBuilder(new Cartridge());
const ctx = new TranspilationContext(tokenize(code), builder);
do {
switch (ctx.nextToken()?.kind) {
case Lexicon.Load: {
const loader = assertKind(
ctx.currentToken?.value as Token,
Lexicon.Load,
);
const source = assertKind(ctx.nextToken(), Lexicon.TextLiteral);
const opener = assertKind(ctx.nextToken(), Lexicon.StructOpener);
ctx.builder?.append(
CartridgeEvent.StructOpen,
[loader, source, opener],
[],
);
ctx.nextTuple();
break;
}
case Lexicon.TypeDefiner: {
const definer = assertKind(
ctx.currentToken?.value as Token,
Lexicon.TypeDefiner,
);
const ident = assertKind(ctx.nextToken(), Lexicon.Identifier);
const opener = assertKind(ctx.nextToken(), Lexicon.StructOpener);
ctx.builder?.append(
CartridgeEvent.StructOpen,
[definer, ident, opener],
[],
);
ctx.nextStruct();
break;
}
}
} while (!ctx.currentToken?.done);

return ctx.builder?.export() ?? "";
};

1 comment on commit 7690f00

@deno-deploy
Copy link

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

Please sign in to comment.