Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Expr.java

00001 package gov.nasa.arc.ase.jpf.expr;
00002 
00003 import java.util.StringTokenizer;
00004 import gov.nasa.arc.ase.jpf.JPF;
00005 
00006 //#ifdef BUCHI
00007 public class Expr {
00008   SubExpr[] terms;
00009 
00010   public Expr() {
00011     terms = new SubExpr[0];
00012   }  
00013   public Expr(Expr e) {
00014     int l = e.terms.length;
00015     
00016     terms = new SubExpr[l];
00017 
00018     for(int i = 0; i < l; i++)
00019       terms[i] = e.terms[i];
00020   }  
00021   void backtrack(JPF jpf, int id) {
00022     terms[id].backtrack();
00023   }  
00024   boolean evaluate(JPF jpf, int id) {
00025     return terms[id].evaluate();
00026   }  
00027   void execute(JPF jpf, int id) {
00028     terms[id].execute();
00029   }  
00030   private void grow() {
00031     int l = terms.length;
00032     SubExpr[] nt = new SubExpr[l+1];
00033     System.arraycopy(terms, 0, nt, 0, l);
00034     terms = nt;
00035   }  
00036   private SubExpr id(SubExpr e) {
00037     for(int i = 0, l = terms.length; i < l; i++)
00038       if(terms[i].equals(e))
00039     return terms[i];
00040     put(e);
00041 
00042     return e;
00043   }  
00044   public SubExpr parse(String e) {
00045     if(e.equals("-")) return new True();
00046 
00047     return parseOr(e);
00048   }  
00049   private SubExpr parseAnd(String e) {
00050     String[] t = tokenize(e, "&");
00051     int l = t.length;
00052 
00053     if(l == 1) return parseNot(e);
00054 
00055     And s = new And(l);
00056 
00057     for(int i = 0; i < l; i++)
00058       s.set(i, parseNot(t[i]));
00059 
00060     return id(s);
00061   }  
00062   private SubExpr parseNot(String e) {
00063     e = e.trim();
00064 
00065     if(!e.startsWith("!")) return parseTerm(e);
00066 
00067     Not s = new Not(parseTerm(e.substring(1)));
00068 
00069     return id(s);
00070   }  
00071   private SubExpr parseOr(String e) {
00072     String[] t = tokenize(e, "|");
00073     int l = t.length;
00074 
00075     if(l == 1) return parseAnd(e);
00076 
00077     Or s = new Or(l);
00078 
00079     for(int i = 0; i < l; i++)
00080       s.set(i, parseAnd(t[i]));
00081 
00082     return id(s);
00083   }  
00084   private SubExpr parseTerm(String e) {
00085     return id(new Evaluate(e));
00086   }  
00087   private int put(SubExpr e) {
00088     int l = terms.length;
00089     grow();
00090     terms[l] = e;
00091     
00092     return l;
00093   }  
00094   private static String[] tokenize(String s, String d) {
00095     StringTokenizer st = new StringTokenizer(s, d);
00096 
00097     int ntokens = st.countTokens();
00098     String[] tokens = new String[ntokens];
00099 
00100     for(int i = 0; i < ntokens; i++)
00101       tokens[i] = st.nextToken();
00102 
00103     return tokens;
00104   }  
00105   String toString(int id) {
00106     return terms[id].toString();
00107   }  
00108 }

Generated at Thu Feb 7 06:45:17 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001