~ubuntu-branches/ubuntu/gutsy/jflex/gutsy

« back to all changes in this revision

Viewing changes to examples/interpreter/Texpinfix.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2002-02-16 13:38:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020216133821-5wsdprpt9xl7ondr
Tags: upstream-1.3.5
ImportĀ upstreamĀ versionĀ 1.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 * Copyright (C) 2001       Gerwin Klein <lsf@jflex.de>                    *
 
3
 * Copyright (C) 2001       Bernhard Rumpe <rumpe@in.tum.de>               *
 
4
 * All rights reserved.                                                    *
 
5
 *                                                                         *
 
6
 * This program is free software; you can redistribute it and/or modify    *
 
7
 * it under the terms of the GNU General Public License. See the file      *
 
8
 * COPYRIGHT for more information.                                         *
 
9
 *                                                                         *
 
10
 * This program is distributed in the hope that it will be useful,         *
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 
13
 * GNU General Public License for more details.                            *
 
14
 *                                                                         *
 
15
 * You should have received a copy of the GNU General Public License along *
 
16
 * with this program; if not, write to the Free Software Foundation, Inc., *
 
17
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 
18
 *                                                                         *
 
19
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
20
 
 
21
 
 
22
/**
 
23
 * AST node for infix expressions
 
24
 */ 
 
25
class Texpinfix extends Texp implements AST {
 
26
  Texp exp1, exp2;            // left and right sub expression
 
27
  char kind;                  // kind ('+', '-', '*', '/')
 
28
 
 
29
  public Texpinfix(Texp e1, char k, Texp e2) {
 
30
    exp1=e1;
 
31
    kind=k;
 
32
    exp2=e2;
 
33
  }
 
34
 
 
35
  public String toString() {
 
36
    return("("+exp1+kind+exp2+")"); 
 
37
  }
 
38
 
 
39
  public void checkcontext(SymTab st) { // context conditions
 
40
    exp1.checkcontext(st);
 
41
    exp2.checkcontext(st);
 
42
  }
 
43
 
 
44
  public void prepInterp(SymTab st) {  // set pointers und indices
 
45
    exp1.prepInterp(st);
 
46
    exp2.prepInterp(st);
 
47
  }
 
48
 
 
49
  public int interpret(int[] in, int[] par) {    
 
50
    int e1 = exp1.interpret(in,par);
 
51
    int e2 = exp2.interpret(in,par);
 
52
 
 
53
    switch (kind) {
 
54
    case '+': return(e1+e2);
 
55
    case '-': return(e1-e2);
 
56
    case '*': return(e1*e2);
 
57
    case '/': return(e1/e2);
 
58
    }
 
59
 
 
60
    return -1;        // error
 
61
  }
 
62
}
 
63
 
 
64