From 8ad488f484c1497399b587bb90c0f9e4537e6f17 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Mon, 4 Apr 2016 11:30:18 +0100 Subject: [PATCH] Persistent canvas for subprograms --- examples/sp_getput.rf | 3 +++ examples/sp_getput.rf.txt | 0 examples/submodule_add.rf | 2 +- examples/submodule_getput.rf | 3 +++ examples/submodule_getput.rf.txt | 1 + specification.md | 5 +++++ src/rufunge.cpp | 32 +++++++++++++++++++++----------- 7 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 examples/sp_getput.rf create mode 100644 examples/sp_getput.rf.txt create mode 100644 examples/submodule_getput.rf create mode 100644 examples/submodule_getput.rf.txt diff --git a/examples/sp_getput.rf b/examples/sp_getput.rf new file mode 100644 index 0000000..e3c74d3 --- /dev/null +++ b/examples/sp_getput.rf @@ -0,0 +1,3 @@ +> v +v _ "a" 51 p R +> 51g R diff --git a/examples/sp_getput.rf.txt b/examples/sp_getput.rf.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/submodule_add.rf b/examples/submodule_add.rf index 53275e7..0cadc66 100644 --- a/examples/submodule_add.rf +++ b/examples/submodule_add.rf @@ -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*, @ diff --git a/examples/submodule_getput.rf b/examples/submodule_getput.rf new file mode 100644 index 0000000..1658468 --- /dev/null +++ b/examples/submodule_getput.rf @@ -0,0 +1,3 @@ +> v +v P "A"0"sp_getput"0 < Load ::sp_getput, call it l. +> A 1 A , 25*, @ diff --git a/examples/submodule_getput.rf.txt b/examples/submodule_getput.rf.txt new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/examples/submodule_getput.rf.txt @@ -0,0 +1 @@ +a diff --git a/specification.md b/specification.md index da18d14..eb84b54 100644 --- a/specification.md +++ b/specification.md @@ -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 ``` diff --git a/src/rufunge.cpp b/src/rufunge.cpp index 2d2e568..38c4033 100644 --- a/src/rufunge.cpp +++ b/src/rufunge.cpp @@ -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;