Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add “Java” Lang #57 (#180)
Also improved syntax highlighting for C.
  • Loading branch information
SirBogman committed Apr 21, 2020
1 parent 3d98e46 commit 6b791f5
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Expand Up @@ -31,6 +31,7 @@ COPY --from=codegolf/lang-fortran / /langs/fortran/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-java / /langs/java/rootfs/
COPY --from=codegolf/lang-javascript / /langs/javascript/rootfs/
COPY --from=codegolf/lang-julia / /langs/julia/rootfs/
COPY --from=codegolf/lang-lisp / /langs/lisp/rootfs/
Expand Down Expand Up @@ -58,6 +59,8 @@ COPY --from=0 /empty /langs/haskell/rootfs/proc/
COPY --from=0 /empty /langs/haskell/rootfs/tmp/
COPY --from=0 /empty /langs/j/rootfs/proc/
COPY --from=0 /empty /langs/j/rootfs/tmp/
COPY --from=0 /empty /langs/java/rootfs/proc/
COPY --from=0 /empty /langs/java/rootfs/tmp/
COPY --from=0 /empty /langs/javascript/rootfs/proc/
COPY --from=0 /empty /langs/javascript/rootfs/tmp/
COPY --from=0 /empty /langs/julia/rootfs/proc/
Expand Down
3 changes: 2 additions & 1 deletion assets/common.css
Expand Up @@ -375,6 +375,7 @@ thead th {
#matrix input.go:not(:checked) ~ .go,
#matrix input.haskell:not(:checked) ~ .haskell,
#matrix input.j:not(:checked) ~ .j,
#matrix input.java:not(:checked) ~ .java,
#matrix input.javascript:not(:checked) ~ .javascript,
#matrix input.julia:not(:checked) ~ .julia,
#matrix input.lisp:not(:checked) ~ .lisp,
Expand Down Expand Up @@ -418,7 +419,7 @@ thead th {
}
#matrix {
/* Increase this number when adding a language. */
grid-template-columns: 3fr repeat(19,1fr);
grid-template-columns: 3fr repeat(20,1fr);
}
#matrix a {
display: initial;
Expand Down
6 changes: 5 additions & 1 deletion assets/hole.js
Expand Up @@ -58,7 +58,11 @@ onload = () => {
if (!langs.find(l => l.id == lang))
location.hash = lang = 'python';

cm.setOption('mode', {name: lang == 'c' ? 'clike' : lang, startOpen: true});
if (lang == 'c' || lang == 'java')
cm.setOption('mode', {name: 'text/x-' + lang, startOpen: true});
else
cm.setOption('mode', {name: lang, startOpen: true});

cm.setValue(lang in solutions ? solutions[lang] : '');

localStorage.setItem('lang', location.hash = lang);
Expand Down
113 changes: 113 additions & 0 deletions assets/includes/vendor/codemirror-clike.js
Expand Up @@ -249,12 +249,125 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
};
});

function words(str) {
var obj = {}, words = str.split(" ");
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
return obj;
}
function contains(words, word) {
if (typeof words === "function") {
return words(word);
} else {
return words.propertyIsEnumerable(word);
}
}
var cKeywords = "auto if break case register continue return default do sizeof " +
"static else struct switch extern typedef union for goto while enum const " +
"volatile inline restrict asm fortran";

// Do not use this. Use the cTypes function below. This is global just to avoid
// excessive calls when cTypes is being called multiple times during a parse.
var basicCTypes = words("int long char short double float unsigned signed " +
"void bool");

// Returns true if identifier is a "C" type.
// C type is defined as those that are reserved by the compiler (basicTypes),
// and those that end in _t (Reserved by POSIX for types)
// http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html
function cTypes(identifier) {
return contains(basicCTypes, identifier) || /.+_t$/.test(identifier);
}

var cBlockKeywords = "case do else for if switch while struct enum union";
var cDefKeywords = "struct enum union";

function cppHook(stream, state) {
if (!state.startOfLine) return false
for (var ch, next = null; ch = stream.peek();) {
if (ch == "\\" && stream.match(/^.$/)) {
next = cppHook
break
} else if (ch == "/" && stream.match(/^\/[\/\*]/, false)) {
break
}
stream.next()
}
state.tokenize = next
return "meta"
}

function pointerHook(_stream, state) {
if (state.prevToken == "type") return "type";
return false;
}

// For C and C++ (and ObjC): identifiers starting with __
// or _ followed by a capital letter are reserved for the compiler.
function cIsReservedIdentifier(token) {
if (!token || token.length < 2) return false;
if (token[0] != '_') return false;
return (token[1] == '_') || (token[1] !== token[1].toLowerCase());
}

function def(mimes, mode) {
if (typeof mimes == "string") mimes = [mimes];
var words = [];
function add(obj) {
if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop))
words.push(prop);
}
add(mode.keywords);
add(mode.types);
add(mode.builtin);
add(mode.atoms);
if (words.length) {
mode.helperType = mimes[0];
CodeMirror.registerHelper("hintWords", mimes[0], words);
}

for (var i = 0; i < mimes.length; ++i)
CodeMirror.defineMIME(mimes[i], mode);
}

def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
name: "clike",
keywords: words(cKeywords),
types: cTypes,
blockKeywords: words(cBlockKeywords),
defKeywords: words(cDefKeywords),
typeFirstDefinitions: true,
atoms: words("NULL true false"),
isReservedIdentifier: cIsReservedIdentifier,
hooks: {
"#": cppHook,
"*": pointerHook,
},
modeProps: {fold: ["brace", "include"]}
});

def("text/x-java", {
name: "clike",
keywords: words("abstract assert break case catch class const continue default " +
"do else enum extends final finally for goto if implements import " +
"instanceof interface native new package private protected public " +
"return static strictfp super switch synchronized this throw throws transient " +
"try volatile while @interface"),
types: words("byte short int long float double boolean char void Boolean Byte Character Double Float " +
"Integer Long Number Object Short String StringBuffer StringBuilder Void"),
blockKeywords: words("catch class do else finally for if switch try while"),
defKeywords: words("class interface enum @interface"),
typeFirstDefinitions: true,
atoms: words("true false null"),
number: /^(?:0x[a-f\d_]+|0b[01_]+|(?:[\d_]+\.?\d*|\.\d+)(?:e[-+]?[\d_]+)?)(u|ll?|l|f)?/i,
hooks: {
"@": function(stream) {
// Don't match the @interface keyword.
if (stream.match('interface', false)) return false;

stream.eatWhile(/[\w\$_]/);
return "meta";
}
},
modeProps: {fold: ["brace", "import"]}
});
});
3 changes: 3 additions & 0 deletions build-langs
Expand Up @@ -15,6 +15,7 @@ declare -A urls=(
["Fortran"]="//gcc.gnu.org/fortran/"
["Haskell"]="//www.haskell.org/ghc/"
["J"]="//www.jsoftware.com"
["Java"]="//openjdk.java.net/"
["JavaScript"]="//v8.dev"
["Julia"]="//julialang.org"
["Lisp"]="//clisp.sourceforge.io"
Expand Down Expand Up @@ -78,6 +79,8 @@ for name in "${sorted_names[@]}"; do
ver=${ver%.}
ver=${ver% d8> }
ver=${ver% Copyright (C) 1994-2019 Lua.org, PUC-Rio}
ver=${ver/) OpenJDK*/}
ver=${ver/*AdoptOpenJDK (build /}
ver=${ver/ (*/}
ver=${ver/ Copyright/}

Expand Down
2 changes: 1 addition & 1 deletion db/0.schema.sql
Expand Up @@ -14,7 +14,7 @@ CREATE TYPE hole AS ENUM (
);

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

Expand Down
2 changes: 2 additions & 0 deletions langs/java/.dockerignore
@@ -0,0 +1,2 @@
*
!/java
27 changes: 27 additions & 0 deletions langs/java/Dockerfile
@@ -0,0 +1,27 @@
FROM adoptopenjdk:14_36-jdk-hotspot as builder

RUN jlink --compress=2 --module-path /opt/java/openjdk/jmods \
--add-modules java.base,jdk.compiler \
--output /jlinked

FROM alpine:3.11 as bashhelper

RUN apk add --no-cache bash

FROM scratch

COPY --from=0 /jlinked /opt/jdk/
COPY --from=0 /lib64/ld-linux-x86-64.so.2 /lib64/
COPY --from=0 /lib/x86_64-linux-gnu /lib/x86_64-linux-gnu/
COPY --from=0 /bin/cat \
/bin/rm /bin/
COPY --from=0 /usr/lib/locale /usr/lib/locale/

