Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 19, 2024
1 parent 7670fae commit 08e4e4e
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 241 deletions.
2 changes: 1 addition & 1 deletion code/cli/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Link> = {
i: { line: ['input', 'file', 'path'] },
Expand Down
191 changes: 185 additions & 6 deletions code/node/action/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = {
// 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<string>,
// run: (cmd: Array<string>) => Promise<void>,
// ) {
// 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<I extends CompileInputFormat>(
source: Compile<I>,
Expand Down Expand Up @@ -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 })
}
}
}
42 changes: 42 additions & 0 deletions code/node/action/format/code.ts
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions code/node/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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) {
// await handleCommand(cmd)
// }
}

export async function decompressWithUnarchiver(source) {
export async function archiveWithUnarchiver(source) {
// const input = IOUnarchiver.parse(source)
// const list = DecompressWithUnarchiver(input)
// for (const cmd of list) {
Expand Down
Loading

0 comments on commit 08e4e4e

Please sign in to comment.