Hacker News new | ask | show | jobs
by akkartik 3357 days ago
To clarify my comment from last night: the following code[1]:

  x:address:array:char <- new [abc]
would look like this in memory, assuming the allocator returned address 1000 as the value of x:

  1000: 3
  1001: 97  # a
  1002: 98  # b
  1003: 99  # c
That's it. There's no trailing null character. Address 1004 is not part of this allocation.

The length of the array cannot be modified, only read (using instruction length). If we need a larger array we must allocate new and copy over, just like in C.

The elements of an array can be read and written using instructions index and put-index, respectively. Here's a fragment of code to make the final character of x a backslash:

  len:num <- length *x
  last:num <- subtract len, 1
  put-index *x, last, 92  # ascii code for backslash
[1] The verbose "address:array:char" (read as "address to an array of characters") is typically abbreviated as "text" in Mu programs. I've written out the full type to make things more explicit.