| 1 | package Inline::MzScheme; | ||
| 2 | $Inline::MzScheme::VERSION = '0.05'; | ||
| 3 | @Inline::MzScheme::ISA = qw(Inline); | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Inline (); | ||
| 8 | use Language::MzScheme (); | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Inline::MzScheme - Inline module for the PLT MzScheme interpreter | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | This document describes version 0.05 of Inline::MzScheme, released | ||
| 17 | June 13, 2004. | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use subs 'perl_multiply'; # have to declare before Inline runs | ||
| 22 | |||
| 23 | use Math::BigInt; | ||
| 24 | use Inline MzScheme => q{ | ||
| 25 | (define (square x) (perl-multiply x x)) | ||
| 26 | (define assoc-list '((1 . 2) (3 . 4) (5 . 6))) | ||
| 27 | (define linked-list '(1 2 3 4 5 6)) | ||
| 28 | (define hex-string (bigint 'as_hex)) | ||
| 29 | }, (bigint => Math::BigInt->new(1792)); | ||
| 30 | |||
| 31 | sub perl_multiply { $_[0] * $_[1] } | ||
| 32 | |||
| 33 | print square(10); # 100 | ||
| 34 | print $hex_string; # 0x700 | ||
| 35 | print $assoc_list->{1}; # 2 | ||
| 36 | print $linked_list->[3]; # 4 | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This module allows you to add blocks of Scheme code to your Perl | ||
| 41 | scripts and modules. | ||
| 42 | |||
| 43 | All user-defined procedures in your Scheme code will be available | ||
| 44 | as Perl subroutines; association lists and hash tables are available | ||
| 45 | as Perl hash refereces; lists and vectors available as array references; | ||
| 46 | boxed values become scalar references. | ||
| 47 | |||
| 48 | Perl subroutines in the same package are imported as Scheme primitives, | ||
| 49 | as long as they are declared before the C<use Inline MzScheme> line. | ||
| 50 | |||
| 51 | Non-word characters in Scheme identifiers are turned into C<_> for Perl. | ||
| 52 | Underscores in Perl identifiers are turned into C<-> for Scheme. | ||
| 53 | |||
| 54 | Additional objects, classes and procedures may be imported into Scheme, | ||
| 55 | by passing them as config parameters to C<use Inline>. See L<Inline> | ||
| 56 | for details about this syntax. | ||
| 57 | |||
| 58 | You can invoke perl objects in Scheme code with the syntax: | ||
| 59 | |||
| 60 | (object 'method arg1 arg2 ...) | ||
| 61 | |||
| 62 | If your method takes named argument lists, this will do: | ||
| 63 | |||
| 64 | (object 'method 'key1 val1 'key2 val2) | ||
| 65 | |||
| 66 | For information about handling MzScheme data in Perl, please see | ||
| 67 | L<Language::MzScheme>. This module is mostly a wrapper around | ||
| 68 | L<Language::MzScheme::scheme_eval_string> with a little auto-binding | ||
| 69 | magic for procedures and input variables. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | # register for Inline | ||
| 74 | sub register { | ||
| 75 | return { | ||
| 76 | language => 'MzScheme', | ||
| 77 | aliases => ['MZSCHEME'], | ||
| 78 | type => 'interpreted', | ||
| 79 | suffix => 'go', | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | |||
| 83 | # check options | ||
| 84 | sub validate { | ||
| 85 | my $self = shift; | ||
| 86 | my $env = $self->{env} ||= Language::MzScheme->new; | ||
| 87 | |||
| 88 | while (@_ >= 2) { | ||
| 89 | my ($key, $value) = (shift, shift); | ||
| 90 | $env->define($key, $value) if $key =~ /^\w/; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # required method - doesn't do anything useful | ||
| 95 | sub build { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | # magic dance steps to a successful Inline compile... | ||
| 99 | my $path = "$self->{API}{install_lib}/auto/$self->{API}{modpname}"; | ||
| 100 | my $obj = $self->{API}{location}; | ||
| 101 | $self->mkpath($path) unless -d $path; | ||
| 102 | $self->mkpath($self->{API}{build_dir}) unless -d $self->{API}{build_dir}; | ||
| 103 | |||
| 104 | # touch my monkey | ||
| 105 | open(OBJECT, ">$obj") or die "Unable to open object file: $obj : $!"; | ||
| 106 | close(OBJECT) or die "Unable to close object file: $obj : $!"; | ||
| 107 | } | ||
| 108 | |||
| 109 | # load the code into the interpreter | ||
| 110 | sub load { | ||
| 111 | my $self = shift; | ||
| 112 | my $code = $self->{API}{code}; | ||
| 113 | my $pkg = $self->{API}{pkg} || 'main'; | ||
| 114 | my $env = $self->{env} ||= Language::MzScheme->new; | ||
| 115 | $env->define_perl_wrappers; | ||
| 116 | |||
| 117 | my %sym = map( | ||
| 118 | ( $_ => 1 ), | ||
| 119 | $env->eval('(namespace-mapped-symbols)') =~ /([^\s()]+)/g | ||
| 120 | ); | ||
| 121 | |||
| 122 | $env->eval($code); | ||
| 123 | |||
| 124 | no strict 'refs'; | ||
| 125 | |||
| 126 | foreach my $sym (sort keys %{"$pkg\::"}) { | ||
| 127 | my $code = *{${"$pkg\::"}{$sym}}{CODE} or next; | ||
| 128 | $sym =~ tr/_/-/; | ||
| 129 | $env->define("$pkg\::$sym", $code) unless $sym{"$pkg\::$sym"}++; | ||
| 130 | $env->define($sym, $code) unless $sym{$sym}++; | ||
| 131 | } | ||
| 132 | |||
| 133 | SYMBOL: | ||
| 134 | foreach my $sym (grep !$sym{$_}, $env->eval('(namespace-mapped-symbols)') =~ /([^\s()]+)/g) { | ||
| 135 | my $obj = $env->lookup($sym); | ||
| 136 | $sym =~ s/\W/_/g; | ||
| 137 | foreach my $type (qw( CODE GLOB )) { | ||
| 138 | $obj->isa($type) or next; | ||
| 139 | *{"$pkg\::$sym"} = $obj->can('to_'.lc($type).'ref')->($obj); | ||
| 140 | next SYMBOL; | ||
| 141 | } | ||
| 142 | *{"$pkg\::$sym"} = \$obj; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | # no info implementation yet | ||
| 147 | sub info { } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ |