Skip to content

Commit 085c463

Browse files
authored
A quine (#470)
* Add quine.wybe sample * Fix typo in json; add more details in sammples/README.md * Formatting; authorship * Add complex test with shorter quine * Better validation in README
1 parent f3ed014 commit 085c463

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

samples/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
Some small example Wybe programs.
1+
# Some small example Wybe programs.
2+
3+
## brainfuck.wybe
4+
5+
A [Brainfuck](https://esolangs.org/wiki/Brainfuck) parser and interpreter.
6+
7+
## json.wybe
8+
9+
A JSON document parser.
10+
11+
## quine.wybe
12+
13+
A [Quine](https://en.wikipedia.org/wiki/Quine_(computing)).
14+
15+
To validate:
16+
```sh
17+
$ wybemk quine
18+
$ ./quine | diff ./quine.wybe -
19+
$ echo $?
20+
0
21+
```

samples/json.wybe

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## json.Wybe
1+
## json.wybe
22
## Author: James Barnes
33
#
44
# A recursive-descent parser for JSON documents in Wybe.

samples/quine.wybe

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## quine.wybe
2+
## Author: James Barnes
3+
#
4+
# A Quine
5+
?listing = [
6+
"## quine.wybe",
7+
"## Author: James Barnes",
8+
"#",
9+
"# A Quine",
10+
"?listing = [",
11+
"]",
12+
"?n_lines = listing^length",
13+
"?preamble_lines = 5",
14+
"for _ in 0..preamble_lines; ?line in listing {",
15+
" !println line",
16+
"}",
17+
"for ?index in 0..n_lines; ?line in listing {",
18+
" for _ in 0..4 {",
19+
" !print ' '",
20+
" }",
21+
" ?quote = 34:char",
22+
" !print quote; !print line; !print quote",
23+
" if { index < n_lines - 1 ::",
24+
" !println ','",
25+
" | else ::",
26+
" !nl",
27+
" }",
28+
"}",
29+
"for ?i in preamble_lines..n_lines {",
30+
" if { ?line = listing[i] ::",
31+
" !println(line)",
32+
" }",
33+
"}"
34+
]
35+
?n_lines = listing^length
36+
?preamble_lines = 5
37+
for _ in 0..preamble_lines; ?line in listing {
38+
!println line
39+
}
40+
for ?index in 0..n_lines; ?line in listing {
41+
for _ in 0..4 {
42+
!print ' '
43+
}
44+
?quote = 34:char
45+
!print quote; !print line; !print quote
46+
if { index < n_lines - 1 ::
47+
!println ','
48+
| else ::
49+
!nl
50+
}
51+
}
52+
for ?i in preamble_lines..n_lines {
53+
if { ?line = listing[i] ::
54+
!println(line)
55+
}
56+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
----------------------------------------------------------------------
2+
- program - output
3+
----------------------------------------------------------------------
4+
["[","]=?L;?q=34:char;if{L=[?a,?b|_]::for _ in L{!print a;!print q};!print',';for _ in L{!print q;!print b}}"]=?L;?q=34:char;if{L=[?a,?b|_]::for _ in L{!print a;!print q};!print',';for _ in L{!print q;!print b}}
5+
["[","]=?L;?q=34:char;if{L=[?a,?b|_]::for _ in L{!print a;!print q};!print',';for _ in L{!print q;!print b}}"]=?L;?q=34:char;if{L=[?a,?b|_]::for _ in L{!print a;!print q};!print',';for _ in L{!print q;!print b}}
6+
----------------------------------------------------------------------
7+
8+

test-cases/complex/testcase_quine.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from complex.utils import *
2+
3+
WYBE_QUINE:str = r"""["[","]=?L;?q=34:char;if{L=[?a,?b|_]::for _ in L{!print a;!print q};!print',';for _ in L{!print q;!print b}}"]=?L;?q=34:char;if{L=[?a,?b|_]::for _ in L{!print a;!print q};!print',';for _ in L{!print q;!print b}}"""
4+
5+
@test_case
6+
def quine(ctx: Context) -> None:
7+
ctx.save_file("quine.wybe", WYBE_QUINE)
8+
_ = ctx.wybe_build_target("quine", False, False, check=True)
9+
_, output = ctx.execute_program("./quine", check=True)
10+
ctx.write_section("program - output", "\n".join([WYBE_QUINE, output]))
11+
12+
assert WYBE_QUINE == output
13+

0 commit comments

Comments
 (0)