From 7690f0028d4dddd17ae41736bfe2f2122a4f528c Mon Sep 17 00:00:00 2001 From: EthanThatOneKid <31261035+EthanThatOneKid@users.noreply.github.com> Date: Sun, 19 Dec 2021 01:47:28 -0800 Subject: [PATCH] Update transpile.ts --- lib/transpile/transpile.ts | 66 ++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/lib/transpile/transpile.ts b/lib/transpile/transpile.ts index 499ed44..2dde7d6 100644 --- a/lib/transpile/transpile.ts +++ b/lib/transpile/transpile.ts @@ -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"; @@ -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 | null = null, public prevTokens: Token[] = [], ) {} @@ -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; @@ -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() ?? ""; };