move JFlex to CE source
[fedora-idea.git] / tools / lexer / jflex-1.4 / examples / interpreter / Tprogram.java
blob08a93d17fc800ce7b684bd97485af0133535406f
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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 /**
23 * AST node for the whole program (top node).
25 * Also contains two symbol tables, one for input variables,
26 * one for function names.
28 * All operations like context check, symbol table build up
29 * etc. start here.
30 */
31 class Tprogram implements AST {
33 Tparlist parlist; // input variables
34 Tdekllist dekllist; // function declarations
35 Texplist explist; // result expressions
36 Texplist arguments; // input values
38 public Tprogram(Tparlist p, Tdekllist d, Texplist e, Texplist a) {
39 parlist=p;
40 dekllist=d;
41 explist=e;
42 arguments=a;
45 public String toString() {
46 return("Program:\n=============\ninput "+parlist+
47 "\nfunctions\n"+dekllist+"\noutput "+explist+
48 "\narguments "+arguments+"\nend");
51 SymTab inputs; // table of input variables
52 SymTab functions; // table of functions
54 public void setSymtabs() { // calculate symbol table entries
55 inputs = new SymTab(); // set input variables
56 parlist.setSymtab(inputs, true, 0);
57 functions = new SymTab(inputs);
58 dekllist.setSymtab(functions);
61 public void printSymtabs() {
62 System.out.print("Input variables-\n"+inputs);
63 System.out.print("Functions-\n"+functions);
64 dekllist.printSymtabs();
67 public void checkcontext() {
68 dekllist.checkcontext(); // CoCo (DefFun,DefVar,Arity)
69 // in function bodies
70 explist.checkcontext(functions); // CoCo (DefFun,DefVar,Arity)
71 // in result expressions
72 arguments.checkcontext(new SymTab()); // CoCo (constants)
73 // in arguments
74 if (arguments.length()!=inputs.size())
75 Main.error("Argument list and input variables list differ!");
78 public void prepInterp() { // set pointers and indices
79 dekllist.prepInterp(functions);
80 explist.prepInterp(functions);
83 public void interpret() {
84 int[] inputEnv = new int[inputs.size()]; // set input
86 arguments.interpret(null,null,inputEnv,0);
88 System.out.println("Result:\n=============");
90 int[] ergebnis = new int[explist.length()];
91 explist.interpret(inputEnv,null,ergebnis,0); // calculate result
93 int i;
94 for (i=explist.length()-1; i > 0; i--)
95 System.out.print(ergebnis[i]+", ");
96 System.out.println(ergebnis[i]);