Skip to content

Instantly share code, notes, and snippets.

@kidd kidd/generlator.pl
Created Mar 11, 2010

Embed
What would you like to do?
#!/usr/bin/perl
#Raimon Grau Cuscó <raimonster AT gmail DOT com>
use strict;
use warnings;
use Data::Dumper;
=head2 selectSignature
returns the arguments to io:format to print the actual parameters
of the function
=over 4
=item Arguments
$fun is the function signature. For example:
a() , a(X) or a({X,[1 ,2 , B] , atom} , hello)
=back
=cut
sub selectSignature {
my ($fun) =@_;
return qq|"$fun~n"| if ($fun =~ m/\(\s*\)/); # no arguments
#strip fun name and parameters
(my $signature = $fun) =~s/.*\(/\(/;
$signature =~ s/\).*/\)/;
my $complSig = $signature;
$signature =~ s/\[[^]]*\]//g;
$signature =~ s/\{[^]]*\}//g;
return qq|"$fun ~w ~n",[$complSig]| if ($signature !~ /,/); # 1 parameter (no commas)
return "$fun ". ('~w ' x split(/,/ , $signature)) . ", [ $complSig ]"; #many params
}
#gets the states and transitions:
# state | message | nextState
my $funs;
my $first = undef;
while (<DATA>) {
chomp;
my @parts = split /\s*\|\s*/;
$funs->{$parts[0]}->{$parts[1]} = $parts[2] ;
$first ||= $parts[0];
}
print <<"EOF";
-module(test).
-export([start/0, f0/0]).
start () ->
Pid = spawn_link(test,f0,[]),
loop (Pid).
loop (Pid) ->
Pid ! list_to_atom(string:strip(io:get_line ("msg: "),right, \$\\n)),
loop (Pid).
f0()-> $first .
EOF
for my $f (keys %$funs) {
my $sign = selectSignature($f);
my $code = <<EOC;
$f ->
\tio:format($sign),
\treceive
EOC
for (keys %{$funs->{$f}}) {
$code .= "\t\t$_ -> " . $funs->{$f}->{$_} . ";\n";
}
# chomp $code;
# chop $code;
$code .= "\t\t_ -> $f";
$code .= "\n";
$code.= "\tend.\n";
print $code;
}
__DATA__
a() | m | b()
a() | accum | a(0)
a(X) | add | a(X+1)
a(X) | sub | a(X-1)
a(X) | quit | a()
a() | caca | c()
b() | ma | c()
c() | quit | notexists:notexists()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.