/* =====================================
 * Generated by Mod-c 0.26.5
 * MODULE codegen
 * ===================================== */

#include "codegen.h"

void codegen(void);

/**
 * Mod-c
 * By M. Scott Reynolds
 * Date 18 April 2026
 * 
 * Main codegen dispacthcer and
 * Initial Context definition.
 *
 * Initial context passed into codegen_generate to setup environment
 * and other options. Target is defined in codegen_common.h
 *  .out = out                          // REQUIRED: Predefined output buffer. 
 *  .ast = ast                          // REQUIRED: Top of the Abstract Syntax Tree
 *  .debug_enabled = true               // REQUIRED: Set to true/false for debug statement generation.
 *  .target = TARGET_C/TARGET_HEADER    // REQUIRED: Output code target. 
 *                                      // Currently only TARGET_C and TARGET_HEADER supported.
 */
#include "dynbuf.h"
#include "codegen_mh.h"
#include "stddef.h"
#include "stdio.h"
#include "stdlib.h"
#include "node.h"
#include "codegen_common.h"
#include "codegen_c.h"
#include "codegen_header.h"
/**
 * Codegen main dispatcher from the given AST using the selected target.
 * The output is written into the provided DynBuf (which must be initialized).
 * Exits program on fatal error. 
 */
void codegen_generate(InitialContext initial)
{
    CodegenContext ctx = {0};
    if ((initial).ast == nil || (initial).out == nil)
    {
        fprintf(stderr, "FATAL ERROR: codegen_generate: NULL initial.ast or NULL initial.out\n");
        exit(1);
    }
    (ctx).out = (initial).out;
    (ctx).ast = (initial).ast;
    (ctx).target = (initial).target;
    (ctx).debug_enabled = (initial).debug_enabled;
    (ctx).assert_off = (initial).assert_off;
    (ctx).dbc_off = (initial).dbc_off;
    (ctx).parent_ctx = nil;
    (ctx).parent_node = nil;
    (ctx).enclosing_func = nil;
    (ctx).enclosing_loop = nil;
    (ctx).indent = 0;
    if ((ctx).debug_enabled)
    {
        fprintf(stderr, "DEBUG: flag has been set\n");
    }
    if ((ctx).assert_off)
    {
        fprintf(stderr, "ASSERT_OFF: flag has been set\n");
    }
    if ((ctx).dbc_off)
    {
        fprintf(stderr, "DBC_OFF: flag has been set\n");
    }
    switch ((initial).target)
    {
        case TARGET_C:
        {
            codegen_c(ctx);
        }
        break;
        case TARGET_HEADER:
        {
            codegen_header(ctx);
        }
        break;
        case TARGET_MH:
        {
            codegen_mh(ctx);
        }
        break;
        case TARGET_VM:
        case TARGET_WASM:
        case TARGET_JSON:
        {
            fprintf(stderr, "ERROR: codegen_generate: target %d not yet implemented\n", (initial).target);
            exit(1);
        }
        break;
        default:
        {
            fprintf(stderr, "ERROR: codegen_generate: unknown target specified: %d\n", (initial).target);
            exit(1);
        }
        break;
    }
}


void codegen(void)
{
}
