Skip to main content
Notice removed Draw attention by CommunityBot
Bounty Ended with no winning answer by CommunityBot
Notice added Draw attention by Seggan
Bounty Started worth 50 reputation by Seggan
Notice removed Reward existing answer by DLosc
Bounty Ended with Aaroneous Miller's answer chosen by DLosc
Notice added Reward existing answer by DLosc
Bounty Started worth 500 reputation by DLosc
Fixed typos
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Your challenge is to write an interpreter for Whitespace. Given a string consisting of spaces, tabs, newlines, and potentialpotentially other characters, as well as possible inputs for the Whitespace program itself, output the result of the given Whitespace program.

Here is an overview of the Whitespace language and its builtins:

Whitespace is a stack-based language which uses only three characters: spaces (ASCII codepoint 32); tabs (ASCII codepoint 9); and newlines (ASCII codepoint 10); all other characters are ignored.
It only has a couple of basic builtins, which I will go over below. Whitespace has both a stack, which can only consistsconsist of integers. As, as well as a heap, which is a map of integers (both the key and value).

Your challenge is to write an interpreter for Whitespace. Given a string consisting of spaces, tabs, newlines, and potential other characters, as well as possible inputs for the Whitespace program itself, output the result of the given Whitespace program.

Here an overview of the Whitespace language and its builtins:

Whitespace is a stack-based language which uses only three characters: spaces (ASCII codepoint 32); tabs (ASCII codepoint 9); and newlines (ASCII codepoint 10); all other characters are ignored.
It only has a couple of basic builtins, which I will go over below. Whitespace has both a stack, which can only consists of integers. As well as a heap, which is a map of integers (both the key and value).

Your challenge is to write an interpreter for Whitespace. Given a string consisting of spaces, tabs, newlines, and potentially other characters, as well as possible inputs for the Whitespace program itself, output the result of the given Whitespace program.

Here is an overview of the Whitespace language and its builtins:

Whitespace is a stack-based language which uses only three characters: spaces (ASCII codepoint 32); tabs (ASCII codepoint 9); and newlines (ASCII codepoint 10); all other characters are ignored.
It only has a couple of basic builtins, which I will go over below. Whitespace has a stack, which can only consist of integers, as well as a heap, which is a map of integers (both the key and value).

