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

📄 eval.c

📁 Coware的LISA指令集描述语言开发包
💻 C
📖 第 1 页 / 共 3 页
字号:
				if ( tmp->type < 0 )
				{
					evalError( "variable already declared", tmp );
				}
				tmp->type = -LISA_TYPE_VARIANT;
			}
			// process next element
			next = next->next;
		}

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				// NOTE: (2002-06-27 Gabriele Budelacci)
				// In PHP language, declaration of variant
				// can be omitted.
				break;
			}
		}
		return;
	}

	//
	//                     while
	//                      / |
	//                 empty
	//                 /   |
	//        condition     statement
	//
	if ( strcmp( node->element, "while" ) == 0 )
	{
		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, "while ( " );
				evalCondition( node->args->args, env );
				fprintf( outstream, " )\n" );
				evalBlock( node->args->next, env, 1 );
				break;
			}
		}
		return;
	}

	evalError( "unimplemented keyword evaluation", node );
}//evalKeyword





// evalNumber:
//	Evaluate a number node in the specified environment.
void evalNumber( struct lisa_node *node, struct lisa_environment *env )
{
	switch ( env->language )
	{
		case LISA_LANG_PHP:
		{
			fprintf( outstream, "%s", node->element );
			break;
		}
	}
}//evalNumber





// evalOperator:
//	Evaluate an operator node in the specified environment.
void evalOperator( struct lisa_node *node, struct lisa_environment *env )
{
	switch ( env->language )
	{
		case LISA_LANG_PHP:
		{
			fprintf( outstream, " %s ", node->element );
			break;
		}
	}
}//evalOperator





// evalString:
//	Evaluate a string node in the specified environment.
void evalString( struct lisa_node *node, struct lisa_environment *env )
{
	switch ( env->language )
	{
		case LISA_LANG_PHP:
		{
			fprintf( outstream, "\"%s\"", node->element );
			break;
		}
	}
}//evalString





