dustmop / co2

Lispy language for creating NES / Famicom software
176 stars 7 forks source link

feature request: (as-data) #1

Open dustmop opened 5 years ago

dustmop commented 5 years ago

Would be nice to have a built-in as-data that would add rows to a data segment, returning a handle that can be used later to access it. Example:

  (my-func (as-data 'my-things 1 2 3))
  ...

(defsub (my-func handle)
  (let ((n) (m) (p)
    (set! n (data-access 'my-things handle 0))
    (set! m (data-access 'my-things handle 1))
    (set! p (data-access 'my-things handle 2))
    ...

Which compiles into:

  lda #3
  jsr my_func
  ...

my_things_data_table:
.db 0, 0, 0
.db 1, 2, 3

my_func:
  ldy local_handle
  lda my_things_data_table,y
  sta local_n
  ...  

This is needed for things like opcodes in interpreter tables, and passing large arg lists to certain functions. I have found myself writing half-hearted implementations of this pattern all over the place.