Redivider/Underload Interpreter

From Esolang
Jump to navigation Jump to search

An Underload interpreter written in Redivider, by User:MizardX

Code

main: run("").

run(stack):
  { /~/;
    fst: head[stack];
    snd: next[stack];
    stack: rest[stack];
    stack: push(fst)[stack];
    stack: push(snd)[stack];
    run(stack) }
| { /:/;
    fst: head[stack];
    run(push(fst)[stack]) }
| { /!/;
    run(tail[stack]) }
| { /\*/;
    fst: head[stack];
    snd: next[stack];
    stack: rest[stack];
    run(push(fst + snd)[stack]) }
| { /\(/;
    value: content;
    /\)/;
    run(push(value)[stack]) }
| { /a/;
    fst: head[stack];
    rest: tail[stack];
    run(push("(" + fst + ")")[stack]) }
| { /[\^]/;
    fst: head[stack];
    rest: tail[stack];
    run(rest)[fst + /.*/] }
| { /S/;
    fst: head[stack];
    rest: tail[stack];
    fst + run(rest) }
#| { invalid: /.+/;
#    "\nInvalid command: \"" + invalid + "\"" }
| "".
    
paren: /\(/ + content + /\)/.
content:
  /[^()]+/ + content
| paren + content
| "".

head: { item: /.*?(?=<>)/; /<>/; item }.
tail: { head; /.*/ }.
next: { head; head }.
rest: { head; head; /.*/ }.
push(value): value + "<>" + /.*/.