Exemplo n.º 1
0
int main(void) {
    DynArr dynarr;
    int i;

    dynarr_create(&dynarr);

    printf("Enter numbers! Stop input by entering not a number.\n");
    for (;;) {
        int num;
        printf("Enter number: ");
        if (scanf("%d", &num) == 1) {
            if (dynarr_append(&dynarr, num) == 0) {
                printf("\n\nError!\n");
                return 1;
            }
        } else {
            break;
        }
    }

    printf("You have entered:");
    for (i = 0; i < dynarr_length(&dynarr); ++i) {
        printf(" %d", dynarr_get(&dynarr, i));
    }
    printf("\n");

    dynarr_destroy(&dynarr);

    return 0;
}
Exemplo n.º 2
0
static void pp_define_object_macro(struct lexer *lexer, const char *name) {
	struct dynarr *darr = store_token_until_newline(lexer);

	// simple check for: #define x x case.
	// a real example is in: /usr/include/bits/confname.h
	//
	// the ultimate way to sovle the (indirectly) referring itself obj/func macro is 
	// constructing the macro expanding tree
	if (dynarr_size(darr) == 1) {
		union token *tok = dynarr_get(darr, 0);
		if (tok->tok_tag == TOK_IDENTIFIER && strcmp(tok->id.s, name) == 0) {
			token_destroy(*tok);
			free(tok);
			dynarr_destroy(darr);
			red("ignore identity obj macro %s", name);
			return;
		}
	}
	struct macro *macro = obj_macro_init(darr);
	define_macro(lexer, name, macro);

#if DUMP_MACRO
	// fprintf(stderr, "%s define the macro %s\n", lexer->cstream->path, name);
	macro_dump(lexer, name, macro);
#endif
}
Exemplo n.º 3
0
// The declarator is for function
static void register_func_parameters_for_typedef(struct parser *parser, struct declarator *declarator) {
	struct dynarr *suff_list = declarator->direct_declarator->suff_list;
	assert(dynarr_size(suff_list) == 1);
	struct direct_declarator_suffix *suff = dynarr_get(suff_list, 0);
	assert(suff->empty_paren || suff->param_type_list != NULL);
	if (suff->param_type_list != NULL) {
		DYNARR_FOREACH_BEGIN(suff->param_type_list->param_decl_list, parameter_declaration, each);
			register_func_parameter_for_typedef(parser, each);
		DYNARR_FOREACH_END();
	}
}