📄 main.c
字号:
// cloneNode:
// Clone a node and, recursively, everyone present
// in args and next lists.
lisa_node *cloneNode( lisa_node *node )
{
lisa_node *n = NULL;
if ( node )
{
// copying the existing node...
n = copyNode( node );
// ...and filling with existing data...
memcpy( n, node, sizeof( lisa_node ) );
// ...finally, do it recursivelly...
n->args = cloneNode( node->args );
n->next = cloneNode( node->next );
}
return n;
}//cloneNode
// copyNode:
// Copy a node without args and next lists.
// USE WITH CAUTIONS !!!
lisa_node *copyNode( lisa_node *node )
{
lisa_node *n = NULL;
if ( node )
{
// creating the new node...
n = createNode();
// ...and filling with existing data...
memcpy( n, node, sizeof( lisa_node ) );
}
return n;
}//copyNode
// createNode:
// Create an empty node structure.
lisa_node *createNode( void )
{
lisa_node *node = malloc( sizeof( lisa_node ) );
if ( node )
{
// set the node as a EMPTY node...
node->args = NULL;
node->next = NULL;
node->element[0] = '\0';
node->type = 0;
node->line = line_count;
}
return node;
}//createNode
// debug:
// Display a debug message.
void debug( char *msg )
{
fprintf( stderr, "%s\n", msg );
}//debug
// destroyEnvironment:
// Destroy an environment.
void destroyEnvironment( lisa_environment *env )
{
destroyNodeR( env->globals );
destroyNodeR( env->parameters );
destroyNodeR( env->declared );
destroyNodeR( env->loops );
if ( env )
free( env );
}//destroyEnvironment
// destroyNode:
// Destroy a node.
void destroyNode( lisa_node *node )
{
if ( node )
free( node );
}//destroyNode
// destroyNodeR:
// Destroy a node recursivelly.
void destroyNodeR( lisa_node *node )
{
// destroy the args chain...
if ( node->args )
destroyNodeR( node->args );
// destroy the next chain...
if ( node->next )
destroyNodeR( node->next );
// finally, destroy the node...
destroyNode( node );
}//destroyNodeR
// emptyNode:
// Return TRUE (!= 0) if the node is an empty node.
int emptyNode( lisa_node *node )
{
if ( node->type )
return 0;
return -1;
}//emptyNode
// error:
// Display a error message and exit.
void error( char *msg )
{
fprintf( stderr, "%s at line %d\n", msg, line_count );
exit( 1 );
}//error
// extractNode:
// Extract a node from the list. On error,
// a NULL value is returned.
lisa_node *extractNode( lisa_node *node, lisa_node *list )
{
struct lisa_node *tmp;
if ( list )
{
// searching node...
tmp = list;
while ( tmp->next )
{
if ( tmp->next == node )
{
tmp->next = node->next;
node->next = NULL;
// Now, node is no more linked...
return node;
}
tmp = tmp->next;
}
}
// node not founded...
return NULL;
}// extractNode
// findNodeByElement:
// Return a node searching it by the element string.
// On error, return a NULL value.
lisa_node *findNodeByElement( char *element, lisa_node *list )
{
struct lisa_node *tmp;
if ( list )
{
// searching node...
tmp = list;
while ( tmp )
{
if ( strcmp( tmp->element, element ) == 0 )
{
// Founded!!!
return tmp;
}
tmp = tmp->next;
}
}
// node not founded...
return NULL;
}//findNodeByElement
// getChar:
// Get the next char from the input stream.
// Return 0 if EOF;
char getChar( void )
{
char ch;
// get current char from the line
ch = line[ line_index ];
if ( ch == '\0' )
{
// empty the previous line...
line[0] = '\0';
// ...read a whole line...
fgets( line, INP_MAX_CHAR, inpstream );
// ...and get first char
line_index = 0;
ch = line[ line_index ];
}
// second '\0' means EOF reached
if ( ch == '\0' )
{
return 0;
}
// increase line number...
if ( ch == '\n' )
{
line_count += 1;
}
// increase index for next time
line_index += 1;
// return current char
return ch;
}//getChar
// getCharS:
// Get the next char from stored script block.
// Return 0 if EOF;
char getCharS( void )
{
char ch;
// get current char from the line
ch = script[ script_index ];
if ( ch != '\0' )
{
// increase index for next time
script_index += 1;
}
// return current char
return ch;
}//getCharS
// getScript
// Get the whole script block until end tag ('%>')
// into the default buffer.
void getScript( void )
{
char ch;
int l;
// restoring the index;
script_index = 0;
l = line_count;
ch = getChar();
// ch = 0 means End Of File reached!!!
while ( ch != 0 )
{
// break if '%>' reached...
if ( ch == '%' )
{
ch = getChar();
if ( ch == '>' )
{
// End of Script Block reached!!!
break;
}
putCharS( '%' );
}
putCharS( ch );
ch = getChar();
}
if ( ch == 0 )
error( "Unclosed Script Block" );
// NOTE: (2002-06-25 Gabriele Budelacci)
// Appending a '\n' char to prevent errors when
// parsing "// comment... %>" in the same line.
putCharS( '\n' );
// End of Script
putCharS( '\0' );
// restoring the index;
script_index = 0;
// restoring the line count;
line_count = l;
}//getScript
// popNode
// Extract the last node in a list (STACK like use).
// Returns NULL if the list is empty.
lisa_node *popNode( lisa_node *list )
{
struct lisa_node *tmp = NULL;
struct lisa_node *t = NULL;
if ( list )
{
if ( list->next )
{
tmp = list;
// scan for end of list...
while ( tmp->next->next )
tmp = tmp->next;
}
}
if ( tmp )
{
t = tmp->next;
tmp->next = NULL;
}
return t;
}//popNode
// processHTML
// Process the HTML source until a code block
// ('<% ... %>' or '<%= ... %>') has reached.
// Returns the last character readed from the
// input stream.
char processHTML( void )
{
char ch;
ch = getChar();
// ch = 0 means End Of File reached!!!
while ( ch != 0 )
{
// break if '<%' or '<%=' reached...
if ( ch == '<' )
{
ch = getChar();
if ( ch == '%' )
{
// Start of Script Block reached!!!
ch = getChar();
if ( ch != '=' )
{
pushbackChar();
ch = '%';
}
break;
}
putChar( '<' );
}
putChar( ch );
ch = getChar();
}
// NOTE: (2002-06-24 Gabriele Budelacci)
// ch = 0 if EOF reached, or
// ch = '%' if a simple block script has founded, or
// ch = '=' if an output block script has founded...
return ch;
}//processHTML
// pushNode
// Insert a node in a list (STACK like use).
void pushNode( lisa_node *node, lisa_node *list )
{
appendNode( node, list );
}//pushNode
// pushbackChar:
// Perform a push-back of last char on
// the input stream.
// NOTE: This method is criticable!
void pushbackChar( void )
{
if ( line_index > 0 )
line_index -= 1;
}//pushbackChar
// pushbackCharS:
// Perform a push-back of last char on
// the stored script block.
// NOTE: This method is criticable!
void pushbackCharS( void )
{
if ( script_index > 0 )
script_index -= 1;
}//pushbackCharS
// putChar:
// Put a char to the output stream.
void putChar( char ch )
{
fputc( ch, outstream );
}//putChar
// putCharS:
// Put a char to the script block.
void putCharS( char ch )
{
script[ script_index ] = ch;
script_index += 1;
}//putCharS
// startScript:
// Write the start tag script to the output stream.
void startScript( void )
{
switch ( global_env->language )
{
case LISA_LANG_PHP:
fprintf( outstream, "<? " );
break;
default:
fprintf( outstream, "<%% " );
}
}//startScript
// startExpression:
// Write the start expression tag script to the output stream.
void startExpression( void )
{
switch ( global_env->language )
{
case LISA_LANG_PHP:
fprintf( outstream, "<?= " );
break;
default:
fprintf( outstream, "<%%= " );
}
}//startExpression
// stopScript:
// Write the stop tag script to the output stream.
void stopScript( void )
{
switch ( global_env->language )
{
case LISA_LANG_PHP:
fprintf( outstream, " ?>" );
break;
default:
fprintf( outstream, " %%>" );
}
}//stopScript
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -