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

📄 compile.c

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 C
📖 第 1 页 / 共 3 页
字号:
    FRAME *frame )
{
    /** Todo: check for empty class name.
        Check for class redeclaration. */

    char* class_module = 0;

    LIST* name = parse_evaluate( p->left->right, frame );
    LIST* bases = 0;

    if (p->left->left)
        bases = parse_evaluate( p->left->left->right, frame );

    class_module = make_class_module(name, bases, frame);    
    evaluate_in_module( class_module, p->right, frame );

    return L0;    
}


/*
 * compile_list() - expand and return a list 
 *
 *  parse->string - character string to expand
 */

LIST *
compile_list(
    PARSE   *parse,
    FRAME *frame )
{
    /* voodoo 1 means: s is a copyable string */
    char *s = parse->string;
    return var_expand( L0, s, s + strlen( s ), frame->args, 1 );
}

/*
 * compile_local() - declare (and set) local variables
 *
 *  parse->left list of variables
 *  parse->right    list of values
 *  parse->third    rules to execute
 */

LIST *
compile_local(
    PARSE   *parse,
    FRAME *frame )
{
    LIST *l;
    SETTINGS *s = 0;
    LIST    *nt = parse_evaluate( parse->left, frame );
    LIST    *ns = parse_evaluate( parse->right, frame );
    LIST    *result;

    if( DEBUG_COMPILE )
    {
        debug_compile( 0, "local", frame);
        list_print( nt );
        printf( " = " );
        list_print( ns );
        printf( "\n" );
    }

    /* Initial value is ns */

    for( l = nt; l; l = list_next( l ) )
        s = addsettings( s, 0, l->string, list_copy( (LIST*)0, ns ) );

    list_free( ns );
    list_free( nt );

    /* Note that callees of the current context get this "local" */
    /* variable, making it not so much local as layered. */

    pushsettings( s );
    result = parse_evaluate( parse->third, frame );
    popsettings( s );

    freesettings( s );

    return result;
}

/*
 * compile_null() - do nothing -- a stub for parsing
 */

LIST *
compile_null(
    PARSE   *parse,
    FRAME *frame )
{
    return L0;
}

/*
 * compile_on() - run rule under influence of on-target variables
 *
 * 	parse->left	list of files to include (can only do 1)
 *	parse->right	rule to run
 *
 * EXPERIMENTAL!
 */

LIST *
compile_on(
	PARSE	*parse,
	FRAME	*frame )
{
	LIST    *nt = parse_evaluate( parse->left, frame );
	LIST	*result = 0;

	if( DEBUG_COMPILE )
	{
	    debug_compile( 0, "on", frame );
	    list_print( nt );
	    printf( "\n" );
	}

	if( nt )
	{
	    TARGET *t = bindtarget( nt->string );
	    pushsettings( t->settings );

	    result = parse_evaluate( parse->right, frame );

	    popsettings( t->settings );
	}

	list_free( nt );

	return result;
}


/*
 * compile_rule() - compile a single user defined rule
 *
 *  parse->string   name of user defined rule
 *  parse->left parameters (list of lists) to rule, recursing left
 *
 * Wrapped around evaluate_rule() so that headers() can share it.
 */

LIST *
compile_rule(
    PARSE   *parse,
    FRAME *frame )
{
    FRAME       inner[1];
    LIST    *result;
    PARSE   *p;
    

    /* Build up the list of arg lists */

    frame_init( inner );
    inner->prev = frame;
    inner->module = frame->module; /* This gets fixed up in evaluate_rule(), below */
    inner->procedure = parse;

    for( p = parse->left; p; p = p->left )
        lol_add( inner->args, parse_evaluate( p->right, frame ) );

    /* And invoke rule */

    result = evaluate_rule( parse->string, inner );

    frame_free( inner );

    return result;
}

static void argument_error( char* message, RULE* rule, FRAME* frame, LIST* arg )
{
    LOL* actual = frame->args;
    assert( frame->procedure != 0 );
    backtrace_line( frame->prev );
    printf( "*** argument error\n* rule %s ( ", frame->rulename );
    lol_print( rule->arguments->data );
    printf( " )\n* called with: ( " );
    lol_print( actual );
    printf( " )\n* %s %s\n", message, arg ? arg->string : "" );
    print_source_line( rule->procedure );
    printf( "see definition of rule '%s' being called\n", rule->name );
    backtrace( frame->prev );
    exit(1);
}

/* define delimiters for type check elements in argument lists (and
 * return type specifications, eventually)
 */
# define TYPE_OPEN_DELIM '['
# define TYPE_CLOSE_DELIM ']'

/* is_type_name - true iff the given string represents a type check
 * specification
 */
static int
is_type_name( char* s )
{
    return s[0] == TYPE_OPEN_DELIM
        && s[strlen(s) - 1] == TYPE_CLOSE_DELIM;
}

/*
 * arg_modifier - if the next element of formal is a single character,
 * return that; return 0 otherwise.  Used to extract "*+?" modifiers
 * from argument lists.
 */
static char
arg_modifier( LIST* formal )
{
    if ( formal->next )
    {
        char *next = formal->next->string;
        if ( next && next[0] != 0 && next[1] == 0 )
            return next[0];
    }
    return 0;
}

