From 5a250fddd291f720795379f5cbfbf601e752d92b Mon Sep 17 00:00:00 2001 From: James Barnes Date: Fri, 4 Oct 2024 18:12:47 +1000 Subject: [PATCH 1/5] Add quine.wybe sample --- samples/quine.wybe | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 samples/quine.wybe diff --git a/samples/quine.wybe b/samples/quine.wybe new file mode 100644 index 00000000..583963e2 --- /dev/null +++ b/samples/quine.wybe @@ -0,0 +1,46 @@ +## quine.wybe +# +# A Quine +?listing = [ +"## quine.wybe", +"#", +"# A Quine", +"?listing = [", +"]", +"?lines = listing^length", +"for _ in 0..4; ?line in listing {", +" !println line", +"}", +"for ?index in 0..lines; ?line in listing {", +" ?quote = 34:char", +" !print quote; !print line; !print quote", +" if { index < lines - 1 ::", +" !println ','", +" | else ::", +" !nl", +" }", +"}", +"for ?i in 4..lines {", +" if { ?line = listing[i] ::", +" !println(line)", +" }", +"}" +] +?lines = listing^length +for _ in 0..4; ?line in listing { + !println line +} +for ?index in 0..lines; ?line in listing { + ?quote = 34:char + !print quote; !print line; !print quote + if { index < lines - 1 :: + !println ',' + | else :: + !nl + } +} +for ?i in 4..lines { + if { ?line = listing[i] :: + !println(line) + } +} From cde8839349127ac113acc97e37ea413ff74bbf2a Mon Sep 17 00:00:00 2001 From: James Barnes Date: Fri, 4 Oct 2024 18:13:18 +1000 Subject: [PATCH 2/5] Fix typo in json; add more details in sammples/README.md --- samples/README.md | 18 +++++++++++++++++- samples/json.wybe | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/samples/README.md b/samples/README.md index 2597e29f..1c072c83 100644 --- a/samples/README.md +++ b/samples/README.md @@ -1 +1,17 @@ -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, run + +```sh +$ wybemk samples/quine && ./samples/quine | diff ./samples/quine.wybe - +``` 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. From 26db5fd57f52b78bc47dd5c5a99bf30940fc2d69 Mon Sep 17 00:00:00 2001 From: James Barnes Date: Fri, 4 Oct 2024 18:31:48 +1000 Subject: [PATCH 3/5] Formatting; authorship --- samples/quine.wybe | 66 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/samples/quine.wybe b/samples/quine.wybe index 583963e2..f4c64e25 100644 --- a/samples/quine.wybe +++ b/samples/quine.wybe @@ -1,45 +1,55 @@ ## quine.wybe +## Author: James Barnes # # A Quine ?listing = [ -"## quine.wybe", -"#", -"# A Quine", -"?listing = [", -"]", -"?lines = listing^length", -"for _ in 0..4; ?line in listing {", -" !println line", -"}", -"for ?index in 0..lines; ?line in listing {", -" ?quote = 34:char", -" !print quote; !print line; !print quote", -" if { index < lines - 1 ::", -" !println ','", -" | else ::", -" !nl", -" }", -"}", -"for ?i in 4..lines {", -" if { ?line = listing[i] ::", -" !println(line)", -" }", -"}" + "## 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)", + " }", + "}" ] -?lines = listing^length -for _ in 0..4; ?line in listing { +?n_lines = listing^length +?preamble_lines = 5 +for _ in 0..preamble_lines; ?line in listing { !println line } -for ?index in 0..lines; ?line in listing { +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 < lines - 1 :: + if { index < n_lines - 1 :: !println ',' | else :: !nl } } -for ?i in 4..lines { +for ?i in preamble_lines..n_lines { if { ?line = listing[i] :: !println(line) } From c4b5d8cd5d855f514b505ae79a0d1e26984e1763 Mon Sep 17 00:00:00 2001 From: James Barnes Date: Fri, 4 Oct 2024 20:30:16 +1000 Subject: [PATCH 4/5] Add complex test with shorter quine --- test-cases/complex/exp/testcase_quine-quine.exp | 8 ++++++++ test-cases/complex/testcase_quine.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test-cases/complex/exp/testcase_quine-quine.exp create mode 100644 test-cases/complex/testcase_quine.py 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 + From afb63b7601498c1d4cc9b212bd04e19d1ccfe788 Mon Sep 17 00:00:00 2001 From: James Barnes Date: Fri, 4 Oct 2024 21:01:10 +1000 Subject: [PATCH 5/5] Better validation in README --- samples/README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/samples/README.md b/samples/README.md index 1c072c83..453b5e09 100644 --- a/samples/README.md +++ b/samples/README.md @@ -10,8 +10,12 @@ A JSON document parser. ## quine.wybe -A [Quine](https://en.wikipedia.org/wiki/Quine_(computing)). To validate, run +A [Quine](https://en.wikipedia.org/wiki/Quine_(computing)). +To validate: ```sh -$ wybemk samples/quine && ./samples/quine | diff ./samples/quine.wybe - +$ wybemk quine +$ ./quine | diff ./quine.wybe - +$ echo $? +0 ```