Skip to content

Commit effe372

Browse files
Add 'sed' lang (#659)
1 parent 3a69fd3 commit effe372

File tree

11 files changed

+75
-5
lines changed

11 files changed

+75
-5
lines changed

config/langs.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,30 @@ fn main() {
691691
}
692692
'''
693693

694+
[Sed]
695+
size = '222 KiB'
696+
version = '4.8'
697+
website = 'https://www.gnu.org/software/sed/'
698+
example = '''
699+
# Printing
700+
1i Hello, World!
701+
702+
# Looping
703+
2,$ b
704+
705+
h
706+
s/.*/1/
707+
:loop
708+
p
709+
y/123456789/23456789X/
710+
/X/ !b loop
711+
s/X/10/
712+
G
713+
714+
# Accessing arguments
715+
# (... automatic, one per line ...)
716+
'''
717+
694718
[SQL]
695719
size = '1.09 MiB'
696720
version = '3.38.4'

css/golfer/holes.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ input.python:not(:checked) ~ .python,
6464
input.raku:not(:checked) ~ .raku,
6565
input.ruby:not(:checked) ~ .ruby,
6666
input.rust:not(:checked) ~ .rust,
67+
input.sed:not(:checked) ~ .sed,
6768
input.sql:not(:checked) ~ .sql,
6869
input.swift:not(:checked) ~ .swift,
6970
input.v:not(:checked) ~ .v,

db/a-schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ CREATE TYPE lang AS ENUM (
4848
'crystal', 'd', 'f-sharp', 'fish', 'fortran', 'go', 'haskell', 'hexagony', 'j',
4949
'java', 'javascript', 'julia', 'k', 'lisp', 'lua', 'nim', 'pascal',
5050
'perl', 'php', 'powershell', 'prolog', 'python', 'raku', 'ruby', 'rust',
51-
'sql', 'swift', 'v', 'viml', 'zig'
51+
'sed', 'sql', 'swift', 'v', 'viml', 'zig'
5252
);
5353

5454
CREATE TYPE medal AS ENUM ('diamond', 'gold', 'silver', 'bronze');

docker/dev.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ COPY --from=codegolf/lang-c ["/", "/langs/c/rootfs/" ] # 1.61 M
4242
COPY --from=codegolf/lang-bash ["/", "/langs/bash/rootfs/" ] # 1.19 MiB
4343
COPY --from=codegolf/lang-sql ["/", "/langs/sql/rootfs/" ] # 1.09 MiB
4444
COPY --from=codegolf/lang-lua ["/", "/langs/lua/rootfs/" ] # 342 KiB
45-
COPY --from=codegolf/lang-k ["/", "/langs/k/rootfs/" ] # 262 KiB
45+
COPY --from=codegolf/lang-k ["/", "/langs/k/rootfs/" ] # 258 KiB
46+
COPY --from=codegolf/lang-sed ["/", "/langs/sed/rootfs/" ] # 222 KiB
4647

4748
COPY run-lang.c ./
4849

docker/live.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ COPY --from=codegolf/lang-c ["/", "/langs/c/rootfs/" ] # 1.61 M
5555
COPY --from=codegolf/lang-bash ["/", "/langs/bash/rootfs/" ] # 1.19 MiB
5656
COPY --from=codegolf/lang-sql ["/", "/langs/sql/rootfs/" ] # 1.09 MiB
5757
COPY --from=codegolf/lang-lua ["/", "/langs/lua/rootfs/" ] # 342 KiB
58-
COPY --from=codegolf/lang-k ["/", "/langs/k/rootfs/" ] # 262 KiB
58+
COPY --from=codegolf/lang-k ["/", "/langs/k/rootfs/" ] # 258 KiB
59+
COPY --from=codegolf/lang-sed ["/", "/langs/sed/rootfs/" ] # 222 KiB
5960

6061
COPY --from=0 /go/code-golf /go/esbuild.json /
6162
COPY /css /css/

hole/play.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ func Play(ctx context.Context, holeID, langID, code string) (score Scorecard) {
208208
case "python":
209209
// Force the stdout and stderr streams to be unbuffered.
210210
cmd.Args = []string{"/usr/bin/python", "-u", "-"}
211+
case "sed":
212+
cmd.Args = []string{"/usr/bin/sed", "-E", "-z", "--", code}
211213
case "swift":
212214
cmd.Args = []string{"/usr/bin/swift", "-module-cache-path", "/tmp", "-"}
213215
default:
@@ -222,13 +224,17 @@ func Play(ctx context.Context, holeID, langID, code string) (score Scorecard) {
222224
args += arg + "\x00"
223225
}
224226
cmd.Stdin = strings.NewReader(args)
227+
case "sed":
228+
// For sed we always need to append a null byte, even if no args exist
229+
args := strings.Join(score.Args, "\x00") + "\x00"
230+
cmd.Stdin = strings.NewReader(args)
225231
default:
226232
cmd.Args = append(cmd.Args, score.Args...)
227233
}
228234

229235
// Code
230236
switch langID {
231-
case "brainfuck", "fish", "javascript":
237+
case "brainfuck", "fish", "javascript", "sed":
232238
// For these code is passed as an argument above.
233239
case "k":
234240
code = preprocessKCode(holeID, code)
@@ -277,7 +283,14 @@ func Play(ctx context.Context, holeID, langID, code string) (score Scorecard) {
277283
} else {
278284
// Trim trailing spaces per line.
279285
// FIXME This is all very hacky, but needed for Sierpiński.
280-
scanner := bufio.NewScanner(bytes.NewReader(stdout.Next(maxLength)))
286+
stdoutContents := stdout.Next(maxLength)
287+
288+
// Postprocess sed output to turn null bytes into newlines
289+
if langID == "sed" {
290+
stdoutContents = bytes.ReplaceAll(stdoutContents, []byte("\x00"), []byte("\n"))
291+
}
292+
293+
scanner := bufio.NewScanner(bytes.NewReader(stdoutContents))
281294
for scanner.Scan() {
282295
score.Stdout = append(
283296
score.Stdout, bytes.TrimRightFunc(scanner.Bytes(), unicode.IsSpace)...)

langs/sed/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM alpine:3.15 as builder
2+
3+
RUN apk add --no-cache build-base curl
4+
5+
RUN curl https://ftp.gnu.org/gnu/sed/sed-4.8.tar.xz | tar xJ
6+
7+
RUN cd sed-4.8 \
8+
&& ./configure --enable-lto LDFLAGS="-static" \
9+
&& make \
10+
&& strip sed/sed
11+
12+
FROM codegolf/lang-base
13+
14+
COPY --from=0 /sed-4.8/sed/sed /usr/bin/
15+
16+
ENTRYPOINT ["sed"]
17+
18+
CMD ["--version"]

latest-langs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ constant @urls = (
2929
'Raku' => 'Rakudo',
3030
'Ruby' => 'Ruby_(programming_language)',
3131
'Rust' => 'Rust_(programming_language)',
32+
'Sed' => 'Sed',
3233
'SQL' => 'SQLite',
3334
'Swift' => 'Swift_(programming_language)',
3435
'VimL' => 'Vim_(text_editor)',

svg/sed.svg

Lines changed: 1 addition & 0 deletions
Loading

views/hole-ng.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ <h1>
151151
<div class="hide info prolog">
152152
<b>prolog_flag(argv, Args)</b> to access the arguments.
153153
</div>
154+
<div class="hide info sed">
155+
Arguments are available via STDIN, each argument is seperated with a null byte.
156+
The code is run with <b>-E</b> and <b>-z</b>.
157+
Output replaces null bytes with newlines.
158+
</div>
154159
<div class="hide info sql">
155160
<b>SELECT arg FROM argv</b> to access the arguments, only the first
156161
column of the first result set will be printed, NULL values will be

0 commit comments

Comments
 (0)