Hello,


I've been playing with closures and subs but I have a little bit of trouble with those.
I'm a bit confused by all different flavours of syntax, I've been trying to read and understand all documents concerning this subject, the most useful was reading the test scripts. However, that is PASM, and I like PIR better :-).


A general question is: what syntax should one use in what context?
(I understand that PCC should be used whenever one wants to interact with other languages. Also, calling functions can be done in many ways)


A more specific question is this:
I'm trying to create a closure, and I think I've implemented this code (it's lua code, but I'm not sure if it's 100% correct,
anyway, I'm trying to get a feeling for how one should translate this). So, actually I have 2 questions;


(1): did I translate the function below correctly (I think the Lua syntax won't be a problem, it's quite straightforward).
(2): what is causing my strange result (see below).


function main()
local p = 123; local q = 345;


   foo(q);
   foo(q);

   function foo(a) # nested function, it does have access to p in "main"
      print(p);
      p = p + p;

      print(typeof(a));
      print(a);
   end
end

I would translate this to this PIR code:

.sub _main
       new_pad 0

       # local p = 123
       .local pmc p
       p = new .PerlInt
       p = 123
       store_lex 0, "p", p

       # local q = 345
       .local pmc q
       q = new .PerlInt
       q = 345
       store_lex 0, "q", q

       newsub $P0, .Closure, _foo

# foo(q)
.arg q $P0()


       # foo(q)
       .arg q
       $P0()

       end
.end

.sub _foo
.param pmc a
# print(p);
find_lex $P0, "p"
print $P0


       # p = p + p;
       $P0 = $P0 + $P0
       store_lex "p", $P0

# print(typeof(a));
$S0 = typeof a print $S0 # ????
# print(a);
print a # ERROR!
.end


My problem with the last 2 statements is:
- the statement print(typeof(a)); print "SArray", which I don't understand.

- "print a" causes an error: "get_string() not implemented in SArray" ( I know that is because of a being an SArray)


I know there are more important issues at the moment, but if someone could spare a moment, that'd be great. I'm trying to get a grip on function syntax.


Regards,

Klaas-Jan Stol






Reply via email to