GOSUB SETUP_LIBRARIES
GOSUB MAIN
END

MAIN:
  GLOBAL G_CLASS_SHAPE$, G_CLASS_MYCLASS$
  dim a(sizeof_MYCLASS), b(sizeof_MYCLASS)

  'create two new instances of myclass
  call myclass(a(), 8,9, 1,5)
  call myclass(b(), 1,2, 5,3)

  'execute a public method on the first instance of myclass
  call myclass_publ_method(a(), 5,6)

  IS_INSTANCE_OF = 0
  call NXS_INSTANCE_OF( IS_INSTANCE_OF, G_CLASS_SHAPE$, "FORM" )
  PRINT "SHAPE:", IS_INSTANCE_OF,
  call NXS_INSTANCE_OF( IS_INSTANCE_OF, G_CLASS_MYCLASS$, "FORM" )
  PRINT "MYCLASS:", IS_INSTANCE_OF
RETURN



'class shape
  sub shape(this(), x,y) 
    G_CLASS_SHAPE$ = "SHAPE"
    CALL NXS_IMPLEMENTS( G_CLASS_SHAPE$, "FORM" )

    RESTORE NXS_INDEXES
    gosub shape_properties
    gosub shape_construct
  end sub

  shape_properties:
    READ p_x
    READ p_y
  return

  shape_construct:
    this(p_x) = x
    this(p_y) = y
  return
'end of shape



'class MYCLASS
  sub myclass(this(), a, b, c, x, y)
    G_CLASS_MYCLASS$ = "MYCLASS"

    RESTORE NXS_INDEXES
    gosub myclass_properties
    gosub myclass_construct
  end sub

  myclass_properties:
    'myclass extends shape
    gosub shape_properties

    READ p_a
    READ p_b

    ' array with ubound 15 (size 16)
    READ P_MY_ARRAY
    CALL NXS_PROP_ADD_DIM( 15 )

    READ p_c
  return

  myclass_construct:
    'super method call
    gosub shape_construct

    this(p_a) = a
    this(p_b) = b
    this(p_c) = c
    gosub myclass_priv_method
  return

  myclass_priv_method:
    print this(p_a), this(p_b), this(p_c)
  return

  sub myclass_publ_method(this(), a, b)
    RESTORE NXS_INDEXES
    gosub myclass_properties
  
    this(p_a) = a
    this(p_b) = b
    gosub myclass_priv_method
  end sub
'END OF MYCLASS



'LIBRARIES
SETUP_LIBRARIES:
  GOSUB nxs_set_CLASS_sizes
RETURN



'NX#

SUB NXS_IMPLEMENTS( CLASS_NAME$, INTERFACE_NAME$ )
  R_IS_INSTANCE_OF = 0
  call NXS_INSTANCE_OF( R_IS_INSTANCE_OF, CLASS_NAME$, INTERFACE_NAME$ )

  if R_IS_INSTANCE_OF then 
    return
  end if

  CLASS_NAME$ = CLASS_NAME$ +"_"+ INTERFACE_NAME$
END SUB

SUB NXS_INSTANCE_OF( R_IS_INSTANCE_OF, CLASS_NAME$, INTERFACE_NAME$ )
  index = INSTR( CLASS_NAME$, INTERFACE_NAME$ )
  R_IS_INSTANCE_OF = index <> 0
END SUB

SUB NXS_COPY_OBJECT( HOST_ARRAY(), PROPERTY, OBJECT_ARRAY() )
  CALL NXS_COPY_ARRAY( HOST_ARRAY(), PROPERTY, OBJECT_ARRAY() )
END SUB

SUB NXS_COPY_ARRAY( HOST_ARRAY(), OFFSET, DATA_ARRAY() )
  if OFFSET + UBOUND( DATA_ARRAY ) > UBOUND( HOST_ARRAY ) THEN
    THROW_ERROR$ = "NXS_COPY_ARRAY OVERFLOW" +str$(1/0)
  END IF

  FOR I=0 TO UBOUND( DATA_ARRAY )
    HOST_ARRAY( I +OFFSET ) = DATA_ARRAY( I )
  NEXT I
END SUB


'NX# PROPERTIES

SUB NXS_PROP_ADD_SIZE( PROPERTY_SIZE )
  CALL NXS_ADD_READHEAD( PROPERTY_SIZE -1 )
END SUB
    
SUB NXS_PROP_ADD_DIM( PROPERTY_UBOUND )
  CALL NXS_PROP_ADD_SIZE( PROPERTY_UBOUND -1 )
END SUB



'NX# SETUP

nxs_set_CLASS_sizes:
  RESTORE nxs_INDEXES
  gosub myclass_properties
  READ CLASS_size
  sizeof_MYCLASS = CLASS_size -1

  RESTORE nxs_INDEXES
  gosub shape_properties
  READ CLASS_size
  sizeof_shape = CLASS_size -1
RETURN

    
    
'NX# UTILITY

SUB NXS_ADD_READHEAD( ADD_SIZE )
  FOR I=0 TO ADD_SIZE
    READ V
  NEXT I
END SUB

NXS_INDEXES:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20