3

I tried let stringArr = newvec(12); and then attempted to initialize each spot in the array as such: let stringArr!i = newvec(5); but that returns an error telling me I cannot do that. Is there anyone here who can help me with this dinosaur language?

Barmar
  • 741,623
  • 53
  • 500
  • 612
countofmontecristo
  • 381
  • 3
  • 5
  • 15

2 Answers2

3

You're stretching my memory a bit (to the extent of about three decades) but I seem to recall that let was used only for creating new local variables (also functions and possibly other things, but that's not really relevant for your question).

So the statement let stringArr = newvec(12) is valid in creating the new variable stringArr or, more precisely, a 12-cell anonymous vector and the stringArr variable holding the address of that vector.

However, let stringArr!i = newvec(5) is not valid, because stringArr!i isn't actually a new variable. It's simply the memory contents of cell number i in the already-existing stringArr vector.

In other words, the statement let stringArr = newvec(12) creates both the initial pointer cell and the second layer of pointers, which won't point anywhere useful yet:

+-----------+
| stringArr | ---+
+-----------+    |    +-------------+
                 +--> | stringArr!0 | --> ?
                      +-------------+
                      | stringArr!1 | --> ?
                      +-------------+
                      :      :      :
                      +-------------+
                      | stringArr!N | --> ?
                      +-------------+

And, since the pointers already exist, you shouldn't be using let to set them.

It's similar in C in that you wouldn't write:

int xyzzy[10];        // Make array of ten elements.
int xyzzy[0] = 42;    // Set first element to something.

since the second line isn't supposed to be defining a new variable, rather it's intent is simply to set one of the existing elements to a given value, done with xyzzy[0] = 42.


Hence the right way to do what you're trying to achieve in BCPL is to avoid using the let keyword for that second class of statements:

let stringArr = newvec(12)   // Create new vector AND new variable,
                             //   put vector address into cell for
                             //   that variable.
stringArr!i := newvec(5)     // Create new vector, put vector
                             //   address into EXISTING cell.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

The solution is sound but both of my versions of BCPL (Martin Richard's and Robert Nordier's obcpl) complain about newvec() and also require := rather than = in the second line. I got it working with:

let stringArr = getvec(12)
stringArr!i := getvec(12)

John Boutland

wogsland
  • 9,106
  • 19
  • 57
  • 93
  • Thanks for that edit, wogsland. I didn't know (and still don't) how to get the code in that format. John – John Boutland Jan 25 '17 at 02:35
  • John, I suspect you've probably figured it out in the 3.5 years since you posted this answer :-) But, just in case, you have to ensure there's a blank line before your code and that each line of code starts with four spaces. You can mark a text block and then use CTRL-K to code-indent it. Alternatively, there's a triple-backtick method as well but I never use that so can't advise. – paxdiablo Aug 12 '20 at 00:28