Notice removed Current answers are outdated by Fmbalbuena
Bounty Ended with DLosc's answer chosen by Fmbalbuena
Clarified wording
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141
  • Mark a location in the program with a label: NSS, followed by some (optional) S/T which aren't used by other labels/subroutines, followed by an N. I.e. if you're only using a single label in your full program, NSSN would be what to use when code-golfing. If you need two or three labels, you can add NSSSN and/or NSSTN.
    • Although it is possible to have multiple of the same labels in the TIO and vii5args interpreters, it will cause issues, so we assume the input will always only create a label/subroutine once.
    • Also, although NSSN would be a logical first label to use, it's completely valid to use a label NSSTSTSTTTSN instead as only label in the program.
  • Call a subroutine with the given label: NST, followed by some (optional) S/T which aren't used by other labels/subroutines, followed by an N. I.e. NSTTSTSTTTSN would jump to the label TSTSTTTS as subroutine.
  • Jump unconditionally to a label: NSN, followed by some (optional) S/T, followed by an N. I.e. NSNN would jump to the (empty) label N and continue the program flow from there.
  • JumpPop the top integer, and jump to a label if the top of the stackit is exactly 0: NTS, followed by some (optional) S/T, followed by an N. I.e. if the stack is currently [4,1,0] and we'd use NTSSN, it would jump to the label SN and continue the program flow from there (with stack [4,1]). If instead the stack is currently [4,1] and we'd use the NTSSN, it would jump past it to the next builtin below it (with stack [4]).
  • JumpPop the top integer, and jump to a label is the top of the stackif it is negative: NTT, followed by some (optional) S/T, followed by an N. I.e. if the stack is currently [4,1,-10] and we'd use NTTTN, it would jump to the label TN and continue the program flow from there (with stack [4,1]). If instead the stack is currently [4,1] and we'd use the NTTTN, it would jump past it to the next builtin below it (with stack [4]).
    • Minor note: There is no Jump to label if positive builtin available in Whitespace.
  • End a subroutine, and go back to the caller (a.k.a. return): NTN.
  • End the entire program: NNN (everything after that becomes no-ops).
  • Mark a location in the program with a label: NSS, followed by some (optional) S/T which aren't used by other labels/subroutines, followed by an N. I.e. if you're only using a single label in your full program, NSSN would be what to use when code-golfing. If you need two or three labels, you can add NSSSN and/or NSSTN.
    • Although it is possible to have multiple of the same labels in the TIO and vii5args interpreters, it will cause issues, so we assume the input will always only create a label/subroutine once.
    • Also, although NSSN would be a logical first label to use, it's completely valid to use a label NSSTSTSTTTSN instead as only label in the program.
  • Call a subroutine with the given label: NST, followed by some (optional) S/T which aren't used by other labels/subroutines, followed by an N. I.e. NSTTSTSTTTSN would jump to the label TSTSTTTS as subroutine.
  • Jump unconditionally to a label: NSN, followed by some (optional) S/T, followed by an N. I.e. NSNN would jump to the (empty) label N and continue the program flow from there.
  • Jump to a label if the top of the stack is exactly 0: NTS, followed by some (optional) S/T, followed by an N. I.e. if the stack is currently [4,1,0] and we'd use NTSSN, it would jump to the label SN and continue the program flow from there (with stack [4,1]). If instead the stack is currently [4,1] and we'd use the NTSSN, it would jump past it to the next builtin below it (with stack [4]).
  • Jump to a label is the top of the stack is negative: NTT, followed by some (optional) S/T, followed by an N. I.e. if the stack is currently [4,1,-10] and we'd use NTTTN, it would jump to the label TN and continue the program flow from there (with stack [4,1]). If instead the stack is currently [4,1] and we'd use the NTTTN, it would jump past it to the next builtin below it (with stack [4]).
    • Minor note: There is no Jump to label if positive builtin available in Whitespace.
  • End a subroutine, and go back to the caller (a.k.a. return): NTN.
  • End the entire program: NNN (everything after that becomes no-ops).
  • Mark a location in the program with a label: NSS, followed by some (optional) S/T which aren't used by other labels/subroutines, followed by an N. I.e. if you're only using a single label in your full program, NSSN would be what to use when code-golfing. If you need two or three labels, you can add NSSSN and/or NSSTN.
    • Although it is possible to have multiple of the same labels in the TIO and vii5args interpreters, it will cause issues, so we assume the input will always only create a label/subroutine once.
    • Also, although NSSN would be a logical first label to use, it's completely valid to use a label NSSTSTSTTTSN instead as only label in the program.
  • Call a subroutine with the given label: NST, followed by some (optional) S/T which aren't used by other labels/subroutines, followed by an N. I.e. NSTTSTSTTTSN would jump to the label TSTSTTTS as subroutine.
  • Jump unconditionally to a label: NSN, followed by some (optional) S/T, followed by an N. I.e. NSNN would jump to the (empty) label N and continue the program flow from there.
  • Pop the top integer, and jump to a label if it is exactly 0: NTS, followed by some (optional) S/T, followed by an N. I.e. if the stack is currently [4,1,0] and we'd use NTSSN, it would jump to the label SN and continue the program flow from there (with stack [4,1]). If instead the stack is currently [4,1] and we'd use the NTSSN, it would jump past it to the next builtin below it (with stack [4]).
  • Pop the top integer, and jump to a label if it is negative: NTT, followed by some (optional) S/T, followed by an N. I.e. if the stack is currently [4,1,-10] and we'd use NTTTN, it would jump to the label TN and continue the program flow from there (with stack [4,1]). If instead the stack is currently [4,1] and we'd use the NTTTN, it would jump past it to the next builtin below it (with stack [4]).
    • Minor note: There is no Jump to label if positive builtin available in Whitespace.
  • End a subroutine, and go back to the caller (a.k.a. return): NTN.
  • End the entire program: NNN (everything after that becomes no-ops).
