#!/usr/bin/perl -I.

use Dist;
use strict;
use Carp;

sub preshape {
    my @results;
    my $shapes = shift;
    my @shapes = split(/(\s+[-+]\s+)/, $shapes);
    my $s = Dist->new;

    unshift @shapes, ' + ' unless $shapes =~ /^\s+([-+])/;
    while(@shapes) {
	my $expanded;
	my $sexp = shift @shapes;
	my $sign;

	$sexp =~ /\s+([-+])/ or die "BUG analysing $shapes";
	push @results, $sign = " $1 ";

	$s->shape(shift @shapes);
	$expanded = join $sign, $s->to_dealer;
	push @results, $expanded;
    }
    shift @results;
    return join '', @results;
}
	
    

while(<>) {
    s/(shape\s*\{\s*\w+\s*,)([^\}]+)/"$1".&preshape($2)/ge;
    s/shape\s*\{([^\}]+)\}/shape($1)/g;
    s/\#.*$//;
    print;
}

