Add basic Racket support (#4098)

This commit is contained in:
J. Ryan Stinnett
2022-10-05 08:32:45 +01:00
committed by GitHub
parent 99ba052480
commit 80dd763985
11 changed files with 188 additions and 1 deletions

3
.github/labeler.yml vendored
View File

@@ -134,6 +134,9 @@ lang-pony:
lang-python:
- lib/compilers/python.js
- etc/config/python.*.properties
lang-racket:
- lib/compilers/racket.ts
- etc/config/racket.*.properties
lang-ruby:
- lib/compilers/ruby.js
- etc/config/ruby.*.properties

View File

@@ -19,7 +19,8 @@ If you want to add a new language to the site, you should follow this steps:
- If the language is supported by Monaco Editor (You can find the list
[here](https://github.com/microsoft/monaco-editor/tree/main/src/basic-languages)), you should add it to the list of
languages inside the `MonacoEditorWebpackPlugin` config in `webpack.config.js`
languages inside the `MonacoEditorWebpackPlugin` config in
`webpack.config.esm.js`
- If not, you should implement your own language mode; see `static/modes/asm-mode.js` as an example. Don't forget to
_require_ your mode file in `static/modes/_all.ts`, in alphabetical order
- `language-key` is how your language will be referred internally by the code. In the rest of this document, replace

View File

@@ -0,0 +1,12 @@
compilers=&racket
defaultCompiler=racket86
group.racket.groupName=Racket
group.racket.compilers=racket86
group.racket.isSemVer=true
group.racket.baseName=Racket
group.racket.licenseName=MIT and Apache 2
group.racket.licenseLink=https://docs.racket-lang.org/license/
compiler.racket86.semver=8.6
compiler.racket86.exe=/opt/compiler-explorer/racket/racket-8.6/bin/racket
compiler.racket86.raco=/opt/compiler-explorer/racket/racket-8.6/bin/raco

View File

@@ -0,0 +1,13 @@
compilers=racket
compilerType=racket
versionFlag=--version
versionRe=Racket v\d+\.\d+(\.\d+)?
interpreted=true
supportsBinary=false
supportsExecute=false
compiler.racket.name=Racket
compiler.racket.exe=/usr/local/bin/racket
compiler.racket.raco=/usr/local/bin/raco

View File

@@ -0,0 +1,5 @@
#lang racket/base
;; Type your code here, or load an example.
(define (square num)
(* num num))

View File

@@ -79,6 +79,7 @@ export {PonyCompiler} from './pony';
export {PPCICompiler} from './ppci';
export {PtxAssembler} from './ptxas';
export {PythonCompiler} from './python';
export {RacketCompiler} from './racket';
export {RGACompiler} from './rga';
export {RubyCompiler} from './ruby';
export {RustcCgGCCCompiler} from './rustc-cg-gcc';

122
lib/compilers/racket.ts Normal file
View File

@@ -0,0 +1,122 @@
// Copyright (c) 2022, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import path from 'path';
import {CompilationResult, ExecutionOptions} from '../../types/compilation/compilation.interfaces';
import {ParseFilters} from '../../types/features/filters.interfaces';
import {BaseCompiler} from '../base-compiler';
import {logger} from '../logger';
export class RacketCompiler extends BaseCompiler {
private raco: string;
static get key() {
return 'racket';
}
constructor(info, env) {
// Disable output filters, as they currently don't do anything
if (!info.disabledFilters) {
info.disabledFilters = ['labels', 'directives', 'commentOnly', 'trim'];
}
super(info, env);
this.raco = this.compilerProps(`compiler.${this.compiler.id}.raco`);
}
override optionsForFilter(filters: ParseFilters, outputFilename: string, userOptions?: string[]): string[] {
// We currently always compile to bytecode first and then decompile.
// Forcing `binary` on like this ensures `objdump` will be called for
// the decompilation phase.
filters.binary = true;
return [];
}
override supportsObjdump(): boolean {
return true;
}
override getSharedLibraryPathsAsArguments(libraries: object[], libDownloadPath?: string): string[] {
return [];
}
override async runCompiler(
compiler: string,
options: string[],
inputFilename: string,
execOptions: ExecutionOptions,
): Promise<CompilationResult> {
if (!execOptions) {
execOptions = this.getDefaultExecOptions();
}
if (!execOptions.customCwd) {
execOptions.customCwd = path.dirname(inputFilename);
}
// Compile to bytecode via `raco make`
options.unshift('make');
const makeResult = await this.exec(this.raco, options, execOptions);
return this.transformToCompilationResult(makeResult, inputFilename);
}
override getOutputFilename(dirPath: string, outputFilebase: string, key?: any): string {
return path.join(dirPath, 'compiled', `${this.compileFilename.replace('.', '_')}.zo`);
}
override async objdump(
outputFilename: any,
result: any,
maxSize: number,
intelAsm: any,
demangle: any,
filters: ParseFilters,
): Promise<any> {
// Decompile to assembly via `raco decompile` with `disassemble` package
const execOptions: ExecutionOptions = {
maxOutput: maxSize,
customCwd: (result.dirPath as string) || path.dirname(outputFilename),
};
const decompileResult = await this.exec(this.raco, ['decompile', outputFilename], execOptions);
if (decompileResult.code) {
logger.error('Error decompiling via `raco decompile`', decompileResult);
result.asm = `<No output: \`raco decompile\` returned ${decompileResult.code}>`;
}
result.objdumpTime = decompileResult.execTime;
result.asm = this.postProcessObjdumpOutput(decompileResult.stdout);
return result;
}
override processAsm(result: any, filters: any, options: any) {
// TODO: Process and highlight decompiled output
return {
asm: [{text: result.asm}],
};
}
}

View File

@@ -471,6 +471,17 @@ const definitions: Record<LanguageKey, LanguageDefinition> = {
previewFilter: null,
monacoDisassembly: null,
},
racket: {
name: 'Racket',
monaco: 'scheme',
extensions: ['.rkt'],
alias: [],
logoUrl: 'racket.svg',
logoUrlDark: null,
formatter: null,
previewFilter: null,
monacoDisassembly: 'scheme',
},
ruby: {
name: 'Ruby',
monaco: 'ruby',

View File

@@ -62,6 +62,7 @@ export type LanguageKey =
| 'pascal'
| 'pony'
| 'python'
| 'racket'
| 'ruby'
| 'rust'
| 'scala'

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="circle_pieces" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
y="0px" width="511.875px" height="511.824px" viewBox="0 0 511.875 511.824" enable-background="new 0 0 511.875 511.824"
xml:space="preserve">
<circle id="circle" fill="#FFFFFF" cx="256.252" cy="255.986" r="253.093"/>
<path id="blue-piece" fill="#3E5BA9" d="M455.398,412.197c33.792-43.021,53.946-97.262,53.946-156.211
c0-139.779-113.313-253.093-253.093-253.093c-30.406,0-59.558,5.367-86.566,15.197C272.435,71.989,408.349,247.839,455.398,412.197z
"/>
<path id="left-red-piece" fill="#9F1D20" d="M220.003,164.337c-39.481-42.533-83.695-76.312-130.523-98.715
C36.573,112.011,3.159,180.092,3.159,255.986c0,63.814,23.626,122.104,62.597,166.623
C100.111,319.392,164.697,219.907,220.003,164.337z"/>
<path id="bottom-red-piece" fill="#9F1D20" d="M266.638,221.727c-54.792,59.051-109.392,162.422-129.152,257.794
c35.419,18.857,75.84,29.559,118.766,29.559c44.132,0,85.618-11.306,121.74-31.163C357.171,381.712,317.868,293.604,266.638,221.727
z"/>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -64,6 +64,7 @@ const plugins = [
'dart',
'typescript',
'solidity',
'scheme',
],
filename: isDev ? '[name].worker.js' : `[name]${webjackJsHack}worker.[contenthash].js`,
}),