OpenCores
URL https://opencores.org/ocsvn/socgen/socgen/trunk

Subversion Repositories socgen

[/] [socgen/] [trunk/] [tools/] [math/] [perl_arith] - Blame information for rev 134

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 134 jt_eaton
#!/usr/bin/perl
2
use strict;
3
use warnings;
4
use Data::Dumper;
5
# this code can be re-written without any regex/eval,their use is purely for shortening the code
6
 
7
 
8
use constant BASE_PRIORITY =>
9
{
10
        NUMBER          => 2,
11
        OPEN_PARA       => 8,
12
        CLOSED_PARA => 8,
13
        ADD                     => 4,
14
        SUB                     => 4,
15
        MUL                     => 7,
16
        DIV                     => 7,
17
        POW                     => 9,
18
};
19
 
20
use constant DEPTH_BONUS => 10;
21
 
22
my $exp = $ARGV[0];
23
 
24
#my $exp = "5*(12/(32+4))-10";
25
#my $exp = "3**(6-1*4)**2";
26
 
27
#my $exp = -3+(-1-2);
28
#my $exp = (3-(4+6))/2;
29
 
30
my $depth = 0;
31
my @terms;
32
 
33
 
34
 
35
sub delete_at { # delete the term at the index equal to the parameter given to this function
36
        return shift @terms if $_[0] == 0;
37
        return pop @terms if $_[0] == (@terms-1);
38
        my $ret = $terms[ $_[0] ];
39
        @terms = (
40
                @terms[0..$_[0]-1],
41
                @terms[$_[0]+1..@terms-1],
42
        );
43
        return $ret;
44
}
45
 
46
sub insert_at { # inserts a term exactly before the index given as parameter
47
        @terms = (
48
                @terms[0..$_[0]-1],
49
                $_[1],
50
                @terms[$_[0]..@terms-1],
51
        );
52
 
53
}
54
 
55
 
56
sub firstPass {# this builds up the @terms for later use
57
        while( $exp =~ s/^(\-?\d+|\*\*|\*|\/|\+|\-|\(|\))// ) {
58
                my $type=$1;
59
                my $term=$1;
60
                if( @terms>0 &&  $terms[@terms - 1 ]->{type} eq 'NUMBER' && $term =~ /\-\d+/ ) {
61
                        #see if we currently have a negative number,see if before we had a number
62
                        #this means that we're on the wrong track and that - is actually an operator here
63
                        #and not the sign for a negative number
64
                        $exp=$term.$exp;
65
                        $exp=~s/^-//;
66
                        $type = "SUB";
67
                        $term = '-';
68
        #               print "EXP $exp \n";
69
                } else {
70
                        $type =~ s/\-?\d+/NUMBER/;
71
                };
72
                $type =~ s/\(/OPEN_PARA/;
73
                $type =~ s/\)/CLOSED_PARA/;
74
                $type =~ s/\+/ADD/;
75
                $type =~ s/\*\*/POW/;
76
                $type =~ s/\*/MUL/;
77
                $type =~ s/\//DIV/;
78
                $type =~ s/\-$/SUB/;
79
                my ($is_term_para) = $type =~ /OPEN_PARA|CLOSED_PARA/; # this flag will tell us wether the term is or is not a paranthesis
80
                $depth++ if $type eq 'OPEN_PARA';  # if we encounter an open paranthesis we increase depth
81
                $depth-- if $type eq 'CLOSED_PARA';# closed paranthesis we decrease it
82
                push @terms,
83
                {
84
                        type            => $type,
85
                        term            => $term,
86
                        priority        => BASE_PRIORITY->{$type} + $depth*DEPTH_BONUS
87
                }
88
                unless $is_term_para; # we leave out the paranthesis because we no longer need them(their purpose
89
                                                          # was to provide priority information for us)
90
        };
91
}
92
 
93
 
94
sub getPrioritary { # gets most prioritary 3 elements in the current expression
95
 
96
        my @sIndexes = sort { -1 * ( $terms[$a]->{priority} <=> $terms[$b]->{priority} ); } 0..(@terms-1) ;
97
 
98
        my $i = 0; # the index in @sIndexes
99
        my $middleMaxPrio = $sIndexes[$i];
100
 
101
        while( $terms[$middleMaxPrio]->{type} eq 'NUMBER' ) { # if our selected maximum priority element is not a number search for the next most prioritized that is a number
102
#               print "[DEBUG] $terms[$middleMaxPrio]->{type}";
103
                $middleMaxPrio = $sIndexes[++$i];
104
        };
105
 
106
        my $leftNearMax   = $middleMaxPrio -1; # we take the left of $middleMaxPrio
107
        my $rightNearMax  = $middleMaxPrio +1; # and the right of it , becuase these two are surely operands
108
 
109
        my @selectedTerms = map { delete_at $_  } ( $rightNearMax , $middleMaxPrio , $leftNearMax ); # we delete them in inverse order to not alter the stack badly
110
 
111
        return {
112
                selected        => [ @selectedTerms ],
113
                insertIndex     => $leftNearMax,
114
                maxPriority     => $selectedTerms[1]->{priority}, # the middle element will be surely an operator so it will have maximum priority
115
        };
116
}
117
 
118
 
119
#---------------------------------------------------------------------------------------------------------------------
120
 
121
firstPass;
122
 
123
while( @terms > 1 ) {
124
#       print "DEBUG ".Dumper [@terms];
125
 
126
        my $data = getPrioritary;
127
        my @elems = map { $_->{term} } @{ $data->{selected} };
128
        my $expr = sprintf "%s %s %s", reverse @elems;
129
        my $result = eval($expr); # the eval here has just been used for shortening the code,it could have very well been a simple switch on $elems[1]
130
 
131
#       print "DEBUG [$expr]\n";
132
 
133
        insert_at
134
        $data->{insertIndex},
135
        {
136
                type    => 'NUMBER',
137
                term    => $result,
138
                priority=> $data->{maxPriority} - DEPTH_BONUS #we have calculated what was probably a paranthesis therefore we substrac a depth_bonus
139
        };
140
 
141
};
142
 
143
 
144
my $result_out =$terms[0]->{term};
145
 
146
print "$result_out\n";
147
 
148
 
149
 
150
 
151
1

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.