moo2coldc-old.gz

snippets · 2.4 KB · 2010-03-30

MOO to ColdC converter (older version) in Perl.

Download   « Back to the snippets list

#!/u2/consultants/brandon/bin/p
#
# Change the above to your perl, wherever it may reside.
#
##
## MOO -> ColdC convertor, version 1.1
##
## NOTE: THIS DOES NOT PRODUCE COMPLETE OUTPUT, it is intended that this
## be used as a tool to supplement conversion of MOO code to ColdC.
## Because of inherent differences in MOO and ColdC you must read over
## the output and tweak things appropriately.  Keep a close eye on
## object varaibles (MOO Properties).  Encapsulation in ColdC causes
## Conversion to be rather odd.  Commands are also not considered as of
## this release
##
##
## Syntax: moo2coldc < object
##
## Object is the output of a MOO @dump
##
#
# This script was hacked together by a bored befuddled Brandon
# Gillespie sometime around the beginning of September, 1995.

$verb = 0;
%props = ();
%prot = ("parameters", 1, "methods", 1, "code", 1);

%parents = ('$root_class', '$root');

while (<>) {
    if (!$verb) {
        if (/^\@chmod\s+([#\$0-9a-z_]+):(.*)\s*([rxd]*)$/) {
            # command x=method, !x=command, r=['code], !r=[]
            $perms = $3;
            ($perms !~ /r/) && (delete($prot{code}));
        } elsif (/^\@(verb|args)\s+([#\$0-9a-z_]+):(.*)/) {
            $obj = $2;
            $rest = $1;
            if ($line =~ /"/) {
                $rest =~ /"([0-9a-z_\* ]+)"\s+(.*)\s+([rxd]*)\s*$/;
                @names = split(/ /, $1);
                $name = $names[0];
                $args = $2;
                $perms = $3;
                print "// NOTE extra method names: ";
                for (@names[2..$#names]) {
                    print "$_";
                }
            } else {
                $rest =~ /([0-9a-z_\* ]+)\s+(.*)\s+([rxd]*)\s*$/;
                $name = $1;
                $args = $2;
                $perms = $3;
            }
            ($perms !~ /r/) && (delete($prot{code}));
            # I'm at loss for what to do now...
        } elsif (/^\@prop\s+([#\$0-9a-z_]+)\.\"([0-9a-z_]+)\"\s*([{}"0]+)\s*([rc]+)/) {
            $obj = $1;
            $param = $2;
            ($param =~ /_msg$/) && next;
            $data = &moo_2_coldc($3);
            $perms = $3;
            ($perms !~ /r/) && (delete($prot{parameters}));
            ($perms !~ /c/) && 1;   # dunno what to do
            print "\@ap $1,$2 $data\n";
        } elsif (/^\@create\s*(.*)/) {
            @words = split(/ /, $1);
            $parent = $words[0];
            if ($parents{$parent}) {
                $parent = $parents{$parent};
            }
            $name = join(" ",@words[2..$#words]);
            @name = split(/:/, $name);
            print "@create $parent named $name[1]\n";
        } elsif (/^\;([#\$0-9a-z_]+)\.\(\"([0-9a-z_]+)\"\)\s*=\s*(.*)/) {
            $obj = $1;
            $name = $2;
            ($name eq "aliases") && ($name = "name_aliases");
            ($name eq "object_size") && next;
            $val = &moo_2_coldc($3);
            print ";${obj}.set_${name}($val)\n";
        } elsif (/^\@program\s*(.*)/) {
            $line = &moo_2_coldc($1);
            print "\n@program $line\n";
            $verb++;
        } elsif (/^@([0-9a-z_]+)\s+([#\$0-9a-z_]+)\s+is\s+(.*)/) {
            print ";$2.add_message('$1)\n";
            # eventually create a MOO->ColdC message converter
            print ";$2.set_message('$1, $3, $2)\n";
        }
    } else {
        # D'oh, no more verb code.
        if (/^.$/) {
            $verb = 0;
            &convert_verb(*code);
            @code = ();
            print ".\n";
            next;
        } else {
            @code = (@code, $_);
        }
    }
}

sub convert_verb {
    local(*code) = @_;
    local(%vars, $line, $v, $count, $keys);

    # first pass...
    %vars = ();
    for (@code) {
        $line = $_;
        while ($line =~ /([a-z_\*\.]+)\s*=\s*/i) {
            $v = $1;
            $v !~ /\./ && ($vars{$v} = 1) || ($props{$v} = 1);
            $line =~ s/([a-z_\*\.]+)\s*=\s*//i;
        }
    }

    @keys = keys(%vars);
    if ($#keys != -1) {
        $count = 0;

        print "    var ";
        for (@keys) {
            ($count++ == $#keys) && (print "$_;\n\n") || (print "$_, ");
        }
    }

    # actual code conversion
    for (@code) {
        $line = &moo_2_coldc($_);
        print "    $line";
    }
}

sub moo_2_coldc {
    local($_) = @_;

    # get comments out of the way...
    (s/^(\s*)\"\s*(.*)\s*\"\;/$1\/\/ $2/) && (return "$_");

    # Fix lists first...
    s/\{/[/g;
    s/\}/]/g;

    # if we know it will only match once, continue
    (s/^(\s*)else$/${1}} else {/) && (return $_);
    (s/^(\s*)(endif|endfor|endwhile)/${1}}/) && (return $_);

    # control .. things (man am I up too late)
    s/^(\s*)if\s+\((.*)\)\s*/${1}if ($2) {\n/;
    s/^(\s*)elseif\s+\((.*)\)/${1}} else if ($2) {/;
    s/^(\s*)for\s+([a-z_\*])\s+in\s+([\(\[].*[\)\]])\s*/${1}for $2 in $3 {\n/;
    s/^(\s*)while\s+\((.*)\)\s*/${1}while ($2) {\n/;

    # fix 'this'
    s/this:/./g;
    s/this\.//g;
    s/([^a-z_\*])this([^a-z_\*])/${1}this()${2}/g;

    # change obj:verb to obj.method; fix props by making them methods
    s/([#\$0-9a-z_\*]+)\.([a-z_\*]+)/$1.$2()/gi;
    s/([#\$0-9a-z_\*\)]+):([a-z_\*]+)/$1.$2/gi;

    # handle subrange syntax, this doesn't get assignment subranges
    s/([a-z_\*]+)\[([0-9]+)\.\.([0-9]+)\]/${1}.subrange(${2}, ${3})/gi;

    #############################
    # do common verb/method conversions

    #############################
    # do basic function conversions...
    s/typeof\(/type\(/g;
    s/tonum\(/toint\(/g;
    s/length\((.*)\)/${1}.length()/g;
    s/rindex\((.*)\)/${1}.rindex()/g;
    s/match\(/match_regexp(/g;
    s/rmatch\(/match_regexp(/g;
    s/substitute\((.*),\s*(.*)\)/${1}.regexp\(/g;
    s/listinsert\(/insert(/g;
    s/listappend\(/append(/g;
    s/listdelete\(/delete(/g;
    s/listset\(/replace(/g;
    s/chparent\((.*),\s*(.*)\)/${1}.chparents(${2})/g;
    s/parent\((.*)\)/${1}.parents()/g;
    s/children\((.*)\)/${1}.children()/g;
    s/recycle\(/destroy(/g;
    s/max_object\(/next_dbref(/g;
    s/move\((.*)\)/${1}.move_to()/g;

    ## properties/verbs are not the same, PAY ATTENTION
    s/properties\((.*)\)/${1}.parameters()/g;
    s/properties\((.*)\)/${1}.parameters()/g;

    ## Bad
    # s/players\(\)//g;

    return $_;
}

@keys = keys(%props);
if ($#keys > -1) {
    print "\n// Referenced and possibly undefined parameters:\n";
    for (@keys) {
        s/\./,/g;
        print "//     $_\n";
    }
}

@keys = keys(%prot);
if ($#keys > -1) {
    print "\n// Object Protection:\n";
    for (@keys) {
        print "//     \'$_\n";
    }
}

Site Stats

Snippets: 1791
Codebases: 571