⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bwb_fnc.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************        bwb_fnc.c       Interpretation Routines			for Predefined Functions                        for Bywater BASIC Interpreter                        Copyright (c) 1993, Ted A. Campbell                        Bywater Software                        email: tcamp@delphi.com        Copyright and Permissions Information:        All U.S. and international rights are claimed by the author,        Ted A. Campbell.	This software is released under the terms of the GNU General	Public License (GPL), which is distributed with this software	in the file "COPYING".  The GPL specifies the terms under	which users may copy and use the software in this distribution.	A separate license is available for commercial distribution,	for information on which you should contact the author.****************************************************************//*---------------------------------------------------------------*//* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, *//* 11/1995 (eidetics@cerf.net).                                  *//*---------------------------------------------------------------*/#define FSTACKSIZE      32#include <stdio.h>#include <ctype.h>#include <math.h>#include <time.h>#include "bwbasic.h"#include "bwb_mes.h"#if UNIX_CMDS#include <sys/stat.h>#endif#ifndef RAND_MAX			/* added in v1.11 */#define RAND_MAX	32767#endifstatic time_t t;static struct tm *lt;/***************************************************************        FUNCTION:       fnc_init()        DESCRIPTION:    This command initializes the function                        linked list, placing all predefined functions                        in the list.***************************************************************/#if ANSI_Cintfnc_init( int task )#elseintfnc_init( task )   int task;#endif   {   register int n;   struct bwb_function *f;   strcpy( LOCALTASK fnc_start.name, "FNC_START" );   LOCALTASK fnc_start.type = 'X';   LOCALTASK fnc_start.vector = fnc_null;   strcpy( LOCALTASK fnc_end.name, "FNC_END" );   LOCALTASK fnc_end.type = 'x';   LOCALTASK fnc_end.vector = fnc_null;   LOCALTASK fnc_end.next = &LOCALTASK fnc_end;   f = &LOCALTASK fnc_start;   /* now go through each of the preestablished functions and set up      links between them; from this point the program address the functions      only as a linked list (not as an array) */   for ( n = 0; n < FUNCTIONS; ++n )      {      f->next = &( bwb_prefuncs[ n ] );      f = f->next;      }   /* link the last pointer to the end; this completes the list */   f->next = &LOCALTASK fnc_end;   return TRUE;   }/***************************************************************        FUNCTION:       fnc_find()        DESCRIPTION:    This C function attempts to locate                        a BASIC function with the specified name.                        If successful, it returns a pointer to                        the C structure for the BASIC function,                        if not successful, it returns NULL.***************************************************************/#if ANSI_Cstruct bwb_function *fnc_find( char *buffer )#elsestruct bwb_function *fnc_find( buffer )   char *buffer;#endif   {   struct bwb_function * f;   register int n;   static char *tbuf;   static int init = FALSE;   if ( strlen( buffer ) == 0 )      {      return NULL;      }   /* get memory for temporary buffer if necessary */   if ( init == FALSE )      {      init = TRUE;      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_find" )) == NULL )         {#if PROG_ERRORS	 bwb_error( "in fnc_find(): failed to find memory for tbuf" );#else	 bwb_error( err_getmem );#endif	 }      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_find(): called for <%s> ", buffer );   bwb_debug( bwb_ebuf );#endif   strcpy( tbuf, buffer );   bwb_strtoupper( tbuf );   for ( f = CURTASK fnc_start.next; f != &CURTASK fnc_end; f = f->next )      {      if ( strcmp( f->name, tbuf ) == 0 )         {#if INTENSIVE_DEBUG	 sprintf( bwb_ebuf, "in fnc_find(): found <%s> ", f->name );	 bwb_debug( bwb_ebuf );#endif         return f;         }      }   /* search has failed: return NULL */   return NULL;   }/***************************************************************        FUNCTION:       fnc_null()        DESCRIPTION:    This is a null function that can be used                        to fill in a required function-structure                        pointer when needed.***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_null( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_null( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, NUMBER );      }   return &nvar;   }/***************************************************************        FUNCTION:       fnc_tab()	DESCRIPTION:    This C function implements the BASIC TAB()			function, adding tab spaces to a specified			column.			TAB is a core function, i.e., required			for ANSI Minimal BASIC.	SYNTAX:		TAB( number )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_tab( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_tab( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static int init = FALSE;   static char t_string[ 4 ];   bstring *b;   /* initialize nvar if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, (int) STRING );      }   /* check for correct number of parameters */   if ( argc < 1 )      {#if PROG_ERRORS      sprintf( bwb_ebuf, "Not enough parameters (%d) to function TAB().",         argc );      bwb_error( bwb_ebuf );#else      bwb_error( err_syntax );#endif      break_handler();      return NULL;      }   else if ( argc > 1 )      {#if PROG_ERRORS      sprintf( bwb_ebuf, "Too many parameters (%d) to function TAB().",         argc );      bwb_error( bwb_ebuf );#else      bwb_error( err_syntax );#endif      break_handler();      return NULL;      }   t_string[ 0 ] = PRN_TAB;   t_string[ 1 ] = (char) var_getnval( &( argv[ 0 ] ));   t_string[ 2 ] = '\0';   b = var_getsval( &nvar );   str_ctob( b, t_string );   return &nvar;   }#if COMMON_FUNCS/***************************************************************        FUNCTION:       fnc_date()        DESCRIPTION:    This C function implements the BASIC                        predefined DATE$ function, returning                        a string containing the year, month,                        and day of the month.	SYNTAX:		DATE$***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_date( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_date( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static int init = FALSE;   static char *tbuf;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_date" )) == NULL )         {#if PROG_ERRORS         bwb_error( "in fnc_date(): failed to get memory for tbuf" );#else         bwb_error( err_getmem );#endif         }      }   time( &t );   lt = localtime( &t );   sprintf( tbuf, "%02d-%02d-%04d", lt->tm_mon + 1, lt->tm_mday,      1900 + lt->tm_year );   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_time()        DESCRIPTION:    This C function implements the BASIC                        predefined TIME$ function, returning a                        string containing the hour, minute, and                        second count.	SYNTAX:		TIME$***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_time( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_time( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static char *tbuf;   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_time" )) == NULL )         {#if PROG_ERRORS         bwb_error( "in fnc_time(): failed to get memory for tbuf" );#else         bwb_error( err_getmem );#endif         }      }   time( &t );   lt = localtime( &t );   sprintf( tbuf, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min,      lt->tm_sec );   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_chr()        DESCRIPTION:    This C function implements the BASIC                        predefined CHR$ function, returning a                        string containing the single character                        whose ASCII value is the argument to                        this function.	SYNTAX:		CHR$( number )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_chr( int argc, struct bwb_variable *argv, int unique_id  )#elsestruct bwb_variable *fnc_chr( argc, argv, unique_id  )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   char tbuf[ MAXSTRINGSIZE + 1 ];   static int init = FALSE;#if TEST_BSTRING   bstring *b;#endif#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_chr(): entered function, argc <%d>",      argc );   bwb_debug( bwb_ebuf );#endif   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in fnc_chr(): entered function, initialized nvar" );      bwb_debug( bwb_ebuf );#endif      }   /* check arguments */#if PROG_ERRORS   if ( argc < 1 )      {      sprintf( bwb_ebuf, "Not enough arguments to function CHR$()" );      bwb_error( bwb_ebuf );      return NULL;      }   else if ( argc > 1 )      {      sprintf( bwb_ebuf, "Too many parameters (%d) to function CHR$().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }#else   if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )      {      return NULL;      }#endif#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_chr(): entered function, checkargs ok" );   bwb_debug( bwb_ebuf );#endif   tbuf[ 0 ] = (char) var_getnval( &( argv[ 0 ] ) );   tbuf[ 1 ] = '\0';   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );#if TEST_BSTRING   b = var_findsval( &nvar, nvar.array_pos );   sprintf( bwb_ebuf, "in fnc_chr(): bstring name is <%s>", b->name );   bwb_debug( bwb_ebuf );#endif#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_chr(): tbuf[ 0 ] is <%c>", tbuf[ 0 ] );   bwb_debug( bwb_ebuf );#endif   return &nvar;   }/***************************************************************        FUNCTION:       fnc_len()	DESCRIPTION:    This C function implements the BASIC LEN()			function, returning the length of a			specified string in bytes.	SYNTAX:		LEN( string$ )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_len( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_len( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static int init = FALSE;   static char *tbuf;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, NUMBER );      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_len" )) == NULL )         {#if PROG_ERRORS         bwb_error( "in fnc_len(): failed to get memory for tbuf" );#else         bwb_error( err_getmem );#endif         }      }   /* check parameters */#if PROG_ERRORS   if ( argc < 1 )      {      sprintf( bwb_ebuf, "Not enough parameters (%d) to function LEN().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }   else if ( argc > 1 )      {      sprintf( bwb_ebuf, "Too many parameters (%d) to function LEN().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }#else   if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )      {      return NULL;      }#endif   /* return length as an integer */   str_btoc( tbuf, var_getsval( &( argv[ 0 ] )) );   * var_findnval( &nvar, nvar.array_pos )      = (bnumber) strlen( tbuf );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_pos()	DESCRIPTION:    This C function implements the BASIC			POS() function, returning the current			column position for the output device.	SYNTAX:		POS***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_pos( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_pos( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static int init = FALSE;   /* initialize nvar if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, (int) NUMBER );      }   * var_findnval( &nvar, nvar.array_pos ) = (bnumber) prn_col;   return &nvar;   }#endif					/* COMMON_FUNCS */#if MS_FUNCS/***************************************************************        FUNCTION:       fnc_timer()        DESCRIPTION:    This C function implements the BASIC                        predefined TIMER function	SYNTAX:		TIMER***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_timer( int argc, struct bwb_variable *argv, int unique_id  )#elsestruct bwb_variable *fnc_timer( argc, argv, unique_id  )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static time_t now;   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, NUMBER );      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -