From 6adb90fd75f544895ba8ffc960f08ff00dc58949 Mon Sep 17 00:00:00 2001 From: midipix Date: Jan 02 2016 16:57:37 +0000 Subject: action vector and action iteration: initial implementation. --- diff --git a/include/apimagic/apimagic.h b/include/apimagic/apimagic.h index 1139873..a7cc472 100644 --- a/include/apimagic/apimagic.h +++ b/include/apimagic/apimagic.h @@ -63,6 +63,7 @@ struct amgc_input { struct amgc_action { enum amgc_action_type type; int action; + const char * symbol; const char * filter; }; diff --git a/src/apimagic.c b/src/apimagic.c index 6c6408c..dd0d893 100644 --- a/src/apimagic.c +++ b/src/apimagic.c @@ -23,6 +23,16 @@ static ssize_t apimagic_version(struct amgc_driver_ctx * dctx) static void apimagic_perform_unit_actions(struct amgc_unit_ctx * uctx) { + const struct amgc_action * action; + + for (action=uctx->cctx->actions; action->type; action++) + switch (action->action) { + case AMGC_OUTPUT_ENUM: + if (action->symbol) + (void)0; + else + amgc_output_unit_enums(uctx,0,stdout); + } } static int apimagic_exit(struct amgc_driver_ctx * dctx, int nerrors) diff --git a/src/driver/amgc_driver_ctx.c b/src/driver/amgc_driver_ctx.c index 66e4f2f..c25be55 100644 --- a/src/driver/amgc_driver_ctx.c +++ b/src/driver/amgc_driver_ctx.c @@ -28,6 +28,7 @@ extern const struct argv_option amgc_default_options[]; struct amgc_driver_ctx_alloc { struct argv_meta * meta; + struct amgc_action * actions; struct amgc_driver_ctx_impl ctx; uint64_t guard; const char * units[]; @@ -70,9 +71,11 @@ static int amgc_driver_usage( static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc( struct argv_meta * meta, const struct amgc_common_ctx * cctx, - size_t nunits) + size_t nunits, + size_t nactions) { struct amgc_driver_ctx_alloc * ictx; + struct amgc_action * actions; size_t size; struct argv_entry * entry; const char ** units; @@ -83,6 +86,11 @@ static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc( if (!(ictx = calloc(1,size))) return 0; + if (!(actions = calloc(nactions+1,sizeof(*ictx->actions)))) { + free(ictx); + return 0; + } + if (cctx) memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx)); @@ -92,6 +100,8 @@ static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc( ictx->meta = meta; ictx->ctx.fdtmpin = -1; + ictx->actions = actions; + ictx->ctx.actions = actions; ictx->ctx.ctx.units = ictx->units; return &ictx->ctx; } @@ -130,6 +140,7 @@ int amgc_get_driver_ctx( struct argv_meta * meta; struct argv_entry * entry; size_t nunits; + size_t nactions; const char * program; options = amgc_default_options; @@ -138,6 +149,7 @@ int amgc_get_driver_ctx( return -1; nunits = 0; + nactions= 0; program = argv_program_name(argv[0]); memset(&cctx,0,sizeof(cctx)); @@ -162,6 +174,10 @@ int amgc_get_driver_ctx( case TAG_LANG_STD: cctx.std = amgc_lang_std_from_string(entry->arg); break; + + default: + nactions++; + break; } } else nunits++; @@ -170,11 +186,22 @@ int amgc_get_driver_ctx( if (amgc_init_cparser()) return amgc_get_driver_ctx_fail(meta); - if (!(ctx = amgc_driver_ctx_alloc(meta,&cctx,nunits))) + if (!(ctx = amgc_driver_ctx_alloc(meta,&cctx,nunits,nactions))) return amgc_get_driver_ctx_fail(meta); + /* create action vector */ + for (entry=meta->entries,nactions=0; entry->fopt || entry->arg; entry++) + if (entry->fopt) + switch (entry->tag) { + case TAG_PRINT_ENUMS: + ctx->actions[nactions].type = AMGC_ACTION_OUTPUT; + ctx->actions[nactions++].action = AMGC_OUTPUT_ENUM; + break; + } + ctx->ctx.program = program; ctx->ctx.cctx = &ctx->cctx; + ctx->cctx.actions = ctx->actions; ctx->cctx.ccenv = &ctx->ccenv; *pctx = &ctx->ctx; @@ -192,7 +219,7 @@ int amgc_create_driver_ctx( if (!(meta = argv_get(argv,amgc_default_options,0))) return -1; - if (!(ctx = amgc_driver_ctx_alloc(meta,cctx,0))) + if (!(ctx = amgc_driver_ctx_alloc(meta,cctx,0,0))) return amgc_get_driver_ctx_fail(0); ctx->ctx.cctx = &ctx->cctx; @@ -216,6 +243,7 @@ static void amgc_exit_cparser(void) static void amgc_free_driver_ctx_impl(struct amgc_driver_ctx_alloc * ictx) { argv_free(ictx->meta); + free(ictx->actions); free(ictx); amgc_exit_cparser(); } diff --git a/src/internal/apimagic_driver_impl.h b/src/internal/apimagic_driver_impl.h index be747aa..d070c19 100644 --- a/src/internal/apimagic_driver_impl.h +++ b/src/internal/apimagic_driver_impl.h @@ -14,12 +14,14 @@ enum app_tags { TAG_HELP, TAG_VERSION, + TAG_PRINT_ENUMS, TAG_LANG_STD, }; struct amgc_driver_ctx_impl { struct amgc_common_ctx cctx; struct amgc_driver_ctx ctx; + struct amgc_action * actions; struct compilation_env_t ccenv; int fdtmpin; }; diff --git a/src/skin/amgc_skin_default.c b/src/skin/amgc_skin_default.c index 4fdbbe9..407bad1 100644 --- a/src/skin/amgc_skin_default.c +++ b/src/skin/amgc_skin_default.c @@ -17,6 +17,9 @@ const struct argv_option amgc_default_options[] = { {"help", 'h',TAG_HELP,ARGV_OPTARG_OPTIONAL,0,"short|long",0, "show usage information [listing %s options only]"}, + {"print-enums", 0,TAG_PRINT_ENUMS,ARGV_OPTARG_NONE,0,0,0, + "output the translation unit's enums."}, + {"std", '\0',TAG_LANG_STD,ARGV_OPTARG_REQUIRED, ARGV_OPTION_HYBRID_EQUAL,AMGC_LANG_STD_PARADIGM,0, "set language standard"},