Hacker News new | ask | show | jobs
by panic 3358 days ago
I reduced the need for escaping further by having a backslash escape a whole series of backslashes rather than just the very next character.

How do you write the string with characters '\' and ']'? It seems like the natural way to write it, [\\\]], would end up being lexed as [\\\] (a string with two backslashes) followed by an unmatched string end character ']'.

1 comments

It's just [\\]]. "\\]" is an escape for "\]".
I took this as an opportunity to finally try out mu. Very readable code!

Is there a way to write a string that ends with a backslash? The way `slurp_one_past_backslashes` is used seems to make that impossible.

EDIT: I thought typing a literal 0 byte might work, but apparently mu uses a different mechanism to delimit strings in memory.

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.
Ah, you're right! Trailing backslashes can't be represented. I need to rethink this.

If you want to construct it from scratch, strings and arrays in general are prefixed with their length. put-index on the final index would be the workaround.