1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
# Copyright (C) 2009, Parrot Foundation.
# interlangs.pir
# An example of language interoperability
# First build perl6, ecmascript and pipp
# Then do:
# ../../parrot -L /yourparrotdir/languages/rakudo \
# -L /yourparrotdir/languages/ecmascript \
# -L /yourparrotdir/languages/pipp \
# interlangs.pir
#-----------------------------------------------------------------------
.sub main :main
.local pmc perl6func, jsfunc, pippfunc
say 'Loading languages and compiling...'
# Compile functions
perl6func = get_perl6_func()
jsfunc = get_js_func()
pippfunc = get_pipp_func()
# Call js and pipp functions directly
say "\nDirect calls\n"
jsfunc('pir')
pippfunc('pir')
# Pass js and pipp functions to perl6 function
say "\nCalls from perl6\n"
$S1 = perl6func('pir', jsfunc)
print 'Returned: '
say $S1
$S1 = perl6func('pir', pippfunc)
print 'Returned: '
say $S1
say "\nBye!"
.end
#-----------------------------------------------------------------------
# Compile perl6 code that return a function,
# execute it, and return the result.
.sub get_perl6_func
load_bytecode 'perl6.pbc'
.local pmc compiler, code, function
compiler = compreg 'Perl6'
code = compiler.'compile'(<<'ENDCODE')
sub ($a, $b)
{
$b('perl6->' ~ $a);
'Hello from a perl6 sub, ' ~ $a;
};
ENDCODE
function = code()
.return(function)
.end
#-----------------------------------------------------------------------
# Compile ecmascript code that define a function,
# execute it and get the function from the
# js namespace.
.sub get_js_func
load_bytecode 'js.pbc'
.local pmc compiler, code, block, ns, function
compiler = compreg 'JS'
code = compiler.'compile'(<<'JSCODE')
function myecmascriptfunc(n)
{
print ('Hello from ecmascript,', n);
}
JSCODE
block = code()
ns = get_root_global 'js'
function = ns['myecmascriptfunc']
.return(function)
.end
#-----------------------------------------------------------------------
# Compile php code that define a function,
# and get the function from the pipp
# namespace
.sub get_pipp_func
load_bytecode 'pipp.pbc'
.local pmc compiler, code, ns, function
compiler = compreg 'Pipp'
code = compiler.'compile'(<<'PIPPCODE')
<?php
function phpfunc($msg)
{
echo "Hello from pipp, $msg\n";
}
?>
PIPPCODE
ns = get_root_global 'pipp'
function = ns['phpfunc']
.return(function)
.end
#-----------------------------------------------------------------------
# That's all folks!
########################################################################
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
|