Morphene Turing Tarpit
morphene is a programming language created for the first PLT Games.
Its core feature is the ability to redefine what happens to any input character. The rules that define this behaviour make up a context. Contexts are similar to states in a state machine, with the difference that they work like the JS prototype chain. That is, opening a new context will inherit all the rules already defined in the currently opened context.
Furthermore you have stacks for storage much like in a pushdown automaton. In principle this language should be equivalent in power to a turing machine (Proof left to the author as an exercise some time in the distant future).
If you want to code in morphene it is strongly recommended to find out how to type unicode characters on your operating system. (Hint for Mac users: enable the keyboard 'Unicode hex input')
To play with it, execute
npm install -g morphene and run the REPL with
.help to see the REPL meta-commands.
The language has one fixed stack, called the first stack, that can be accessed and used everywhere. In addition to this each context has its own stack that is private to this context. It is called the second stack.
There is always only one active stack which the stack commands use, that is why
the language also has two registers,
$collect. These are
When morphene reads a character it tries to find a rule to apply to this
character. If it cannot find such a rule the default is to put it into
Multi-character inputs are first matched and then exploded into their characters
and the execution continues with the first one.
Character choice is almost arbitrary. How lucky that you can redefine all of them.
|2ff0||switch to first stack|
|2ff2||switch to second stack|
|2ff1||pop from active stack to |
|2ff6||collect input. |
|2ff5||uncollect to |
|2ffb||dup on active stack|
|2205||do nothing -> drops character|
|2229||intersection -> |
|222a||union -> |
|2192||Create new command that matches |
|21c9||Create new command that matches |
|2219||Magic input that matches all unmatched chars. The default rule can be written as ∙→∩|
A context is just a stack of definitions, and → just pushes a new definition. When the system looks up a definition for an input, it walks down the stack. The active contexts are also arranged in a stack, which results in a prototype chain like behaviour.
When you switch to a context, it is simply put ontop of the active-context-stack.
⿷ becomes easier to explain: it executes
$input in the topmost active
context, or the last one that was switched to.
Only contexts you switch to by hand are considered for
|2ffa||Put a reference to the current context into |
|2ff8||Open new context, all subsequent →/⇉ go into this context. This does not make the new context active!|
|2ff9||Close currently open context, puts reference to it into |
|2ff4||Pops top context off the context stack - basically goes back to previous context.|
The topmost context, the one the programme starts in, is special: when your define rules are active immediately. For all other contexts the definitions are not active until you actually switch to them.
|2190||Input char into |
_Caution:_ code with input will probably break with the `-i` flag of the interpreter.
With commands unalterable once defined, there can be no reference to the command
itself inside. But fear not! It is possible with
$input in the topmost context, it can be used for
recursion and late binding.
Consider a programme that echos what you input. For this to work we need to be able to bind longer pieces of code to a symbol.
; make a new context for single quote strings ⿸ ; append all chars to $input ∙→∪ ; but ' ends this context '→⿴ ; put context reference into $collect ⿹⿶ ; let ' switch to that context '⇉⿵
Now for the programme.
; put code for * in $collect ; repeat: input; output; * into $input; execute ; this works because * is defined as '∩' at the current line '←⇇*⿷'⿶ ; redefine and run * *⇉⿵ *
You can try this programme with
The only thing missing is comparisons. Equality is easily done with a new context and two rules, one for the same input and one for any other.
But what about
<? This is where
compact come in: These two commands
make it possible to compose larger data structures.
|☖||2616||Spread top of first stack into a new context's second stack. _It does not pop this item off the stack!_|
|☗||2617||Compact the whole second stack into one item and put it on top of the first stack. The first item on the first stack determines the way the stack is interpreted. Compact also pops the current context and goes back to the one before.|
Spread opens a new context and expands whatever is in top of the first stack into the second stack of this new context.
0s. This pattern is bewteeen 8 and 32 bits long. The least significant bit is on top.
Compact pops what is at the top of the first stack and looks at it. Depending on the value it compacts the second stack differently. In any case compact pops the topmost context and thus throws the second stack away if you don't save a reference to it somewhere.
c- Interpret each stack item as characters. This produces a string.
b- Interpret binary. This takes up to 32 bits off the stack and interpretes the resulting number as a UTF-8 character.
s- Save as structural type. This basically just saves the stack as is.
Look at the examples folder for fun and profit.