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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [gjdoc/] [expr/] [Evaluator.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* gnu.classpath.tools.gjdoc.expr.Evaluator
2
   Copyright (C) 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
02111-1307 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.classpath.tools.gjdoc.expr;
39
 
40
import java.io.StringReader;
41
import java.math.BigInteger;
42
import antlr.RecognitionException;
43
import antlr.TokenStreamException;
44
import java.util.Set;
45
 
46
public class Evaluator
47
{
48
   /**
49
    *  Try to evaluate the given Java expression in the context of the
50
    *  given environment.
51
    *
52
    *  @param expression the Java expression to evaluate. The
53
    *  expression string must not include a terminating semicolon.
54
    *
55
    *  @param source the FieldDoc (part of) whose constant field value
56
    *  expression is being evaluated.  Used to prevent circular
57
    *  references.
58
    *
59
    *  @param environment callback hook used by the Evaluator to query
60
    *  the value of static fields referenced in the expression.
61
    *
62
    *  @return a Java object representing the value of the expression,
63
    *  or <code>null</code> if the expression evaluates to
64
    *  <code>null</code>.
65
    *
66
    *  @throws IllegalExpressionException if the expression is
67
    *  invalid, uses unsupported syntax constructs (e.g. method calls,
68
    *  array access) or references unknown static fields.
69
    */
70
   public static Object evaluate(String expression,
71
                                 Set visitedFields,
72
                                 EvaluatorEnvironment environment)
73
      throws IllegalExpressionException
74
   {
75
      try {
76
         JavaLexer lexer = new JavaLexer(new StringReader(expression));
77
         JavaRecognizer recognizer = new JavaRecognizer(lexer);
78
         Expression e = recognizer.expression();
79
         ConstantExpression value = e.evaluate(new Context(environment, visitedFields));
80
         return value.asObject();
81
      }
82
      catch (RecognitionException e) {
83
         throw new IllegalExpressionException(e);
84
      }
85
      catch (TokenStreamException e) {
86
         throw new IllegalExpressionException(e);
87
      }
88
   }
89
 
90
   static int parseInt(String stringValue)
91
   {
92
      int base = 10;
93
 
94
      if (stringValue.startsWith("0x")) {
95
         base = 16;
96
         stringValue = stringValue.substring(2);
97
      }
98
      else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
99
         base = 8;
100
         stringValue = stringValue.substring(1);
101
      }
102
      while (stringValue.length() > 1 && stringValue.startsWith("0")) {
103
         stringValue = stringValue.substring(1);
104
      }
105
 
106
      if (10 == base) {
107
         return Integer.parseInt(stringValue);
108
      }
109
      else {
110
         long result = Long.parseLong(stringValue, base);
111
 
112
         if (result > Integer.MAX_VALUE) {
113
            result -= 0x100000000L;
114
         }
115
 
116
         if (result > Integer.MAX_VALUE) {
117
            throw new NumberFormatException(result + " > " + Integer.MAX_VALUE);
118
         }
119
         else if (result < Integer.MIN_VALUE) {
120
            throw new NumberFormatException(result + " < " + Integer.MIN_VALUE);
121
         }
122
         else {
123
            return (int)result;
124
         }
125
      }
126
   }
127
 
128
   static long parseLong(String stringValue)
129
   {
130
      int base = 10;
131
 
132
      if (stringValue.startsWith("0x")) {
133
         base = 16;
134
         stringValue = stringValue.substring(2);
135
      }
136
      else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
137
         base = 8;
138
         stringValue = stringValue.substring(1);
139
      }
140
      while (stringValue.length() > 1 && stringValue.startsWith("0")) {
141
         stringValue = stringValue.substring(1);
142
      }
143
 
144
      BigInteger bigInt = new BigInteger(stringValue, base);
145
      long result = bigInt.longValue();
146
      return result;
147
   }
148
}

powered by: WebSVN 2.1.0

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