' Compile-time configuration with #IF/#DEFINE
#COMPILE EXE
#DIM ALL

#DEFINE FEATURE_LOGGING AS 1
#DEFINE FEATURE_CACHE AS 1
#DEFINE FEATURE_ENCRYPTION AS 0
#DEFINE FEATURE_COMPRESSION AS 0

#DEFINE LOG_LEVEL_ERROR AS 0
#DEFINE LOG_LEVEL_WARN AS 1
#DEFINE LOG_LEVEL_INFO AS 2
#DEFINE LOG_LEVEL_DEBUG AS 3

#DEFINE CONFIG_LOG_LEVEL AS LOG_LEVEL_INFO
#DEFINE CACHE_SIZE AS 1024
#DEFINE BUFFER_SIZE AS 4096

MACRO LogMsg(level, msg)
    #IF CONFIG_LOG_LEVEL >= level
        PRINT "[LOG] "; msg
    #ENDIF
END MACRO

MACRO CacheInit()
    #IF FEATURE_CACHE
        PRINT "[CACHE] size="; CACHE_SIZE
    #ENDIF
END MACRO

MACRO CacheFlush()
    #IF FEATURE_CACHE
        PRINT "[CACHE] flushed"
    #ENDIF
END MACRO

FUNCTION ComputeHash(BYVAL n AS LONG) AS LONG
    LOCAL i  AS LONG
    LOCAL h  AS LONG
    h = &H5555
    FOR i = 1 TO n
        h = h XOR i
        h = h * 33 + h
    NEXT i
    FUNCTION = h
END FUNCTION

SUB ProcessCached(BYVAL size AS LONG)
    CacheInit()
    LOCAL i AS LONG
    FOR i = 1 TO size STEP 64
        LogMsg(LOG_LEVEL_DEBUG, "line at " + STR$(i))
    NEXT i
    CacheFlush()
END SUB

FUNCTION Calculate(BYVAL a AS LONG, BYVAL b AS LONG) AS LONG
    LogMsg(LOG_LEVEL_DEBUG, "calc " + STR$(a) + "," + STR$(b))
    LOCAL r AS LONG
    r = a * b + a - b
    FUNCTION = r
END FUNCTION

SUB ValidateInput(BYVAL v AS LONG, BYVAL mn AS LONG, BYVAL mx AS LONG)
    LogMsg(LOG_LEVEL_INFO, "validate " + STR$(v))
    IF v < mn OR v > mx THEN
        PRINT "FAIL: "; v; " not in range"
    ELSE
        PRINT "PASS: "; v; " in range"
    END IF
END SUB

FUNCTION SumRange(BYVAL n AS LONG) AS LONG
    LOCAL i  AS LONG
    LOCAL s  AS LONG
    FOR i = 1 TO n
        s = s + i
    NEXT i
    FUNCTION = s
END FUNCTION

FUNCTION Average(BYVAL n AS LONG) AS LONG
    IF n > 0 THEN
        FUNCTION = SumRange(n) / n
    ELSE
        FUNCTION = 0
    END IF
END FUNCTION

SUB ProcessData(BYVAL mode AS LONG)
    #IF FEATURE_ENCRYPTION
        LogMsg(LOG_LEVEL_INFO, "encryption active")
        IF mode = 1 THEN
            PRINT "  AES mode"
        ELSE
            PRINT "  XOR mode"
        END IF
    #ELSE
        LogMsg(LOG_LEVEL_INFO, "encryption disabled")
    #ENDIF
    CacheInit()
    CacheFlush()
END SUB

SUB ShowFeatures()
    PRINT "Features:"
    #IF FEATURE_LOGGING
        PRINT "  - LOGGING: level="; CONFIG_LOG_LEVEL
    #ENDIF
    #IF FEATURE_CACHE
        PRINT "  - CACHE: size="; CACHE_SIZE
    #ENDIF
    #IF FEATURE_ENCRYPTION
        PRINT "  - ENCRYPTION: on"
    #ELSE
        PRINT "  - ENCRYPTION: off"
    #ENDIF
    #IF FEATURE_COMPRESSION
        PRINT "  - COMPRESSION: on"
    #ELSE
        PRINT "  - COMPRESSION: off"
    #ENDIF
END SUB

SUB TestLogLevels()
    LogMsg(LOG_LEVEL_ERROR, "error message")
    LogMsg(LOG_LEVEL_WARN, "warning message")
    LogMsg(LOG_LEVEL_INFO, "info message")
    LogMsg(LOG_LEVEL_DEBUG, "debug message")
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== Config Macros Demo ==="
    ShowFeatures()
    TestLogLevels()
    ValidateInput(50, 0, 100)
    ValidateInput(200, 0, 100)
    ProcessData(0)
    PRINT "calc result = "; Calculate(7, 3)
    ProcessCached(BUFFER_SIZE)
    PRINT "buffer size = "; BUFFER_SIZE
    PRINT "sum 1..10 = "; SumRange(10)
    PRINT "average 1..10 = "; Average(10)
    PRINT "hash = "; ComputeHash(100)
    PRINT "=== End Config Macros ==="
    FUNCTION = 0
END FUNCTION
