Skip to content

Commit

Permalink
Add “Go” Lang code-golf#102 (code-golf#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBogman authored Apr 15, 2020
1 parent 13cf35f commit 5feb18b
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ FROM scratch
COPY --from=codegolf/lang-bash / /langs/bash/rootfs/
COPY --from=codegolf/lang-brainfuck / /langs/brainfuck/rootfs/
COPY --from=codegolf/lang-c / /langs/c/rootfs/
COPY --from=codegolf/lang-go / /langs/go/rootfs/
COPY --from=codegolf/lang-haskell / /langs/haskell/rootfs/
COPY --from=codegolf/lang-j / /langs/j/rootfs/
COPY --from=codegolf/lang-javascript / /langs/javascript/rootfs/
Expand All @@ -48,6 +49,8 @@ COPY --from=0 /empty /langs/brainfuck/rootfs/proc/
COPY --from=0 /empty /langs/brainfuck/rootfs/tmp/
COPY --from=0 /empty /langs/c/rootfs/proc/
COPY --from=0 /empty /langs/c/rootfs/tmp/
COPY --from=0 /empty /langs/go/rootfs/proc/
COPY --from=0 /empty /langs/go/rootfs/tmp/
COPY --from=0 /empty /langs/haskell/rootfs/proc/
COPY --from=0 /empty /langs/haskell/rootfs/tmp/
COPY --from=0 /empty /langs/j/rootfs/proc/
Expand Down
5 changes: 4 additions & 1 deletion assets/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,12 @@ thead th {
text-decoration: none;
}

/* When adding a new language, update grid-template-columns for #matrix below. */
#matrix input,
#matrix input.bash:not(:checked) ~ .bash,
#matrix input.brainfuck:not(:checked) ~ .brainfuck,
#matrix input.c:not(:checked) ~ .c,
#matrix input.go:not(:checked) ~ .go,
#matrix input.haskell:not(:checked) ~ .haskell,
#matrix input.j:not(:checked) ~ .j,
#matrix input.javascript:not(:checked) ~ .javascript,
Expand Down Expand Up @@ -414,7 +416,8 @@ thead th {
max-width: 1280px;
}
#matrix {
grid-template-columns: 3fr repeat(17,1fr);
/* Increase this number when adding a language. */
grid-template-columns: 3fr repeat(18,1fr);
}
#matrix a {
display: initial;
Expand Down
1 change: 1 addition & 0 deletions assets/hole.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/* include vendor/codemirror-bash.js */
/* include vendor/codemirror-brainfuck.js */
/* include vendor/codemirror-go.js */
/* include vendor/codemirror-haskell.js */
/* include vendor/codemirror-javascript.js */
/* include vendor/codemirror-julia.js */
Expand Down
179 changes: 179 additions & 0 deletions assets/includes/vendor/codemirror-go.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/LICENSE

