diff --git a/samples/README.md b/samples/README.md index 2597e29f..453b5e09 100644 --- a/samples/README.md +++ b/samples/README.md @@ -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 +``` diff --git a/samples/json.wybe b/samples/json.wybe index 5565b314..1868d3d4 100644 --- a/samples/json.wybe +++ b/samples/json.wybe @@ -1,4 +1,4 @@ -## json.Wybe +## json.wybe ## Author: James Barnes # # A recursive-descent parser for JSON documents in Wybe. diff --git a/samples/quine.wybe b/samples/quine.wybe new file mode 100644 index 00000000..f4c64e25 --- /dev/null +++ b/samples/quine.wybe @@ -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) + } +} diff --git a/test-cases/complex/exp/testcase_quine-quine.exp b/test-cases/complex/exp/testcase_quine-quine.exp new file mode 100644 index 00000000..9aa7f770 --- /dev/null +++ b/test-cases/complex/exp/testcase_quine-quine.exp @@ -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}} +---------------------------------------------------------------------- + + diff --git a/test-cases/complex/testcase_quine.py b/test-cases/complex/testcase_quine.py new file mode 100644 index 00000000..76372175 --- /dev/null +++ b/test-cases/complex/testcase_quine.py @@ -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 +