diff options
author | Norbert Preining <norbert@preining.info> | 2021-12-28 03:01:00 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2021-12-28 03:01:00 +0000 |
commit | ecdf859b6ce481abfd530425dcf6f0f764bd0001 (patch) | |
tree | 13bc161dc046876ac6c92fce5f9f5034ba9aa573 /graphics/asymptote/process.cc | |
parent | 790995b7e79697514364450bf9c04f1b8d500838 (diff) |
CTAN sync 202112280300
Diffstat (limited to 'graphics/asymptote/process.cc')
-rw-r--r-- | graphics/asymptote/process.cc | 302 |
1 files changed, 124 insertions, 178 deletions
diff --git a/graphics/asymptote/process.cc b/graphics/asymptote/process.cc index acfd2f5e7f..9c3d2d313f 100644 --- a/graphics/asymptote/process.cc +++ b/graphics/asymptote/process.cc @@ -16,7 +16,7 @@ #include "program.h" #include "interact.h" #include "envcompleter.h" -#include "parser.h" + #include "fileio.h" #include "stack.h" @@ -136,7 +136,7 @@ public: : runnable(base->getPos()), base(base) {} void prettyprint(ostream &out, Int indent) { - absyntax::prettyname(out, "interactiveRunnable", indent); + absyntax::prettyname(out, "interactiveRunnable", indent, base->getPos()); base->prettyprint(out, indent+1); } @@ -151,10 +151,7 @@ public: } }; -enum transMode { - TRANS_INTERACTIVE, - TRANS_NORMAL -}; + // How to run a runnable in runnable-at-a-time mode. @@ -187,165 +184,122 @@ void runAutoplain(coenv &e, istack &s) { runRunnable(r,e,s); } -// Abstract base class for the core object being run in line-at-a-time mode, it -// may be a block of code, file, or interactive prompt. -struct icore { - virtual ~icore() {} - - virtual void doParse() = 0; - virtual void doList() = 0; - -public: +// icore - // preRun and postRun are the optional activities that take place before and - // after running the code specified. They can be overridden by a derived - // class that wishes different behaviour. - virtual void preRun(coenv &e, istack &s) { - if(getSetting<bool>("autoplain")) - runAutoplain(e,s); - } - virtual void run(coenv &e, istack &s, transMode tm=TRANS_NORMAL) = 0; +// preRun and postRun are the optional activities that take place before and +// after running the code specified. They can be overridden by a derived +// class that wishes different behaviour. +void icore::preRun(coenv &e, istack &s) { + if(getSetting<bool>("autoplain")) + runAutoplain(e,s); +} - virtual void postRun(coenv &, istack &s) { - run::exitFunction(&s); - } +void icore::postRun(coenv &, istack &s) { + run::exitFunction(&s); +} - virtual void doRun(bool purge=false, transMode tm=TRANS_NORMAL) { - em.sync(); - if(em.errors()) - return; +void icore::doRun(bool purge, transMode tm) { + em.sync(); + if(em.errors()) + return; - try { - if(purge) run::purge(); + try { + if(purge) run::purge(); - penv pe; - env base_env(pe.ge()); - coder base_coder(nullPos, "icore::doRun"); - coenv e(base_coder,base_env); + penv pe; + env base_env(pe.ge()); + coder base_coder(nullPos, "icore::doRun"); + coenv e(base_coder,base_env); - vm::interactiveStack s; - s.setInitMap(pe.ge().getInitMap()); - s.setEnvironment(&e); + vm::interactiveStack s; + s.setInitMap(pe.ge().getInitMap()); + s.setEnvironment(&e); - preRun(e,s); + preRun(e,s); - if(purge) run::purge(); + if(purge) run::purge(); - // Now that everything is set up, run the core. - run(e,s,tm); + // Now that everything is set up, run the core. + run(e,s,tm); - postRun(e,s); + postRun(e,s); - } catch(std::bad_alloc&) { - outOfMemory(); - } catch(quit) { - // Exception to quit running the current code. Nothing more to do. - } catch(handled_error) { - em.statusError(); - } + } catch(std::bad_alloc&) { + outOfMemory(); + } catch(quit const&) { + // Exception to quit running the current code. Nothing more to do. + } catch(handled_error const&) { + em.statusError(); + } - run::cleanup(); + run::cleanup(); - em.clear(); - } + em.clear(); +} - virtual void process(bool purge=false) { - if (!interactive && getSetting<bool>("parseonly")) - doParse(); - else if (getSetting<bool>("listvariables")) - doList(); - else - doRun(purge); - } -}; -// Abstract base class for one-time processing of an abstract syntax tree. -class itree : public icore { - string name; +void icore::process(bool purge) { + if (!interactive && getSetting<bool>("parseonly")) + doParse(); + else if (getSetting<bool>("listvariables")) + doList(); + else + doRun(purge); +} - block *cachedTree; -public: - itree(string name="<unnamed>") - : name(name), cachedTree(0) {} +itree::itree(string name) : name(name), cachedTree(0) { } - // Build the tree, possibly throwing a handled_error if it cannot be built. - virtual block *buildTree() = 0; - virtual block *getTree() { - if (cachedTree==0) { - try { - cachedTree=buildTree(); - } catch(handled_error) { - em.statusError(); - return 0; - } +block *itree::getTree() { + if (cachedTree==0) { + try { + cachedTree=buildTree(); + } catch(handled_error const&) { + em.statusError(); + return 0; } - return cachedTree; } + return cachedTree; +} - virtual string getName() { - return name; - } +string itree::getName() { + return name; +} - void doParse() { - block *tree=getTree(); - em.sync(); - if(tree && !em.errors()) - tree->prettyprint(cout, 0); - } +void itree::doParse() { + block *tree=getTree(); + em.sync(); + if(tree && !em.errors()) + tree->prettyprint(cout, 0); +} - void doList() { - block *tree=getTree(); - if (tree) { - penv pe; - record *r=tree->transAsFile(pe.ge(), symbol::trans(getName())); - r->e.list(r); - } +void itree::doList() { + block *tree=getTree(); + if (tree) { + penv pe; + record *r=tree->transAsFile(pe.ge(), symbol::trans(getName())); + r->e.list(r); } +} - void run(coenv &e, istack &s, transMode tm=TRANS_NORMAL) { - block *tree=getTree(); - if (tree) { - for(mem::list<runnable *>::iterator r=tree->stms.begin(); - r != tree->stms.end(); ++r) { - processData().fileName=(*r)->getPos().filename(); - if(!em.errors() || getSetting<bool>("debug")) - runRunnable(*r,e,s,tm); - } +void itree::run(coenv &e, istack &s, transMode tm) { + block *tree=getTree(); + if (tree) { + for(mem::list<runnable *>::iterator r=tree->stms.begin(); + r != tree->stms.end(); ++r) { + processData().fileName=(*r)->getPos().filename(); + if(!em.errors() || getSetting<bool>("debug")) + runRunnable(*r,e,s,tm); } } +} - void doExec(transMode tm=TRANS_NORMAL) { - // Don't prepare an environment to run the code if there isn't any code. - if (getTree()) - icore::doRun(false,tm); - } -}; - -class icode : public itree { - block *tree; - -public: - icode(block *tree, string name="<unnamed>") - : itree(name), tree(tree) {} - - block *buildTree() { - return tree; - } -}; - -class istring : public itree { - string str; - -public: - istring(const string& str, string name="<eval>") - : itree(name), str(str) {} - - block *buildTree() { - return parser::parseString(str, getName()); - } -}; +void itree::doExec(transMode tm) { + // Don't prepare an environment to run the code if there isn't any code. + if (getTree()) + icore::doRun(false,tm); +} void printGreeting(bool interactive) { if(!getSetting<bool>("quiet")) { @@ -356,53 +310,46 @@ void printGreeting(bool interactive) { } } -class ifile : public itree { - string filename; - string outname; - string outname_save; +ifile::ifile(const string& filename) + : itree(filename), + filename(filename), + outname(stripDir(stripExt(string(filename == "-" ? settings::outname() : filename), suffix))) {} -public: - ifile(const string& filename) - : itree(filename), - filename(filename), - outname(stripDir(stripExt(string(filename == "-" ? settings::outname() : filename), suffix))) {} +block *ifile::buildTree() { + return !filename.empty() ? parser::parseFile(filename,"Loading") : 0; +} - block *buildTree() { - return !filename.empty() ? parser::parseFile(filename,"Loading") : 0; - } +void ifile::preRun(coenv& e, istack& s) { + outname_save=getSetting<string>("outname"); + if(stripDir(outname_save).empty()) + Setting("outname")=outname_save+outname; - void preRun(coenv& e, istack& s) { - outname_save=getSetting<string>("outname"); - if(stripDir(outname_save).empty()) - Setting("outname")=outname_save+outname; + itree::preRun(e, s); +} - itree::preRun(e, s); - } +void ifile::postRun(coenv &e, istack& s) { + itree::postRun(e, s); - void postRun(coenv &e, istack& s) { - itree::postRun(e, s); + Setting("outname")=outname_save; +} - Setting("outname")=outname_save; +void ifile::process(bool purge) { + if(verbose > 1) printGreeting(false); + try { + init(); + } catch(handled_error const&) { } - void process(bool purge=false) { - if(verbose > 1) printGreeting(false); - try { - init(); - } catch(handled_error) { - } - - if (verbose >= 1) - cout << "Processing " << outname << endl; + if (verbose >= 1) + cout << "Processing " << outname << endl; - try { - icore::process(purge); - } - catch(handled_error) { - em.statusError(); - } + try { + icore::process(purge); } -}; + catch(handled_error const&) { + em.statusError(); + } +} // Add a semi-colon terminator, if one is not there. string terminateLine(const string line) { @@ -783,7 +730,6 @@ class iprompt : public icore { // Continue taking input until a termination command is received from xasy. block *parseXasyLine(string line) { - #ifdef __MSDOS__ const string EOT="\x04\r\n"; #else @@ -823,7 +769,7 @@ class iprompt : public icore { run::updateFunction(&s); uptodate=false; - } catch(handled_error) { + } catch(handled_error const&) { vm::indebugger=false; } catch(interrupted&) { // Turn off the interrupted flag. @@ -873,7 +819,7 @@ public: interact::init_interactive(); try { setPath("",true); - } catch(handled_error) { + } catch(handled_error const&) { } do { @@ -975,11 +921,11 @@ public: } catch(std::bad_alloc&) { // TODO: give calling application useful message. cerr << "out of memory" << endl; - } catch (quit) { + } catch (quit const&) { // I'm not sure whether this counts as successfully running the code or // not. cerr << "quit exception" << endl; - } catch (handled_error) { + } catch (handled_error const&) { // Normally, this is the result of an error that changes the return code // of the free-standing asymptote program. // An error should probably be reported to the application calling the |