NX OOP pattern

5

SP4CEBAR 2026-10-10 20:55 (Edited)

object oriented programming

Version 2 below is way better

version 1

(not recommended, use version 2 instead)

the structs array could also live in working ram

n_classes = 1
n_properties = 1
dim global structs(n_classes, n_properties), class_indexes(n_classes)

call my_class(0, 8,9)
call my_class(1, 1,2)

my_class_public_method(0, 5,6)

sub my_class(n, a, b)
  gosub register_class
  gosub load_class

  this_a = a
  this_b = b
  gosub my_private_method

  gosub save_class
end sub

my_private_method:
  print this_a
return

sub my_class_public_method(n, c, d)
  gosub load_class
  this_a = c
  this_b = d
  gosub my_private_method
  gosub save_class
end sub



'vendor

register_class:
  class_indexes(n) = -1
return

load_class:
  gosub validate_class_index
  this_a = structs(n, 0)
  this_b = structs(n, 1)
return

save_class:
  gosub validate_class_index
  structs(n, 0) = this_a
  structs(n, 1) = this_b
return

validate_class_index:
  if not class_indexes(n) then gosub throw_uninitislized_error
return

throw_uninitislized_error:
  error_class_unititialized=n/0
return


SP4CEBAR 2026-10-10 21:33 (Edited)

The above code has the issue that the variable names are fixed, the code below would be more practical

version 2


GOSUB VENDOR
GOSUB MAIN
END

MAIN:
  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
  myclass_public_method(a(), 5,6)
RETURN



'class shape
sub shape(this(), x,y)
  RESTORE 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, x, y)
  RESTORE INDEXES
  gosub myclass_properties
  gosub myclass_construct
end sub

myclass_properties:
  READ p_a
  READ p_b

  'aka "extends shape"
  gosub shape_properties
return

myclass_construct:
  this(p_a) = a
  this(p_b) = b
  gosub myclass_private_method

  'aka "super()"
  gosub shape_construct
end sub

myclass_private_method:
  print this(p_a)
return

sub myclass_public_method(this(), c, d)
  gosub myclass_properties
  this(p_a) = c
  this(p_b) = d
  gosub myclass_private_method
end sub
'END OF MYCLASS


'vendor

VENDOR:
  GOSUB DEFINE_CLASS_size
RETURN

DEFINE_CLASS_size:
  RESTORE INDEXES
  gosub myclass_properties
  READ CLASS_size
  sizeof_MYCLASS = CLASS_size -1

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


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


SP4CEBAR 2026-10-10 22:01 (Edited)

This turned out way better than expected!

However by the time you need OOP to keep your project structurized you probably would soon run out of tokens anyway


SP4CEBAR 2026-10-10 22:26 (Edited)

version 2 - RAM

If you prefer RAM over arrays you could do as below

advantages

gosub vendor
gosub main
end

main:
A = $A000
call myclass(A, 8,9)
return


'class MYCLASS
sub myclass(this, a, b)
  RESTORE INDEXES
  gosub myclass_properties
  gosub myclass_construct
end sub

myclass_properties:
  READ p_a
  READ p_b
return

myclass_construct:
  poke this+p_a, a
  poke this+p_b, b
RETURN
'end of myclass


'vendor
VENDOR:
  GOSUB DEFINE_CLASS_size
RETURN

DEFINE_CLASS_size:
  RESTORE INDEXES
  gosub myclass_properties
  READ CLASS_size
  sizeof_MYCLASS = CLASS_size -1
RETURN

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


qwaffe 2026-10-11 02:47

Thanks, i hate it


SP4CEBAR 2026-10-11 07:33 (Edited)

lol, you should learn to embrace it.
I hated it before I tried it but found that OOP's added structure is really nice for monstrously large projects

The disadvantage is repetitive definitions such as

this->a = a
this->b = b
this->c = c

But I've seen that a new version of PHP has a really nice shorthand way to write this

Refactoring.guru has a really nice catalog of OOP design patterns


