

Task
Produce a graphical or ASCII-art representation of a Hilbert curve of at least order 3.

DEFINE MAXSIZE="12"
 
INT ARRAY
  dxStack(MAXSIZE),dyStack(MAXSIZE)
BYTE ARRAY
  depthStack(MAXSIZE),stageStack(MAXSIZE)
BYTE stacksize=[0]
 
BYTE FUNC IsEmpty()
  IF stacksize=0 THEN RETURN (1) FI
RETURN (0)
 
BYTE FUNC IsFull()
  IF stacksize=MAXSIZE THEN RETURN (1) FI
RETURN (0)
 
PROC Push(INT dx,dy BYTE depth,stage)
  IF IsFull() THEN Break() FI
  dxStack(stacksize)=dx dyStack(stacksize)=dy
  depthStack(stacksize)=depth
  stageStack(stackSize)=stage
  stacksize==+1
RETURN
 
PROC Pop(INT POINTER dx,dy BYTE POINTER depth,stage)
  IF IsEmpty() THEN Break() FI
  stacksize==-1
  dx^=dxStack(stacksize) dy^=dyStack(stacksize)
  depth^=depthStack(stacksize)
  stage^=stageStack(stacksize)
RETURN
 
PROC DrawHilbert(INT x BYTE y INT dx,dy BYTE depth)
  BYTE stage
 
  Plot(x,y)
  Push(dx,dy,depth,0)
 
  WHILE IsEmpty()=0
  DO
    Pop(@dx,@dy,@depth,@stage)
    IF stage<3 THEN
      Push(dx,dy,depth,stage+1)
    FI
    IF stage=0 THEN
      IF depth>1 THEN
        Push(dy,dx,depth-1,0)
      FI
    ELSEIF stage=1 THEN
      x==+dx y==+dy
      DrawTo(x,y)
      IF depth>1 THEN
        Push(dx,dy,depth-1,0)
      FI
    ELSEIF stage=2 THEN
      x==+dy y==+dx
      DrawTo(x,y)
      IF depth>1 THEN
        Push(dx,dy,depth-1,0)
      FI
    ELSEIF stage=3 THEN
      x==-dx y==-dy
      DrawTo(x,y)
      IF depth>1 THEN
        Push(-dy,-dx,depth-1,0)
      FI
    FI
  OD
RETURN
 
PROC Main()
  BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
 
  Graphics(8+16)
  Color=1
  COLOR1=$0C
  COLOR2=$02
 
  DrawHilbert(64,1,0,3,6)
 
  DO UNTIL CH#$FF OD
  CH=$FF
RETURN