home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CALCPLUS.ZIP / CALCLIB.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-02  |  8.2 KB  |  337 lines

  1. /*******************************************************
  2.  
  3.     The CalcPlus Class Library Version 1.0,
  4.     Author: Vladimir Schipunov, Copyright (C) 1996
  5.  
  6.     This library is free software. Permission to use,
  7.     copy, modify and redistribute the CalcPlus library
  8.     for any purpose is hereby granted without fee,
  9.     provided that the above copyright notice appear
  10.     in all copies.
  11.  
  12. *******************************************************/
  13.  
  14. #include <iostream.h>
  15. #include <stdlib.h>
  16. #include "calcexpr.h"
  17. #include "calctype.h"
  18.  
  19. //
  20. //  This is an example of how new functions can be added
  21. //  to the interpreter. Note that you must register
  22. //  function or procedure specifying its name, address and
  23. //  number of arguments (see at EOF).
  24. //  Enumeration of arguments begins from 0.
  25. //
  26.  
  27. //------------------------------------------------------
  28. //
  29. //  Creating new array of the length n
  30. //
  31. //------------------------------------------------------
  32.  
  33. USER_FUNC( NewArray )
  34.     DEF_ARGV( 0, n, Long )
  35.     RETURNS( Array )
  36.     for( int i = 0; i < (long)n; i++ )
  37.         ret.add( new CNil );
  38. USER_END
  39.  
  40. //------------------------------------------------------
  41. //
  42. //  Returns length of the array
  43. //
  44. //------------------------------------------------------
  45.  
  46. USER_FUNC( Len )
  47.     DEF_ARGX( 0, x )
  48.     RETURNS( Long )
  49.     int t = x.type();
  50.     if( t!=idString && t!=idArray )
  51.         USER_ERR("Wrong type in calling LEN");
  52.     ret = x.size();
  53. USER_END
  54.  
  55. //------------------------------------------------------
  56. //
  57. //  Extracts substring from the given string
  58. //
  59. //------------------------------------------------------
  60.  
  61. USER_FUNC( Substr )
  62.     DEF_ARGV( 0, str, String )
  63.     DEF_ARGV( 1, pos, Long )
  64.     DEF_ARGV( 2, len, Long )
  65.     delete xpf->v;
  66.     xpf->v = new CString( str.s()+
  67.         (int)((long)pos), (int)((long)len+1) );
  68.     RETURNS( String );
  69.     ret.zero = 1;
  70.     ret[len] = 0;
  71. USER_END
  72.  
  73. //------------------------------------------------------
  74. //
  75. //  Returns array of the strings as
  76. //  the description of structure
  77. //
  78. //------------------------------------------------------
  79.  
  80. USER_FUNC( Structure )
  81.     DEF_ARGV( 0, str, String )
  82.     LexObj *obj = 0;
  83.     for( XBlock *x = xpf->xblock(); x && !obj;
  84.         x = x->father ? x->father->xblock() : 0 )
  85.             obj = x->structs( str );
  86.     if( !obj )
  87.         USER_ERR("Structure not found");
  88.     delete xpf->v;
  89.     xpf->v = ((LexStruct*)obj)->data->copy();
  90. USER_END
  91.  
  92. //------------------------------------------------------
  93. //
  94. //  Truncates spaces from the right end of the string
  95. //
  96. //------------------------------------------------------
  97.  
  98. USER_FUNC( Left )
  99.     DEF_ARGV( 0, str, String )
  100.     delete xpf->v;
  101.     xpf->v = new CString( str.left(0).s());
  102. USER_END;
  103.  
  104. //------------------------------------------------------
  105. //
  106. //  Simple RTTI
  107. //
  108. //------------------------------------------------------
  109.  
  110. USER_FUNC( Type )
  111.     DEF_ARGX( 0, x )
  112.     RETURNS( String )
  113.     ret = "?";
  114.     ret[0] = *x.name();
  115. USER_END;
  116.  
  117. //------------------------------------------------------
  118. //
  119. //  Removes element from the array
  120. //
  121. //------------------------------------------------------
  122.  
  123. USER_FUNC( Adel )
  124.     DEF_ARGV( 0, array, Array )
  125.     DEF_ARGX( 1, index )
  126.     RETURNS( Bool )
  127.     int t = index.type();
  128.     if( t!=idString && t!=idLong )
  129.         USER_ERR("Wrong type in calling ADEL");
  130.     int r = t==idString ?
  131.         array.remove( array.index( (CString&)index )):
  132.         array.remove( (CLong&)  index );
  133.     ret = !r;
  134. USER_END
  135.  
  136. //------------------------------------------------------
  137. //
  138. //  Adds new element to the array
  139. //
  140. //------------------------------------------------------
  141.  
  142. USER_PROC( Aadd )
  143.     DEF_ARGV( 0, array, Array )
  144.     DEF_ARGX( 1, x )
  145.     array.add( x.copy());
  146. USER_END
  147.  
  148. //------------------------------------------------------
  149. //
  150. //  Checks whether function defined or not
  151. //
  152. //------------------------------------------------------
  153.  
  154. USER_FUNC( Defined )
  155.     DEF_ARGV( 0, str, String )
  156.     RETURNS( Bool )
  157.     LexObj *obj = 0;
  158.     for( XBlock *x = xpf->xblock(); x && !obj;
  159.         x = x->father ? x->father->xblock() : 0 )
  160.             obj = x->funcs( str );
  161.     ret = obj ? 1 : 0;
  162. USER_END
  163.  
  164. //------------------------------------------------------
  165. //
  166. //  Is variable empty
  167. //
  168. //------------------------------------------------------
  169.  
  170. USER_FUNC( Empty )
  171.     DEF_ARGX( 0, x )
  172.     RETURNS( Bool )
  173.     ret = x.empty();
  174. USER_END
  175.  
  176. //------------------------------------------------------
  177. //
  178. //  Converts value to string
  179. //
  180. //------------------------------------------------------
  181.  
  182. USER_FUNC( Str )
  183.     DEF_ARGX( 0, x )
  184.     RETURNS( String )
  185.     ret = x.s();
  186. USER_END
  187.  
  188. //------------------------------------------------------
  189. //
  190. //  Reads value of the variable from the text buffer.
  191. //  Variable should be passed by reference.
  192. //
  193. //------------------------------------------------------
  194.  
  195. USER_PROC( Read )
  196.     DEF_ARGX( 0, x )
  197.     DEF_ARGV( 1, s, String )
  198.     char buf[256];
  199.     s.s( buf );
  200.     x.read( buf );
  201. USER_END
  202.  
  203. //------------------------------------------------------
  204. //
  205. //  Exit from program
  206. //
  207. //------------------------------------------------------
  208.  
  209. USER_PROC( Quit )
  210.     DEF_ARGX( 0, x )
  211.     cerr << x << endl;
  212.     exit( 1 );
  213. USER_END
  214.  
  215. //------------------------------------------------------
  216. //
  217. //  Gets string from stdin
  218. //
  219. //------------------------------------------------------
  220.  
  221. USER_FUNC( Getstr )
  222.     DEF_ARGV( 0, s, String )
  223.     RETURNS( String )
  224.     char buf[ 256 ];
  225.     cout << s << flush;
  226.     cin.getline( buf, sizeof( buf ));
  227.     ret = buf;
  228. USER_END
  229.  
  230. //------------------------------------------------------
  231. //
  232. //  Returns argument of command line
  233. //
  234. //------------------------------------------------------
  235.  
  236. USER_FUNC( Argv )
  237.     DEF_ARGV( 0, i, Long )
  238.     RETURNS( String )
  239.     if( 0<=(long)i && (long)i<XUserFunction::Argc )
  240.         ret = XUserFunction::Argv[(int)((long)i)];
  241. USER_END
  242.  
  243. //------------------------------------------------------
  244. //
  245. //  Returns argument count given in the command line
  246. //
  247. //------------------------------------------------------
  248.  
  249. USER_FUNC( Argc )
  250.     RETURNS( Long )
  251.     ret = XUserFunction::Argc;
  252. USER_END
  253.  
  254. //------------------------------------------------------
  255. //
  256. //  Converts string to number
  257. //
  258. //------------------------------------------------------
  259.  
  260. USER_FUNC( Val )
  261.     DEF_ARGV( 0, s, String )
  262.     RETURNS( Long )
  263.     char buf[256];
  264.     s.s( buf );
  265.     ret.read( buf );
  266. USER_END
  267.  
  268. //------------------------------------------------------
  269. //
  270. //  Converts number or string to date
  271. //
  272. //------------------------------------------------------
  273.  
  274. USER_FUNC( Date )
  275.     DEF_ARGX( 0, x )
  276.     RETURNS( Date )
  277.     if( x.id == idString )
  278.     {
  279.         char buf[256];
  280.         x.s( buf );
  281.         ret.read( buf );
  282.     }
  283.     if( x.id == idLong )
  284.         ret.set( (CLong&) x );
  285. USER_END
  286.  
  287. //------------------------------------------------------
  288. //
  289. //  Reads enviroment variable
  290. //
  291. //------------------------------------------------------
  292.  
  293. USER_FUNC( Getenv )
  294.     DEF_ARGV( 0, var, String )
  295.     RETURNS( String )
  296.     const char* s = getenv( var );
  297.     ret = s ? s : "";
  298. USER_END
  299.  
  300. //------------------------------------------------------
  301. //
  302. //  Performs a system call
  303. //
  304. //------------------------------------------------------
  305.  
  306. USER_FUNC( System )
  307.     DEF_ARGV( 0, command, String )
  308.     RETURNS( Long )
  309.     ret = system( command );
  310. USER_END
  311.  
  312. //------------------------------------------------------
  313. void UserLib()
  314. {
  315.     RegFunc( "ARRAY", NewArray );
  316.     RegFunc( "LEN", Len );
  317.     RegFunc( "SUBSTR", Substr, 3 );
  318.     RegFunc( "STRUCTURE", Structure );
  319.     RegFunc( "LEFT", Left );
  320.     RegFunc( "ADEL", Adel, 2 );
  321.     RegProc( "AADD", Aadd, 2 );
  322.     RegFunc( "DEFINED", Defined );
  323.     RegFunc( "EMPTY", Empty );
  324.     RegFunc( "STR", Str );
  325.     RegProc( "READ", Read, 2 );
  326.     RegFunc( "ARGV", Argv );
  327.     RegProc( "QUIT", Quit );
  328.     RegFunc( "GETSTR", Getstr );
  329.     RegFunc( "ARGC", Argc, 0 );
  330.     RegFunc( "VAL", Val );
  331.     RegFunc( "DATE", Date );
  332.     RegFunc( "TYPE", Type );
  333.     RegFunc( "GETENV", Getenv );
  334.     RegFunc( "SYSTEM", System );
  335. }
  336.  
  337.