Mindscrew

From Esolang
Jump to: navigation, search
This page formerly redirected to brainfuck. If you meant to go there, click the link now.
If you insist that this language's name should be changed to avoid confusion, mention it at the talk page.
Like brainfuck, mindscrew is to be written in lowercase except at the beginning of a sentence.
Mindscrew has recently received a massive overhaul which completely rewrites the structure of the language.

Mindscrew is an esoteric programming language and extension of brainfuck, designed by User:Rdococ. It aims to extend brainfuck with procedures, like pbrain and similar derivatives.

Structure

Mindscrew's structure is equivalent to that of brainfuck, plus the following:

  • A secondary tape, which may hold procedures.
  • A secondary procedure pointer to go along with the tape.

We will name these the FUNC tape and FUNC pointers for convenience, and by contrast, the main tape and pointer will be called the VALUE tape and VALUE pointer. But first, let's not forget:

  • The call stack.

Additional implementation details

  • Tail-call optimization is a requirement, for reasons explained below.
  • Cells should either be 8-bit unsigned integers, 8-bit signed wraparound integers, or optimize ([-][:]) and ([-[:]]) to primitives that set the current VALUE cell to 0.
  • The implementation should have an input buffer, enabling multiple-character input.

If you want, you can add optional flags to enable programmers and users to tweak the settings to their own tastes.

Instructions

Mindscrew's instructions are much the same as brainfuck, except with the following additions & changes:

Instruction Description
{ Moves the FUNC pointer to the left one cell.
} Moves the FUNC pointer to the right one cell.
(...) Overwrites the cell at the FUNC pointer with the instructions between parentheses.
: Runs the code at the current FUNC pointer.
] In mindscrew, the ] operation is a NOP - the idea is that a procedure must call itself recursively to facilitate looping.

As you can see, mindscrew takes inspiration from pbrain, another brainfuck derivative. However, it includes the FUNC tape and FUNC pointer to avoid the problems with determining which procedures will be called in the examination of pbrain code, and it modifies the existing brainfuck instructions to take advantage of these procedures.

Examples

If there are any semantic or syntactical errors in these examples, do not hesitate to mention it on the talk page.

Cat program

Any program which must loop at least once must create a procedure, thus the requirement for TCO - here is an example:

(,[.:]):

This cat program assumes that EOF == 0, and loops infinitely until , returns 0. One which additionally supports EOF == no change is more complicated, but not by too much:

(,[.}:{:])}(-[:]){:

...okay, I might have to walk you through this one.

  1. Firstly, we create a procedure in FUNC cell 0.
  2. We then move to FUNC cell 1, and write a secondary helper procedure.
  3. We move back to FUNC cell 0, and begin the first procedure:
  1. We ask for input.
  2. If the result is non-zero:
  1. We output the result.
  2. We move to FUNC cell 1 and call the latter procedure, which ensures that the current VALUE cell will be 0 for the next loop:
  1. Inside, we decrease the value at the current VALUE cell by 1.
  2. If it is nonzero, we recall the secondary, helper procedure we're in right now.
  1. We then move back to FUNC cell 0, and recall the procedure.

Computational Class

Brainfuck programs can easily be translated to mindscrew programs with similar semantics by replacing [ with }(, and ] with [:])[:]{. Here is an example, involving the "Hello, world!" program demonstrated in brainfuck:

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

And its equivalent in mindscrew:

++++++++}(>++++}(>++>+++>+++>+<<<<-[:])[:]{>+>+>->>+}(<[:])[:]{<-[:])[:]{>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

As you can see, this transformation makes programs longer than they could be, but it works (hopefully). Most notably, each first-level, non-nested }( could be stripped of the } character as long as you also strip the aforementioned loop's { at the end.

Additionally, I say that the mindscrew equivalents have similar semantics because each loop is not run until it is called with a : outside the loop. Other than that, the above works in a mindscrew interpreter. (I have created a crude Lua interpreter to test the above, and after correcting a bajillion errors in my code, it works. The interpreter is still a work in progress, however.)