From 08e4e4e30d98c6a4bc7b8961012725ed2f827610 Mon Sep 17 00:00:00 2001 From: Lance Pollard Date: Fri, 19 Jan 2024 02:36:35 -0800 Subject: [PATCH] more --- code/cli/task.ts | 2 +- code/node/action/compile.ts | 191 ++++++++++++++++++++++++++- code/node/action/format/code.ts | 42 ++++++ code/node/archive.ts | 4 +- code/node/code.ts | 226 -------------------------------- code/node/document.ts | 5 - code/node/index.ts | 1 - 7 files changed, 230 insertions(+), 241 deletions(-) create mode 100644 code/node/action/format/code.ts delete mode 100644 code/node/code.ts diff --git a/code/cli/task.ts b/code/cli/task.ts index 39b1224..e0a05b6 100644 --- a/code/cli/task.ts +++ b/code/cli/task.ts @@ -56,9 +56,9 @@ import { buildInputMapping, transferInput, } from './parse.js' -import { formatRust } from '../node/code.js' import { inspectSystem } from '../node/system.js' import { compile, compileInternal } from '../node/action/compile.js' +import { formatRust } from '../node/action/format/code.js' export const CONVERT_KEY: Record = { i: { line: ['input', 'file', 'path'] }, diff --git a/code/node/action/compile.ts b/code/node/action/compile.ts index e3e2c1a..0fa4a1f 100644 --- a/code/node/action/compile.ts +++ b/code/node/action/compile.ts @@ -9,14 +9,142 @@ import { CompileJavaModel, CompileRustModel, CompileSwiftModel, - compileC, - compileCpp, - compileJava, - compileRust, - compileSwift, } from '..' -import kink from '~/code/shared/kink' import { flattenObjectSafe } from '~/code/shared/object' +import { + CompileLlvmIrToAssembly, + buildCommandToCompileLlvmIrToAssembly, + FormatKotlin, + CompileSwift, + buildCommandToCompileSwift, + buildCommandToCompileRust, + CompileRust, + CompileC, + CompileCpp, + CompileJava, +} from '../../shared/index.js' +import kink from '../../shared/kink.js' +import { handleCommand } from '../command.js' +import { ChildProcessError } from '../process.js' +import ansiToHtml from 'ansi-to-html' +// import Parser from 'tree-sitter' +// import JavaScript from 'tree-sitter-javascript' +// import Swift from 'tree-sitter-swift' +// import fsp from 'fs/promises' + +// // compileToAsm({ +// // input: { +// // format: 'js', +// // file: { path: 'test/file/code/quicksort/quicksort.js' }, +// // }, +// // }) + +// // compileToAsm({ +// // input: { +// // format: 'js', +// // file: { path: 'test/file/code/quicksort/quicksort.swift' }, +// // }, +// // }) + +// const TREE_SITTER_LANGUAGE: Record = { +// js: JavaScript, +// swift: Swift, +// } + +// export async function compileToAsm(input: CompileToAst) { +// const parser = new Parser() +// parser.setLanguage(TREE_SITTER_LANGUAGE[input.input.format]) +// const code = await fsp.readFile(input.input.file.path, 'utf-8') +// const tree = parser.parse(code) +// return traverse(tree.rootNode) + +// function traverse(node) { +// const out: any = { +// form: node.type, +// base: node.startPosition, +// head: node.endPosition, +// } +// if (node.type.match(/^[\w_]+$/)) { +// switch (node.type) { +// case 'identifier': +// case 'number': +// case 'string': +// case 'comment': +// case 'string_fragment': +// case 'template_string': +// case 'simple_identifier': +// case 'var': +// case 'let': +// case 'const': +// case 'true': +// case 'return': +// case 'false': +// out.text = node.text +// break +// } +// } else { +// out.text = node.text +// } +// if (node.children?.length) { +// out.nest = [] +// for (const child of node.children) { +// out.nest.push(traverse(child)) +// } +// } +// return out +// } +// } + +// function define( +// lang: string, +// build: (x) => Array, +// run: (cmd: Array) => Promise, +// ) { +// const IO = BuildBaseFileInputOutputModel.superRefine( +// transform_input_output_file(lang, lang), +// ) + +// CALL_CODE[String(`/${lang}/format`)] = async (source: any) => { +// const input = IO.parse(source) +// const cmd = build(input) +// await run(cmd) +// } +// } + +// define('kotlin', FormatKotlin, runFormatKotlinCommand) +// define('swift', FormatSwift, runFormatSwiftCommand) +// define('rust', FormatRust, runFormatRustCommand) +// define('python', FormatPython, runFormatPythonCommand) +// define('ruby', FormatRuby, runFormatRubyCommand) +// define( +// 'assembly', +// FormatAssembly, +// runFormatAssemblyCommand, +// ) +// define('c', FormatC, runFormatCCommand) +// define('cpp', FormatCpp, runFormatCCommand) +// define('java', FormatJava, runFormatCCommand) + +// CSharp: .cs +// Java: .java +// JavaScript: .mjs .js .ts +// Json: .json +// Objective-C: .m .mm +// Proto: .proto .protodevel +// TableGen: .td +// TextProto: .textpb .pb.txt .textproto .asciipb +// Verilog: .sv .svh .v .vh + +// cargo rustc --release -- --emit asm +// rustc --emit=asm +// rustc --emit=llvm-ir +// cargo rustc -- --emit=llvm-ir + +// objdump -S --x86-asm-syntax=intel example.out +// objdump -f assert +// objdump -x assert + +// file magic numbers: https://gist.github.com/Qti3e/6341245314bf3513abb080677cd1c93b export async function compile( source: Compile, @@ -73,3 +201,54 @@ export async function compileInternal(source: BuildFormatInputOutput) { link: Object.keys(flattenObjectSafe(source) as object), }) } + +export async function compileLlvmIrToAssembly( + input: CompileLlvmIrToAssembly, +) { + const list = buildCommandToCompileLlvmIrToAssembly(input) + for (const cmd of list) { + await handleCommand(cmd) + } + return input.output.file.path +} + +// wat2wasm simple.wat -o simple.wasm + +export async function compileKotlin(input: FormatKotlin) { + // return await runFormatKotlinCommand(cmd) +} + +export async function compileC(input: CompileC) { + // return await runFormatKotlinCommand(cmd) +} + +export async function compileJava(input: CompileJava) { + // return await runFormatKotlinCommand(cmd) +} + +export async function compileCpp(input: CompileCpp) { + // return await runFormatKotlinCommand(cmd) +} + +export async function compileSwift(input: CompileSwift) { + const [cmd] = buildCommandToCompileSwift(input) + handleCommand(cmd!) + // return await runFormatSwiftCommand(cmd) +} + +export async function compileRust(input: CompileRust) { + const [cmd] = buildCommandToCompileRust(input) + try { + await handleCommand(cmd!) + } catch (e) { + if (e instanceof ChildProcessError) { + if (e.data.stderr) { + throw kink('compilation_error', { note: e.data.stderr }) + } else { + throw kink('compilation_error', { note: e.data.stdout ?? '' }) + } + } else if (e instanceof Error) { + throw kink('compilation_error', { note: e.message }) + } + } +} diff --git a/code/node/action/format/code.ts b/code/node/action/format/code.ts new file mode 100644 index 0000000..b8fa4e3 --- /dev/null +++ b/code/node/action/format/code.ts @@ -0,0 +1,42 @@ +import { + FormatRust, + FormatKotlin, + FormatSwift, + buildCommandToFormatRust, + FormatPython, + FormatRuby, + FormatAssembly, + FormatC, +} from '../../../shared/index.js' +import { handleCommand } from '../../command.js' +// https://github.com/jgm/pandoc/tree/main/test +// https://github.com/SheetJS/test_files + +export async function formatKotlin(input: FormatKotlin) { + // return await runFormatKotlinCommand(cmd) +} + +export async function formatSwift(input: FormatSwift) { + // return await runFormatSwiftCommand(cmd) +} + +export async function formatRust(input: FormatRust) { + const [cmd] = buildCommandToFormatRust(input) + return await handleCommand(cmd!) +} + +export async function formatPython(input: FormatPython) { + // return await runFormatPythonCommand(cmd) +} + +export async function formatRuby(input: FormatRuby) { + // return await runFormatRubyCommand(cmd) +} + +export async function formatAssembly(input: FormatAssembly) { + // return await runFormatAssemblyCommand(cmd) +} + +export async function formatC(input: FormatC) { + // return await runFormatCCommand(cmd) +} diff --git a/code/node/archive.ts b/code/node/archive.ts index dd3b956..8dc3a24 100644 --- a/code/node/archive.ts +++ b/code/node/archive.ts @@ -6,7 +6,7 @@ import { handleCommand } from './command.js' // await run7zCommand(cmd) // } -export async function decompressWith7z(source) { +export async function archiveWith7z(source) { // const input = IO7z.parse(source) // const list = DecompressWith7z(input) // for (const cmd of list) { @@ -14,7 +14,7 @@ export async function decompressWith7z(source) { // } } -export async function decompressWithUnarchiver(source) { +export async function archiveWithUnarchiver(source) { // const input = IOUnarchiver.parse(source) // const list = DecompressWithUnarchiver(input) // for (const cmd of list) { diff --git a/code/node/code.ts b/code/node/code.ts deleted file mode 100644 index fc878aa..0000000 --- a/code/node/code.ts +++ /dev/null @@ -1,226 +0,0 @@ -import { stripAnsiFromText } from '../cli/logging.js' -import { - FormatRust, - CompileLlvmIrToAssembly, - buildCommandToCompileLlvmIrToAssembly, - FormatKotlin, - FormatSwift, - buildCommandToFormatRust, - FormatPython, - FormatRuby, - FormatAssembly, - FormatC, - CompileToAst, - CompileSwift, - buildCommandToCompileSwift, - buildCommandToCompileRust, - CompileRust, - CompileC, - CompileCpp, - CompileJava, -} from '../shared/index.js' -import kink from '../shared/kink.js' -import { handleCommand } from './command.js' -import { ChildProcessError } from './process.js' -import ansiToHtml from 'ansi-to-html' -// import Parser from 'tree-sitter' -// import JavaScript from 'tree-sitter-javascript' -// import Swift from 'tree-sitter-swift' -// import fsp from 'fs/promises' - -// // compileToAsm({ -// // input: { -// // format: 'js', -// // file: { path: 'test/file/code/quicksort/quicksort.js' }, -// // }, -// // }) - -// // compileToAsm({ -// // input: { -// // format: 'js', -// // file: { path: 'test/file/code/quicksort/quicksort.swift' }, -// // }, -// // }) - -// const TREE_SITTER_LANGUAGE: Record = { -// js: JavaScript, -// swift: Swift, -// } - -// export async function compileToAsm(input: CompileToAst) { -// const parser = new Parser() -// parser.setLanguage(TREE_SITTER_LANGUAGE[input.input.format]) -// const code = await fsp.readFile(input.input.file.path, 'utf-8') -// const tree = parser.parse(code) -// return traverse(tree.rootNode) - -// function traverse(node) { -// const out: any = { -// form: node.type, -// base: node.startPosition, -// head: node.endPosition, -// } -// if (node.type.match(/^[\w_]+$/)) { -// switch (node.type) { -// case 'identifier': -// case 'number': -// case 'string': -// case 'comment': -// case 'string_fragment': -// case 'template_string': -// case 'simple_identifier': -// case 'var': -// case 'let': -// case 'const': -// case 'true': -// case 'return': -// case 'false': -// out.text = node.text -// break -// } -// } else { -// out.text = node.text -// } -// if (node.children?.length) { -// out.nest = [] -// for (const child of node.children) { -// out.nest.push(traverse(child)) -// } -// } -// return out -// } -// } - -// function define( -// lang: string, -// build: (x) => Array, -// run: (cmd: Array) => Promise, -// ) { -// const IO = BuildBaseFileInputOutputModel.superRefine( -// transform_input_output_file(lang, lang), -// ) - -// CALL_CODE[String(`/${lang}/format`)] = async (source: any) => { -// const input = IO.parse(source) -// const cmd = build(input) -// await run(cmd) -// } -// } - -// define('kotlin', FormatKotlin, runFormatKotlinCommand) -// define('swift', FormatSwift, runFormatSwiftCommand) -// define('rust', FormatRust, runFormatRustCommand) -// define('python', FormatPython, runFormatPythonCommand) -// define('ruby', FormatRuby, runFormatRubyCommand) -// define( -// 'assembly', -// FormatAssembly, -// runFormatAssemblyCommand, -// ) -// define('c', FormatC, runFormatCCommand) -// define('cpp', FormatCpp, runFormatCCommand) -// define('java', FormatJava, runFormatCCommand) - -// CSharp: .cs -// Java: .java -// JavaScript: .mjs .js .ts -// Json: .json -// Objective-C: .m .mm -// Proto: .proto .protodevel -// TableGen: .td -// TextProto: .textpb .pb.txt .textproto .asciipb -// Verilog: .sv .svh .v .vh - -// cargo rustc --release -- --emit asm -// rustc --emit=asm -// rustc --emit=llvm-ir -// cargo rustc -- --emit=llvm-ir - -// objdump -S --x86-asm-syntax=intel example.out -// objdump -f assert -// objdump -x assert - -// file magic numbers: https://gist.github.com/Qti3e/6341245314bf3513abb080677cd1c93b - -export async function compileLlvmIrToAssembly( - input: CompileLlvmIrToAssembly, -) { - const list = buildCommandToCompileLlvmIrToAssembly(input) - for (const cmd of list) { - await handleCommand(cmd) - } - return input.output.file.path -} - -// https://github.com/jgm/pandoc/tree/main/test -// https://github.com/SheetJS/test_files - -export async function formatKotlin(input: FormatKotlin) { - // return await runFormatKotlinCommand(cmd) -} - -export async function formatSwift(input: FormatSwift) { - // return await runFormatSwiftCommand(cmd) -} - -export async function formatRust(input: FormatRust) { - const [cmd] = buildCommandToFormatRust(input) - return await handleCommand(cmd!) -} - -export async function formatPython(input: FormatPython) { - // return await runFormatPythonCommand(cmd) -} - -export async function formatRuby(input: FormatRuby) { - // return await runFormatRubyCommand(cmd) -} - -export async function formatAssembly(input: FormatAssembly) { - // return await runFormatAssemblyCommand(cmd) -} - -export async function formatC(input: FormatC) { - // return await runFormatCCommand(cmd) -} - -// wat2wasm simple.wat -o simple.wasm - -export async function compileKotlin(input: FormatKotlin) { - // return await runFormatKotlinCommand(cmd) -} - -export async function compileC(input: CompileC) { - // return await runFormatKotlinCommand(cmd) -} - -export async function compileJava(input: CompileJava) { - // return await runFormatKotlinCommand(cmd) -} - -export async function compileCpp(input: CompileCpp) { - // return await runFormatKotlinCommand(cmd) -} - -export async function compileSwift(input: CompileSwift) { - const [cmd] = buildCommandToCompileSwift(input) - handleCommand(cmd!) - // return await runFormatSwiftCommand(cmd) -} - -export async function compileRust(input: CompileRust) { - const [cmd] = buildCommandToCompileRust(input) - try { - await handleCommand(cmd!) - } catch (e) { - if (e instanceof ChildProcessError) { - if (e.data.stderr) { - throw kink('compilation_error', { note: e.data.stderr }) - } else { - throw kink('compilation_error', { note: e.data.stdout ?? '' }) - } - } else if (e instanceof Error) { - throw kink('compilation_error', { note: e.message }) - } - } -} diff --git a/code/node/document.ts b/code/node/document.ts index 0064556..b4e9c98 100644 --- a/code/node/document.ts +++ b/code/node/document.ts @@ -1,10 +1,5 @@ import fs from 'node:fs/promises' import { - ConvertDocumentWithCalibreModel, - ConvertDocumentWithLibreOfficeModel, - ConvertDocumentWithPandocModel, - ConvertLatexToPdfWithPdfLatexModel, - SlicePdfModel, ConvertDocumentWithCalibre, ConvertDocumentWithLibreOffice, ConvertDocumentWithPandoc, diff --git a/code/node/index.ts b/code/node/index.ts index 85e74ab..a9843d1 100644 --- a/code/node/index.ts +++ b/code/node/index.ts @@ -1,7 +1,6 @@ export * from '../shared/index.js' export * from './document.js' -export * from './code.js' export * from './archive.js' export * from './image.js' export * from './video.js'