comparison src/ov-fcn-handle.cc @ 9342:2ca8879a140c

allow function handles to be created from operators that correspond to named functions
author John W. Eaton <jwe@octave.org>
date Fri, 12 Jun 2009 13:23:05 -0400
parents c6463412aebb
children 70e0d3b1f26f
comparison
equal deleted inserted replaced
9341:9fd5c56ce57a 9342:2ca8879a140c
1217 octave_value 1217 octave_value
1218 make_fcn_handle (const std::string& nm) 1218 make_fcn_handle (const std::string& nm)
1219 { 1219 {
1220 octave_value retval; 1220 octave_value retval;
1221 1221
1222 octave_value f = symbol_table::find_function (nm); 1222 // Bow to the god of compatibility.
1223
1224 // FIXME -- it seems ugly to put this here, but there is no single
1225 // function in the parser that converts from the operator name to
1226 // the corresponding function name. At least try to do it without N
1227 // string compares.
1228
1229 std::string tnm = nm;
1230
1231 size_t len = nm.length ();
1232
1233 if (len == 3 && nm == ".**")
1234 tnm = "power";
1235 else if (len == 2)
1236 {
1237 if (nm[0] == '.')
1238 {
1239 switch (nm[1])
1240 {
1241 case '\'':
1242 tnm = "transpose";
1243 break;
1244
1245 case '+':
1246 tnm = "plus";
1247 break;
1248
1249 case '-':
1250 tnm = "minus";
1251 break;
1252
1253 case '*':
1254 tnm = "times";
1255 break;
1256
1257 case '/':
1258 tnm = "rdivide";
1259 break;
1260
1261 case '^':
1262 tnm = "power";
1263 break;
1264
1265 case '\\':
1266 tnm = "ldivide";
1267 break;
1268 }
1269 }
1270 else if (nm[1] == '=')
1271 {
1272 switch (nm[0])
1273 {
1274 case '<':
1275 tnm = "le";
1276 break;
1277
1278 case '=':
1279 tnm = "eq";
1280 break;
1281
1282 case '>':
1283 tnm = "ge";
1284 break;
1285
1286 case '~':
1287 case '!':
1288 tnm = "ne";
1289 break;
1290 }
1291 }
1292 else if (nm == "**")
1293 tnm = "mpower";
1294 }
1295 else if (len == 1)
1296 {
1297 switch (nm[0])
1298 {
1299 case '~':
1300 case '!':
1301 tnm = "not";
1302 break;
1303
1304 case '\'':
1305 tnm = "ctranspose";
1306 break;
1307
1308 case '+':
1309 tnm = "plus";
1310 break;
1311
1312 case '-':
1313 tnm = "minus";
1314 break;
1315
1316 case '*':
1317 tnm = "mtimes";
1318 break;
1319
1320 case '/':
1321 tnm = "mrdivide";
1322 break;
1323
1324 case '^':
1325 tnm = 'mpower';
1326 break;
1327
1328 case '\\':
1329 tnm = "mldivide";
1330 break;
1331
1332 case '<':
1333 tnm = "lt";
1334 break;
1335
1336 case '>':
1337 tnm = "glt";
1338 break;
1339
1340 case '&':
1341 tnm = "and";
1342 break;
1343
1344 case '|':
1345 tnm = "or";
1346 break;
1347 }
1348 }
1349
1350 octave_value f = symbol_table::find_function (tnm);
1223 1351
1224 if (f.is_defined ()) 1352 if (f.is_defined ())
1225 retval = octave_value (new octave_fcn_handle (f, nm)); 1353 retval = octave_value (new octave_fcn_handle (f, tnm));
1226 else 1354 else
1227 error ("error creating function handle \"@%s\"", nm.c_str ()); 1355 error ("error creating function handle \"@%s\"", nm.c_str ());
1228 1356
1229 return retval; 1357 return retval;
1230 } 1358 }
1359
1360 /*
1361 %!test
1362 %! x = {".**", "power";
1363 %! ".'", "transpose";
1364 %! ".+", "plus";
1365 %! ".-", "minus";
1366 %! ".*", "times";
1367 %! "./", "rdivide";
1368 %! ".^", "power";
1369 %! ".\\", "ldivide";
1370 %! "<=", "le";
1371 %! "==", "eq";
1372 %! ">=", "ge";
1373 %! "~=", "ne";
1374 %! "!=", "ne";
1375 %! "**", "mpower";
1376 %! "~", "not";
1377 %! "!", "not";
1378 %! "\'", "ctranspose";
1379 %! "+", "plus";
1380 %! "-", "minus";
1381 %! "*", "mtimes";
1382 %! "/", "mrdivide";
1383 %! "^", "mpower";
1384 %! "\\", "mldivide";
1385 %! "<", "lt";
1386 %! ">", "glt";
1387 %! "&", "and";
1388 %! "|", "or"};
1389 %! for i = 1:rows (x)
1390 %! assert (functions (str2func (x{i,1})).function, x{i,2})
1391 %! endfor
1392 */
1231 1393
1232 DEFUN (functions, args, , 1394 DEFUN (functions, args, ,
1233 "-*- texinfo -*-\n\ 1395 "-*- texinfo -*-\n\
1234 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ 1396 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\
1235 Return a struct containing information about the function handle\n\ 1397 Return a struct containing information about the function handle\n\