Created
November 24, 2016 08:35
-
-
Save denkspuren/50849f028b82299e53f1692e0ae7db82 to your computer and use it in GitHub Desktop.
Proof of concept: Kernel for concatenative programming in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.*; | |
class Stakk { // Stack is taken by standard library | |
static Function<Stakk,Stakk> _mrot = (s -> s.rot().rot()); | |
static Function<Stakk,Stakk> _over = (s -> s.swap().dup().apply(_mrot)); | |
static Function<Stakk,Stakk> _2dup = (s -> s.apply(_over).apply(_over)); | |
static Function<Stakk,Stakk> _call = (s -> s.dup().dip().drop()); | |
static Function<Stakk,Stakk> _2dip = (s -> s.swap().quote(_s -> _s.dip()).dip()); | |
static Function<Stakk,Stakk> _keep = (s -> s.quote(_s -> _s.dup()).dip().dip()); | |
static Function<Stakk,Stakk> _if = (s -> s.choose().apply(_call)); | |
Stakk previous; | |
Object element; | |
private Stakk(Stakk previous, Object element) { | |
this.previous = previous; | |
this.element = element; | |
} | |
Stakk() { this(null,null); } | |
Stakk isEmpty() { return new Stakk(this, previous == null); } | |
Stakk push(Object o) { return new Stakk(this,o); } | |
Stakk drop() { | |
if (previous == null) throw new IllegalArgumentException("Stack Underflow"); | |
return this.previous; | |
} | |
Stakk dup() { return this.push(this.top()); } | |
Stakk swap() { | |
Object x = drop().top(); | |
Object y = this.top(); | |
return drop().drop().push(y).push(x); | |
} | |
Stakk rot() { | |
Object x = drop().drop().top(); | |
Object y = drop().top(); | |
Object z = this.top(); | |
return drop().drop().drop().push(y).push(z).push(x); | |
} | |
Stakk choose() { | |
Boolean choice = (Boolean) drop().drop().top(); | |
Object a = drop().top(); | |
Object b = this.top(); | |
return drop().drop().drop().push(choice ? a : b); | |
} | |
private <T> Object top() { return (T) element; } | |
<T extends Integer, Long, Float, Double> Stakk add() { | |
T x = (T) drop().top(); | |
T y = (T) this.top(); | |
return drop().drop().push(x + y); | |
} | |
<T extends Integer, Long, Float, Double> Stakk mul() { | |
T x = (T) drop().top(); | |
T y = (T) this.top(); | |
return drop().drop().push(x * y); | |
} | |
Stakk isEqual() { | |
Object x = drop().top(); | |
Object y = this.top(); | |
return drop().drop().push(x.equals(y)); | |
} | |
Stakk apply(Function<Stakk,Stakk> f) { | |
return f.apply(this); | |
} | |
Stakk quote(Function<Stakk,Stakk> f) { | |
return this.push(f); | |
} | |
Stakk dip() { // ( x quot -- x ) | |
Function<Stakk,Stakk> f = (Function<Stakk,Stakk>) this.top(); | |
Object x = drop().top(); | |
Stakk ds = drop().drop(); | |
return f.apply(ds).push(x); | |
} | |
String show() { | |
if (previous == null) return "["; | |
return previous.show() + " " + element.toString(); | |
} | |
public String toString() { | |
return show() + " ]"; | |
} | |
} | |
/* JShell interaction | |
jshell> Stakk s = new Stakk().push(4).push(5).apply(Stakk._over) | |
s ==> [ 4 5 4 ] | |
jshell> s.push(5).isEqual() | |
$135 ==> [ 4 5 false ] | |
jshell> $135.quote(s1 -> s1.push(1).add()) | |
$136 ==> [ 4 5 false $Lambda$44/156545103@149494d8 ] | |
jshell> $136.quote(s1 -> s1.push(-1).add()) | |
$137 ==> [ 4 5 false $Lambda$44/156545103@149494d8 $Lambda$45/1684015092@481a15ff ] | |
jshell> $137.apply(Stakk._if) | |
$138 ==> [ 4 4 ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment