📄 bwb_int.c
字号:
/***************************************************************f bwb_int.c Line Interpretation Routines 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). *//* *//* Those additionally marked with "DD" were at the suggestion of *//* Dale DePriest (daled@cadence.com). *//*---------------------------------------------------------------*/#include <stdio.h>#include <ctype.h>#include "bwbasic.h"#include "bwb_mes.h"/*************************************************************** FUNCTION: adv_element() DESCRIPTION: This function reads characters in <buffer> beginning at <pos> and advances past a line element, incrementing <pos> appropri- ately and returning the line element in <element>.***************************************************************/#if ANSI_Cintadv_element( char *buffer, int *pos, char *element )#elseintadv_element( buffer, pos, element ) char *buffer; int *pos; char *element;#endif { int loop; /* control loop */ int e_pos; /* position in element buffer */ int str_const; /* boolean: building a string constant */ /* advance beyond any initial whitespace */ adv_ws( buffer, pos );#if INTENSIVE_DEBUG sprintf( bwb_ebuf, "in adv_element(): receieved <%s>.", &( buffer[ *pos ] )); bwb_debug( bwb_ebuf );#endif /* now loop while building an element and looking for an element terminator */ loop = TRUE; e_pos = 0; element[ e_pos ] = '\0'; str_const = FALSE; while ( loop == TRUE ) { switch( buffer[ *pos ] ) { case ',': /* element terminators */ case ';':#if MULTISEG_LINES case ':':#endif case '=': case ' ': case '\t': /* case '\0': */ /* Removed by JBV (found by DD) */ case '\n': case '\r': if ( str_const == TRUE ) { element[ e_pos ] = buffer[ *pos ]; ++e_pos; ++( *pos ); element[ e_pos ] = '\0'; } else { return TRUE; } break; case '\0': /* Added by JBV (found by DD) */ if ( str_const == TRUE ) /* termination of string constant */ { element[ e_pos ] = '\"'; element[ ++e_pos ] = '\0'; } return TRUE; break; case '\"': /* string constant */ element[ e_pos ] = buffer[ *pos ]; ++e_pos; ++( *pos ); element[ e_pos ] = '\0'; if ( str_const == TRUE ) /* termination of string constant */ { return TRUE; } else /* beginning of string constant */ { str_const = TRUE; } break; default: element[ e_pos ] = buffer[ *pos ]; ++e_pos; ++( *pos ); element[ e_pos ] = '\0'; break; } } /* This should not happen */ return FALSE; }/*************************************************************** FUNCTION: adv_ws() DESCRIPTION: This function reads characters in <buffer> beginning at <pos> and advances past any whitespace, incrementing <pos> appropri- ately.***************************************************************/#if ANSI_Cintadv_ws( char *buffer, int *pos )#elseintadv_ws( buffer, pos ) char *buffer; int *pos;#endif { int loop; loop = TRUE; while ( loop == TRUE ) { switch( buffer[ *pos ] ) { case ' ': case '\t': ++( *pos ); break; default: return TRUE; } } /* This should not happen */ return FALSE; }/*************************************************************** FUNCTION: adv_eos() DESCRIPTION: This function reads characters in <buffer> beginning at <pos> and advances to the end of a segment delimited by ':', incrementing <pos> appropriately.***************************************************************/#if MULTISEG_LINES#if ANSI_Cintadv_eos( char *buffer, int *pos )#elseintadv_eos( buffer, pos ) char *buffer; int *pos;#endif { int loop; loop = TRUE; while ( loop == TRUE ) { if ( is_eol( buffer, pos ) == TRUE ) { return FALSE; } switch( buffer[ *pos ] ) { case ':': /* end of segment marker */ ++( *pos ); return TRUE; case '\"': /* begin quoted string */ ++( *pos ); while ( buffer[ *pos ] != '\"' ) { if ( is_eol( buffer, pos ) == TRUE ) { return FALSE; } else { ++( *pos ); } } break; default: ++( *pos ); } } /* This should not happen */ return FALSE; }#endif /* MULTISEG_LINES *//*************************************************************** FUNCTION: bwb_strtoupper() DESCRIPTION: This function converts the string in <buffer> to upper-case characters.***************************************************************/#if ANSI_Cintbwb_strtoupper( char *buffer )#elseintbwb_strtoupper( buffer ) char *buffer;#endif { char *p; p = buffer; while ( *p != '\0' ) { if ( islower( *p ) != FALSE ) { *p = (char) toupper( *p ); } ++p; } return TRUE; }/*************************************************************** FUNCTION: line_start() DESCRIPTION: This function reads a line buffer in <buffer> beginning at the position <pos> and attempts to determine (a) the position of the line number in the buffer (returned in <lnpos>), (b) the line number at this position (returned in <lnum>), (c) the position of the BASIC command in the buffer (returned in <cmdpos>), (d) the position of this BASIC command in the command table (returned in <cmdnum>), and (e) the position of the beginning of the rest of the line (returned in <startpos>). Although <startpos> must be returned as a positive integer, the other searches may fail, in which case FALSE will be returned in their positions. <pos> is not incremented.***************************************************************/#if ANSI_Cintline_start( char *buffer, int *pos, int *lnpos, int *lnum, int *cmdpos, int *cmdnum, int *startpos )#elseintline_start( buffer, pos, lnpos, lnum, cmdpos, cmdnum, startpos ) char *buffer; int *pos; int *lnpos; int *lnum; int *cmdpos; int *cmdnum; int *startpos;#endif { static int position; static char *tbuf; static int init = FALSE; /* 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 ), "line_start")) == NULL ) {#if PROG_ERRORS bwb_error( "in line_start(): failed to get memory for tbuf" );#else bwb_error( err_getmem );#endif } }#if INTENSIVE_DEBUG sprintf( bwb_ebuf, "in line_start(): pos <%d> buffer <%s>", *pos, buffer ); bwb_debug( bwb_ebuf );#endif /* set initial values */ *startpos = position = *pos; *cmdpos = *lnpos = *pos; *cmdnum = *lnum = -1; /* check for null line */ adv_ws( buffer, &position ); if ( buffer[ position ] == '\0' ) {#if INTENSIVE_DEBUG bwb_debug( "in line_start(): found NULL line" );#endif *cmdnum = getcmdnum( CMD_REM ); return TRUE; } /* advance beyond the first element */ *lnpos = position; scan_element( buffer, &position, tbuf ); adv_ws( buffer, &position ); /* test for a line number in the first element */ if ( is_numconst( tbuf ) == TRUE ) /* a line number */ { *lnum = atoi( tbuf ); *startpos = position; /* temp */ *cmdpos = position; scan_element( buffer, &position, tbuf ); /* advance past next element */#if INTENSIVE_DEBUG sprintf( bwb_ebuf, "in line_start(): new element is <%s>", tbuf ); bwb_debug( bwb_ebuf );#endif#if STRUCT_CMDS if ( is_label( tbuf ) == TRUE ) { *cmdnum = getcmdnum( CMD_LABEL ); adv_ws( buffer, &position ); *startpos = position; } else if ( is_cmd( tbuf, cmdnum ) == TRUE )#else if ( is_cmd( tbuf, cmdnum ) == TRUE )#endif { adv_ws( buffer, &position ); *startpos = position; } else if ( is_let( &( buffer[ *cmdpos ] ), cmdnum ) == TRUE ) { *cmdpos = -1; } else { *cmdpos = *cmdnum = -1; } } /* not a line number */ else { *lnum = -1; *lnpos = -1;#if INTENSIVE_DEBUG sprintf( bwb_ebuf, "in line_start(): no line number, element <%s>.", tbuf ); bwb_debug( bwb_ebuf );#endif#if STRUCT_CMDS if ( is_label( tbuf ) == TRUE ) {#if INTENSIVE_DEBUG sprintf( bwb_ebuf, "in line_start(): label detected <%s>.", tbuf ); bwb_debug( bwb_ebuf );#endif *cmdnum = getcmdnum( CMD_LABEL ); adv_ws( buffer, &position ); *startpos = position; } else if ( is_cmd( tbuf, cmdnum ) == TRUE )#else if ( is_cmd( tbuf, cmdnum ) == TRUE )#endif { adv_ws( buffer, &position ); *startpos = position; } else if ( is_let( &( buffer[ position ] ), cmdnum ) == TRUE ) { adv_ws( buffer, &position ); *cmdpos = -1; } else { *cmdpos = *cmdnum = -1; } }#if INTENSIVE_DEBUG sprintf( bwb_ebuf, "in line_start(): lnpos <%d> lnum <%d>", *lnpos, *lnum ); bwb_debug( bwb_ebuf ); sprintf( bwb_ebuf, "in line_start(): cmdpos <%d> cmdnum <%d> startpos <%d>", *cmdpos, *cmdnum, *startpos ); bwb_debug( bwb_ebuf );#endif /* return */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -