Skip to content

fwg/morphene

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

morphene

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 morphene -i. Type .help to see the REPL meta-commands.

More details please

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, $input and $collect. These are string-like buffers.

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 $input. Multi-character inputs are first matched and then exploded into their characters and the execution continues with the first one.

Predefined commands

Character choice is almost arbitrary. How lucky that you can redefine all of them.

Stacks

2ff0 switch to first stack
2ff2 switch to second stack

Stack operations

2ff1 pop from active stack to $input
2ff3 push $collect onto active stack
2ff6 collect input. $collect += $input
2ff5 uncollect to $input. $input = slice($collect, -1)
2ff7 execute $input. This interprets whatever is in $input in the topmost active context. What this means is explained further in the section about contexts. Sorry for the forward reference.
2ffb dup on active stack

Other commands

2205do nothing -> drops character
2229intersection -> $input = char
222aunion -> $input += char

Rule definition

2192 Create new command that matches $input and expands to the next character. The code that the new command expands to is bound to the current context. That means that if you redefine the symbols it uses after this rule, this will not change the behaviour of this rule.
21c9 Create new command that matches $input and expands to what the execution of the next character puts into $input.
2219 Magic input that matches all unmatched chars. The default rule can be written as ∙→∩

Contexts

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.

Now 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 $input. When the ref is executed, the context is switched.
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 $input. When the ref is executed, the context is switched.
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.

Input and output

2190 Input char into $input. This suspends execution and resumes as soon as there is an input on stdin, effectively blocking.
_Caution:_ code with input will probably break with the `-i` flag of the interpreter.
21c7 Ouput $input to stdout.

No recursion?

With commands unalterable once defined, there can be no reference to the command itself inside. But fear not! It is possible with .

Because executes $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 bin/morphene code/03_echo.morphene.

Spread and compact

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 spread and 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.

  • If the input is a single character, its bit pattern is spread on the stack in 1s and 0s. This pattern is bewteeen 8 and 32 bits long. The least significant bit is on top.
  • If the input is a string, its characters are spread on the stack.
  • If the input is a more complex type, its elements are spread on the stack.

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.

Compact arguments:

  • 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.

About

PLT Games Turing Tarpit

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published