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

Subversion Repositories socgen

[/] [socgen/] [trunk/] [tools/] [math/] [lib.pm.safe] - Blame information for rev 134

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 134 jt_eaton
#/**********************************************************************/
2
#/*                                                                    */
3
#/*             -------                                                */
4
#/*            /   SOC  \                                              */
5
#/*           /    GEN   \                                             */
6
#/*          /    TOOL    \                                            */
7
#/*          ==============                                            */
8
#/*          |            |                                            */
9
#/*          |____________|                                            */
10
#/*                                                                    */
11
#/*                                                                    */
12
#/*                                                                    */
13
#/*  Author(s):                                                        */
14
#/*      - John Eaton, jt_eaton@opencores.org                          */
15
#/*                                                                    */
16
#/**********************************************************************/
17
#/*                                                                    */
18
#/*    Copyright (C) <2010-2015>                */
19
#/*                                                                    */
20
#/*  This source file may be used and distributed without              */
21
#/*  restriction provided that this copyright statement is not         */
22
#/*  removed from the file and that any derivative work contains       */
23
#/*  the original copyright notice and the associated disclaimer.      */
24
#/*                                                                    */
25
#/*  This source file is free software; you can redistribute it        */
26
#/*  and/or modify it under the terms of the GNU Lesser General        */
27
#/*  Public License as published by the Free Software Foundation;      */
28
#/*  either version 2.1 of the License, or (at your option) any        */
29
#/*  later version.                                                    */
30
#/*                                                                    */
31
#/*  This source is distributed in the hope that it will be            */
32
#/*  useful, but WITHOUT ANY WARRANTY; without even the implied        */
33
#/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR           */
34
#/*  PURPOSE.  See the GNU Lesser General Public License for more      */
35
#/*  details.                                                          */
36
#/*                                                                    */
37
#/*  You should have received a copy of the GNU Lesser General         */
38
#/*  Public License along with this source; if not, download it        */
39
#/*  from http://www.opencores.org/lgpl.shtml                          */
40
#/*                                                                    */
41
#/**********************************************************************/
42
 
43
use strict;
44
use warnings;
45
 
46
package math::lib;
47
 
48
#/*********************************************************************************************/
49
#/                                                                                            */
50
#/  Arithmetic parser                                                                         */
51
#/                                                                                            */
52
#/  parse a line and replace all occurrences of symbol with its value                         */
53
#/                                                                                            */
54
#/  my $line = math::lib::parse($line,$symbol,$value);                                        */
55
#/                                                                                            */
56
#/                                                                                            */
57
#/*********************************************************************************************/
58
 
59
 
60
sub parse {
61
    my $line  = shift;
62
    my $symbol  = shift;
63
    my $value  = shift;
64
 
65
    my $len = length $line;
66
    my $state         = "math";
67
    my $start_sym     = -1;
68
    my $stop_sym      = -1;
69
    my $match         = "___";
70
    my $math_only     = 0;
71
 
72
 
73
 
74
 
75
#    print " $line $symbol $value $len  \n";
76
 
77
    for (my $i=0; $i < $len; $i++)
78
        {
79
        my $char = substr($line,$i,1);
80
        $_ = $char;
81
 
82
 
83
        unless ($state eq "constant")
84
               {
85
               if(/[a-zA-Z_]/)
86
                 {
87
                 $state = "symbol";
88
                 $math_only = $math_only +1;
89
                 if($start_sym eq "-1") {$start_sym = $i;}
90
                 }
91
               }
92
        if(/[0-9+-\/]/)
93
          {
94
          if($state eq "symbol") {$stop_sym = $i;}
95
          $state = "math";
96
          }
97
 
98
        if(/[\*]/)
99
          {
100
          if($state eq "symbol")            {    $stop_sym = $i;}
101
          $state = "math";
102
          }
103
 
104
 
105
        if(/['`]/)
106
          {
107
          $state = "constant";
108
          $math_only = $math_only +1;
109
          }
110
 
111
        if(($state ne "symbol") && (($stop_sym ne "-1")  ) )
112
          {
113
          my $str_len  = $stop_sym-$start_sym;
114
          my $substring = substr($line,$start_sym,$str_len);
115
          if($substring eq $symbol)
116
            {
117
            $match = "MATCH";
118
            $math_only = $math_only - $str_len;
119
            my $new_line = "";
120
            my $end_line = "";
121
 
122
            if($start_sym  eq "0") { $new_line  =$value; }
123
            else
124
               {
125
               $new_line  = substr($line,0,$start_sym);
126
               $new_line  ="${new_line}${value}";
127
               }
128
            my $line_len = $len - $stop_sym;
129
            $end_line = substr($line,$stop_sym,$line_len);
130
            $new_line  ="${new_line}${end_line}";
131
#            print "$substring $stop_sym  $start_sym  $str_len $match ==  $new_line  \n";
132
            $new_line = parse($new_line,$symbol,$value) ;
133
            return($new_line);
134
            }
135
          else
136
            {
137
            $match = "NO_MATCH";
138
#            print "$substring $stop_sym  $start_sym  $str_len $match \n";
139
            }
140
          $start_sym  = -1;
141
          $stop_sym = -1;
142
          }
143
 
144
#        print "$i   $char  $state  $start_sym $stop_sym       \n";
145
 
146
        if(($state eq "symbol") && ($i eq $len -1) )
147
           {
148
           my $str_len  = $len-$start_sym;
149
           my $substring = substr($line,$start_sym,$str_len );
150
 
151
           if($substring eq $symbol)
152
              {
153
              $match = "MATCH";
154
              $math_only = $math_only - $str_len;
155
#              print "MATCH $substring $start_sym $str_len  \n";
156
              my $new_line = "";
157
              my $end_line = "";
158
              if($start_sym  eq "0") { $new_line  =$value;}
159
              else
160
                {
161
                $new_line  = substr($line,0,$start_sym);
162
                $new_line  ="${new_line}${value}";
163
                }
164
 
165
#             print "$math_only  $new_line  \n";
166
             if($math_only eq 0) {return   ( solve($new_line));}
167
             else                {return   ( $new_line);}
168
              }
169
           else
170
              {
171
              $match = "NO_MATCH";
172
#              print "$substring $stop_sym  $start_sym  $str_len $match \n";
173
              }
174
           }
175
 
176
         }
177
#     print "$math_only  $line  \n";
178
     if($math_only eq 0)     { return (  solve($line));}
179
     else                    { return ($line);}
180
 
181
}
182
 
183
 
184
 
185
 
186
sub solve {
187
    my $line  = shift;
188
 
189
#    print "SOLVE $line  \n";
190
 
191
 
192
 
193
#my $result = `./tools/math/perl_arith $line `;
194
    return ($line);
195
 
196
}
197
1

powered by: WebSVN 2.1.0

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