KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Tprogram


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 the whole program (top node).
24  *
25  * Also contains two symbol tables, one for input variables,
26  * one for function names.
27  *
28  * All operations like context check, symbol table build up
29  * etc. start here.
30  */

31 class Tprogram implements AST {
32
33   Tparlist parlist; // input variables
34
Tdekllist dekllist; // function declarations
35
Texplist explist; // result expressions
36
Texplist arguments; // input values
37

38   public Tprogram(Tparlist p, Tdekllist d, Texplist e, Texplist a) {
39     parlist=p;
40     dekllist=d;
41     explist=e;
42     arguments=a;
43   }
44
45   public String JavaDoc toString() {
46     return("Program:\n=============\ninput "+parlist+
47            "\nfunctions\n"+dekllist+"\noutput "+explist+
48            "\narguments "+arguments+"\nend");
49   }
50
51   SymTab inputs; // table of input variables
52
SymTab functions; // table of functions
53

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);
59   }
60
61   public void printSymtabs() {
62     System.out.print("Input variables-\n"+inputs);
63     System.out.print("Functions-\n"+functions);
64     dekllist.printSymtabs();
65   }
66
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!");
76   }
77
78   public void prepInterp() { // set pointers and indices
79
dekllist.prepInterp(functions);
80     explist.prepInterp(functions);
81   }
82   
83   public void interpret() {
84     int[] inputEnv = new int[inputs.size()]; // set input
85

86     arguments.interpret(null,null,inputEnv,0);
87
88     System.out.println("Result:\n=============");
89
90     int[] ergebnis = new int[explist.length()];
91     explist.interpret(inputEnv,null,ergebnis,0); // calculate result
92

93     int i;
94     for (i=explist.length()-1; i > 0; i--)
95       System.out.print(ergebnis[i]+", ");
96     System.out.println(ergebnis[i]);
97   }
98 }
99
100
Popular Tags