home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / ML / STD.C < prev    next >
C/C++ Source or Header  |  1996-04-04  |  8KB  |  392 lines

  1. /*
  2.  *        標準関数
  3.  *
  4.  *        T.Koabayashi    1994.5.22
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #include <assert.h>
  12.  
  13. #include "parse.h"
  14. #include "exec.h"
  15. #include "strclass.h"
  16. #include "err.h"
  17.  
  18. #include "inlib.h"
  19.  
  20. static    int        StdToString( int, int, DataStruct* );
  21. static    int        StdPrint( int, int, DataStruct* );
  22. static    int        StdTypeof( int, int, DataStruct* );
  23. static    int        StdSizeof( int, int, DataStruct* );
  24. static    int        StdCallFunction( int, int, DataStruct* );
  25. static    int        StdSqrt( int, int, DataStruct* );
  26. static    int        StdSin( int, int, DataStruct* );
  27. static    int        StdCos( int, int, DataStruct* );
  28. static    int        StdTan( int, int, DataStruct* );
  29. static    int        StdArcSin( int, int, DataStruct* );
  30. static    int        StdArcCos( int, int, DataStruct* );
  31. static    int        StdArcTan( int, int, DataStruct* );
  32. static    int        StdArcTan2( int, int, DataStruct* );
  33. static    int        StdLog( int, int, DataStruct* );
  34. static    int        StdExp( int, int, DataStruct* );
  35. static    int        StdAbs( int, int, DataStruct* );
  36. static    int        StdObjectCount( int, int, DataStruct* );
  37. static    int        StdStackPtr( int, int, DataStruct* );
  38.  
  39. /*
  40.  *        標準関数の定義
  41.  */
  42. void    SetStdFunction()
  43. {
  44.     FunctionSet( 0, "print",    StdPrint );
  45.     FunctionSet( 0, "tostring",    StdToString );
  46.     FunctionSet( 0, "typeof",    StdTypeof );
  47.     FunctionSet( 0, "sizeof",    StdSizeof );
  48.     FunctionSet( 0, "call",        StdCallFunction );
  49.     FunctionSet( 0, "sqrt",        StdSqrt );
  50.     FunctionSet( 0, "sin",        StdSin );
  51.     FunctionSet( 0, "cos",        StdCos );
  52.     FunctionSet( 0, "tan",        StdTan );
  53.     FunctionSet( 0, "asin",        StdArcSin );
  54.     FunctionSet( 0, "acos",        StdArcCos );
  55.     FunctionSet( 0, "atan",        StdArcTan );
  56.     FunctionSet( 0, "atan2",    StdArcTan2 );
  57.     FunctionSet( 0, "log",        StdLog );
  58.     FunctionSet( 0, "exp",        StdExp );
  59.     FunctionSet( 0, "abs",        StdAbs );
  60.     FunctionSet( 0, "ObjectCount",    StdObjectCount );
  61.     FunctionSet( 0, "StackPtr",    StdStackPtr );
  62. }
  63.  
  64. /*    文字列に変換    */
  65. static    int        StdToString( ident, args, buf )
  66. int        ident ;
  67. int        args ;
  68. DataStruct    *buf ;
  69. {
  70.     int        i, len, size ;
  71.     char    strbuf[STRING_CHARS], tmpbuf[STRING_CHARS], *pstr ;
  72.     StringClass    *str ;
  73.  
  74. #if 0
  75.     if ( args >= 2 )
  76.     {
  77.         assert( buf[0].type == TYPE_OBJECT );
  78.         if ( buf[1].type == TYPE_INT &&
  79.              -STRING_CHARS < buf[1].id.i && buf[1].id.i < STRING_CHARS )
  80.         {
  81.             size = buf[1].id.i ;
  82.         }
  83.         else
  84.         {
  85.             ExecError( "2 番目の引数が不正です( %s )", "tostring" );
  86.             return RETURN_VOID ;
  87.         }
  88.  
  89.         CallFunctionLocal( ident, 1, buf );
  90.  
  91.         str = (StringClass*)buf[0].od.ptr ;
  92.         len = strlen( str->str );
  93.         if ( size > 0 )
  94.         {
  95.             strbuf[0] = '\0' ;
  96.             for( i = len ; i < size ; i++ )
  97.                 strcat( strbuf, " " );
  98.             strcat( strbuf, str->str );
  99.         }
  100.         else
  101.         {
  102.             strcpy( strbuf, str->str );
  103.             for( i = len ; i < - size ; i++ )
  104.                 strcat( strbuf, " " );
  105.         }
  106.     }
  107. #else
  108.     if ( args >= 2 )
  109.     {
  110.         if ( buf[1].type == TYPE_INT &&
  111.              -STRING_CHARS < buf[1].id.i && buf[1].id.i < STRING_CHARS )
  112.         {
  113.             size = buf[1].id.i ;
  114.         }
  115.         else
  116.         {
  117.             ExecError( "2 番目の引数が不正です( %s )", "tostring" );
  118.             return RETURN_VOID ;
  119.         }
  120.  
  121.         if (buf[0].type == TYPE_OBJECT) {
  122.             CallFunctionLocal( ident, 1, buf );
  123.  
  124.             str = (StringClass*)buf[0].od.ptr ;
  125.             pstr = str->str;
  126.         } else {
  127.             DataToString( tmpbuf, buf );
  128.             pstr = tmpbuf;
  129.         }
  130.         len = strlen( pstr );
  131.         if ( size > 0 )
  132.         {
  133.             strbuf[0] = '\0' ;
  134.             for( i = len ; i < size ; i++ )
  135.                 strcat( strbuf, " " );
  136.             strcat( strbuf, pstr);
  137.         }
  138.         else
  139.         {
  140.             strcpy( strbuf, pstr );
  141.             for( i = len ; i < - size ; i++ )
  142.                 strcat( strbuf, " " );
  143.         }
  144.     }
  145. #endif
  146.     else
  147.     {
  148.         DataToString( strbuf, buf );
  149.     }
  150.  
  151.     buf = StackAlloc( 1 );
  152.     buf->type = TYPE_OBJECT ;
  153.     buf->od.ptr = (Object*)StringSet( strbuf );
  154.  
  155.     return RETURN_RETURN ;
  156. }
  157.  
  158. /*    表示    */
  159. static    int        StdPrint( ident, args, buf )
  160. int        ident ;
  161. int        args ;
  162. DataStruct    *buf ;
  163. {
  164.     int        i ;
  165.     StringClass    *str ;
  166.     DataStruct    *top ;
  167.  
  168.     ident = IdentSearch( IdentFunction, "tostring" );
  169.  
  170.     for( i = 0 ; i < args ; i++ )
  171.     {
  172.         StackPush( buf+i );
  173.         top = StackTop();
  174.         CallFunctionLocal( ident, 1, top );
  175.         StackRelease( top );
  176.  
  177.         assert( top->type == TYPE_OBJECT );
  178.         str = (StringClass*)top->od.ptr ;
  179.         printf( str->str );
  180.         if ( i < args - 1 )
  181.             printf( " " );
  182.         StackPop();
  183.     }
  184.     return RETURN_VOID ;
  185. }
  186.  
  187. /*    型を返す    */
  188. static    int        StdTypeof( ident, args, buf )
  189. int        ident ;
  190. int        args ;
  191. DataStruct    *buf ;
  192. {
  193.     DataStruct    *top ;
  194.  
  195.     top = StackAlloc( 1 );
  196.     top->type = TYPE_TYPE ;
  197.     top->td.t = buf->type ;
  198.  
  199.     return RETURN_RETURN ;
  200. }
  201.  
  202. /*    配列サイズを返す    */
  203. static    int        StdSizeof( ident, args, buf )
  204. int        ident ;
  205. int        args ;
  206. DataStruct    *buf ;
  207. {
  208.     ArgCheck( "sizeof", args, buf, TYPE_ARRAY, TYPE_NOASN );
  209.  
  210.     StackPushInt( buf->ad.size );
  211.     return RETURN_RETURN ;
  212. }
  213.  
  214. /*    関数実行    */
  215. static    int        StdCallFunction( ident, args, buf )
  216. int        ident ;
  217. int        args ;
  218. DataStruct    *buf ;
  219. {
  220.     if ( buf->type != TYPE_FUNC )
  221.         ExecError( "1 番目の引数の型が不正です( %s )", "callfunction" );
  222.     return CallFunctionLocal( buf->funcd.ident, args-1, buf+1 );
  223. }
  224.  
  225. /*
  226.  *    数値関数
  227.  */
  228. static    int        StdSqrt( ident, args, buf )
  229. int        ident ;
  230. int        args ;
  231. DataStruct    *buf ;
  232. {
  233.     double    ret ;
  234.  
  235.     ArgCheck( "sqrt", args, buf, TYPE_INT|TYPE_REAL, TYPE_NOASN );
  236.  
  237.     if ( buf->type == TYPE_REAL )
  238.         ret = sqrt( (double)buf->rd.r );
  239.     else
  240.         ret = sqrt( (double)buf->id.i );
  241.  
  242.     StackPushReal( ret );
  243.     return RETURN_RETURN ;
  244. }
  245.  
  246. static    int        StdSin( ident, args, buf )
  247. int        ident ;
  248. int        args ;
  249. DataStruct    *buf ;
  250. {
  251.     ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
  252.  
  253.     StackPushReal( sin( (double)buf->rd.r ) );
  254.     return RETURN_RETURN ;
  255. }
  256.  
  257. static    int        StdCos( ident, args, buf )
  258. int        ident ;
  259. int        args ;
  260. DataStruct    *buf ;
  261. {
  262.     ArgCheck( "cos", args, buf, TYPE_REAL, TYPE_NOASN );
  263.  
  264.     StackPushReal( cos( (double)buf->rd.r ) );
  265.     return RETURN_RETURN ;
  266. }
  267.  
  268. static    int        StdTan( ident, args, buf )
  269. int        ident ;
  270. int        args ;
  271. DataStruct    *buf ;
  272. {
  273.     ArgCheck( "tan", args, buf, TYPE_REAL, TYPE_NOASN );
  274.  
  275.     StackPushReal( tan( (double)buf->rd.r ) );
  276.     return RETURN_RETURN ;
  277. }
  278.  
  279. static    int        StdArcSin( ident, args, buf )
  280. int        ident ;
  281. int        args ;
  282. DataStruct    *buf ;
  283. {
  284.     ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
  285.  
  286.     StackPushReal( asin( (double)buf->rd.r ) );
  287.     return RETURN_RETURN ;
  288. }
  289.  
  290. static    int        StdArcCos( ident, args, buf )
  291. int        ident ;
  292. int        args ;
  293. DataStruct    *buf ;
  294. {
  295.     ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
  296.  
  297.     StackPushReal( acos( (double)buf->rd.r ) );
  298.     return RETURN_RETURN ;
  299. }
  300.  
  301. static    int        StdArcTan( ident, args, buf )
  302. int        ident ;
  303. int        args ;
  304. DataStruct    *buf ;
  305. {
  306.     ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
  307.  
  308.     StackPushReal( atan( (double)buf->rd.r ) );
  309.     return RETURN_RETURN ;
  310. }
  311.  
  312. static    int        StdArcTan2( ident, args, buf )
  313. int        ident ;
  314. int        args ;
  315. DataStruct    *buf ;
  316. {
  317.     double x, y;
  318.     ArgCheck( "sin", args, buf, TYPE_REAL|TYPE_INT, TYPE_REAL|TYPE_INT, TYPE_NOASN );
  319.  
  320.     if ( buf[0].type == TYPE_REAL ) {
  321.         y = buf[0].rd.r;
  322.     } else {
  323.         y = (double)buf[0].id.i;
  324.     }
  325.  
  326.     if ( buf[1].type == TYPE_REAL ) {
  327.         x = buf[1].rd.r;
  328.     } else {
  329.         x = (double)buf[1].id.i;
  330.     }
  331.  
  332.     StackPushReal( atan2( y, x ) );
  333.     return RETURN_RETURN ;
  334. }
  335.  
  336. static    int        StdLog( ident, args, buf )
  337. int        ident ;
  338. int        args ;
  339. DataStruct    *buf ;
  340. {
  341.     ArgCheck( "log", args, buf, TYPE_REAL, TYPE_NOASN );
  342.  
  343.     StackPushReal( log( (double)buf->rd.r ) );
  344.     return RETURN_RETURN ;
  345. }
  346.  
  347. static    int        StdExp( ident, args, buf )
  348. int        ident ;
  349. int        args ;
  350. DataStruct    *buf ;
  351. {
  352.     ArgCheck( "exp", args, buf, TYPE_REAL, TYPE_NOASN );
  353.  
  354.     StackPushReal( exp( (double)buf->rd.r ) );
  355.     return RETURN_RETURN ;
  356. }
  357.  
  358. static    int        StdAbs( ident, args, buf )
  359. int        ident ;
  360. int        args ;
  361. DataStruct    *buf ;
  362. {
  363.     ArgCheck( "abs", args, buf, TYPE_INT|TYPE_REAL, TYPE_NOASN );
  364.  
  365.     if ( buf->type == TYPE_REAL )
  366.         StackPushReal( fabs( (double)buf->rd.r ) );
  367.     else
  368.         StackPushInt( abs( buf->id.i ) );
  369.     return RETURN_RETURN ;
  370. }
  371.  
  372. static    int        StdObjectCount( ident, args, buf )
  373. int        ident ;
  374. int        args ;
  375. DataStruct    *buf ;
  376. {
  377.     StackPushInt( ObjectCount );
  378.     return RETURN_RETURN ;
  379. }
  380.  
  381. static    int        StdStackPtr( ident, args, buf )
  382. int        ident ;
  383. int        args ;
  384. DataStruct    *buf ;
  385. {
  386.     extern    int        StackPtr ;
  387.  
  388.     StackPushInt( StackPtr );
  389.     return RETURN_RETURN ;
  390. }
  391.  
  392.