File: Eval.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (218 lines) | stat: -rw-r--r-- 7,758 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (c) 2009 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "Array.hpp"
#include "Interpreter.hpp"
#include "Algorithms.hpp"

//@@Signature
//sfunction eval EvalFunction
//inputs try_clause catch_clause
//outputs varargout
//DOCBLOCK freemat_eval
static QString PrePendCallVars(QString line, int nargout) {
  QString gp;
  if (nargout > 1)
    gp += "[";
  for (int i=0;i<nargout-1;i++)
    gp += QString("t___%1,").arg(i);
  gp += QString("t___%1").arg(nargout-1);
  if (nargout > 1)
    gp += "] = " + line + ";\n";
  else
    gp += " = " + line + ";\n";
  return gp;
}

static ArrayVector RetrieveCallVars(Interpreter *eval, int nargout) {
  ArrayVector retval;
  for (int i=0;i<nargout;i++) {
    QString tname = QString("t___%1").arg(i);
    Array tval;
    ArrayReference ptr = eval->getContext()->lookupVariable(tname);
    if (!ptr.valid())
      tval = EmptyConstructor();
    else
      tval = *ptr;
    eval->getContext()->deleteVariable(tname);
    retval.push_back(tval);
  }
  return retval;
}


static ArrayVector EvalTryFunction(int nargout, Interpreter* eval, QString try_buf, 
				   QString catch_buf, bool retrieveVars, int popSpec) {
  ArrayVector retval;
  bool autostop;
  autostop = eval->AutoStop();
  eval->setAutoStop(false);
  bool save_trycatch_flag(eval->getTryCatchActive());
  eval->setTryCatchActive(true);
  Context *context = eval->getContext();
  PopContext saver(context,popSpec);
  int eval_depth = context->scopeDepth();
  try {
    eval->evaluateString(try_buf,true);
    if (retrieveVars)
      retval = RetrieveCallVars(eval,nargout);
  } catch (Exception &e) {
    while (context->scopeDepth() < eval_depth) context->restoreScope(1);
    while (context->scopeDepth() > eval_depth) context->popScope();
    eval->evaluateString(catch_buf,false);
    if (retrieveVars)
      retval = RetrieveCallVars(eval,nargout);
  }
  eval->setTryCatchActive(save_trycatch_flag);
  eval->setAutoStop(autostop);
  return retval;
}

static ArrayVector EvalTryFunction(int nargout, const ArrayVector& arg, Interpreter* eval, int popSpec) {
  if (nargout > 0) {
    QString try_line = arg[0].asString();
    QString try_buf = PrePendCallVars(try_line,nargout);
    QString catch_line = arg[1].asString();
    QString catch_buf = PrePendCallVars(catch_line,nargout);
    return EvalTryFunction(nargout,eval,try_buf,catch_buf,true,popSpec);
   } else {
    QString try_line = arg[0].asString();
    QString catch_line = arg[1].asString();
    QString try_buf = try_line + "\n";
    QString catch_buf = catch_line + "\n";
    return EvalTryFunction(nargout,eval,try_buf,catch_buf,false,popSpec);
  }
}

ArrayVector TraceFunction(int nargout, const ArrayVector& arg, Interpreter* eval);

static ArrayVector EvalNoTryFunction(int nargout, const ArrayVector& arg, Interpreter* eval, int popSpec) {
  if (nargout > 0) {
    QString line = arg[0].asString();
    QString buf = PrePendCallVars(line,nargout);
    PopContext saver(eval->getContext(),popSpec);
    eval->evaluateString(buf);
    return RetrieveCallVars(eval,nargout);
  } else {
    QString line = arg[0].asString();
    QString buf = line + "\n";
    PopContext saver(eval->getContext(),popSpec);
    eval->evaluateString(buf);
    return ArrayVector();
  }
}

ArrayVector EvalFunction(int nargout, const ArrayVector& arg,Interpreter* eval){
  eval->getContext()->deactivateCurrentScope(); // Make us invisible
  if (arg.size() == 0)
    throw Exception("eval function takes at least one argument - the string to execute");
  if (arg.size() == 2)
    return EvalTryFunction(nargout, arg, eval, 0);
  return EvalNoTryFunction(nargout, arg, eval, 0);
}

//@@Signature
//sfunction evalin EvalInFunction
//inputs varargin
//outputs x y z
//DOCBLOCK freemat_evalin
ArrayVector EvalInFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  if (arg.size() < 2)
    throw Exception("evalin function requires a workspace (scope) specifier (either 'caller' or 'base') and an expression to evaluate");
  QString spec_str = arg[0].asString();
  int popspec = 0;
  if (spec_str=="base")
    popspec = -1;
  else if (spec_str=="caller")
    popspec = 2;
  else
    throw Exception("evalin function requires the first argument to be either 'caller' or 'base'");
  ArrayVector argcopy(arg);
  argcopy.pop_front();
  if (arg.size() == 3)
    return EvalTryFunction(nargout,argcopy,eval,popspec);
  else
    return EvalNoTryFunction(nargout,argcopy,eval,popspec);
}

//@@Signature
//sfunction assignin AssignInFunction
//inputs workspace variablename value
//outputs none
//DOCBLOCK freemat_assignin
ArrayVector AssignInFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  if (arg.size() < 3)
    throw Exception("assignin function requires a workspace (scope) specifier (either 'caller' or 'base') a variable name and a value to assign");
  QString spec_str = arg[0].asString();
  int popspec = 0;
  if (spec_str=="base")
    popspec = -1;
  else if (spec_str=="caller") 
    popspec = 2;
  else
    throw Exception("assignin function requires the first argument to be either 'caller' or 'base'");
  QString varname = arg[1].asString();
  Array varvalue = arg[2];
  PopContext saver(eval->getContext(),popspec);
  eval->getContext()->insertVariable(varname,varvalue);
  return ArrayVector();
}


ArrayVector TraceFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  qDebug() << "**********************************************************************\n";
  // walk the trace of 
  while (eval->getContext()->activeScopeName() != "base") {
    qDebug() << "Scope is " << eval->getContext()->activeScopeName();
    qDebug() << "Variables " << eval->getContext()->listAllVariables();
    eval->getContext()->bypassScope(1);
  }
  qDebug() << "Scope is " << eval->getContext()->activeScopeName();
  qDebug() << "Variables " << eval->getContext()->listAllVariables();
  qDebug() << "**********************************************************************\n";
  eval->getContext()->restoreBypassedScopes();
  return ArrayVector();
}

//@@Signature
//sfunction feval FevalFunction
//inputs varargin
//outputs varargout
//DOCBLOCK freemat_feval
ArrayVector FevalFunction(int nargout, const ArrayVector& arg,Interpreter* eval){
  if (arg.size() == 0)
    throw Exception("feval function requires at least one argument");
  if (!arg[0].isString())
    throw Exception("first argument to feval must be the name of a function (i.e., a string) a function handle, or a user defined class");
  eval->getContext()->deactivateCurrentScope(); // Make feval call invisible
  FuncPtr funcDef;
  if (arg[0].isString()) {
    QString fname = arg[0].asString();
    if (!eval->lookupFunction(fname,funcDef)) {
      throw Exception(QString("function ") + fname + " undefined!");
    }
  } else 
    throw Exception("argument to feval must be a string");
  funcDef->updateCode(eval);
  if (funcDef->scriptFlag)
    throw Exception("cannot use feval on a script");
  ArrayVector newarg(arg);
  newarg.pop_front();
  return eval->doFunction(funcDef,newarg,nargout);
}