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 >
Wrap
C/C++ Source or Header
|
1996-04-04
|
8KB
|
392 lines
/*
* 標準関数
*
* T.Koabayashi 1994.5.22
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <assert.h>
#include "parse.h"
#include "exec.h"
#include "strclass.h"
#include "err.h"
#include "inlib.h"
static int StdToString( int, int, DataStruct* );
static int StdPrint( int, int, DataStruct* );
static int StdTypeof( int, int, DataStruct* );
static int StdSizeof( int, int, DataStruct* );
static int StdCallFunction( int, int, DataStruct* );
static int StdSqrt( int, int, DataStruct* );
static int StdSin( int, int, DataStruct* );
static int StdCos( int, int, DataStruct* );
static int StdTan( int, int, DataStruct* );
static int StdArcSin( int, int, DataStruct* );
static int StdArcCos( int, int, DataStruct* );
static int StdArcTan( int, int, DataStruct* );
static int StdArcTan2( int, int, DataStruct* );
static int StdLog( int, int, DataStruct* );
static int StdExp( int, int, DataStruct* );
static int StdAbs( int, int, DataStruct* );
static int StdObjectCount( int, int, DataStruct* );
static int StdStackPtr( int, int, DataStruct* );
/*
* 標準関数の定義
*/
void SetStdFunction()
{
FunctionSet( 0, "print", StdPrint );
FunctionSet( 0, "tostring", StdToString );
FunctionSet( 0, "typeof", StdTypeof );
FunctionSet( 0, "sizeof", StdSizeof );
FunctionSet( 0, "call", StdCallFunction );
FunctionSet( 0, "sqrt", StdSqrt );
FunctionSet( 0, "sin", StdSin );
FunctionSet( 0, "cos", StdCos );
FunctionSet( 0, "tan", StdTan );
FunctionSet( 0, "asin", StdArcSin );
FunctionSet( 0, "acos", StdArcCos );
FunctionSet( 0, "atan", StdArcTan );
FunctionSet( 0, "atan2", StdArcTan2 );
FunctionSet( 0, "log", StdLog );
FunctionSet( 0, "exp", StdExp );
FunctionSet( 0, "abs", StdAbs );
FunctionSet( 0, "ObjectCount", StdObjectCount );
FunctionSet( 0, "StackPtr", StdStackPtr );
}
/* 文字列に変換 */
static int StdToString( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int i, len, size ;
char strbuf[STRING_CHARS], tmpbuf[STRING_CHARS], *pstr ;
StringClass *str ;
#if 0
if ( args >= 2 )
{
assert( buf[0].type == TYPE_OBJECT );
if ( buf[1].type == TYPE_INT &&
-STRING_CHARS < buf[1].id.i && buf[1].id.i < STRING_CHARS )
{
size = buf[1].id.i ;
}
else
{
ExecError( "2 番目の引数が不正です( %s )", "tostring" );
return RETURN_VOID ;
}
CallFunctionLocal( ident, 1, buf );
str = (StringClass*)buf[0].od.ptr ;
len = strlen( str->str );
if ( size > 0 )
{
strbuf[0] = '\0' ;
for( i = len ; i < size ; i++ )
strcat( strbuf, " " );
strcat( strbuf, str->str );
}
else
{
strcpy( strbuf, str->str );
for( i = len ; i < - size ; i++ )
strcat( strbuf, " " );
}
}
#else
if ( args >= 2 )
{
if ( buf[1].type == TYPE_INT &&
-STRING_CHARS < buf[1].id.i && buf[1].id.i < STRING_CHARS )
{
size = buf[1].id.i ;
}
else
{
ExecError( "2 番目の引数が不正です( %s )", "tostring" );
return RETURN_VOID ;
}
if (buf[0].type == TYPE_OBJECT) {
CallFunctionLocal( ident, 1, buf );
str = (StringClass*)buf[0].od.ptr ;
pstr = str->str;
} else {
DataToString( tmpbuf, buf );
pstr = tmpbuf;
}
len = strlen( pstr );
if ( size > 0 )
{
strbuf[0] = '\0' ;
for( i = len ; i < size ; i++ )
strcat( strbuf, " " );
strcat( strbuf, pstr);
}
else
{
strcpy( strbuf, pstr );
for( i = len ; i < - size ; i++ )
strcat( strbuf, " " );
}
}
#endif
else
{
DataToString( strbuf, buf );
}
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)StringSet( strbuf );
return RETURN_RETURN ;
}
/* 表示 */
static int StdPrint( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int i ;
StringClass *str ;
DataStruct *top ;
ident = IdentSearch( IdentFunction, "tostring" );
for( i = 0 ; i < args ; i++ )
{
StackPush( buf+i );
top = StackTop();
CallFunctionLocal( ident, 1, top );
StackRelease( top );
assert( top->type == TYPE_OBJECT );
str = (StringClass*)top->od.ptr ;
printf( str->str );
if ( i < args - 1 )
printf( " " );
StackPop();
}
return RETURN_VOID ;
}
/* 型を返す */
static int StdTypeof( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
DataStruct *top ;
top = StackAlloc( 1 );
top->type = TYPE_TYPE ;
top->td.t = buf->type ;
return RETURN_RETURN ;
}
/* 配列サイズを返す */
static int StdSizeof( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "sizeof", args, buf, TYPE_ARRAY, TYPE_NOASN );
StackPushInt( buf->ad.size );
return RETURN_RETURN ;
}
/* 関数実行 */
static int StdCallFunction( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
if ( buf->type != TYPE_FUNC )
ExecError( "1 番目の引数の型が不正です( %s )", "callfunction" );
return CallFunctionLocal( buf->funcd.ident, args-1, buf+1 );
}
/*
* 数値関数
*/
static int StdSqrt( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
double ret ;
ArgCheck( "sqrt", args, buf, TYPE_INT|TYPE_REAL, TYPE_NOASN );
if ( buf->type == TYPE_REAL )
ret = sqrt( (double)buf->rd.r );
else
ret = sqrt( (double)buf->id.i );
StackPushReal( ret );
return RETURN_RETURN ;
}
static int StdSin( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( sin( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdCos( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "cos", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( cos( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdTan( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "tan", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( tan( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdArcSin( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( asin( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdArcCos( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( acos( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdArcTan( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "sin", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( atan( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdArcTan2( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
double x, y;
ArgCheck( "sin", args, buf, TYPE_REAL|TYPE_INT, TYPE_REAL|TYPE_INT, TYPE_NOASN );
if ( buf[0].type == TYPE_REAL ) {
y = buf[0].rd.r;
} else {
y = (double)buf[0].id.i;
}
if ( buf[1].type == TYPE_REAL ) {
x = buf[1].rd.r;
} else {
x = (double)buf[1].id.i;
}
StackPushReal( atan2( y, x ) );
return RETURN_RETURN ;
}
static int StdLog( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "log", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( log( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdExp( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "exp", args, buf, TYPE_REAL, TYPE_NOASN );
StackPushReal( exp( (double)buf->rd.r ) );
return RETURN_RETURN ;
}
static int StdAbs( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "abs", args, buf, TYPE_INT|TYPE_REAL, TYPE_NOASN );
if ( buf->type == TYPE_REAL )
StackPushReal( fabs( (double)buf->rd.r ) );
else
StackPushInt( abs( buf->id.i ) );
return RETURN_RETURN ;
}
static int StdObjectCount( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
StackPushInt( ObjectCount );
return RETURN_RETURN ;
}
static int StdStackPtr( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
extern int StackPtr ;
StackPushInt( StackPtr );
return RETURN_RETURN ;
}