# bash is used for the /usr/bin/java script. I couldn't get it to work with busybox sh.
COPY --from=1 /bin/bash /bin/
COPY --from=1 /lib/ld-musl-x86_64.so.1 /lib
COPY --from=1 /usr/lib/libncursesw.so.6 /usr/lib/
COPY --from=1 /usr/lib/libreadline.so.8 /usr/lib/
COPY java /usr/bin/

ENTRYPOINT ["/opt/jdk/bin/java", "--version"]
27 changes: 27 additions & 0 deletions langs/java/java
@@ -0,0 +1,27 @@
#!/bin/bash -e

export LANG=C.UTF-8
export LANGUAGE=C:en
export LC_CTYPE="C.UTF-8"
export LC_NUMERIC="C.UTF-8"
export LC_TIME="C.UTF-8"
export LC_COLLATE="C.UTF-8"
export LC_MONETARY="C.UTF-8"
export LC_MESSAGES="C.UTF-8"
export LC_PAPER="C.UTF-8"
export LC_NAME="C.UTF-8"
export LC_ADDRESS="C.UTF-8"
export LC_TELEPHONE="C.UTF-8"
export LC_MEASUREMENT="C.UTF-8"
export LC_IDENTIFICATION="C.UTF-8"
export LC_ALL=C.UTF-8

shift
cat - > /tmp/code.java
/opt/jdk/bin/javac /tmp/code.java
rm /tmp/code.java

cd /tmp
shopt -s nullglob
printf -v class %s *.class
/opt/jdk/bin/java "${class/.class*/}" "$@"
13 changes: 10 additions & 3 deletions latest-langs
Expand Up @@ -19,6 +19,11 @@ constant @langs = (
'https://www.haskell.org/ghc/download.html',
/ 'download_ghc' .+? ">" (<[\d.]>+) /,

'Java',
# Note: need to manually change the number in this url for major releases.
'https://raw.githubusercontent.com/AdoptOpenJDK/openjdk-docker/master/14/jdk/alpine/Dockerfile.hotspot.releases.slim',
/ 'ENV JAVA_VERSION jdk-' (<[\d+.]>+) /,

'Julia',
'https://julialang.org/downloads/',
/ 'stable release: v' (<[\d.]>+) /,
Expand Down Expand Up @@ -59,13 +64,15 @@ constant @langs = (
constant $len = @langs[0, 3*.chars.max;

my %current = (get('https://code-golf.io/about') ~~ m:g/
'<tr><th' .+? '>' (.+?) '<' .*? (<[\d.]> ** 2..*)
'<tr><th' .+? '>' (.+?) '<' .*? (<[\d+.]> ** 2..*)
/)».list.flat».Str.Hash;

for @langs -> $lang, $url, $re {
my $ver = get($url) ~~ $re ?? ~$0 !! '';

printf " %*s %s\n", $len, $lang, $ver if $ver ne %current{$lang};
my $current = %current{$lang};
if $ver ne $current && "$ver.0" ne $current {
printf " %*s %s\n", $len, $lang, $ver;
}
}

sub get ($url) {
Expand Down
1 change: 1 addition & 0 deletions routes/types.go
Expand Up @@ -31,6 +31,7 @@ var langs = []Lang{
{"go", "Go"},
{"haskell", "Haskell"},
{"j", "J"},
{"java", "Java"},
{"javascript", "JavaScript"},
{"julia", "Julia"},
{"lisp", "Lisp"},
Expand Down
1 change: 1 addition & 0 deletions routes/versions.go
Expand Up @@ -8,6 +8,7 @@ const versionTable = "" +
"<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=//www.jsoftware.com>website</a>" +
"<tr><th class=java>Java<td>14+36<td class=wide><a href=//openjdk.java.net/>website</a>" +
"<tr><th class=javascript>JavaScript<td>V8 7.4.51<td class=wide><a href=//v8.dev>website</a>" +
"<tr><th class=julia>Julia<td>1.4.1<td class=wide><a href=//julialang.org>website</a>" +
"<tr><th class=lisp>Lisp<td>GNU CLISP 2.49.92<td class=wide><a href=//clisp.sourceforge.io>website</a>" +
Expand Down
3 changes: 3 additions & 0 deletions t/args.t
Expand Up @@ -55,6 +55,9 @@ import System.Environment;main=do x<-getArgs;mapM putStrLn$drop 1 x
j
echo>2}.ARGV
java
class A{public static void main(String[] args){for (var s:args){System.out.println(s);}}}
javascript
arguments.forEach(a => print(a))
Expand Down
1 change: 1 addition & 0 deletions views/java.svg
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 6b791f5

Please sign in to comment.