DirectBrainFUCK

From Esolang
Jump to navigation Jump to search

DirectBrainFUCK is a brainfuck-like programming language, but you can directly move the pointer.

Peculiarity

DirectBrainFUCK is derived from brainfuck; however, the number of cells has to be limited to 64, accessible by a one-based index.

Also, its nearly impossible to change the value of a cell that the memory pointer is pointing at, unless you use the input instruction to let the user press a key corresponding to an ASCII value.

The language lacks the control flow commands [ and ], excluding it in particular from offering conditional and iterative facilities.

Instructions

Character What it does
< Moves the memory pointer one cell to the left.
> Moves the memory pointer one cell to the right.
mv Moves the memory pointer to the cell with the one-based index v, for which must hold: 1 ≤ v ≤ 64.
, Prompts the user for an input character and stores its ASCII code in the current cell.
. Prints the ASCII character associated with the value of the current cell.

Examples

The following example provides a one-time cat program:

 ,.

This cat program demonstrates the utilization of direct cell access using the dedicated m instruction:

>>,<<m3.

Implementations

A very simple implementation in Common Lisp is provided:

(defun interpret-DirectBrainFUCK (code &aux (position 0))
  (declare (type string code) (type fixnum position))
  (symbol-macrolet ((token (the character (char code position))))
    (let ((memory  (make-array 64 :element-type 'integer :initial-element 0))
          (pointer 0))
      (declare (type (vector integer 64) memory) (type (integer 0 63) pointer))
      (loop while (< position (length code)) do
        (case token
          (#\< (decf pointer))
          (#\> (incf pointer))
          (#\m (incf position)
               (setf pointer
                 (loop with start of-type fixnum = position
                       while   (and (< position (length code)) (digit-char-p token))
                       do      (incf position)
                       finally (return (1- (parse-integer code :start start :end position)))))
               (decf position))
          (#\, (format T "~&Please input a character: ")
               (setf (aref memory pointer) (char-code (read-char)))
               (clear-input))
          (#\. (write-char (code-char (aref memory pointer))))
          (T   NIL))
        (incf position)))))