-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f8dd96
commit dadf556
Showing
11 changed files
with
201 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { | ||
ExtendDescriptor, | ||
ImportName, | ||
ImportSource, | ||
LabDescriptor, | ||
ProcedureDescriptor, | ||
VariableDescriptor, | ||
} from "./types.ts"; | ||
import { importsOf } from "./imports.ts"; | ||
|
||
export const LAB_IMPORT_SOURCE = "./labs.ts"; | ||
|
||
export function generateLab(descriptor: LabDescriptor): string { | ||
const instructions = generateInstructions(descriptor); | ||
return `import { Lab } from "${ | ||
descriptor.labsImportSource ?? LAB_IMPORT_SOURCE | ||
}"; | ||
${generateImports(importsOf(descriptor))} | ||
export const ${descriptor.name} = new Lab()${ | ||
instructions.length === 0 ? "" : `\n${indent(...instructions)}` | ||
};`; | ||
} | ||
|
||
export function generateImports( | ||
imports: Map<ImportSource, Set<ImportName>>, | ||
): string { | ||
return Array.from(imports) | ||
.toSorted(([a], [b]) => a.localeCompare(b)) | ||
.map(([importSource, names]) => { | ||
return generateImport(Array.from(names).toSorted(), importSource); | ||
}) | ||
.join("\n"); | ||
} | ||
|
||
export function generateImport(names: string[], importSource: string): string { | ||
return `import { ${names.join(", ")} } from "${importSource}"`; | ||
} | ||
|
||
export function generateInstructions(descriptor: LabDescriptor): string[] { | ||
const instructions: string[] = []; | ||
if (descriptor.extends) { | ||
instructions.push(...generateExtends(descriptor.extends)); | ||
} | ||
|
||
if (descriptor.variables) { | ||
instructions.push(...generateVariables(descriptor.variables)); | ||
} | ||
|
||
if (descriptor.procedures) { | ||
instructions.push(...generateProcedures(descriptor.procedures)); | ||
} | ||
|
||
return instructions; | ||
} | ||
|
||
export function generateExtends(descriptors: ExtendDescriptor[]): string[] { | ||
return descriptors.map((descriptor) => generateExtend(descriptor)); | ||
} | ||
|
||
export function generateExtend(descriptor: ExtendDescriptor): string { | ||
return `.extend(${descriptor.import.name})`; | ||
} | ||
|
||
export function generateVariables(descriptors: VariableDescriptor[]): string[] { | ||
return descriptors.map((descriptor) => generateVariable(descriptor)); | ||
} | ||
|
||
export function generateVariable(descriptor: VariableDescriptor): string { | ||
return `.variable(${descriptor.name}, ${descriptor.value})`; | ||
} | ||
|
||
export function generateProcedures( | ||
descriptors: ProcedureDescriptor[], | ||
): string[] { | ||
return descriptors.map((descriptor) => generateProcedure(descriptor)); | ||
} | ||
|
||
export function generateProcedure(descriptor: ProcedureDescriptor): string { | ||
return `.procedure(${descriptor.name}, ${descriptor.import.name})`; | ||
} | ||
|
||
export function indent(...lines: string[]): string { | ||
return lines.map((line) => ` ${line}`).join("\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ImportName, ImportSource, LabDescriptor } from "./types.ts"; | ||
|
||
export function importsOf( | ||
descriptor: LabDescriptor, | ||
): Map<ImportSource, Set<ImportName>> { | ||
const imports = new Map<ImportSource, Set<ImportName>>(); | ||
for (const extend of descriptor.extends ?? []) { | ||
if (!imports.has(extend.import.source)) { | ||
imports.set(extend.import.source, new Set()); | ||
} | ||
|
||
imports.get(extend.import.source)!.add(extend.import.name); | ||
} | ||
|
||
for (const procedure of descriptor.procedures ?? []) { | ||
if (!imports.has(procedure.import.source)) { | ||
imports.set(procedure.import.source, new Set()); | ||
} | ||
|
||
imports.get(procedure.import.source)!.add(procedure.import.name); | ||
} | ||
|
||
return imports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface LabDescriptor { | ||
name: string; | ||
labsImportSource?: ImportSource; | ||
extends?: ExtendDescriptor[]; | ||
variables?: VariableDescriptor[]; | ||
procedures?: ProcedureDescriptor[]; | ||
} | ||
|
||
export interface VariableDescriptor { | ||
name: string; | ||
value: string; | ||
} | ||
|
||
export interface ExtendDescriptor { | ||
import: Importable; | ||
} | ||
|
||
export interface ProcedureDescriptor { | ||
name: string; | ||
import: Importable; | ||
} | ||
|
||
export interface Importable { | ||
name: ImportName; | ||
source: ImportSource; | ||
} | ||
|
||
export type ImportSource = string; | ||
export type ImportName = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tasks": { | ||
"example": "deno run ./example/example.ts", | ||
"generate:example": "deno run -Ar https://deno.land/x/generate/cli/main.ts ./example/gen.ts" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { generateLab } from "../codegen/codegen.ts"; | ||
|
||
await Deno.writeTextFile( | ||
"./my_lab.ts", | ||
generateLab({ | ||
name: "myLab", | ||
labsImportSource: "../labs.ts", | ||
extends: [ | ||
{ import: { name: "notesLab", source: "../notes.ts" } }, | ||
{ import: { name: "linksLab", source: "../links.ts" } }, | ||
], | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { myLab } from "./my_lab.ts"; | ||
|
||
if (import.meta.main) { | ||
main(); | ||
} | ||
|
||
function main() { | ||
const note1 = myLab.execute( | ||
"notes.add", | ||
{ content: "Hello, world!" }, | ||
); | ||
const note2 = myLab.execute( | ||
"notes.add", | ||
{ content: "Goodbye, world!" }, | ||
); | ||
myLab.execute("links.link", { ids: [note1.id, note2.id] }); | ||
|
||
console.log(`Note 1: ${note1.id}`); | ||
console.log(`Note 2: ${note2.id}`); | ||
console.log("Linked notes:"); | ||
for (const linkable of myLab.execute("links.list", {})) { | ||
console.log( | ||
`- ${linkable.id} => ${linkable.links.map(({ id }) => id).join(", ")}`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//deno:generate deno run --allow-write codegen.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Lab } from "../labs.ts"; | ||
import { linksLab } from "../links.ts"; | ||
import { notesLab } from "../notes.ts"; | ||
|
||
export const myLab = new Lab() | ||
.extend(notesLab) | ||
.extend(linksLab); |