Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HELLO_WORLD = """\
; Hello, world!
0"Hello, world!"(>)w%10>"""
ECHO = """\
; Echos whatever it receives on STDIN. Dies on EOF.
1(%<#>+)w"""
HQ9p = """\
; HQ9+ interpreter for Clem
; Read program from STDIN
(0)1(%<#@.$+)w%/%
; Stack now looks like (x_1, x_2, ..., x_n, -1)
#
/+(
-
; If x == 72 ('H'), print "Hello, world!"
#72(-$-$)w%
#(%%1 0)w%-(%
0"Hello, world!"(>)w%10>
0)w%
; If x == 81 ('Q'), print source code
#81(-$-$)w%
#(%%1 0)w%-(%
; char, list, program
@@ ; program, char, list
# ; program, program, char, list
/+(->/+)w% ; program, c, list (print program)
@ ; char, list, program
0)w%
; If x == 57 ('9'), print 99 bottles
#57(-$-$)w%
#(%%1 0)w%-(%
99
(
#c0" bottles of beer on the wall, " (>)w% 10>
#c0" bottles of beer." (>)w% 10>
0"Take one down, pass it around, " (>)w% 10>
-
##
(
c0" bottles of beer on the wall." (>)w% 10>
%
1
0
)w%
-
(
0"No bottles of beer on the wall." (>)w% 10>
%
0
)w%
)w%
0)w%
; If x == 43 ('+'), increment the accumulator
#72(-$-$)w%
#(%%1 0)w%-(%
; Do nothing?
0)w%
%
/+
)w%
"""
MULTIPLY = """\
; An example of a multiplication algorithm
%s
%s
; Multiplication
; b, a
0@ ; b, a, prod
(
$ ; a, b, prod
@ ; a, prod, b
# ; a, a, prod, b
@ ; a, prod, a, b
($+$-)w%% ; prod += a, a, b
@ ; a, b, prod
$ ; b, a, prod
- ; b--, a, prod
)
w ; 0, a, a*b
%%%% ; a*b
c
"""
NINE_NINE_PROG = """\
; 99 bottles
1
(
#c0" bottles of beer on the wall, " (>)w% 10>
#c0" bottles of beer." (>)w% 10>
0"Take one down, pass it around, " (>)w% 10>
- ; n--
## ; n, n, n
(
c0" bottles of beer on the wall." (>)w% 10> ; n, n
% ; n
1 ; 1, n
0 ; 0, 1, n
)w% ; n ? (1, n) : (0, n)
- ; n ? (0, n) : (-1, n)
(
0"No bottles of beer on the wall." (>)w% 10>
% ; n
0 ; 0, n
)w% ; n
)w%
"""
NINE_NINE_OUT = '99 bottles of beer on the wall, \r\n99 bottles of beer.\r\nTake one down, pass it around, \r\n98 bottles of beer on the wall.\r\n98 bottles of beer on the wall, \r\n98 bottles of beer.\r\nTake one down, pass it around, \r\n97 bottles of beer on the wall.\r\n97 bottles of beer on the wall, \r\n97 bottles of beer.\r\nTake one down, pass it around, \r\n96 bottles of beer on the wall.\r\n96 bottles of beer on the wall, \r\n96 bottles of beer.\r\nTake one down, pass it around, \r\n95 bottles of beer on the wall.\r\n95 bottles of beer on the wall, \r\n95 bottles of beer.\r\nTake one down, pass it around, \r\n94 bottles of beer on the wall.\r\n94 bottles of beer on the wall, \r\n94 bottles of beer.\r\nTake one down, pass it around, \r\n93 bottles of beer on the wall.\r\n93 bottles of beer on the wall, \r\n93 bottles of beer.\r\nTake one down, pass it around, \r\n92 bottles of beer on the wall.\r\n92 bottles of beer on the wall, \r\n92 bottles of beer.\r\nTake one down, pass it around, \r\n91 bottles of beer on the wall.\r\n91 bottles of beer on the wall, \r\n91 bottles of beer.\r\nTake one down, pass it around, \r\n90 bottles of beer on the wall.\r\n90 bottles of beer on the wall, \r\n90 bottles of beer.\r\nTake one down, pass it around, \r\n89 bottles of beer on the wall.\r\n89 bottles of beer on the wall, \r\n89 bottles of beer.\r\nTake one down, pass it around, \r\n88 bottles of beer on the wall.\r\n88 bottles of beer on the wall, \r\n88 bottles of beer.\r\nTake one down, pass it around, \r\n87 bottles of beer on the wall.\r\n87 bottles of beer on the wall, \r\n87 bottles of beer.\r\nTake one down, pass it around, \r\n86 bottles of beer on the wall.\r\n86 bottles of beer on the wall, \r\n86 bottles of beer.\r\nTake one down, pass it around, \r\n85 bottles of beer on the wall.\r\n85 bottles of beer on the wall, \r\n85 bottles of beer.\r\nTake one down, pass it around, \r\n84 bottles of beer on the wall.\r\n84 bottles of beer on the wall, \r\n84 bottles of beer.\r\nTake one down, pass it around, \r\n83 bottles of beer on the wall.\r\n83 bottles of beer on the wall, \r\n83 bottles of beer.\r\nTake one down, pass it around, \r\n82 bottles of beer on the wall.\r\n82 bottles of beer on the wall, \r\n82 bottles of beer.\r\nTake one down, pass it around, \r\n81 bottles of beer on the wall.\r\n81 bottles of beer on the wall, \r\n81 bottles of beer.\r\nTake one down, pass it around, \r\n80 bottles of beer on the wall.\r\n80 bottles of beer on the wall, \r\n80 bottles of beer.\r\nTake one down, pass it around, \r\n79 bottles of beer on the wall.\r\n79 bottles of beer on the wall, \r\n79 bottles of beer.\r\nTake one down, pass it around, \r\n78 bottles of beer on the wall.\r\n78 bottles of beer on the wall, \r\n78 bottles of beer.\r\nTake one down, pass it around, \r\n77 bottles of beer on the wall.\r\n77 bottles of beer on the wall, \r\n77 bottles of beer.\r\nTake one down, pass it around, \r\n76 bottles of beer on the wall.\r\n76 bottles of beer on the wall, \r\n76 bottles of beer.\r\nTake one down, pass it around, \r\n75 bottles of beer on the wall.\r\n75 bottles of beer on the wall, \r\n75 bottles of beer.\r\nTake one down, pass it around, \r\n74 bottles of beer on the wall.\r\n74 bottles of beer on the wall, \r\n74 bottles of beer.\r\nTake one down, pass it around, \r\n73 bottles of beer on the wall.\r\n73 bottles of beer on the wall, \r\n73 bottles of beer.\r\nTake one down, pass it around, \r\n72 bottles of beer on the wall.\r\n72 bottles of beer on the wall, \r\n72 bottles of beer.\r\nTake one down, pass it around, \r\n71 bottles of beer on the wall.\r\n71 bottles of beer on the wall, \r\n71 bottles of beer.\r\nTake one down, pass it around, \r\n70 bottles of beer on the wall.\r\n70 bottles of beer on the wall, \r\n70 bottles of beer.\r\nTake one down, pass it around, \r\n69 bottles of beer on the wall.\r\n69 bottles of beer on the wall, \r\n69 bottles of beer.\r\nTake one down, pass it around, \r\n68 bottles of beer on the wall.\r\n68 bottles of beer on the wall, \r\n68 bottles of beer.\r\nTake one down, pass it around, \r\n67 bottles of beer on the wall.\r\n67 bottles of beer on the wall, \r\n67 bottles of beer.\r\nTake one down, pass it around, \r\n66 bottles of beer on the wall.\r\n66 bottles of beer on the wall, \r\n66 bottles of beer.\r\nTake one down, pass it around, \r\n65 bottles of beer on the wall.\r\n65 bottles of beer on the wall, \r\n65 bottles of beer.\r\nTake one down, pass it around, \r\n64 bottles of beer on the wall.\r\n64 bottles of beer on the wall, \r\n64 bottles of beer.\r\nTake one down, pass it around, \r\n63 bottles of beer on the wall.\r\n63 bottles of beer on the wall, \r\n63 bottles of beer.\r\nTake one down, pass it around, \r\n62 bottles of beer on the wall.\r\n62 bottles of beer on the wall, \r\n62 bottles of beer.\r\nTake one down, pass it around, \r\n61 bottles of beer on the wall.\r\n61 bottles of beer on the wall, \r\n61 bottles of beer.\r\nTake one down, pass it around, \r\n60 bottles of beer on the wall.\r\n60 bottles of beer on the wall, \r\n60 bottles of beer.\r\nTake one down, pass it around, \r\n59 bottles of beer on the wall.\r\n59 bottles of beer on the wall, \r\n59 bottles of beer.\r\nTake one down, pass it around, \r\n58 bottles of beer on the wall.\r\n58 bottles of beer on the wall, \r\n58 bottles of beer.\r\nTake one down, pass it around, \r\n57 bottles of beer on the wall.\r\n57 bottles of beer on the wall, \r\n57 bottles of beer.\r\nTake one down, pass it around, \r\n56 bottles of beer on the wall.\r\n56 bottles of beer on the wall, \r\n56 bottles of beer.\r\nTake one down, pass it around, \r\n55 bottles of beer on the wall.\r\n55 bottles of beer on the wall, \r\n55 bottles of beer.\r\nTake one down, pass it around, \r\n54 bottles of beer on the wall.\r\n54 bottles of beer on the wall, \r\n54 bottles of beer.\r\nTake one down, pass it around, \r\n53 bottles of beer on the wall.\r\n53 bottles of beer on the wall, \r\n53 bottles of beer.\r\nTake one down, pass it around, \r\n52 bottles of beer on the wall.\r\n52 bottles of beer on the wall, \r\n52 bottles of beer.\r\nTake one down, pass it around, \r\n51 bottles of beer on the wall.\r\n51 bottles of beer on the wall, \r\n51 bottles of beer.\r\nTake one down, pass it around, \r\n50 bottles of beer on the wall.\r\n50 bottles of beer on the wall, \r\n50 bottles of beer.\r\nTake one down, pass it around, \r\n49 bottles of beer on the wall.\r\n49 bottles of beer on the wall, \r\n49 bottles of beer.\r\nTake one down, pass it around, \r\n48 bottles of beer on the wall.\r\n48 bottles of beer on the wall, \r\n48 bottles of beer.\r\nTake one down, pass it around, \r\n47 bottles of beer on the wall.\r\n47 bottles of beer on the wall, \r\n47 bottles of beer.\r\nTake one down, pass it around, \r\n46 bottles of beer on the wall.\r\n46 bottles of beer on the wall, \r\n46 bottles of beer.\r\nTake one down, pass it around, \r\n45 bottles of beer on the wall.\r\n45 bottles of beer on the wall, \r\n45 bottles of beer.\r\nTake one down, pass it around, \r\n44 bottles of beer on the wall.\r\n44 bottles of beer on the wall, \r\n44 bottles of beer.\r\nTake one down, pass it around, \r\n43 bottles of beer on the wall.\r\n43 bottles of beer on the wall, \r\n43 bottles of beer.\r\nTake one down, pass it around, \r\n42 bottles of beer on the wall.\r\n42 bottles of beer on the wall, \r\n42 bottles of beer.\r\nTake one down, pass it around, \r\n41 bottles of beer on the wall.\r\n41 bottles of beer on the wall, \r\n41 bottles of beer.\r\nTake one down, pass it around, \r\n40 bottles of beer on the wall.\r\n40 bottles of beer on the wall, \r\n40 bottles of beer.\r\nTake one down, pass it around, \r\n39 bottles of beer on the wall.\r\n39 bottles of beer on the wall, \r\n39 bottles of beer.\r\nTake one down, pass it around, \r\n38 bottles of beer on the wall.\r\n38 bottles of beer on the wall, \r\n38 bottles of beer.\r\nTake one down, pass it around, \r\n37 bottles of beer on the wall.\r\n37 bottles of beer on the wall, \r\n37 bottles of beer.\r\nTake one down, pass it around, \r\n36 bottles of beer on the wall.\r\n36 bottles of beer on the wall, \r\n36 bottles of beer.\r\nTake one down, pass it around, \r\n35 bottles of beer on the wall.\r\n35 bottles of beer on the wall, \r\n35 bottles of beer.\r\nTake one down, pass it around, \r\n34 bottles of beer on the wall.\r\n34 bottles of beer on the wall, \r\n34 bottles of beer.\r\nTake one down, pass it around, \r\n33 bottles of beer on the wall.\r\n33 bottles of beer on the wall, \r\n33 bottles of beer.\r\nTake one down, pass it around, \r\n32 bottles of beer on the wall.\r\n32 bottles of beer on the wall, \r\n32 bottles of beer.\r\nTake one down, pass it around, \r\n31 bottles of beer on the wall.\r\n31 bottles of beer on the wall, \r\n31 bottles of beer.\r\nTake one down, pass it around, \r\n30 bottles of beer on the wall.\r\n30 bottles of beer on the wall, \r\n30 bottles of beer.\r\nTake one down, pass it around, \r\n29 bottles of beer on the wall.\r\n29 bottles of beer on the wall, \r\n29 bottles of beer.\r\nTake one down, pass it around, \r\n28 bottles of beer on the wall.\r\n28 bottles of beer on the wall, \r\n28 bottles of beer.\r\nTake one down, pass it around, \r\n27 bottles of beer on the wall.\r\n27 bottles of beer on the wall, \r\n27 bottles of beer.\r\nTake one down, pass it around, \r\n26 bottles of beer on the wall.\r\n26 bottles of beer on the wall, \r\n26 bottles of beer.\r\nTake one down, pass it around, \r\n25 bottles of beer on the wall.\r\n25 bottles of beer on the wall, \r\n25 bottles of beer.\r\nTake one down, pass it around, \r\n24 bottles of beer on the wall.\r\n24 bottles of beer on the wall, \r\n24 bottles of beer.\r\nTake one down, pass it around, \r\n23 bottles of beer on the wall.\r\n23 bottles of beer on the wall, \r\n23 bottles of beer.\r\nTake one down, pass it around, \r\n22 bottles of beer on the wall.\r\n22 bottles of beer on the wall, \r\n22 bottles of beer.\r\nTake one down, pass it around, \r\n21 bottles of beer on the wall.\r\n21 bottles of beer on the wall, \r\n21 bottles of beer.\r\nTake one down, pass it around, \r\n20 bottles of beer on the wall.\r\n20 bottles of beer on the wall, \r\n20 bottles of beer.\r\nTake one down, pass it around, \r\n19 bottles of beer on the wall.\r\n19 bottles of beer on the wall, \r\n19 bottles of beer.\r\nTake one down, pass it around, \r\n18 bottles of beer on the wall.\r\n18 bottles of beer on the wall, \r\n18 bottles of beer.\r\nTake one down, pass it around, \r\n17 bottles of beer on the wall.\r\n17 bottles of beer on the wall, \r\n17 bottles of beer.\r\nTake one down, pass it around, \r\n16 bottles of beer on the wall.\r\n16 bottles of beer on the wall, \r\n16 bottles of beer.\r\nTake one down, pass it around, \r\n15 bottles of beer on the wall.\r\n15 bottles of beer on the wall, \r\n15 bottles of beer.\r\nTake one down, pass it around, \r\n14 bottles of beer on the wall.\r\n14 bottles of beer on the wall, \r\n14 bottles of beer.\r\nTake one down, pass it around, \r\n13 bottles of beer on the wall.\r\n13 bottles of beer on the wall, \r\n13 bottles of beer.\r\nTake one down, pass it around, \r\n12 bottles of beer on the wall.\r\n12 bottles of beer on the wall, \r\n12 bottles of beer.\r\nTake one down, pass it around, \r\n11 bottles of beer on the wall.\r\n11 bottles of beer on the wall, \r\n11 bottles of beer.\r\nTake one down, pass it around, \r\n10 bottles of beer on the wall.\r\n10 bottles of beer on the wall, \r\n10 bottles of beer.\r\nTake one down, pass it around, \r\n9 bottles of beer on the wall.\r\n9 bottles of beer on the wall, \r\n9 bottles of beer.\r\nTake one down, pass it around, \r\n8 bottles of beer on the wall.\r\n8 bottles of beer on the wall, \r\n8 bottles of beer.\r\nTake one down, pass it around, \r\n7 bottles of beer on the wall.\r\n7 bottles of beer on the wall, \r\n7 bottles of beer.\r\nTake one down, pass it around, \r\n6 bottles of beer on the wall.\r\n6 bottles of beer on the wall, \r\n6 bottles of beer.\r\nTake one down, pass it around, \r\n5 bottles of beer on the wall.\r\n5 bottles of beer on the wall, \r\n5 bottles of beer.\r\nTake one down, pass it around, \r\n4 bottles of beer on the wall.\r\n4 bottles of beer on the wall, \r\n4 bottles of beer.\r\nTake one down, pass it around, \r\n3 bottles of beer on the wall.\r\n3 bottles of beer on the wall, \r\n3 bottles of beer.\r\nTake one down, pass it around, \r\n2 bottles of beer on the wall.\r\n2 bottles of beer on the wall, \r\n2 bottles of beer.\r\nTake one down, pass it around, \r\n1 bottles of beer on the wall.\r\n1 bottles of beer on the wall, \r\n1 bottles of beer.\r\nTake one down, pass it around, \r\nNo bottles of beer on the wall.\r\n'
test_data = [
('negative', [
("0 - 1 c c", "1-1"),
("0- 1 c c", "1-1"),
("0 -1 c c", "-10"),
("0-1 c c", "-10"),
("0--1cc", "-1-1"),
]),
('hello world', [(HELLO_WORLD, "Hello, world!\n")]),
('input', [
(ECHO, "oombga", "oombga"),
(ECHO, "zom a a 940f ()EF() EFK(", "zom a a 940f ()EF() EFK("),
(ECHO, "ah\nah\nah", "ah\nah\nah"),
]),
('multiply', [
(MULTIPLY % (21, 10), '210'),
(MULTIPLY % (66, 10), str(66*10)),
(MULTIPLY % (13, 15), str(13*15)),
]),
('nn', [
(NINE_NINE_PROG, NINE_NINE_OUT),
]),
('hq9+', [
(HQ9p, "Q", "Q"),
(HQ9p, "rtQrt", "rtQrt"),
(HQ9p, 'Hello, world!\nHQ', "HQ"),
(HQ9p, NINE_NINE_OUT, "9"),
]),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.