Thies C . Arntzen <[EMAIL PROTECTED]> wrote:

> Am 04.12.2003 um 15:17 schrieb Leopold Toetsch:

>> Can you use native STRINGs for that?

> sure, if we only knew the type of the variable we're accessing:

> function dump0($a)
> {
> echo printf("%s\n",$a[0]);
> }

> dump0("hallo");  // called with a string
> dump0(array("thies", "arntzen")); // with an array

> would produce (in php):

> h            // string offset
> thies        // array index

> how do you think we should generate "good" imc code for that?

dump0() is obviously non-prototyped (or takes PMCs) which doesn't differ
here. So I would do:

  .sub _subscripted
  .param pmc p
  .param int idx
  does I0, p, "array"
  if I0, handle_array
  .include "pmctypes.pasm"
  typeof I0, p
  eq I0, .PerlString, handle_string
  # unhandled type
  exit 1
  .local pmc elem
handle_array:
  elem = p[idx];
  ...
handle_string:
  $S0 = p
  $S1 = $S0[idx] # or substr $S1, p, idx, 1
  elem = new .PerlString
  elem = $S1
  ...

absolutely untested.

But it seems, that subscripting of string PMCs yields another PMC in the
general case, so its really probably simpler to use your proposed
change. You can easily experiment with custom PMCs, if you use
dynclasses/*.

> re,
> thies

leo

Reply via email to