/*
 * type_check - checks that each element of values satisfies the
 * requirements of type_name.
 *
 *      caller   - the frame of the rule calling the rule whose
 *                 arguments are being checked
 *
 *      called   - the rule being called
 *
 *      arg_name - a list element containing the name of the argument
 *                 being checked
 */
static void
type_check( char* type_name, LIST *values, FRAME* caller, RULE* called, LIST* arg_name )
{
    static module_t *typecheck = 0;

    /* if nothing to check, bail now */
    if ( !values || !type_name )
        return;

    if ( !typecheck )
        typecheck = bindmodule(".typecheck");

    /* if the checking rule can't be found, also bail */
    {
        RULE checker_, *checker = &checker_;

        checker->name = type_name;
        if ( !typecheck->rules || !hashcheck( typecheck->rules, (HASHDATA**)&checker ) )
            return;
    }
    
    exit_module( caller->module );
    
    while ( values != 0 )
    {
        LIST *error;
        FRAME frame[1];
        frame_init( frame );
        frame->module = typecheck;
        frame->prev = caller;

        enter_module( typecheck );
        /* Prepare the argument list */
        lol_add( frame->args, list_new( L0, values->string ) );
        error = evaluate_rule( type_name, frame );
        
        exit_module( typecheck );
        
        if ( error )
            argument_error( error->string, called, caller, arg_name );

        frame_free( frame );
		values = values->next;
    }

    enter_module( caller->module );
}

/*
 * collect_arguments() - local argument checking and collection
 */
static SETTINGS *
collect_arguments( RULE* rule, FRAME* frame )
{
    SETTINGS *locals = 0;
    
    LOL* all_actual = frame->args;
    LOL *all_formal = rule->arguments ? rule->arguments->data : 0;
    if ( all_formal ) /* Nothing to set; nothing to check */
    {
        int max = all_formal->count > all_actual->count
            ? all_formal->count
            : all_actual->count;
        
        int n;
        for ( n = 0; n < max ; ++n )
        {
            LIST *actual = lol_get( all_actual, n );
            char *type_name = 0;
            
            LIST *formal;
            for ( formal = lol_get( all_formal, n ); formal; formal = formal->next )
            {
                char* name = formal->string;

                if ( is_type_name(name) )
                {
                    if ( type_name )
                        argument_error( "missing argument name before type name:", rule, frame, formal );
                    
                    if ( !formal->next )
                        argument_error( "missing argument name after type name:", rule, frame, formal );

                    type_name = formal->string;
                }
                else
                {
                    LIST* value = 0;
                    char modifier;
                    LIST* arg_name = formal; /* hold the argument name for type checking */
                    
                    /* Stop now if a variable number of arguments are specified */
                    if ( name[0] == '*' && name[1] == 0 )
                        return locals;

                    modifier = arg_modifier( formal );
                
                    if ( !actual && modifier != '?' && modifier != '*' )
                        argument_error( "missing argument", rule, frame, formal );

                    switch ( modifier )
                    {
                    case '+':
                    case '*':
                        value = list_copy( 0, actual );
                        actual = 0;
                        /* skip an extra element for the modifier */
                        formal = formal->next; 
                        break;
                    case '?':
                        /* skip an extra element for the modifier */
                        formal = formal->next; 
                        /* fall through */
                    default:
                        if ( actual ) /* in case actual is missing */
                        {
                            value = list_new( 0, actual->string );
                            actual = actual->next;
                        }
                    }
                
                    locals = addsettings( locals, 0, name, value );
                    type_check( type_name, value, frame, rule, arg_name );
                    type_name = 0;
                }
            }
            
            if ( actual )
            {
                argument_error( "extra argument", rule, frame, actual );
            }
        }
    }
    return locals;
}

struct profile_info
{
    char* name;                 /* name of rule being called */
    clock_t cumulative;         /* cumulative time spent in rule */
    clock_t net;                /* time spent in rule proper */
    unsigned long num_entries;  /* number of time rule was entered */
    unsigned long stack_count;  /* number of the times this function is present in stack */
};
typedef struct profile_info profile_info;

struct profile_frame
{
    profile_info* info;               /* permanent storage where data accumulates */
    clock_t overhead;                 /* overhead for profiling in this call */
    clock_t entry_time;               /* time of last entry to rule */
    struct profile_frame* caller;     /* stack frame of caller */
    clock_t subrules;                 /* time spent in subrules */
};
typedef struct profile_frame profile_frame;

static profile_frame* profile_stack = 0;
static struct hash* profile_hash = 0;

static void profile_enter( char* rulename, profile_frame* frame )
{
    clock_t start = clock();
    profile_info info, *p = &info;
    
    if ( !profile_hash )
        profile_hash = hashinit(sizeof(profile_info), "profile");

    info.name = rulename;
    
    if ( hashenter( profile_hash, (HASHDATA **)&p ) )
        p->cumulative = p->net = p->num_entries = p->stack_count = 0;

    ++(p->num_entries);
    ++(p->stack_count);
    
    frame->info = p;
    
    frame->caller = profile_stack;
    profile_stack = frame;

    frame->entry_time = clock();
    frame->overhead = 0;
    frame->subrules = 0;

    /* caller pays for the time it takes to play with the hash table */
    if ( frame->caller )
        frame->caller->overhead += frame->entry_time - start;
}
    
static void profile_exit(profile_frame* frame)
{
    /* cumulative time for this call */
    clock_t t = clock() - frame->entry_time - frame->overhead;

⌨️ 快捷键说明

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