// evalTAGKeyword:
//	Evaluate a TAG keyword node in the specified environment.
void evalTAGKeyword( struct lisa_node *node, struct lisa_environment *env )
{
	struct lisa_environment *e;
	struct lisa_node        *tmp, *next;

	if ( strcmp( node->element, "case" ) == 0 )
	{
		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		// create a child environment...
		global_env = childEnvironment( global_env );

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " case " );
				eval( node->args, env );
				fprintf( outstream, ": " );
				break;
			}
		}

		return;
	}

	if ( strcmp( node->element, "default" ) == 0 )
	{
		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		// create a child environment...
		global_env = childEnvironment( global_env );

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " default: " );
				break;
			}
		}

		return;
	}

	if ( strcmp( node->element, "else" ) == 0 )
	{
		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		// create a child environment...
		global_env = childEnvironment( global_env );

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " else: " );
				break;
			}
		}
		return;
	}

	if ( strcmp( node->element, "endif" ) == 0 )
	{
		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " endif; " );
				break;
			}
		}
		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		return;
	}

	if ( ( strcmp( node->element, "endforeach" ) == 0 ) ||
		 ( strcmp( node->element, "next" ) == 0 ) )
	{
		int type;
		struct lisa_node *n = NULL;

		// get the type of loop...
		n = popNode( global_env->loops );
		type = n->type;
		if ( type < 0 ) type = -type;

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				switch ( type )
				{
					case LISA_TYPE_ARRAY:
					{
						fprintf( outstream, " endforeach; " );
						break;
					}
					case LISA_TYPE_SET:
					{
						fprintf( outstream, " endfor; " );
						break;
					}
				}
				break;
			}
		}

		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		return;
	}

	if ( strcmp( node->element, "endswitch" ) == 0 )
	{
		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " endswitch; " );
				break;
			}
		}
		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		return;
	}

	if ( strcmp( node->element, "endwhile" ) == 0 )
	{
		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " endwhile; " );
				break;
			}
		}
		// replace the parent environment...
		e = env->parent;
		destroyEnvironment( global_env );
		global_env = e;

		return;
	}

	if ( strcmp( node->element, "foreach" ) == 0 )
	{
		int type;
		struct lisa_node *n = NULL;

		// check if the identifier is declared...
		tmp = findNodeByElement( node->args->element, env->declared );
		if ( tmp == NULL )
			evalError( "undeclared variable", node->args );

		// get the identifier type...
		type = tmp->type;
		if ( type < 0 ) type = -type;

		// create a child environment...
		global_env = childEnvironment( env );

		// appending the variable type on the environment loops list...
		n = copyNode( tmp );
		n->args = NULL;
		n->next = NULL;
		pushNode( n, global_env->loops );

		// switch on array or set
		switch ( type )
		{
			case LISA_TYPE_ARRAY:
			{
				switch ( env->language )
				{
					case LISA_LANG_PHP:
					{
						fprintf( outstream, " foreach ( $%s as ", node->args->element );

						// declaring the identifiers as variants...
						next = node->args->next;
						while ( next )
						{
							// search the node in declared list...
							tmp = findNodeByElement( next->element, env->declared );
							// if not exists, append a new identifier...
							if ( tmp == NULL )
							{
								tmp = cloneNode( next );
								// NOTE: (2002-07-12 Gabriele Budelacci)
								//	When a new variable is declared whithin a block,
								//	then the type is negated.
								tmp->type = -LISA_TYPE_VARIANT;		// please note minus '-' sign
								appendNode( tmp, env->declared );
							}
							else
							{
								// The identifier is already declared...
								// If the type is positive, the identifier can be redeclared...
								if ( tmp->type < 0 )
								{
									evalError( "variable already declared", tmp );
								}
								tmp->type = -LISA_TYPE_VARIANT;
							}
							// process next element
							next = next->next;
						}

						// eval the identifier list...
						next = node->args->next;
						fprintf( outstream, "$%s", next->element );
						if ( next->next )
							evalError( "no more than one variable allowed while processing an array", next );

						fprintf( outstream, " ): " );
						break;
					}
				}
				break;
			}

			case LISA_TYPE_SET:
			{
				int i;

				switch ( env->language )
				{
					case LISA_LANG_PHP:
					{
						fprintf( outstream, " for ( $_index_=0 ; $_index_<_foreach_( $%s ) ; $_index_++ ): ", node->args->element );
						fprintf( outstream, "?>\n" );
						fprintf( outstream, "\t<? _fetch_( $%s );\n", node->args->element );

							// declaring the identifiers as variants...
							next = node->args->next;
							i = 0;
							while ( next )
							{
								// search the node in declared list...
								tmp = findNodeByElement( next->element, env->declared );
								// if not exists, append a new identifier...
								if ( tmp == NULL )
								{
									tmp = cloneNode( next );
									// NOTE: (2002-07-12 Gabriele Budelacci)
									//	When a new variable is declared whithin a block,
									//	then the type is negated.
									tmp->type = -LISA_TYPE_VARIANT;		// please note minus '-' sign
									appendNode( tmp, global_env->declared );
								}
								else
								{
									// The identifier is already declared...
									// If the type is positive, the identifier can be redeclared...
									if ( tmp->type < 0 )
									{
										evalError( "variable already declared", tmp );
									}
									tmp->type = -LISA_TYPE_VARIANT;
								}
								fprintf( outstream, "\t$%s = _scan_( $%s, %d );\n", tmp->element, node->args->element, i++ );
								// process next element
								next = next->next;
							}
							// NOTE: (2002-09-04 Gabriele Budelacci)
							//	Endtag ('?>') will be written by the caller function, so I don't
							//	put them out...
							fprintf( outstream, "\t" );

						break;
					}
				}
				break;
			}

			default:
			{
				evalError( "wrong type variable", node->args );
			}
		}
		return;
	}

	if ( strcmp( node->element, "if" ) == 0 )
	{
		// create a child environment...
		global_env = childEnvironment( env );

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " if ( " );
				evalCondition( node->args->args, env );
				fprintf( outstream, " ): " );
				break;
			}
		}
		return;
	}

	if ( strcmp( node->element, "switch" ) == 0 )
	{
		// create a child environment...
		global_env = childEnvironment( env );

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " switch ( " );
				evalCondition( node->args->args, env );
				fprintf( outstream, " ):\n" );
				// NOTE: (2002-09-12 Gabriele Budelacci)
				//	Now it will must evalued a 'case:' or
				//	'default:' statement.
				//	(it was done by main.c)
				break;
			}
		}
		return;
	}

	if ( strcmp( node->element, "while" ) == 0 )
	{
		// create a child environment...
		global_env = childEnvironment( env );

		switch ( env->language )
		{
			case LISA_LANG_PHP:
			{
				fprintf( outstream, " while ( " );
				evalCondition( node->args->args, env );
				fprintf( outstream, " ): " );
				break;
			}
		}
		return;
	}

	evalError( "unimplemented TAG keyword evaluation", node );
}//evalTAGKeyword


⌨️ 快捷键说明

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