civboot / fngi

a readable language that grows from the silicon
The Unlicense
60 stars 3 forks source link

Specialized Arr, No generics (in bootstrap) #23

Closed vitiral closed 1 year ago

vitiral commented 1 year ago

I've been rethinking generics (#17) and I don't think they should be in the language.

Before I go into reasons, let me overview some of the design thoughts here:

Requiring generics for Arr types basically meant an explosion of complexity in the bootstrapping compiler, and I was starting to get really sad about it. To make matters worse, I felt that to have generics we should also have the "better names" using [...] -- but that requires many things -- including another BBA to store the types we find, specialized BST methods (not hard but definitely more complexity) and increased memory usage on every node, etc.

Arr types

An Arr type is just a contiguous memory region of zero or more of a specific type. Usage includes:

\ Usage in a struct with unknown size
struct CStr [ U1 len; Arr[? U1] dat]

\ Usage as a local with known size for demo purposes
fn foo do (
  var s: Arr[12, U1]
  var h: Str = |hello world| \ demo of some memory to copy
  ptr.move(s, h.dat, h.len)
  StdOut.print(Str(s, h.len))
)

I realized that the Arr type can very easily be a "specialized" type. Basically: Arr can be a specialized type because pointers/references are already specialized types. Only a small amount of additional logic needs to be added to support arrays.

Other types (Str for instance) can implement methods -- but under the hood they will all use pointer arithmetic on an Arr field.