Persistent canvas for subprograms

master
rubenwardy 2016-04-04 11:30:18 +01:00
parent e5089f9b31
commit 8ad488f484
7 changed files with 34 additions and 12 deletions

3
examples/sp_getput.rf Normal file
View File

@ -0,0 +1,3 @@
> v
v _ "a" 51 p R
> 51g R

View File

View File

@ -1,3 +1,3 @@
> v
v P "A"0"add"0 < Load ::add, call it l.
v P "A"0"add"0 < Load ::add, call it A
> 2 2 A . 25*, @

View File

@ -0,0 +1,3 @@
> v
v P "A"0"sp_getput"0 < Load ::sp_getput, call it l.
> A 1 A , 25*, @

View File

@ -0,0 +1 @@
a

View File

@ -94,6 +94,11 @@ For example, to pass "hello" as a parameter would be:
| R | rtn | Return to caller |
| L | lock | Hide the stack before a certain cell - use to stop bugs overwriting previous program's stack elements. |
Calling P will load the subprogram's canvas into memory. Any changes to the Canvas
in one subprogram call will be kept and seen by the next.
See submodule_getput.rf which calls sp_getput.rf
### Examples
```

View File

@ -265,7 +265,10 @@ public:
char y = th->pop();
char x = th->pop();
Canvas *c = th->cursor->canvas;
th->push(c->get(x, y));
char v = c->get(x, y);
th->push(v);
std::cerr << "Got " << v << " from " << (int)x << ", " << (int)y << std::endl;
th->move();
}
@ -281,6 +284,7 @@ public:
char v = th->pop();
Canvas *c = th->cursor->canvas;
c->set(x, y, v);
std::cerr << "Put " << v << " to " << (int)x << ", " << (int)y << std::endl;
th->move();
}
@ -321,25 +325,18 @@ public:
class StartRufungeSR : public Subroutine
{
public:
Canvas *canvas = new Canvas();
std::string module;
std::string file;
virtual void run(VM *vm, Thread *th)
{
Canvas *d = new Canvas();
std::string filepath = std::string("examples/") + file + ".rf";
if (d->readFromFile(filepath.c_str())) {
std::cerr << "Loaded " << filepath << std::endl;
} else {
std::cerr << "Error! Unable to load SR "
<< module << "::" << file << std::endl;
th->state = ETS_DEAD;
return;
}
std::cerr << "Entering " << filepath << std::endl;
th->move();
Cursor *cursor = new Cursor();
cursor->canvas = d;
cursor->canvas = canvas;
th->pushCursor(cursor);
vm->assignStandardSR(th);
}
@ -369,10 +366,23 @@ public:
c = th->pop();
}
// Load canvas
Canvas *d = new Canvas();
std::string filepath = std::string("examples/") + file + ".rf";
if (d->readFromFile(filepath.c_str())) {
std::cerr << "Loaded " << filepath << std::endl;
} else {
std::cerr << "Error! Unable to load SR "
<< module << "::" << file << std::endl;
th->state = ETS_DEAD;
return;
}
// Load as operator
StartRufungeSR *sr = new StartRufungeSR;
sr->module = module;
sr->file = file;
sr->canvas = d;
th->cursor->operators[op] = vm->loadSubroutine(sr);
std::cerr << "Loaded rufunge operator " << op << " to be "
<< module << "::" << file << std::endl;