Browse Source

Early support for swift. test with `make EXTRA_ARGS="--language Swift"`

Huge thanks to @adamnemecek for his help: this is mostly a
merge of his branch.
dev/git-series/gccdum
Matt Godbolt 5 years ago
parent
commit
db8bf4e520
  1. 16
      .idea/libraries/Cargo__rustfilt_.xml
  2. 2
      etc/config/builtin.swift.properties
  3. 5
      etc/config/swift.defaults.properties
  4. 1
      examples/swift/todo.swift
  5. 19
      lib/base-compiler.js
  6. 19
      lib/compilers/swift.js
  7. 27
      static/editor.js
  8. 240
      static/swift-mode.js
  9. 4
      views/example.swift
  10. 1
      views/index.pug

16
.idea/libraries/Cargo__rustfilt_.xml

@ -1,18 +1,18 @@
<component name="libraryTable">
<library name="Cargo &lt;rustfilt&gt;">
<CLASSES>
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/kernel32-sys-0.2.2" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-0.1.80" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/thread_local-0.2.7" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-build-0.1.1" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-0.2.2" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/thread-id-2.0.0" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/aho-corasick-0.5.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/utf8-ranges-0.1.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.18" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-syntax-0.3.9" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-0.2.8" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/kernel32-sys-0.2.2" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-syntax-0.3.9" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/utf8-ranges-0.1.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/thread_local-0.2.7" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-0.1.80" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-demangle-0.1.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/aho-corasick-0.5.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-0.2.2" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/thread-id-2.0.0" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-0.1.11" />
</CLASSES>
<JAVADOC />

2
etc/config/builtin.swift.properties

@ -0,0 +1,2 @@
sourcepath=./examples/swift/
extensionRe=.*\.swift$

5
etc/config/swift.defaults.properties

@ -0,0 +1,5 @@
compileFilename=example.swift
compileToAsm=-emit-assembly
supportsBinary=false
compilerType=swift
postProcess=swift-demangle

1
examples/swift/todo.swift

@ -0,0 +1 @@
// needs some swift examples

19
lib/base-compiler.js

