Skip to content

Commit

Permalink
Support forward time travel
Browse files Browse the repository at this point in the history
  • Loading branch information
cwesson committed Aug 20, 2023
1 parent 270077e commit 9c23bbb
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/FungeUniverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Funge {
FungeUniverse::FungeUniverse(std::istream& file, Field::FileFormat fmt, const FungeConfig& cfg):
age(0),
running(true),
locked(false),
exitcode(0),
config(cfg),
debug(*this),
Expand All @@ -34,6 +35,7 @@ FungeUniverse::FungeUniverse(std::istream& file, Field::FileFormat fmt, const Fu
FungeUniverse::FungeUniverse(const FungeConfig& cfg):
age(0),
running(true),
locked(false),
exitcode(0),
config(cfg),
debug(*this),
Expand Down Expand Up @@ -128,9 +130,11 @@ void FungeUniverse::operator()(){
FungeRunner* runner = *it;

mutex.unlock();
if(runner->isRunning()){
runner->tick();
}
do {
if(runner->isRunning()){
runner->tick();
}
} while(locked);
mutex.lock();

if(&runner->getUniverse() == this){
Expand Down Expand Up @@ -180,6 +184,14 @@ bool FungeUniverse::isRunning() const{
return running && alive;
}

void FungeUniverse::lock(){
locked = true;
}

void FungeUniverse::unlock(){
locked = false;
}

Field& FungeUniverse::getField(){
return field;
}
Expand Down
125 changes: 123 additions & 2 deletions src/fingerprint/FingerprintTRDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,139 @@

#include "FingerprintTRDS.h"
#include "FungeUniverse.h"
#include "FungeUtilities.h"

namespace Funge {

FingerprintTRDS::FingerprintTRDS(FungeRunner& r) :
Fingerprint(r, {'P'})
Fingerprint(r, {'C', 'D', 'E', 'G', 'I', 'J', 'P', 'R', 'S', 'T', 'U', 'V'}),
space(),
space_rel(NONE),
time(0),
time_rel(NONE),
vec(),
vec_rel(NONE),
jump_space(),
jump_time(0),
jump_vec(),
jumping(false)
{}

FungeError FingerprintTRDS::execute(inst_t cmd){
switch(cmd){
case 'P':{
case 'C':{
runner.getUniverse().unlock();
} break;
case 'D':{
space = popVector(runner);
space_rel = ABSOLUTE;
} break;
case 'E':{
space = popVector(runner);
space_rel = RELATIVE;
} break;
case 'G':{
stack.top().push(runner.getUniverse().getAge());
} break;
case 'I':{
space = jump_space;
time = jump_time;
space_rel = ABSOLUTE;
time_rel = ABSOLUTE;
vec = jump_vec;
vec_rel = ABSOLUTE;
} break;
case 'J':{
FungeError ret = ERROR_BLOCK;
ssize_t current_time = runner.getUniverse().getAge();
if(!jumping){
jump_space = runner.getIP().getPos();
jump_time = current_time;
jump_vec = runner.getIP().getDelta();
switch(space_rel){
case NONE:
space = jump_space;
break;
case ABSOLUTE:
break;
case RELATIVE:
space += jump_space;
space_rel = ABSOLUTE;
break;
}
switch(time_rel){
case NONE:
time = jump_time;
break;
case ABSOLUTE:
break;
case RELATIVE:
time += jump_time;
time_rel = ABSOLUTE;
break;
}

switch(vec_rel){
case NONE:
vec = jump_vec;
break;
case ABSOLUTE:
break;
case RELATIVE:
break;
}
if(time_rel == RELATIVE){
time += jump_time;
time_rel = ABSOLUTE;
}
if(time > jump_time){
// Jumping to future, just block
ret = ERROR_BLOCK;
jumping = true;
}if(time < jump_time){
// Jumping to past not supported
ret = ERROR_UNSPEC;
} // else jump to now
}else if(time > current_time){
// Waiting for universe to catchup
ret = ERROR_BLOCK;
}else{
// Jump complete
jumping = false;
runner.getIP().setPos(space);
runner.getIP().setDelta(vec);
}
return ret;
} break;
case 'P':{
stack.top().push(0);
} break;
case 'R':{
space = {0};
space_rel = NONE;
time = 0;
time_rel = NONE;
vec = {0};
vec_rel = NONE;
jump_space = {0};
jump_time = 0;
jump_vec = {0};
} break;
case 'S':{
runner.getUniverse().lock();
} break;
case 'T':{
time = stack.top().pop();
time_rel = ABSOLUTE;
} break;
case 'U':{
time = stack.top().pop();
time_rel = RELATIVE;
} break;
case 'V':{
vec = popVector(runner);
vec_rel = ABSOLUTE;
} break;
default:
return ERROR_UNIMP;
}
Expand Down
18 changes: 18 additions & 0 deletions src/fingerprint/include/FingerprintTRDS.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ class FingerprintTRDS : public Fingerprint {
virtual ~FingerprintTRDS() = default;

virtual FungeError execute(inst_t cmd) override;

private:
enum relativity {
NONE,
ABSOLUTE,
RELATIVE
};

Vector space;
relativity space_rel;
ssize_t time;
relativity time_rel;
Vector vec;
relativity vec_rel;
Vector jump_space;
ssize_t jump_time;
Vector jump_vec;
bool jumping;
};

}
11 changes: 11 additions & 0 deletions src/include/FungeUniverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ class FungeUniverse {

void operator()();

/**
* Lock universe to current runner.
*/
void lock();

/**
* Unlock universe.
*/
void unlock();

FungeDebugger& getDebugger();
const std::string& getName() const;
const std::vector<std::string>& arguments() const;
Expand All @@ -127,6 +137,7 @@ class FungeUniverse {
private:
size_t age;
bool running;
bool locked;
int exitcode;
FungeConfig config;
FungeDebugger debug;
Expand Down
2 changes: 1 addition & 1 deletion test/smoketest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test_simple test/test_subr.b98 "7 6 5 4 3 2 1 362880 "
test_simple test/test_mvrs.b98 "1 Hello test2 3 "
test_simple test/test_fing.b98 "1 5 10 100 100 "
test_simple test/test_fish.b98 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 5 4 3 2 1 "
test_simple test/test_trds.b98 "8 "
test_simple test/test_trds.b98 "8 7 6 5 4 3 2 1 "
# Edge Cases
test_simple test/kk.b98 "!12 "
test_simple test/k_quote.b98 "2 "
Expand Down
4 changes: 3 additions & 1 deletion test/test_trds.b98
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
"SDRT"4(P.)@
"SDRT"4(G.#vt52Df7+TJ)@
>zzzzzzzz4567S....C@
123...@

0 comments on commit 9c23bbb

Please sign in to comment.