From 85beff34d0aabf71402a199de2c44297a71dee71 Mon Sep 17 00:00:00 2001 From: Ethan Davidson <31261035+EthanThatOneKid@users.noreply.github.com> Date: Thu, 23 Dec 2021 11:51:33 -0800 Subject: [PATCH] added some changes with 3 failures --- lib/transpile/cartridge/cartridge.ts | 15 ++++++- lib/transpile/cartridge/mod.ts | 2 + lib/transpile/transpile.ts | 58 +++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/lib/transpile/cartridge/cartridge.ts b/lib/transpile/cartridge/cartridge.ts index 8a0116f..e37732d 100644 --- a/lib/transpile/cartridge/cartridge.ts +++ b/lib/transpile/cartridge/cartridge.ts @@ -34,6 +34,17 @@ export type CartridgeEventReturnType = ( | null ); +// TODO: Refactor PropertyDefinition interface to be more strict using the +// list of possible definitions as a guide. +// Possible Property Definitions +// - example: string +// - example?: string +// - example: { nestedExample: string } +// - example: async % string; Promise +// - example: fn % async % string; () => Promise +// - example: fn % (a: string, async % string); (a: string) => Promise +// - example: fn % (cb: fn % (async % _), number); (cb: () => Promise) => number + export interface PropertyDefinition { optional?: boolean; modifier?: string; @@ -177,11 +188,11 @@ export class Cartridge { return executionResult ?? null; } - public getType(type: string): string | undefined { + public getType(type?: string): string | undefined { return this.typemap[type as ReservedType]; } - public getMod(mod: string): ModHandler | undefined { + public getMod(mod?: string): ModHandler | undefined { return this.typemap[mod as Modifier]; } } diff --git a/lib/transpile/cartridge/mod.ts b/lib/transpile/cartridge/mod.ts index 0ac3f5d..ded76b9 100644 --- a/lib/transpile/cartridge/mod.ts +++ b/lib/transpile/cartridge/mod.ts @@ -3,5 +3,7 @@ export type { CartridgeEventContext, CartridgeHandler, CartridgeHandlerMap, + ModHandler, + Modifier, PropertyDefinition, } from "./cartridge.ts"; diff --git a/lib/transpile/transpile.ts b/lib/transpile/transpile.ts index 78b0951..5da3d9e 100644 --- a/lib/transpile/transpile.ts +++ b/lib/transpile/transpile.ts @@ -1,6 +1,10 @@ import { Lexicon, Token, tokenize } from "./tokenize/mod.ts"; import { Cartridge, CartridgeEvent } from "./cartridge/mod.ts"; -import type { PropertyDefinition } from "./cartridge/mod.ts"; +import type { + ModHandler, + Modifier, + PropertyDefinition, +} from "./cartridge/mod.ts"; import { TextBuilder } from "./text_builder/mod.ts"; // import { Lang } from "../constants/lang.ts"; import { assertKind } from "./utils.ts"; @@ -40,6 +44,43 @@ export class TranspilationContext { return curr.value; } + public nextMod(currentToken?: Token): PropertyDefinition["value"] { + const initialToken = currentToken ?? this.nextToken(); + const mods: ModHandler[] = []; + let mod = this.cartridge.getMod(initialToken?.value); + while (mod !== undefined) { + mods.push(mod); + const modSymbol = assertKind(this.nextToken(), Lexicon.Modifier); + const wildToken = this.nextToken(); + + switch (wildToken?.kind) { + case Lexicon.Identifier: { + const result = mods.reduceRight( + (result: string, modify: ModHandler) => modify(result), + wildToken.value, + ); + return result; + } + + case Lexicon.TupleOpener: { + const results = + } + + } + mod = this.cartridge.getMod(this.nextToken()?.value); + } + } + + // public computeMods( + // tokens: Token[], + // ...mods: ModHandler[] + // ): string | undefined { + // return mods.reduceRight( + // (result: string[], mod: ModHandler) => [mod(...result)], + // tokens.map(({ value }) => this.cartridge.getType(value) ?? value), + // ).pop(); + // } + /** * Consumes the next struct, tuple, or value. */ @@ -63,6 +104,7 @@ export class TranspilationContext { // if (modifier !== undefined) { // if ident is known modifier, await nextModifier(); // } + def.value = wildToken.value; break; } @@ -86,20 +128,26 @@ export class TranspilationContext { const result: PropertyDefinition["struct"] = {}; while (true) { + // expects identifier or '}' const ident = assertKind( this.nextToken(), Lexicon.Identifier, Lexicon.StructCloser, ); - if (ident.kind === Lexicon.StructCloser) break; + if (ident.is(Lexicon.StructCloser)) { + break; + } + + // expects ':' or '?:' const propertyDefiner = assertKind( this.nextToken(), Lexicon.PropertyDefiner, Lexicon.PropertyOptionalDefiner, ); - // 1st token of right-hand expression + // 1st token of right-hand expression (e.g. identifier, text literal, or + // '{'). const wildToken = await this.nextToken(); switch (wildToken?.kind) { @@ -121,7 +169,7 @@ export class TranspilationContext { default: { throw new Error( - `Expected struct opener, tuple opener, or type value, but got ${wildToken}`, + `Expected struct opener or type value, but got ${wildToken}`, ); } } @@ -133,7 +181,7 @@ export class TranspilationContext { /** * @todo implement */ - public async nextTuple(): Promise { + public nextTuple(): PropertyDefinition["tuple"]{ return []; } }