From c2e9aabfd775349d2cc704761779671f87187465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20de=20Souza=20Villa=C3=A7a=20Medeiros?= Date: Sun, 25 Sep 2022 17:06:02 -0300 Subject: [PATCH] Add basic support for Hook programming language (#4075) * Add basic support for Hook programming language --- .github/labeler.yml | 4 + CONTRIBUTORS.md | 1 + etc/config/hook.amazon.properties | 21 ++++ etc/config/hook.defaults.properties | 20 +++ examples/hook/default.hk | 3 + lib/compilers/_all.js | 1 + lib/compilers/hook.ts | 55 +++++++++ lib/languages.ts | 11 ++ static/modes/_all.ts | 1 + static/modes/hook-mode.ts | 182 ++++++++++++++++++++++++++++ types/languages.interfaces.ts | 1 + 11 files changed, 300 insertions(+) create mode 100644 etc/config/hook.amazon.properties create mode 100644 etc/config/hook.defaults.properties create mode 100644 examples/hook/default.hk create mode 100644 lib/compilers/hook.ts create mode 100644 static/modes/hook-mode.ts diff --git a/.github/labeler.yml b/.github/labeler.yml index 27da72cce..682cd3471 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -75,6 +75,10 @@ lang-hlsl: - lib/compilers/hlsl.js - etc/config/hlsl.*.properties - static/modes/hlsl-mode.ts +lang-hook: + - lib/compilers/hook.ts + - etc/config/hook.*.properties + - static/modes/hook-mode.ts lang-jakt: - lib/compilers/jakt.ts - etc/config/jakt.*.properties diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index b39dfe33c..4b403e19e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -122,3 +122,4 @@ From oldest to newest contributor, we would like to thank: - [Ross Smyth](https://github.com/RossSmyth) - [Mike Urbach](https://github.com/mikeurbach) - [J. Ryan Stinnett](https://github.com/jryans) +- [Fábio S. V. Medeiros](https://github.com/fabiosvm) diff --git a/etc/config/hook.amazon.properties b/etc/config/hook.amazon.properties new file mode 100644 index 000000000..0b19fa7e2 --- /dev/null +++ b/etc/config/hook.amazon.properties @@ -0,0 +1,21 @@ + +interpreted=true +supportsBinary=false +supportsExecute=true +versionFlag=--version +needsMulti=false + +compilerType=hook +compilers=&hook +defaultCompiler=hook010 + +group.hook.compilers=hook010 +group.hook.groupName=Hook +group.hook.isSemVer=true +group.hook.baseName=Hook +group.hook.licenseName=MIT +group.hook.licenseLink=https://github.com/fabiosvm/hook-lang/blob/main/LICENSE + +compiler.hook010.exe=/opt/compiler-explorer/hook-0.1.0/bin/hook +compiler.hook010.semver=0.1.0 + diff --git a/etc/config/hook.defaults.properties b/etc/config/hook.defaults.properties new file mode 100644 index 000000000..6bd8287cc --- /dev/null +++ b/etc/config/hook.defaults.properties @@ -0,0 +1,20 @@ + +interpreted=true +supportsBinary=false +supportsExecute=true +versionFlag=--version +needsMulti=false + +compilerType=hook +compilers=&hook +defaultCompiler=hook010def + +group.hook.compilers=hook010def +group.hook.groupName=Hook +group.hook.isSemVer=true +group.hook.baseName=Hook +group.hook.licenseName=MIT +group.hook.licenseLink=https://github.com/fabiosvm/hook-lang/blob/main/LICENSE + +compiler.hook010def.exe=hook +compiler.hook010def.semver=0.1.0 diff --git a/examples/hook/default.hk b/examples/hook/default.hk new file mode 100644 index 000000000..d49200b88 --- /dev/null +++ b/examples/hook/default.hk @@ -0,0 +1,3 @@ +fn square(num) { + return num * num; +} diff --git a/lib/compilers/_all.js b/lib/compilers/_all.js index d7c034966..4056ecac7 100644 --- a/lib/compilers/_all.js +++ b/lib/compilers/_all.js @@ -58,6 +58,7 @@ export {GCCRSCompiler} from './gccrs'; export {GolangCompiler} from './golang'; export {HaskellCompiler} from './haskell'; export {HLSLCompiler} from './hlsl'; +export {HookCompiler} from './hook'; export {ISPCCompiler} from './ispc'; export {JaktCompiler} from './jakt'; export {JavaCompiler} from './java'; diff --git a/lib/compilers/hook.ts b/lib/compilers/hook.ts new file mode 100644 index 000000000..55489c1b1 --- /dev/null +++ b/lib/compilers/hook.ts @@ -0,0 +1,55 @@ +// 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'; + +export class HookCompiler extends BaseCompiler { + static get key(): string { + return 'hook'; + } + + override optionsForFilter(filters: ParseFilters): string[] { + return ['--dump']; + } + + override getOutputFilename(dirPath: string): string { + return path.join(dirPath, 'example.out'); + } + + override async runCompiler( + compiler: string, + options: string[], + inputFilename: string, + execOptions: ExecutionOptions, + ): Promise { + const dirPath = path.dirname(inputFilename); + const outputFilename = this.getOutputFilename(dirPath); + options.push(outputFilename); + return super.runCompiler(compiler, options, inputFilename, execOptions); + } +} diff --git a/lib/languages.ts b/lib/languages.ts index 296f2bf07..24470331c 100644 --- a/lib/languages.ts +++ b/lib/languages.ts @@ -350,6 +350,17 @@ const definitions: Record = { previewFilter: null, monacoDisassembly: null, }, + hook: { + name: 'Hook', + monaco: 'hook', + extensions: ['.hk', '.hook'], + alias: [], + logoUrl: null, + logoUrlDark: null, + formatter: null, + previewFilter: null, + monacoDisassembly: null, + }, ispc: { name: 'ispc', monaco: 'ispc', diff --git a/static/modes/_all.ts b/static/modes/_all.ts index 1dcd0390d..cb38d4e14 100644 --- a/static/modes/_all.ts +++ b/static/modes/_all.ts @@ -43,6 +43,7 @@ import './fortran-mode'; import './gccdump-rtl-gimple-mode'; import './haskell-mode'; import './hlsl-mode'; +import './hook-mode'; import './ispc-mode'; import './jakt-mode'; import './llvm-ir-mode'; diff --git a/static/modes/hook-mode.ts b/static/modes/hook-mode.ts new file mode 100644 index 000000000..c06c48162 --- /dev/null +++ b/static/modes/hook-mode.ts @@ -0,0 +1,182 @@ +// 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. + +const monaco = require('monaco-editor'); + +function definition() { + return { + defaultToken: 'invalid', + + keywords: [ + 'as', + 'break', + 'continue', + 'del', + 'do', + 'else', + 'false', + 'fn', + 'for', + 'from', + 'if', + 'if!', + 'loop', + 'match', + 'mut', + 'nil', + 'return', + 'struct', + 'true', + 'use', + 'val', + 'while', + 'while!', + ], + typeKeywords: [], + operators: [ + '..', + '.', + '|=', + '||', + '|', + '^=', + '^', + '&=', + '&&', + '&', + '=>', + '==', + '=', + '!=', + '!', + '>=', + '>>=', + '>>', + '>', + '<=', + '<<=', + '<<', + '<', + '+=', + '++', + '+', + '-=', + '--', + '-', + '*=', + '*', + '/=', + '/', + '~/=', + '~/', + '~', + '%=', + '%', + ], + + symbols: /[=>](?!@symbols)/, '@brackets'], + [ + /@symbols/, + { + cases: { + '@operators': 'operator', + '@default': '', + }, + }, + ], + + // numbers + [/\d*\.\d+([eE][-+]?\d+)?[fFdD]?/, 'number.float'], + [/0[xX][0-9a-fA-F_]*[0-9a-fA-F][Ll]?/, 'number.hex'], + [/0o[0-7_]*[0-7][Ll]?/, 'number.octal'], + [/0[bB][0-1_]*[0-1][Ll]?/, 'number.binary'], + [/\d+/, 'number'], + + // delimiter: after number because of .\d floats + [/[;,.]/, 'delimiter'], + + // strings + [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string + [/c?\\\\.*$/, 'string'], + [/c?"/, 'string', '@string'], + + // characters + [/'[^\\']'/, 'string'], + [/(')(@escapes)(')/, ['string', 'string.escape', 'string']], + [/'/, 'string.invalid'], + ], + + whitespace: [ + [/[ \r\n]+/, 'white'], + [/\/\*/, 'comment', '@comment'], + [/\/\+/, 'comment', '@comment'], + [/\/\/.*$/, 'comment'], + [/\t/, 'comment.invalid'], + ], + + comment: [ + [/[^/*]+/, 'comment'], + [/\/\*/, 'comment.invalid'], + [/[/*]/, 'comment'], + ], + + string: [ + [/[^\\"]+/, 'string'], + [/@escapes/, 'string.escape'], + [/\\./, 'string.escape.invalid'], + [/"/, 'string', '@pop'], + ], + }, + }; +} + +monaco.languages.register({id: 'hook'}); +monaco.languages.setMonarchTokensProvider('hook', definition()); + +export {}; diff --git a/types/languages.interfaces.ts b/types/languages.interfaces.ts index 77eeb1ec8..d99e550b7 100644 --- a/types/languages.interfaces.ts +++ b/types/languages.interfaces.ts @@ -49,6 +49,7 @@ export type LanguageKey = | 'go' | 'haskell' | 'hlsl' + | 'hook' | 'ispc' | 'jakt' | 'java'