' L-system fractal drawing: tree, snowflake, dragon curve rules
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL iter  AS LONG
    LOCAL numIterations  AS LONG
    LOCAL ruleIdx  AS LONG
    LOCAL numRules  AS LONG
    LOCAL strLen  AS LONG
    LOCAL axiom AS STRING
    LOCAL production AS STRING
    LOCAL current AS STRING
    LOCAL nextStr AS STRING
    LOCAL ch AS STRING
    LOCAL angle  AS LONG
    LOCAL x  AS LONG
    LOCAL y  AS LONG
    LOCAL dir  AS LONG
    LOCAL xPos  AS DOUBLE
    LOCAL yPos  AS DOUBLE
    LOCAL dirAngle AS DOUBLE
    LOCAL lenScale  AS DOUBLE
    LOCAL lenVal  AS DOUBLE
    LOCAL stackX(100) AS DOUBLE
    LOCAL stackY(100) AS DOUBLE
    LOCAL stackDir(100) AS DOUBLE
    LOCAL stackPtr AS LONG
    LOCAL rules(10) AS STRING
    LOCAL numRulesDefined AS LONG
    LOCAL testIdx AS LONG

    PRINT "=== L-System Fractal Generator ==="
    PRINT

    numRulesDefined = 3
    PRINT "Available L-Systems:"
    PRINT "  1. Tree (binary branching)"
    PRINT "  2. Koch Snowflake"
    PRINT "  3. Dragon Curve"
    PRINT

    PRINT "--- L-System 1: Tree ---"
    PRINT "  Axiom: F"
    PRINT "  Rule: F -> FF+[+F-F-F]-[-F+F+F]"
    PRINT

    axiom = "F"
    strLen = LEN(axiom)
    PRINT "  Iteration 0: " & axiom
    PRINT "  Length: " & STR$(strLen)

    current = axiom
    FOR iter = 1 TO 3
        nextStr = ""
        FOR i = 1 TO LEN(current)
            ch = MID$(current, i, 1)
            IF ch = "F" THEN
                nextStr = nextStr + "FF+[+F-F-F]-[-F+F+F]"
            ELSE
                nextStr = nextStr + ch
            END IF
        NEXT i
        current = nextStr
        strLen = LEN(current)
        PRINT "  Iteration " & FORMAT$(iter) & ": length=" & STR$(strLen)
        IF strLen < 60 THEN
            PRINT "    " & LEFT$(current, 60)
        END IF
    NEXT iter
    PRINT

    PRINT "--- L-System 2: Koch Snowflake ---"
    PRINT "  Axiom: F++F++F"
    PRINT "  Rule: F -> F-F++F-F"
    PRINT

    axiom = "F++F++F"
    PRINT "  Iteration 0: " & axiom
    current = axiom
    FOR iter = 1 TO 3
        nextStr = ""
        FOR i = 1 TO LEN(current)
            ch = MID$(current, i, 1)
            IF ch = "F" THEN
                nextStr = nextStr + "F-F++F-F"
            ELSE
                nextStr = nextStr + ch
            END IF
        NEXT i
        current = nextStr
        strLen = LEN(current)
        PRINT "  Iteration " & FORMAT$(iter) & ": length=" & STR$(strLen)
    NEXT iter
    PRINT

    PRINT "--- L-System 3: Dragon Curve ---"
    PRINT "  Axiom: FX"
    PRINT "  Rules: X -> X+YF+, Y -> -FX-Y"
    PRINT

    axiom = "FX"
    current = axiom
    FOR iter = 1 TO 4
        nextStr = ""
        FOR i = 1 TO LEN(current)
            ch = MID$(current, i, 1)
            IF ch = "X" THEN
                nextStr = nextStr + "X+YF+"
            ELSE
                IF ch = "Y" THEN
                    nextStr = nextStr + "-FX-Y"
                ELSE
                    nextStr = nextStr + ch
                END IF
            END IF
        NEXT i
        current = nextStr
        strLen = LEN(current)
        PRINT "  Iteration " & FORMAT$(iter) & ": length=" & STR$(strLen)
    NEXT iter
    PRINT

    PRINT "--- L-System Symbols ---"
    PRINT "  F = Draw forward"
    PRINT "  + = Turn right (25 degrees)"
    PRINT "  - = Turn left (25 degrees)"
    PRINT "  [ = Push state to stack"
    PRINT "  ] = Pop state from stack"
    PRINT "  X, Y = Control variables (no draw)"
    PRINT

    PRINT "--- Turtle State Simulation ---"
    PRINT "  Angle increment: 25 degrees"
    PRINT "  Forward length: 10 units"
    PRINT "  Stack depth: up to 100"
    PRINT "  Initial pos: (0, 0) facing up"

    PRINT
    PRINT "L-system fractal generator complete."

    FUNCTION = 0
END FUNCTION
