1 /*
2
3 This file is part of the Maude 2 interpreter.
4
5 Copyright 1997-2003 SRI International, Menlo Park, CA 94025, USA.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20
21 */
22
23 //
24 // Code for erewrite command.
25 //
26
27 void
eRewrite(const Vector<Token> & subject,Int64 limit,Int64 gas,bool debug)28 Interpreter::eRewrite(const Vector<Token>& subject, Int64 limit, Int64 gas, bool debug)
29 {
30 if (DagNode* d = makeDag(subject))
31 {
32 if (getFlag(SHOW_COMMAND))
33 {
34 UserLevelRewritingContext::beginCommand();
35 cout << "erewrite ";
36 printModifiers(limit, gas);
37 cout << d << " ." << endl;
38 if (xmlBuffer != 0)
39 xmlBuffer->generateErewrite(d, limit, gas);
40 }
41 UserLevelRewritingContext* context = new UserLevelRewritingContext(d);
42 context->setObjectMode(ObjectSystemRewritingContext::EXTERNAL);
43 VisibleModule* fm = currentModule->getFlatModule();
44
45 startUsingModule(fm);
46 if (getFlag(AUTO_CLEAR_RULES))
47 fm->resetRules();
48 beginRewriting(debug);
49 Timer timer(getFlag(SHOW_TIMING));
50 context->fairStart((gas == NONE) ? 1 : gas);
51 doExternalRewriting(context, limit);
52 endRewriting(timer, context, fm, &Interpreter::eRewriteCont);
53 }
54 }
55
56 void
doExternalRewriting(UserLevelRewritingContext * context,Int64 limit)57 Interpreter::doExternalRewriting(UserLevelRewritingContext* context, Int64 limit)
58 {
59 for (;;)
60 {
61 //
62 // Fair rewrite until we can make no further progress.
63 //
64 bool progress;
65 do
66 {
67 DebugAdvisory("calling fairTraversal()");
68 progress = context->fairTraversal(limit);
69 if (limit == 0)
70 return; // ran out of rewrites
71 }
72 while (progress);
73 //
74 // Now check for external events.
75 //
76 DebugAdvisory("calling PseudoThread::eventLoop()");
77 int r = PseudoThread::eventLoop();
78 DebugAdvisory("PseudoThread::eventLoop() returned " << r);
79 //cerr << "PseudoThread::eventLoop() returned " << r << endl;
80 if (r != PseudoThread::EVENT_HANDLED)
81 {
82 //
83 // No external events - check to see if we were interrupted.
84 // For the moment we treat interrupts just like having no external
85 // events, except that we clear the interrupt.
86 //
87 if (r & PseudoThread::INTERRUPTED)
88 UserLevelRewritingContext::clearInterrupt();
89 return;
90 }
91 }
92 }
93
94 void
eRewriteCont(Int64 limit,bool debug)95 Interpreter::eRewriteCont(Int64 limit, bool debug)
96 {
97 UserLevelRewritingContext* context = savedContext;
98 VisibleModule* fm = savedModule;
99 savedContext = 0;
100 savedModule = 0;
101 continueFunc = 0;
102 if (xmlBuffer != 0 && getFlag(SHOW_COMMAND))
103 xmlBuffer->generateContinue("erewrite", fm, limit);
104 context->clearCount();
105 beginRewriting(debug);
106 Timer timer(getFlag(SHOW_TIMING));
107 doExternalRewriting(context, limit);
108 endRewriting(timer, context, fm, &Interpreter::eRewriteCont);
109 }
110