SP4CEBAR 2026-10-11 07:41 (Edited)

Three more additions


SP4CEBAR 2026-10-12 20:19 (Edited)

Can arrays and other classes be used as parameters?

array implementation

In the array implementation of classes you could store an array by just reserving a few parameters, so you can just add the index like this(p_myarray + my_index). You just need to make sure it doesn't go out of bounds

The array implementation couldn't really store other objects (an instance of a class is an object).

Ram implementation

The RAM implementation could easily store a pointer (memory address) to another class, for arrays you can malloc a section and store the data type elsewhere, the type tells you the size of each item so you can get the address belonging to an index of the array


SP4CEBAR 2026-10-12 20:22

I think the RAM implementation is the best we have so far


SP4CEBAR 2026-10-12 20:32

Who would like to make an OOP abstraction layer on top of NX's registers?


SP4CEBAR 2026-10-14 15:18 (Edited)

Arrays in RAM

if an array were used as a property of a class, it should be be malloced and their reference (address) should be stored as a class property

peek this+ P_myarray + array_index * size_of_one_item

how to get the length of an array

malloc knows how large a block is so that can be retrieved (I'm not sure if it provides a subprogram to do that yet), then you simply subtract the 2-byte-header from it and divide it by the size_of_one_item


SP4CEBAR 2026-10-14 15:49 (Edited)

datatypes

with this framework, datatypes could be added, but doing so makes it more complicated and loses some of the elegance of the code examples above

type definitions

types could be defined like

global    type_u_int_8,    type_int_8,    type_char
global pointer_u_int_8, pointer_int_8, pointer_char
   type_u_int_8 = 1
pointer_u_int_8 = 1.1
   type_int_8   = 1.01
pointer_int_8   = 1.11
   type_char    = 1.02
pointer_char    = 1.12

global    type_u_int_16,    type_int_16,    type_float_16
global pointer_u_int_16, pointer_int_16, pointer_float_16
   type_u_int_16 = 2
pointer_u_int_16 = 2.1
   type_int_16   = 2.01
pointer_int_16   = 2.11
   type_float_16 = 2.02
pointer_float_16 = 2.12

global    type_u_int_32,    type_int_32,    type_float_32
global pointer_u_int_32, pointer_int_32, pointer_float_32
   type_u_int_32 = 4
pointer_u_int_32 = 4.1
   type_int_32   = 4.01
pointer_int_32   = 4.11
   type_float_32 = 4.02
pointer_float_32 = 4.12

what are pointers

pointers are memory addresses, if you were to look up the address of a pointer, you will find a value of the given type with the size from the pointer type

there's one side note, doing this here will usually get you a part of a header from malloc, see the next section for more details

semantics that allow you to get type details

comparisons like if( my_type_variable = type_u_int_8 ) allow you to check the type. And int(my_type_variable) gets you the size of the type in bytes.

below are more things you can extract (only fraq is an intermediary variable)

size_of_type = int(my_type_variable)
frac = my_type_variable - int(my_type_variable)
type_of_type = int(frac * 10)
index_of_type = (frac * 10 - int(frac * 10)) * 10

type_of_type: 0 are normal types, 1 are pointers to types, 2 are classes, 3 are arrays, 4 are pointers allocated without malloc (for example if it is a single byte or an NX register or it is part of a malloced region or it is a property of another class)

all pointers except the one of type_of_type = 4 assumes that it points to a block in memory that was allocated using malloc which starts with a 2-byte header (holding the size of the block as well as whether it is allocated), so peeking it will get you this header first

why are the sizes in bytes?

The sizes are in bytes for convenience, however this does mean that a variable with a size like 10 bits is unsupported by this format and should use 12 bits (3 bytes). If you need other sizes if you for example want maximum memory efficiency or you want to read files that use such efficient data storage, you should use a type format that stores the sizes in bytes.

class types definition

each class should define its own pointer type. The size encoded in the type should match the size that is allocated for your class

like

class_myclass = 6.2
class_myotherclass = 4.2
class_mythirdclass = 4.21

Assignment of types to class properties

types should be assigned to class properties in the properties subroutine of a class like below

p_my_property = 0
t_my_property = type_u_int_16

p_my_other_property= p_myproperty + t_myproperty
t_my_other_property = type_char

Using types

and then these can be read from and written to ram, but for support for all data types it is recommended to use custom peek and poke subprograms like:

call set(this+ p_myproperty, t_myproperty, value)
call get(this+ p_myproperty, t_myproperty, value)

although, this is more elegant as peek can be used in-line

poke this+ p_myproperty, value
value = peek(this+ p_myproperty)

arrays of a type

Each array property should use a pointer (memory address) version of a type (C also allows you to set a type as a pointer). In C, an array is a pointer so

for example, an array of bytes should use the type pointer_u_int_8 meaning that it holds an address that points to an unsigned integer of size 8

peek this+ p_myarray + int(t_myarray)* array_index

I also added an encoding option for array types (see "semantics that allow you to get type details" above)

optimizations

smarter type definition

global    type_u_int_8,    type_int_8,    type_char
global pointer_u_int_8, pointer_int_8, pointer_char

index = 0
pointer_index = 0
call assign_type   (index        ,    type_u_int_8, 1)
call assign_pointer(pointer_index, pointer_u_int_8, 1)
call assign_type   (index        ,    type_int_8  , 1)
call assign_pointer(pointer_index, pointer_int_8  , 1)
call assign_type   (index        ,    type_char   , 1)
call assign_pointer(pointer_index, pointer_char   , 1)

sub assign_type(index, type_variable, size)
  type_variable = size + index
  add index, 0.01
end sub

sub assign_pointer(index, type_variable, size)
  type_variable = size + index + 0.1
  add index, 0.01
end sub

sub assign_class(index, type_variable, size)
  type_variable = size + index + 0.2
  add index, 0.01
end sub

or a smarter system that unfortunately limits the type sizes

dim global g_type_counts(255), g_pointer_counts(255), g_class_counts(255)

sub assign_type(type_variable, size)
  type_variable = size + g_type_counts(size) * 0.01
  add g_type_counts(size), 1
end sub

sub assign_pointer(type_variable, size)
  type_variable = size + g_pointer_counts(size) * 0.01 + 0.1
  add g_type_counts(size), 1
end sub

sub assign_class(type_variable, size)
  type_variable = size + g_class_counts(size) * 0.01 + 0.2
  add g_type_counts(size), 1
end sub

alternative implementation

types could instead be added as a prefix in RAM this gives the advantage that you can forget about it and let the peek and poke subprograms handle it. But this is wasteful for the memory in my opinion


SP4CEBAR 2026-10-14 16:12 (Edited)

freeing data

the free_object and free_array subprograms need to be defined that look for all pointers inside the properties of a class or the type of an array and free them using the free_array or free_object subprogram depending on the type it found

this is all based on the free function


SP4CEBAR 2026-10-18 17:33 (Edited)

Improved Assignment of types to class properties

Call new_class

T_my_property = type_u_int_16
P_my_property = g_property_index
call add_property(T_my_property)


'NX++ modules

sub new_class
  g_property_index = 0
end sub

sub add_property(type)
  size = int(type)
  add g_property_index, size
end sub


SP4CEBAR 2026-10-18 17:39 (Edited)

Type building examples

T_my_property = pointer_to_value + type_u_int_8

t_my_array = type_u_int_8
call to_array_type(t_my_array, 6)


'NX++ modules

sub to_array_type(type, array_length)
  value_size = int(type)
  value_type_part = type - value_size
  type = pointer_to_array +value_type_part +value_size *array_length
end sub

It would be cool if it could support pointers (ram address) to another pointer to another pointer to a value like:

T_my_property = pointer_to_value *3 + type_u_int_8


SP4CEBAR 2026-10-24 18:38 (Edited)

version 2.3 Ram arrays

Like the RAM version but uses one big data array instead of RAM.

Advantages

Practical

disadvantages


Log in to reply.