Skip to content

Commit

Permalink
A quine (#470)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jimbxb authored Oct 8, 2024
1 parent f3ed014 commit 085c463
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
22 changes: 21 additions & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
Some small example Wybe programs.
# Some small example Wybe programs.

## brainfuck.wybe

A [Brainfuck](https://esolangs.org/wiki/Brainfuck) parser and interpreter.

## json.wybe

A JSON document parser.

## quine.wybe

A [Quine](https://en.wikipedia.org/wiki/Quine_(computing)).

To validate:
```sh
$ wybemk quine
$ ./quine | diff ./quine.wybe -
$ echo $?
0
```
2 changes: 1 addition & 1 deletion samples/json.wybe
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## json.Wybe
## json.wybe
## Author: James Barnes
#
# A recursive-descent parser for JSON documents in Wybe.
Expand Down
56 changes: 56 additions & 0 deletions samples/quine.wybe
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## quine.wybe
## Author: James Barnes
#
# A Quine
?listing = [
"## quine.wybe",
"## Author: James Barnes",
"#",
"# A Quine",
"?listing = [",
"]",
"?n_lines = listing^length",
"?preamble_lines = 5",
"for _ in 0..preamble_lines; ?line in listing {",
" !println line",
"}",
"for ?index in 0..n_lines; ?line in listing {",
" for _ in 0..4 {",
" !print ' '",
" }",
" ?quote = 34:char",
" !print quote; !print line; !print quote",
" if { index < n_lines - 1 ::",
" !println ','",
" | else ::",
" !nl",
" }",
"}",
"for ?i in preamble_lines..n_lines {",
" if { ?line = listing[i] ::",
" !println(line)",
" }",
"}"
]
?n_lines = listing^length
?preamble_lines = 5
for _ in 0..preamble_lines; ?line in listing {
!println line
}
for ?index in 0..n_lines; ?line in listing {
for _ in 0..4 {
!print ' '
}
?quote = 34:char
!print quote; !print line; !print quote
if { index < n_lines - 1 ::
!println ','
| else ::
!nl
}
}
for ?i in preamble_lines..n_lines {
if { ?line = listing[i] ::
!println(line)
}
}
8 changes: 8 additions & 0 deletions test-cases/complex/exp/testcase_quine-quine.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
----------------------------------------------------------------------
- program - output
----------------------------------------------------------------------
["[","]=?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}}
["[","]=?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}}
----------------------------------------------------------------------


13 changes: 13 additions & 0 deletions test-cases/complex/testcase_quine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from complex.utils import *

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}}"""

@test_case
def quine(ctx: Context) -> None:
ctx.save_file("quine.wybe", WYBE_QUINE)
_ = ctx.wybe_build_target("quine", False, False, check=True)
_, output = ctx.execute_program("./quine", check=True)
ctx.write_section("program - output", "\n".join([WYBE_QUINE, output]))

assert WYBE_QUINE == output

0 comments on commit 085c463

Please sign in to comment.