@ -411,14 +411,10 @@ Compile.prototype.postProcess = function (result, outputFilename, filters) {
return result;
}
if (postProcess.length) {
var postCommand = 'cat "' + outputFilename + '" | ' + postProcess.join(" | ");
const postCommand = 'cat "' + outputFilename + '" | ' + postProcess.join(" | ");
return this.exec("bash", ["-c", postCommand], {maxOutput: maxSize})
.then(function (postResult) {
result.asm = postResult.stdout;
if (postResult.code !== 0) {
result.asm = "<Error during post processing: " + postResult.code + ">";
}
return result;
.then((postResult) => {
return this.handlePostProcessResult(result, postResult);
});
} else {
return this.readFile(outputFilename).then(function (contents) {
@ -443,6 +439,15 @@ Compile.prototype.postProcess = function (result, outputFilename, filters) {
return Promise.all([asmPromise, optPromise, execPromise]);
};
Compile.prototype.handlePostProcessResult = function(result, postResult) {
result.asm = postResult.stdout;
if (postResult.code !== 0) {
result.asm = "<Error during post processing: " + postResult.code + ">";
logger.error("Error during post-processing", result);
}
return result;
};
Compile.prototype.checkOptions = function (options) {
var error = this.env.findBadOptions(options);
if (error.length > 0) return "Bad options: " + error.join(", ");

19
lib/compilers/swift.js

@ -0,0 +1,19 @@
const Compile = require('../base-compiler'),
logger = require('../logger').logger;
function compileSwift(info, env) {
const compiler = new Compile(info, env);
compiler.handlePostProcessResult = function (result, postResult) {
result.asm = postResult.stdout;
// Seems swift-demangle like to exit with error 1
if (postResult.code !== 0 && !result.asm) {
result.asm = "<Error during post processing: " + postResult.code + ">";
logger.error("Error during post-processing", result);
}
return result;
};
return compiler.initialise();
}
module.exports = compileSwift;

27
static/editor.js

@ -38,6 +38,7 @@ define(function (require) {
require('./rust-mode');
require('./ispc-mode');
require('./haskell-mode');
require('./swift-mode');
var loadSave = new loadSaveLib.LoadSave();
@ -95,6 +96,10 @@ define(function (require) {
cmMode = "haskell";
extensions = ['.hs'];
break;
case "swift":
cmMode = "swift";
extensions = ['.swift'];
break;
}
var root = this.domRoot.find(".monaco-placeholder");
@ -437,18 +442,18 @@ define(function (require) {
if (reveal && lineNum)
this.editor.revealLineInCenter(lineNum);
this.decorations.linkedCode = lineNum === -1 || !lineNum ?
[]
:
[
{
range: new monaco.Range(lineNum, 1, lineNum, 1),
options: {
isWholeLine: true,
linesDecorationsClassName: 'linked-code-decoration-margin',
inlineClassName: 'linked-code-decoration-inline'
[]
:
[
{
range: new monaco.Range(lineNum, 1, lineNum, 1),
options: {
isWholeLine: true,
linesDecorationsClassName: 'linked-code-decoration-margin',
inlineClassName: 'linked-code-decoration-inline'
}
}
}
];
];
this.updateDecorations();
}
};

240
static/swift-mode.js

@ -0,0 +1,240 @@
// With thanks to https://github.com/carabina/vscode-swift/blob/master/swiftDef.js
// (MIT licensed)
define(function (require) {
'use strict';
var jquery = require('jquery');
var monaco = require('monaco');
function definition() {
return {
displayName: '',
name: 'swift',
mimeTypes: [],
fileExtensions: [],
defaultToken: '',
// used in the editor to insert comments (ctrl+/ or shift+alt+A)
lineComment: '// ',
blockCommentStart: '/*',
blockCommentEnd: '*/',
// the default separators except `@`
wordDefinition: /(-?\d*\.\d\w*)|([^\`\~\!\#\$\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
autoClosingPairs: [
['"', '"'],
['\'', '\''],
['{', '}'],
['[', ']'],
['(', ')'],
],
brackets: [
{open: '{', close: '}', token: 'delimiter.curly'},
{open: '[', close: ']', token: 'delimiter.square'},
{open: '(', close: ')', token: 'delimiter.parenthesis'},
{open: '<', close: '>', token: 'delimiter.angle'}
],
editorOptions: {tabSize: 4, insertSpaces: true},
keywords: [
"__COLUMN__",
"__FILE__",
"__FUNCTION__",
"__LINE__",
"as",
"associativity",
"break",
"case",
"class",
"continue",
"convenience",
"default",
"deinit",
"didSet",
"do",
"dynamic",
"dynamicType",
"else",
"enum",
"extension",
"fallthrough",
"final",
"for",
"func",
"get",
"if",
"import",
"in",
"infix",
"init",
"inout",
"internal",
"is",
"lazy",
"left",
"let",
"mutating",
"nil",
"none",
"nonmutating",
"operator",
"optional",
"override",
"postfix",
"precedence",
"prefix",
"private",
"protocol",
"Protocol",
"public",
"required",
"return",
"right",
"self",
"Self",
"set",
"static",
"struct",
"subscript",
"super",
"switch",
"Type",
"typealias",
"unowned",
"var",
"weak",
"where",
"while",
"willSet",
"FALSE",
"TRUE",
],
// namespaceFollows: [
// 'namespace',
// 'using',
// ],
// parenFollows: [
// 'if',
// 'for',
// 'while',
// 'switch',
// 'foreach',
// 'using',
// 'catch'
// ],
operators: [
'=',
'??',
'||',
'&&',
'|',
'^',
'&',
'==',
'!=',
'<=',
'>=',
'<<',
'+',
'-',
'*',
'/',
'%',
'!',
'~',
'++',
'--',
'+=',
'-=',
'*=',
'/=',
'%=',
'&=',
'|=',
'^=',
'<<=',
'>>=',
'>>',
'=>'
],
symbols: /[=><!~?:&|+\-*\/\^%]+/,
// escape sequences
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
// The main tokenizer for our languages
tokenizer: {
root: [
[/\@?[a-zA-Z_]\w*/, {
cases: {
// '@namespaceFollows': { token: 'keyword.$0', next: '@namespace' },
'@keywords': {token: 'keyword.$0', next: '@qualified'},
'@default': {token: 'identifier', next: '@qualified'}
}
}],
{include: '@whitespace'},
[/}/, {
cases: {
'$S2==interpolatedstring': {token: 'string.quote', bracket: '@close', next: '@pop'},
'@default': '@brackets'
}
}],
[/[{}()\[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[/@symbols/, {cases: {'@operators': 'delimiter', '@default': ''}}],
[/\@"/, {token: 'string.quote', bracket: '@open', next: '@litstring'}],
[/\$"/, {token: 'string.quote', bracket: '@open', next: '@interpolatedstring'}],
[/\d*\.\d+([eE][\-+]?\d+)?[fFdD]?/, 'number.float'],
[/0[xX][0-9a-fA-F]+/, 'number.hex'],
[/\d+/, 'number'],
[/[;,.]/, 'delimiter'],
[/"([^"\\]|\\.)*$/, 'string.invalid'],
[/"/, {token: 'string.quote', bracket: '@open', next: '@string'}],
[/'[^\\']'/, 'string'],
[/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
[/'/, 'string.invalid']
],
qualified: [
[/[a-zA-Z_][\w]*/, {cases: {'@keywords': {token: 'keyword.$0'}, '@default': 'identifier'}}],
[/\./, 'delimiter'],
['', '', '@pop'],
],
// namespace: [
// { include: '@whitespace' },
// [/[A-Z]\w*/, 'namespace'],
// [/[\.=]/, 'delimiter'],
// ['', '', '@pop'],
// ],
comment: [
[/[^\/*]+/, 'comment'],
['\\*/', 'comment', '@pop'],
[/[\/*]/, 'comment']
],
string: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, {token: 'string.quote', bracket: '@close', next: '@pop'}]
],
litstring: [
[/[^"]+/, 'string'],
[/""/, 'string.escape'],
[/"/, {token: 'string.quote', bracket: '@close', next: '@pop'}]
],
interpolatedstring: [
[/[^\\"{]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/{{/, 'string.escape'],
[/}}/, 'string.escape'],
[/{/, {token: 'string.quote', bracket: '@open', next: 'root.interpolatedstring'}],
[/"/, {token: 'string.quote', bracket: '@close', next: '@pop'}]
],
whitespace: [
[/^[ \t\v\f]*#\w.*$/, 'namespace.cpp'],
[/[ \t\v\f\r\n]+/, ''],
[/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
],
},
}
}
monaco.languages.register({id: 'swift'});
monaco.languages.setMonarchTokensProvider('swift', definition());
});

4
views/example.swift

@ -0,0 +1,4 @@
// Type your code here, or load an example.
func square(n: Int) -> Int {
return n * n
}

1
views/index.pug

@ -94,5 +94,6 @@ html(lang="en")
when "D": include example.d
when "ispc": include example.ispc
when "Haskell": include example.hs
when "Swift": include example.swift
include popups.pug

Loading…
Cancel
Save