Type fix
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141
  • Push a number to the stack: SS, followed by either an S/T for positive/negative respectively, followed by some S and/or T which is the binary representation of the number (S=0; T=1), followed by a trailing newline N. Some examples:
    • SSSTN pushes the number 1; a positive integer with binary 1.
    • SSTTSN pushes the number -2; a negative integer with binary 10.
    • SSSTSTSN pushes the number 10; a positive integer with binary 1010.
    • SSTTTSSTSSN pushes the number -100; a negative integer with binary 1100100.
    • Pushing number 0 is an edge case, since it can be done in multiple ways. Some examples:
      • SSSN: push a positive integer without any binary digits.
      • SSTN: push a negative integer without any binary digits.
      • SSSSN: push a positive integer with binary 0.
      • SSTSSSN: push a negative integer with binary 000.
  • Duplicate the top of the stack: SNS.
  • Copy the 0-based \$n\$th item from the top of the stack to the top of the stack: STS followed by a number similar as mentioned earlier (excluding the leading SS). I.e. let's say the stack currently contains the integers [47,12,0,55], then we could use STSSTSN to copy the 0-based 2nd item (which is the 12 in this case) to the top. So the stack becomes: [47,12,0,55,12].
    • NOTE: This index may not be negative. On TIO this would result in a negative index error, but that same program would push a 0 in the vii5ard interpreter, and it could even be different in yet another interpreter. For the sake of this challenge, you can therefore assume a given copy will never be negative. So the copy will always start with STSS, followed by the binary of the top-to-bottom index, followed by a trailing N.
  • Swap the top two items on the stack: SNT.
  • Discard the top item of the stack: SNN.
  • Discard/slice \$n\$ items from the top of the stack, but keep the top item: STN, followed by a number similar as mentioned earlier (excluding the leading SS). I.e. let's say the stack currently contains the integers [1,2,3,4,5,6,7], then we could use STNSTTN to discard 3 items from the stack (except the top one). So the stack becomes: [1,2,3,7].
    • You can again assume no negative slice values will be used, so this will always start with SNNSSTNS, followed by the amount to slice, followed by a trailing N.
  • Push a number to the stack: SS, followed by either an S/T for positive/negative respectively, followed by some S and/or T which is the binary representation of the number (S=0; T=1), followed by a trailing newline N. Some examples:
    • SSSTN pushes the number 1; a positive integer with binary 1.
    • SSTTSN pushes the number -2; a negative integer with binary 10.
    • SSSTSTSN pushes the number 10; a positive integer with binary 1010.
    • SSTTTSSTSSN pushes the number -100; a negative integer with binary 1100100.
    • Pushing number 0 is an edge case, since it can be done in multiple ways. Some examples:
      • SSSN: push a positive integer without any binary digits.
      • SSTN: push a negative integer without any binary digits.
      • SSSSN: push a positive integer with binary 0.
      • SSTSSSN: push a negative integer with binary 000.
  • Duplicate the top of the stack: SNS.
  • Copy the 0-based \$n\$th item from the top of the stack to the top of the stack: STS followed by a number similar as mentioned earlier (excluding the leading SS). I.e. let's say the stack currently contains the integers [47,12,0,55], then we could use STSSTSN to copy the 0-based 2nd item (which is the 12 in this case) to the top. So the stack becomes: [47,12,0,55,12].
    • NOTE: This index may not be negative. On TIO this would result in a negative index error, but that same program would push a 0 in the vii5ard interpreter, and it could even be different in yet another interpreter. For the sake of this challenge, you can therefore assume a given copy will never be negative. So the copy will always start with STSS, followed by the binary of the top-to-bottom index, followed by a trailing N.
  • Swap the top two items on the stack: SNT.
  • Discard the top item of the stack: SNN.
  • Discard/slice \$n\$ items from the top of the stack, but keep the top item: STN, followed by a number similar as mentioned earlier (excluding the leading SS). I.e. let's say the stack currently contains the integers [1,2,3,4,5,6,7], then we could use STNSTTN to discard 3 items from the stack (except the top one). So the stack becomes: [1,2,3,7].
    • You can again assume no negative slice values will be used, so this will always start with SNNS, followed by the amount to slice, followed by a trailing N.
  • Push a number to the stack: SS, followed by either an S/T for positive/negative respectively, followed by some S and/or T which is the binary representation of the number (S=0; T=1), followed by a trailing newline N. Some examples:
    • SSSTN pushes the number 1; a positive integer with binary 1.
    • SSTTSN pushes the number -2; a negative integer with binary 10.
    • SSSTSTSN pushes the number 10; a positive integer with binary 1010.
    • SSTTTSSTSSN pushes the number -100; a negative integer with binary 1100100.
    • Pushing number 0 is an edge case, since it can be done in multiple ways. Some examples:
      • SSSN: push a positive integer without any binary digits.
      • SSTN: push a negative integer without any binary digits.
      • SSSSN: push a positive integer with binary 0.
      • SSTSSSN: push a negative integer with binary 000.
  • Duplicate the top of the stack: SNS.
  • Copy the 0-based \$n\$th item from the top of the stack to the top of the stack: STS followed by a number similar as mentioned earlier (excluding the leading SS). I.e. let's say the stack currently contains the integers [47,12,0,55], then we could use STSSTSN to copy the 0-based 2nd item (which is the 12 in this case) to the top. So the stack becomes: [47,12,0,55,12].
    • NOTE: This index may not be negative. On TIO this would result in a negative index error, but that same program would push a 0 in the vii5ard interpreter, and it could even be different in yet another interpreter. For the sake of this challenge, you can therefore assume a given copy will never be negative. So the copy will always start with STSS, followed by the binary of the top-to-bottom index, followed by a trailing N.
  • Swap the top two items on the stack: SNT.
  • Discard the top item of the stack: SNN.
  • Discard/slice \$n\$ items from the top of the stack, but keep the top item: STN, followed by a number similar as mentioned earlier (excluding the leading SS). I.e. let's say the stack currently contains the integers [1,2,3,4,5,6,7], then we could use STNSTTN to discard 3 items from the stack (except the top one). So the stack becomes: [1,2,3,7].
    • You can again assume no negative slice values will be used, so this will always start with STNS, followed by the amount to slice, followed by a trailing N.
Notice added Current answers are outdated by Fmbalbuena
Bounty Started worth 50 reputation by Fmbalbuena
Commonmark migration
Source Link
Loading
added 138 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
deleted 655 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
edited body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading
Tweeted twitter.com/StackCodeGolf/status/1223259690286624768
added 4 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading