Execute SNUSP/D: Difference between revisions
Content added Content deleted
m (re-implemented) |
m (changed, _join_ state of new thread thread should be inherited from original thread) |
||
Line 15: | Line 15: | ||
::*The original specification does not require threads execution in a specific order. For this command to be useful, the order of execution of tasks(threads) becomes important; |
::*The original specification does not require threads execution in a specific order. For this command to be useful, the order of execution of tasks(threads) becomes important; |
||
::*In this implementation, the order of execution is first-created-first-executed; |
::*In this implementation, the order of execution is first-created-first-executed; |
||
::*The original specification specifies that ''(&)SPLIT''-ed old thread skips the immediate code (see below |
::*The original specification specifies that ''(&)SPLIT''-ed old thread skips the immediate code (see below a->b->c->d example), which may lead to anti-intuition codes (which is good for an esoteric language :). This implementation retain old-thread-skips-immediate-code behavior in ''BLOATED'' mode, but new-thread-skips-immediate-code in ''SUPERNATURAL'' mode ( A->B->C->D example). 1->2->...->8 is thread creation order in ''SUPERNATURAL'' mode. |
||
:::<code style="line-height: 1em;">$&\ |
:::<code style="line-height: 1em;">$*&\==&\:&\:&\:=\<br> ; ~ ~ ~ ~ &\=> new(B)<br> ; 2A 3B 4C 5D \=> old(A)<br> ; \=!\=!\=!\===*..=.#<br> ; /=!/=!/=!/===*.=.=.#<br> ; 1b 6c 7d 8a &/=> old(a)<br> ; ~ ~ ~ ~ /=> new(b)<br> \==&/;&/;&/;=/ <br></code> |
||
::*The splited new thread inherites free/join/wait-state from the old thread. |
|||
*'''^ : wrap''' (teleport): |
*'''^ : wrap''' (teleport): |
||
#The code pointer will bounce back to the code space boundary in its reverse direction; |
#The code pointer will bounce back to the code space boundary in its reverse direction; |
||
#then forward and stop after the first '''^''' it encounter in normal direction. |
#then forward and stop after the first '''^''' it encounter in normal direction. |
||
<d>module snud ; |
<d>module snud ; |
||
private import std.string, std.random ; |
private import std.string, std.random ; |
||
// io interface, which has to be defined in another module |
// io interface, which has to be defined in another module |
||
Line 32: | Line 33: | ||
int rnd(int b) { return b < 0 ? (-rand()) % (-b + 1) : rand() % (b + 1) ; } |
int rnd(int b) { return b < 0 ? (-rand()) % (-b + 1) : rand() % (b + 1) ; } |
||
// simple stack template |
// simple stack template |
||
void push(T)(inout T[] stk, T value) { stk ~= value ; } |
void push(T)(inout T[] stk, T value) { stk ~= value ; } |
||
T pop(T)(inout T[] stk, bool discard = true) { |
T pop(T)(inout T[] stk, bool discard = true) { |
||
T top = stk[$-1] ; |
T top = stk[$-1] ; |
||
if(discard) stk.length = stk.length - 1 ; |
if(discard) stk.length = stk.length - 1 ; |
||
return top ; |
return top ; |
||
} |
} |
||
// a 2x tuple type |
// a 2x tuple type |
||
struct X(U,V = U) { |
struct X(U,V = U) { |
||
U x ; V y ; |
U x ; V y ; |
||
void to(ref U a, ref V b) { a = x ; b = y ; } |
void to(ref U a, ref V b) { a = x ; b = y ; } |
||
void from(U a, V b) { x = a ; y = b ; } |
void from(U a, V b) { x = a ; y = b ; } |
||
} |
} |
||
alias X!(int) I2 ; // intxint, used as code pointer, memory pointer & direction |
alias X!(int) I2 ; // intxint, used as code pointer, memory pointer & direction |
||
alias X!(I2) ST ; // (intxint)x(intxint), used as cpu state [cp,dp] |
alias X!(I2) ST ; // (intxint)x(intxint), used as cpu state [cp,dp] |
||
enum Mode : uint {CORE = 1, MODULAR, BLOATED, SUPERNATURAL } ; |
enum Mode : uint {CORE = 1, MODULAR, BLOATED, SUPERNATURAL } ; |
||
Line 72: | Line 73: | ||
void opIndexAssign(int value, I2 key) { cells[key] = value ; } |
void opIndexAssign(int value, I2 key) { cells[key] = value ; } |
||
int opIndex(I2 key) { |
int opIndex(I2 key) { |
||
int* vp = key in cells ; // get value pointer of the key, null if no such key |
int* vp = key in cells ; // get value pointer of the key, null if no such key |
||
if(vp is null) { cells[key] = 0 ; return 0 ; } // initialize the value/key pair |
if(vp is null) { cells[key] = 0 ; return 0 ; } // initialize the value/key pair |
||
return *vp ; // return the already existed value |
return *vp ; // return the already existed value |
||
Line 80: | Line 81: | ||
final class CPU { |
final class CPU { |
||
final class Task { |
final class Task { |
||
enum {FREE, JOINED, WAITING } |
enum {FREE, JOINED, WAITING } |
||
const int id ; |
const int id ; |
||
I2 cp, dp, mp ; |
I2 cp, dp, mp ; |
||
int join = FREE ; |
int join = FREE ; |
||
bool quit = false ; |
bool quit = false ; |
||
private ST[] stack ; |
private ST[] stack ; |
||
private char curCode ; |
private char curCode ; |
||
⚫ | |||
this(I2 Cp, I2 Dp, I2 Mp) |
this(I2 Cp, I2 Dp, I2 Mp, int joinstate = FREE) { |
||
cp = Cp ; dp = Dp ; mp = Mp ; id = Id++ ; |
|||
if((join = joinstate) == JOINED) joinwait++ ; |
|||
⚫ | |||
private void fwd(int step = 1) { with(cp) from(x + dp.x*step, y + dp.y*step) ; } |
private void fwd(int step = 1) { with(cp) from(x + dp.x*step, y + dp.y*step) ; } |
||
private void rfx(int dir) { with(dp) from(dir*y, dir*x) ; } |
private void rfx(int dir) { with(dp) from(dir*y, dir*x) ; } |
||
// _outer_ is D keyword for an inner class to ref outer class |
// _outer_ is D keyword for an inner class to ref outer class |
||
private bool hasCode() { return this.outer.hasCode(cp) ; } |
private bool hasCode() { return this.outer.hasCode(cp) ; } |
||
char getCode() { return this.outer.getCode(cp) ; } |
char getCode() { return this.outer.getCode(cp) ; } |
||
Task execute() { |
Task execute() { |
||
curCode = getCode ; |
curCode = getCode ; |
||
Line 118: | Line 121: | ||
case '#' : |
case '#' : |
||
if(stack.length > 0) // return from subroutine |
if(stack.length > 0) // return from subroutine |
||
{ stack.pop().to(cp,dp) ; fwd ; break ; } |
{ stack.pop().to(cp,dp) ; fwd ; break ; } |
||
case '\0': // else process as \0, = quit |
case '\0': // else process as \0, = quit |
||
quit = true ; goto RET ; |
quit = true ; goto RET ; |
||
case '&' : |
case '&' : |
||
if(tasksMode == Mode.BLOATED) // old task skip immediate code |
if(tasksMode == Mode.BLOATED) // old task skip immediate code |
||
{ fwd ; queued ~= new Task(cp, dp, mp) ; break ; } |
{ fwd ; queued ~= new Task(cp, dp, mp, join) ; break ; } |
||
else // new task skip immediate code |
else // new task skip immediate code |
||
{ fwd(2) ; queued ~= new Task(cp, dp, mp) ; fwd(-2) ; break ; } |
{ fwd(2) ; queued ~= new Task(cp, dp, mp, join) ; fwd(-2) ; break ; } |
||
case '~' : fwd ; m[mp] = getCode ; break ; // read next code as input |
case '~' : fwd ; m[mp] = getCode ; break ; // read next code as input |
||
case '*' : // join/wait threads |
case '*' : // join/wait threads |
||
Line 146: | Line 149: | ||
if(quit && join == JOINED) joinwait-- ; |
if(quit && join == JOINED) joinwait-- ; |
||
return this ; |
return this ; |
||
} |
} |
||
} |
} |
||
this(IIO inputoutput) { m = new Memory ; io = inputoutput ; } |
this(IIO inputoutput) { m = new Memory ; io = inputoutput ; } |
||
bool hasCode(I2 codePtr) |
bool hasCode(I2 codePtr) |
||
{ with(codePtr) return !(x < 0 || y < 0 || x >= width || y >= lines) ; } |
{ with(codePtr) return !(x < 0 || y < 0 || x >= width || y >= lines) ; } |
||
char getCode(I2 codePtr) |
char getCode(I2 codePtr) |
||
{ with(codePtr) return hasCode(codePtr) ? src[y][x] : '\0' ; } |
{ with(codePtr) return hasCode(codePtr) ? src[y][x] : '\0' ; } |
||
string program() { if(prog is null) prog = join(src,"\n") ; return prog ; } |
string program() { if(prog is null) prog = join(src,"\n") ; return prog ; } |
||
CPU load(string codes) { |
CPU load(string codes) { |
||
src = splitlines(codes) ; width = 0 ; lines = src.length ; prog = null ; |
src = splitlines(codes) ; width = 0 ; lines = src.length ; prog = null ; |
||
foreach(k,l; src) |
foreach(k,l; src) |
||
{ if ((src[k] = stripr(tr(l,"\0"," "))).length > width) width = src[k].length ; } |
{ if ((src[k] = stripr(tr(l,"\0"," "))).length > width) width = src[k].length ; } |
||
foreach(k,l; src) src[k] = l ~ repeat(" ", width - l.length) ; |
foreach(k,l; src) src[k] = l ~ repeat(" ", width - l.length) ; |
||
debugInput = pfind(&start, src, "debug[", "]debug") ; |
debugInput = pfind(&start, src, "debug[", "]debug") ; |
||
pfind(&start, src, "$") ; |
pfind(&start, src, "$") ; |
||
if(start.x < 0) start = I2(0,0) ; else start.x++ ; |
if(start.x < 0) start = I2(0,0) ; else start.x++ ; |
||
return this ; |
return this ; |
||
Line 175: | Line 178: | ||
} |
} |
||
int run(string codes,Mode mode = Mode.SUPERNATURAL, bool useDebugInput = true) |
int run(string codes,Mode mode = Mode.SUPERNATURAL, bool useDebugInput = true) |
||
{ return load(codes).initialize(mode, useDebugInput).run() ; } |
{ return load(codes).initialize(mode, useDebugInput).run() ; } |
||
int run() { while(nTaskLeft) run1Tick ; return m[lastmp] ; } |
int run() { while(nTaskLeft) run1Tick ; return m[lastmp] ; } |
||
bool run1Tick() { |
bool run1Tick() { |
||
if(nTaskLeft > 0) { |
if(nTaskLeft > 0) { |
||
nTaskLeft = 0 ; tick++ ; |
nTaskLeft = 0 ; tick++ ; |
||
foreach(tsk ; tasks) // execute & update task |
foreach(tsk ; tasks) // execute & update task |
||
if((tasks[nTaskLeft] = tsk.execute).quit == false) |
if((tasks[nTaskLeft] = tsk.execute).quit == false) |
||
nTaskLeft++ ; |
nTaskLeft++ ; |
||
tasks.length = nTaskLeft ; |
tasks.length = nTaskLeft ; |
||
Line 189: | Line 192: | ||
return nTaskLeft > 0 ; |
return nTaskLeft > 0 ; |
||
} |
} |
||
static const string[] command = ["<>+-,.!?/\\\0","@#",":;&%","~*^"] ; |
static const string[] command = ["<>+-,.!?/\\\0","@#",":;&%","~*^"] ; |
||
Memory m ; |
Memory m ; |
||
IIO io ; |
IIO io ; |
||
string prog, debugInput ; |
string prog, debugInput ; |
||
Mode tasksMode ; |
Mode tasksMode ; |