Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 19, 2024
1 parent 96e5268 commit 28bc35a
Show file tree
Hide file tree
Showing 21 changed files with 1,544 additions and 817 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"8.3.2","version":"3.47.1","indent":" ","lineEnding":"\n","rules":{"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"attribute_placement":"ignore","on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"test\/file\/code\/quicksort\/quicksort.php":"4282e04e5e1112bf0dad02375c761f4a"}}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,19 @@ RUN ln -s /home/python/venv/bin/antlr4-parse /usr/bin/antlr4-parse

RUN /home/python/venv/bin/pip install patool
RUN ln -s /home/python/venv/bin/patool /usr/bin/patool

RUN apt-get install apt-transport-https
RUN wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/dart.gpg
RUN echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | tee /etc/apt/sources.list.d/dart_stable.list
RUN apt-get update -y -q
RUN apt-get -y install dart

RUN apt-get -y install php-cli
RUN curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
RUN HASH=`curl -sS https://composer.github.io/installer.sig` \
&& php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

RUN wget https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases/download/v3.13.0/php-cs-fixer.phar -O php-cs-fixer
RUN chmod a+x php-cs-fixer
RUN mv php-cs-fixer /usr/local/bin/php-cs-fixer
44 changes: 44 additions & 0 deletions code/node/call/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import {
} from '~/code/shared/type/call/compile'
import {
BuildFormatInputOutput,
CompileCModel,
CompileCppModel,
CompileJavaModel,
CompileRustModel,
CompileSwiftModel,
compileC,
compileCpp,
compileJava,
compileRust,
compileSwift,
} from '..'
import kink from '~/code/shared/kink'
Expand All @@ -22,6 +30,42 @@ export async function compileInternal(source: BuildFormatInputOutput) {
const input = CompileSwiftModel.parse(source)
return await compileSwift(input)
}
case 'rust': {
const input = CompileRustModel.parse(source)
return await compileRust(input)
}
case 'c': {
const input = CompileCModel.parse(source)
return await compileC(input)
}
case 'cpp': {
const input = CompileCppModel.parse(source)
return await compileCpp(input)
}
case 'cs': {
// const input = CompileJavaModel.parse(source)
// return await compileJava(input)
}
case 'java': {
const input = CompileJavaModel.parse(source)
return await compileJava(input)
}
case 'haskell': {
// const input = CompileJavaModel.parse(source)
// return await compileJava(input)
}
case 'go': {
// const input = CompileJavaModel.parse(source)
// return await compileJava(input)
}
case 'kotlin': {
// const input = CompileJavaModel.parse(source)
// return await compileJava(input)
}
case 'clojure': {
// const input = CompileJavaModel.parse(source)
// return await compileJava(input)
}
}

throw kink('task_not_implemented', {
Expand Down
39 changes: 36 additions & 3 deletions code/node/code.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stripAnsiFromText } from '../cli/logging.js'
import {
FormatRust,
CompileLlvmIrToAssembly,
Expand All @@ -12,8 +13,16 @@ import {
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'
Expand Down Expand Up @@ -181,13 +190,37 @@ 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: FormatRust) {
const [cmd] = buildCommandToFormatRust(input)
return await handleCommand(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 })
}
}
}
59 changes: 55 additions & 4 deletions code/shared/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CompileC,
CompileCpp,
CompileLlvmIrToAssembly,
CompileRust,
CompileSwift,
FormatAssembly,
FormatCodeWithClangFormat,
Expand Down Expand Up @@ -31,7 +32,7 @@ export function buildCommandToCompileSwift(input: CompileSwift) {

if (input.output.format === 'assembly') {
cmd.list.push(`-emit-assembly`)
} else if (input.output.format === 'llvm') {
} else if (input.output.format === 'llvm-ir') {
cmd.list.push(`-emit-ir`)
}

Expand Down Expand Up @@ -63,6 +64,44 @@ export function buildCommandToFormatRust(input: FormatRust) {
return [cmd]
}

export function buildCommandToCompileRust(input: CompileRust) {
const cmd = getCommand('rustc')

if (input.output.format === 'assembly') {
cmd.list.push(`--emit`, `asm`)
} else if (input.output.format === 'llvm-ir') {
cmd.list.push(`--emit`, 'llvm-ir')
} else if (input.output.format === 'llvm-bitcode') {
cmd.list.push(`--emit`, 'llvm-bc')
} else if (input.output.format === 'mir') {
cmd.list.push(`--emit`, 'mir')
}

if (input.output.optimize) {
cmd.list.push('-O')
}

if (input.explain) {
cmd.list.push('--explain')
}

if (input.output.target) {
cmd.list.push('--target', input.output.target)
}

// cmd.list.push(`--error-format`, `json`)
cmd.list.push(`--color`, `always`)
cmd.list.push(`--diagnostic-width`, String(92))

cmd.list.push(
`"${input.input.file.path}"`,
'-o',
`"${input.output.file.path}"`,
)

return [cmd]
}

export function buildCommandToFormatPython(input: FormatPython) {
const cmd = getCommand('black')
cmd.list.push(input.input.file.path)
Expand Down Expand Up @@ -90,12 +129,18 @@ export async function buildCommandToCompileC(input: CompileC) {
const cmd = getCommand(`clang`)
cmd.list.push(`-O${input.optimizationLevel}`)

if (input.output.format === 'llvm') {
if (input.output.format === 'llvm-ir') {
cmd.list.push(`-S`, `-emit-llvm`)
} else if (input.output.format === 'llvm-bitcode') {
cmd.list.push(`-c`, `-emit-llvm`)
} else if (input.output.format === 'assembly') {
cmd.list.push(`-S`)
}

if (input.output.architecture) {
cmd.list.push(`-target`, input.output.architecture)
}

if (input.output.syntax && input.output.format === 'assembly') {
cmd.list.push(`-masm`, `${input.output.syntax}`)
}
Expand All @@ -117,12 +162,18 @@ export async function buildCommandToCompileCpp(input: CompileCpp) {
const cmd = getCommand(`clang++`)
cmd.list.push(`-O${input.optimizationLevel}`)

if (input.output.format === 'llvm') {
if (input.output.format === 'llvm-ir') {
cmd.list.push(`-S`, `-emit-llvm`)
} else if (input.output.format === 'llvm-bitcode') {
cmd.list.push(`-c`, `-emit-llvm`)
} else if (input.output.format === 'assembly') {
cmd.list.push(`-S`)
}

if (input.output.architecture) {
cmd.list.push(`-target`, input.output.architecture)
}

if (input.output.syntax && input.output.format === 'assembly') {
cmd.list.push(`-masm`, `${input.output.syntax}`)
}
Expand All @@ -142,7 +193,7 @@ export async function buildCommandToCompileCpp(input: CompileCpp) {

// rubocop --autocorrect file.rb

export async function buildCommandToDecompileBinary() {
export async function buildCommandToDisassembleBinary() {
const cmd = getCommand(`objdump`)
cmd.list.push(
`-S`,
Expand Down
4 changes: 4 additions & 0 deletions code/shared/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const COMMAND_NAME = [
'patool',
'identify',
'perltidy',
'php-cs-fixer',
] as const

export type CommandName = (typeof COMMAND_NAME)[number]
Expand Down Expand Up @@ -77,6 +78,7 @@ export const COMMAND: Record<CommandName, Array<string> | undefined> = {
'ebook-convert': ['ebook-convert'],
soffice: ['soffice'],
jupyter: ['jupyter'],
'php-cs-fixer': ['php-cs-fixer'],
docx2pdf: process.platform.match(/win32|darwin/)
? ['docx2pdf']
: undefined,
Expand All @@ -102,3 +104,5 @@ export function setCommand(
export type Command = { name: CommandName; list: CommandCall }
export type CommandCall = Array<string>
export type CommandList = Array<Command>

// php-cs-fixer fix test/file/code/quicksort/quicksort.php
16 changes: 8 additions & 8 deletions code/shared/type/call/compile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {
CompressMp4WithFfmpeg,
CompileDocumentWithCalibre,
CalibreInputFormat,
CalibreOutputFormat,
FfmpegFormat,
ImageMagickInputFormat,
ImageMagickOutputFormat,
CompileSwift,
CompileRust,
CompileC,
BackendCompilationOutput,
RustOutputFormat,
CompileCpp,
} from '../cast'

// // https://www.reddit.com/r/typescript/comments/199eutl/typescript_error_type_string_cannot_be_used_to/
Expand All @@ -21,14 +16,19 @@ export type CompileFormat = {
}
rust: {
input: 'rust'
output: BackendCompilationOutput
output: RustOutputFormat
extend: CompileRust
}
c: {
input: 'c'
output: BackendCompilationOutput
extend: CompileC
}
cpp: {
input: 'cpp'
output: BackendCompilationOutput
extend: CompileCpp
}
}

export type ExtractBySubKey<
Expand Down
Loading

0 comments on commit 28bc35a

Please sign in to comment.