WrongFuck

From Esolang
Jump to navigation Jump to search

WrongFuck is brainfuck, but wrong.

Overview

It runs off of the same brainfuck tape and pointer. All instructions are ran the same way.

Translation from brainfuck

brainfuck WrongFuck
+ >
- <
> ]
< [
[ ,
] .
. +
, -

Example programs

I didn't do any long programs because I'm lazy

Cat program

brainfuck:

,.[,.]

WrongFuck:

-+,-+.

Truth-machine

The following implements a truth-machine tantamount to the brainfuck ,.[-->+[>>]<[.]<<] program:

-+,<<]>,]].[,+.[[.

Interpreter

The following routines, implemented in Common Lisp, apply themselves to the conversion betwixt WrongFuck and brainfuck:

(deftype destination ()
  "The ``destination`` type defines a sink for output operations, the
   circumference of which amplects, without the claim of the
   contingency's exhaustion, the functions ``format`` and
   ``write-char``."
  '(or null (eql T) stream string))

;;; -------------------------------------------------------

(defun translate-brainfuck-to-WrongFuck (brainfuck-code
                                         &optional (destination NIL))
  "Transliterates the BRAINFUCK-CODE to the equivalent WrongFuck
   program, writes the resulting code to the DESTINATION, and returns
   for a non-``NIL`` DESTINATION the ``NIL`` value; otherwise produces
   and responds with a fresh string comprehending the output."
  (declare (type string      brainfuck-code))
  (declare (type destination destination))
  (the (or null string)
    (if destination
      (flet ((write-WrongFuck-command (wrongFuck-token)
              "Writes the WRONGFUCK-TOKEN to the DESTINATION and returns
               no value."
              (declare (type character wrongFuck-token))
              (format destination "~c" wrongFuck-token)
              (values)))
        (loop
          for brainfuck-token of-type character across brainfuck-code
          do
            (case brainfuck-token
              (#\+       (write-WrongFuck-command #\>))
              (#\-       (write-WrongFuck-command #\<))
              (#\>       (write-WrongFuck-command #\]))
              (#\<       (write-WrongFuck-command #\[))
              (#\[       (write-WrongFuck-command #\,))
              (#\]       (write-WrongFuck-command #\.))
              (#\.       (write-WrongFuck-command #\+))
              (#\,       (write-WrongFuck-command #\-))
              (otherwise NIL))))
      (with-output-to-string (output)
        (declare (type string-stream output))
        (translate-brainfuck-to-WrongFuck brainfuck-code output)))))

;;; -------------------------------------------------------

(defun translate-WrongFuck-to-brainfuck (wrongFuck-code
                                         &optional (destination NIL))
  "Transliterates the piece of WRONGFUCK-CODE to its brainfuck
   equivalent, writes the resulting program to the DESTINATION, and
   returns for a non-``NIL`` DESTINATION the ``NIL`` value; otherwise,
   for a ``NIL`` DESTINATION, responds with a fresh string comprehending
   the output."
  (declare (type string      wrongFuck-code))
  (declare (type destination destination))
  (the (or null string)
    (if destination
      (flet ((write-brainfuck-command (brainfuck-token)
              "Writes the BRAINFUCK-TOKEN to the DESTINATION and returns
               no value."
              (declare (type character brainfuck-token))
              (format destination "~c" brainfuck-token)
              (values)))
        (loop
          for wrongFuck-token of-type character across wrongFuck-code
          do
            (case wrongFuck-token
              (#\>       (write-brainfuck-command #\+))
              (#\<       (write-brainfuck-command #\-))
              (#\]       (write-brainfuck-command #\>))
              (#\[       (write-brainfuck-command #\<))
              (#\,       (write-brainfuck-command #\[))
              (#\.       (write-brainfuck-command #\]))
              (#\+       (write-brainfuck-command #\.))
              (#\-       (write-brainfuck-command #\,))
              (otherwise NIL))))
      (with-output-to-string (output)
        (declare (type string-stream output))
        (translate-WrongFuck-to-brainfuck wrongFuck-code output)))))