' debug_trace.pb — Function call trace simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL traceDepth AS LONG
    LOCAL traceEvents(200) AS LONG
    LOCAL traceTypes(200) AS LONG
    LOCAL traceValues(200) AS LONG
    LOCAL traceCount AS LONG
    LOCAL maxTrace AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL currentDepth AS LONG
    LOCAL eventType AS LONG
    LOCAL eventValue AS LONG
    LOCAL indentStr AS STRING
    LOCAL iter AS LONG
    LOCAL numEvents AS LONG
    LOCAL depthStr AS STRING
    LOCAL result AS LONG

    maxTrace = 100

    PRINT "=== Function Call Trace ==="

    FOR numEvents = 1 TO 20
        eventType = numEvents MOD 2
        eventValue = numEvents * 10

        IF eventType = 0 THEN
            currentDepth = currentDepth + 1
            traceEvents(traceCount) = numEvents
            traceTypes(traceCount) = 0
            traceValues(traceCount) = currentDepth
            traceCount = traceCount + 1

            indentStr = ""
            FOR i = 1 TO currentDepth
                indentStr = indentStr + "  "
            NEXT i

            IF numEvents = 1 THEN PRINT indentStr + "-> main()"
            IF numEvents = 2 THEN PRINT indentStr + "-> readConfig()"
            IF numEvents = 3 THEN PRINT indentStr + "-> parseOptions()"
            IF numEvents = 4 THEN PRINT indentStr + "-> initModules()"
            IF numEvents = 5 THEN PRINT indentStr + "-> loadPlugin()"
            IF numEvents = 6 THEN PRINT indentStr + "-> registerCallback()"
            IF numEvents = 8 THEN PRINT indentStr + "-> processEvent()"
            IF numEvents = 10 THEN PRINT indentStr + "-> validateData()"
            IF numEvents = 12 THEN PRINT indentStr + "-> writeOutput()"
            IF numEvents = 14 THEN PRINT indentStr + "-> flushBuffer()"
            IF numEvents = 16 THEN PRINT indentStr + "-> cleanupTemp()"
            IF numEvents = 18 THEN PRINT indentStr + "-> unloadModules()"
            IF numEvents = 20 THEN PRINT indentStr + "-> shutdown()"
        END IF

        IF eventType = 1 THEN
            IF currentDepth > 0 THEN
                indentStr = ""
                FOR i = 1 TO currentDepth
                    indentStr = indentStr + "  "
                NEXT i

                IF numEvents = 3 THEN PRINT indentStr + "<- parseOptions() = OK"
                IF numEvents = 5 THEN PRINT indentStr + "<- loadPlugin() = loaded"
                IF numEvents = 7 THEN PRINT indentStr + "<- processEvent() = handled"
                IF numEvents = 9 THEN PRINT indentStr + "<- readConfig() = done"
                IF numEvents = 11 THEN PRINT indentStr + "<- validateData() = valid"
                IF numEvents = 13 THEN PRINT indentStr + "<- writeOutput() = " + STR$(numEvents * 10)
                IF numEvents = 15 THEN PRINT indentStr + "<- flushBuffer() = flushed"
                IF numEvents = 17 THEN PRINT indentStr + "<- cleanupTemp() = cleaned"
                IF numEvents = 19 THEN PRINT indentStr + "<- unloadModules() = done"

                traceEvents(traceCount) = numEvents
                traceTypes(traceCount) = 1
                traceValues(traceCount) = currentDepth
                traceCount = traceCount + 1
                currentDepth = currentDepth - 1
            END IF
        END IF
    NEXT numEvents

    PRINT "Trace events: " + STR$(traceCount)
    PRINT "Max depth: " + STR$(currentDepth)
    PRINT "=== End Trace ==="
    FUNCTION = 0
END FUNCTION