(function(mod) {
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";

CodeMirror.defineMode("go", function(config) {
var indentUnit = config.indentUnit;

var keywords = {
"break":true, "case":true, "chan":true, "const":true, "continue":true,
"default":true, "defer":true, "else":true, "fallthrough":true, "for":true,
"func":true, "go":true, "goto":true, "if":true, "import":true,
"interface":true, "map":true, "package":true, "range":true, "return":true,
"select":true, "struct":true, "switch":true, "type":true, "var":true,
"bool":true, "byte":true, "complex64":true, "complex128":true,
"float32":true, "float64":true, "int8":true, "int16":true, "int32":true,
"int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true,
"uint64":true, "int":true, "uint":true, "uintptr":true, "error": true,
"rune":true
};

var atoms = {
"true":true, "false":true, "iota":true, "nil":true, "append":true,
"cap":true, "close":true, "complex":true, "copy":true, "delete":true, "imag":true,
"len":true, "make":true, "new":true, "panic":true, "print":true,
"println":true, "real":true, "recover":true
};

var isOperatorChar = /[+\-*&^%:=<>!|\/]/;

var curPunc;

function tokenBase(stream, state) {
var ch = stream.next();
if (ch == '"' || ch == "'" || ch == "`") {
state.tokenize = tokenString(ch);
return state.tokenize(stream, state);
}
if (/[\d\.]/.test(ch)) {
if (ch == ".") {
stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
} else if (ch == "0") {
stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
} else {
stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
}
return "number";
}
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
curPunc = ch;
return null;
}
if (ch == "/") {
if (stream.eat("*")) {
state.tokenize = tokenComment;
return tokenComment(stream, state);
}
if (stream.eat("/")) {
stream.skipToEnd();
return "comment";
}
}
if (isOperatorChar.test(ch)) {
stream.eatWhile(isOperatorChar);
return "operator";
}
stream.eatWhile(/[\w\$_\xa1-\uffff]/);
var cur = stream.current();
if (keywords.propertyIsEnumerable(cur)) {
if (cur == "case" || cur == "default") curPunc = "case";
return "keyword";
}
if (atoms.propertyIsEnumerable(cur)) return "atom";
return "variable";
}

function tokenString(quote) {
return function(stream, state) {
var escaped = false, next, end = false;
while ((next = stream.next()) != null) {
if (next == quote && !escaped) {end = true; break;}
escaped = !escaped && quote != "`" && next == "\\";
}
if (end || !(escaped || quote == "`"))
state.tokenize = tokenBase;
return "string";
};
}

function tokenComment(stream, state) {
var maybeEnd = false, ch;
while (ch = stream.next()) {
if (ch == "/" && maybeEnd) {
state.tokenize = tokenBase;
break;
}
maybeEnd = (ch == "*");
}
return "comment";
}

function Context(indented, column, type, align, prev) {
this.indented = indented;
this.column = column;
this.type = type;
this.align = align;
this.prev = prev;
}
function pushContext(state, col, type) {
return state.context = new Context(state.indented, col, type, null, state.context);
}
function popContext(state) {
if (!state.context.prev) return;
var t = state.context.type;
if (t == ")" || t == "]" || t == "}")
state.indented = state.context.indented;
return state.context = state.context.prev;
}

// Interface

return {
startState: function(basecolumn) {
return {
tokenize: null,
context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
indented: 0,
startOfLine: true
};
},

token: function(stream, state) {
var ctx = state.context;
if (stream.sol()) {
if (ctx.align == null) ctx.align = false;
state.indented = stream.indentation();
state.startOfLine = true;
if (ctx.type == "case") ctx.type = "}";
}
if (stream.eatSpace()) return null;
curPunc = null;
var style = (state.tokenize || tokenBase)(stream, state);
if (style == "comment") return style;
if (ctx.align == null) ctx.align = true;

if (curPunc == "{") pushContext(state, stream.column(), "}");
else if (curPunc == "[") pushContext(state, stream.column(), "]");
else if (curPunc == "(") pushContext(state, stream.column(), ")");
else if (curPunc == "case") ctx.type = "case";
else if (curPunc == "}" && ctx.type == "}") popContext(state);
else if (curPunc == ctx.type) popContext(state);
state.startOfLine = false;
return style;
},

indent: function(state, textAfter) {
if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {
state.context.type = "}";
return ctx.indented;
}
var closing = firstChar == ctx.type;
if (ctx.align) return ctx.column + (closing ? 0 : 1);
else return ctx.indented + (closing ? 0 : indentUnit);
},

electricChars: "{}):",
closeBrackets: "()[]{}''\"\"``",
fold: "brace",
blockCommentStart: "/*",
blockCommentEnd: "*/",
lineComment: "//"
};
});
});
3 changes: 3 additions & 0 deletions build-langs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare -A urls=(
["Bash"]="//www.gnu.org/software/bash/"
["Brainfuck"]="//github.com/code-golf/code-golf/tree/master/langs/brainfuck/"
["C"]="//bellard.org/tcc/"
["Go"]="//golang.org"
["Haskell"]="//www.haskell.org/ghc/"
["J"]="http://jsoftware.com"
["JavaScript"]="//v8.dev"
Expand Down Expand Up @@ -64,6 +65,8 @@ for name in "${sorted_names[@]}"; do
ver=${ver#The Glorious }
ver=${ver#This is }
ver=${ver#rustc }
ver=${ver/go version go/}
ver=${ver/ linux\/amd64/}
ver=${ver/ation/er}
ver=${ver/built /}
ver=${ver/System, /}
Expand Down
2 changes: 1 addition & 1 deletion db/0.schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CREATE TYPE hole AS ENUM (
);

CREATE TYPE lang AS ENUM (
'bash', 'brainfuck', 'c', 'haskell', 'j', 'javascript', 'julia', 'lisp',
'bash', 'brainfuck', 'c', 'go', 'haskell', 'j', 'javascript', 'julia', 'lisp',
'lua', 'nim', 'perl', 'php', 'python', 'raku', 'ruby', 'rust', 'swift'
);

Expand Down
2 changes: 2 additions & 0 deletions hole/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func Play(ctx context.Context, holeID, langID, code string) (score Scorecard) {
cmd.Args = []string{"/usr/bin/bash", "-s", "-"}
case "c":
cmd.Args = []string{"/usr/bin/tcc", "-run", "-"}
case "go":
cmd.Args = []string{"/usr/bin/run-go"}
case "haskell", "javascript", "php":
cmd.Args = []string{"/usr/bin/" + langID, "--"}
case "j":
Expand Down
2 changes: 2 additions & 0 deletions langs/go/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!/run-go
9 changes: 9 additions & 0 deletions langs/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:1.14.2-alpine as builder

FROM scratch

COPY --from=0 / /

COPY run-go /usr/bin/run-go

ENTRYPOINT ["/usr/local/go/bin/go", "version"]
9 changes: 9 additions & 0 deletions langs/go/run-go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh -e

# Separate compile and link relies on environment variables less than "go run" does.
# "go run" also doesn't support reading the file from stdin.
cat - > /tmp/code.go
/usr/local/go/bin/go tool compile -o /tmp/code.o /tmp/code.go
/usr/local/go/bin/go tool link -o /tmp/code /tmp/code.o
rm /tmp/code.go /tmp/code.o
/tmp/code "$@"
4 changes: 4 additions & 0 deletions latest-langs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ constant @langs = (
'http://download.savannah.gnu.org/releases/tinycc/?C=M&O=D',
/ 'tcc-' (<[\d.]>+) '.tar.bz2' /,

'Go',
'https://golang.org/project/',
/ 'var goVersion = "go' (<[\d.]>+) '"' /,

'Haskell',
'https://www.haskell.org/ghc/download.html',
/ 'download_ghc' .+? ">" (<[\d.]>+) /,
Expand Down
1 change: 1 addition & 0 deletions routes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var langs = []Lang{
{"bash", "Bash"},
{"brainfuck", "Brainfuck"},
{"c", "C"},
{"go", "Go"},
{"haskell", "Haskell"},
{"j", "J"},
{"javascript", "JavaScript"},
Expand Down
1 change: 1 addition & 0 deletions routes/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const versionTable = "" +
"<tr><th class=bash>Bash<td>5.0.16<td class=wide><a href=//www.gnu.org/software/bash/>website</a>" +
"<tr><th class=brainfuck>Brainfuck<td>1.0.0<td class=wide><a href=//github.com/code-golf/code-golf/tree/master/langs/brainfuck/>website</a>" +
"<tr><th class=c>C<td>Tiny C Compiler 0.9.27<td class=wide><a href=//bellard.org/tcc/>website</a>" +
"<tr><th class=go>Go<td>1.14.2<td class=wide><a href=//golang.org>website</a>" +
"<tr><th class=haskell>Haskell<td>Glasgow Haskell Compiler 8.6.5<td class=wide><a href=//www.haskell.org/ghc/>website</a>" +
"<tr><th class=j>J<td>8.07.07<td class=wide><a href=http://jsoftware.com>website</a>" +
"<tr><th class=javascript>JavaScript<td>V8 7.4.51<td class=wide><a href=//v8.dev>website</a>" +
Expand Down
1 change: 1 addition & 0 deletions views/go.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5feb18b

Please sign in to comment.