Skip to content

Instantly share code, notes, and snippets.

@arahansa
Last active November 19, 2019 01:04
Show Gist options
  • Save arahansa/581ca376292fe2c7ecf3a8205de371b4 to your computer and use it in GitHub Desktop.
Save arahansa/581ca376292fe2c7ecf3a8205de371b4 to your computer and use it in GitHub Desktop.
CPS 자바연습
package com.arahansa;
public class CIterator {
public static class Result{
boolean done;
Object value;
public Result(boolean done) {
this.done = done;
}
public Result(boolean done, Object value) {
this.done = done;
this.value = value;
}
public Object getValue() {
return value;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
}
final static Result DONE = new Result(true);
Continuation target;
public CIterator(Continuation cont) {
this.target = cont;
}
public Result next(){
Continuation target = this.target;
if(target == null)
return DONE;
target.suspended();
if(target.isStop()) return CIterator.DONE;
if(target.isPass()){
this.target = target.next;
return this.next();
}else{
Result result = new Result(false, target.value);
this.target = target.getNext();
return result;
}
}
}
package com.arahansa;
import java.util.function.Function;
public class CodeSpitz {
public static void main(String[] args) {
Function<Object, CIterator> aaa = a -> new Context()
.set("a", a)
.set("b", null)
.next(new Continuation(0, cont -> {
if (1 != 1) cont.stop();
cont.resume(Continuation.PASS, null);
})).next(new Continuation(1, cont -> {
cont.set("a", ((Integer) cont.get("a")) + 1);
cont.set("b", cont.get("a"));
cont.resume(cont.get("b"), 0);
})).iterator();
CIterator iterator = aaa.apply(0);
CIterator.Result result;
for (int i = 0; i < 10; i++) {
result = iterator.next();
System.out.println("iter value : " + result.getValue());
}
}
}
package com.arahansa;
import java.util.HashMap;
import java.util.Map;
public class Context {
Map<String, Object> lexicalContext = new HashMap<>();
Map<Integer, Continuation> contTable = new HashMap<>();
Continuation start = null;
Continuation end = null;
public Context set(String key, Object value){
lexicalContext.put(key, value);
return this;
}
public Object get(String key){
return lexicalContext.get(key);
}
public void setCont(int key, Continuation cont) {
contTable.put(key, cont);
}
public Continuation getCont(int key){
return contTable.get(key);
}
public Context next(Continuation cont){
if(this.start == null){
this.start = cont;
this.end = cont;
}else{
this.end.setNext(cont);
this.end = cont;
}
cont.setContext(this);
return this;
}
public CIterator iterator(){
return new CIterator(this.start);
}
}
package com.arahansa;
import java.util.function.Consumer;
public class Continuation {
final static Continuation STOP = new Continuation();
final static Continuation PASS = new Continuation();
int key;
Context context;
Object value;
Continuation next;
Consumer<Continuation> block;
private Continuation() {
}
public Continuation(int key, Consumer<Continuation> block) {
this.key = key;
this.block = block;
}
public void set(String key, Object value){
this.context.set(key, value);
}
public Object get(String key){
return this.context.get(key);
}
public void setContext(Context context) {
this.context = context;
context.setCont(this.key, this);
}
public void setNext(Continuation cont) {
this.next = cont;
}
public Continuation getNext() {
return this.next;
}
public Object getValue(){
return value;
}
public boolean isStop(){
return this.value == Continuation.STOP;
}
public boolean isPass(){
return this.value == Continuation.PASS;
}
public void suspended(){
this.value = Continuation.STOP;
block.accept(this);
}
public void resume(Object v, Integer next){
this.value = v == null ? Continuation.PASS : v;
if(next != null){
this.next = this.context.getCont(next);
}
}
public static void main(String[] args) {
}
public void stop() {
this.value = STOP;
}
}
@arahansa
Copy link
Author

arahansa commented Nov 5, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment