/*
* JTable.java
*
* Created on February 23, 2005, 11:14 PM
*/
package compiler;
import java.util.* ;
/**
*
* @author Administrator
*/
abstract class varObject extends java.lang.Object{
String type_of_object ;//method or object
}
class varSymObject extends varObject{
public String scopemodifier ;
public boolean staticscope ;
public String type ;
varSymObject(String scopemodifier, boolean staticscope,
String type){
this.scopemodifier = scopemodifier
;
this.staticscope = staticscope
;
this.type = type;
this.type_of_object = "variable"
;
}
public String toString(){
return("[ "+scopemodifier+" "+staticscope+"
"+type+" ]");
}
}
class methodSymObject extends varObject{
public String scopemodifier ;
public boolean staticscope ;
public String type ;
public String specific_type_method ;
public LinkedList params ;
methodSymObject(String scopemodifier, boolean staticscope,
String type, LinkedList prs,String specific_type_method){
this.scopemodifier = scopemodifier
;
this.staticscope = staticscope
;
this.type = type;
this.params = prs;
/*if(prs != null){
this.params
= new parameternode[prs.length] ;
for(int
i=0 ; i<params.length ; i++ ){
params[i] = new parameternode(prs[i].type,prs[i].identifier);
}
}else{
this.params
= null ;
}*/
this.specific_type_method = specific_type_method
;
this.type_of_object = "function"
;
}
public String toString(){
return("[ "+scopemodifier+" "+staticscope+"
"+type+" "+specific_type_method+" "+params+" ]");
}
}
public class JTable {
private java.util.Hashtable symTable ;
private java.util.Stack stack ;
public String classname ;
public String methodname ;
private int currentScopeID ;
private int uniqueScopeID ;
/** Creates a new instance of JTable */
public JTable() {
symTable = new java.util.Hashtable();
stack = new java.util.Stack();
classname = null ;
methodname = null ;
uniqueScopeID = currentScopeID
= 0;
}
public void enterScope(){
this.uniqueScopeID++;
this.currentScopeID=this.uniqueScopeID;
stack.push(new java.lang.Integer(this.uniqueScopeID));
}
public void leaveScope(){
stack.pop();
this.currentScopeID--;
methodname = null ;
}
public boolean insert(String name, String scopemodifier,
boolean staticscope, String type){
if ( currentScopeID == 0 ){
if (classname
== null && type.equals("class")){
classname = name ;
}else{
System.out.println("No defined scope") ;
return false ;
}
return
true;
}
if(name.equals(classname)){
return
false;
}
if(name.equals(methodname)){
return
false;
}
if (type.equals("method")){
if (methodname
== null){
methodname = name ;
}else{
System.out.println("Error : nested procs") ;
return false ;
}
}
varSymObject node = new varSymObject(scopemodifier,staticscope,type);
if(symTable.containsKey(name)){
if(getScopeID(name).equals(new
Integer(this.currentScopeID))){
return false;
//System.out.println("Redeclaration is not allowed");
//System.exit(1);
}else{
Hashtable obj = (Hashtable)symTable.get(name);
obj.put(new Integer(this.currentScopeID),node);
}
}else{
Hashtable
varTable=new Hashtable();
varTable.put(new
Integer(this.currentScopeID),node);
symTable.put(new
java.lang.String(name),varTable);
}
return true;
}
public boolean insert(String name, String scopemodifier,
boolean staticscope, String type, LinkedList params,String spt){
if ( currentScopeID == 0 ){
if (classname
== null && type.equals("class")){
classname = name ;
}else{
System.out.println("No defined scope") ;
return false ;
}
return
true;
}
if(name.equals(classname)){
if(!spt.equals("constructor"))
return false;
}
if(name.equals(methodname)){
return
false;
}
if (type.equals("method")){
if (methodname
== null){
methodname = name ;
}else{
System.out.println("Error : nested procs") ;
return false ;
}
}
methodSymObject node = new methodSymObject(scopemodifier,staticscope,type,params,spt);
if(symTable.containsKey(name)){
if(getScopeID(name).equals(new
Integer(this.currentScopeID))){
return false;
//System.out.println("Redeclaration is not allowed");
//System.exit(1);
}else{
Hashtable obj = (Hashtable)symTable.get(name);
obj.put(new java.lang.Integer(this.currentScopeID),node);
}
}else{
Hashtable
varTable=new Hashtable();
varTable.put(new
java.lang.Integer(this.currentScopeID),node);
symTable.put(new
java.lang.String(name),varTable);
}
return true;
}
public Integer getScopeID(String name){
Integer it1 = null;
varObject vo1 = null;
Object scopelist[] = (Object[])stack.toArray();
java.util.Arrays.sort(scopelist);
Hashtable obj = (Hashtable)symTable.get(name);
if(obj == null)
return
null;
else{
java.util.Enumeration
subtable = obj.keys();
while(subtable.hasMoreElements()){
Integer it2=(Integer)subtable.nextElement();
varObject vo2=(varObject)obj.get(it2);
if(contains(scopelist,it2)){
if(it1 == null && vo1 == null){
it1 = it2;
vo1 = vo2;
}else{
if(it1.compareTo(it2)<=0){
it1=it2;
vo1=vo2;
}
}
}
}
}
return it1;
}
private boolean contains(Object list[],Integer value){
int l=list.length;
for(int i=0;i<l;i++){
if(((Integer)list[i]).equals(value))
return true;
}
return false;
}
public varObject lookup(String name){
Integer it1 = null;
varObject vo1 = null;
Object scopelist[] = (Object[])stack.toArray();
java.util.Arrays.sort(scopelist);
java.util.Hashtable obj = (java.util.Hashtable)symTable.get(name);
if(obj == null)
return
vo1;
else{
java.util.Enumeration
subtable = obj.keys();
while(subtable.hasMoreElements()){
Integer it2=(Integer)subtable.nextElement();
varObject vo2=(varObject)obj.get(it2);
if(contains(scopelist,it2)){
if(it1 == null && vo1 == null){
it1 = it2;
vo1 = vo2;
}else{
if(it1.compareTo(it2)<=0){
it1=it2;
vo1=vo2;
}
}
}
}
}
System.out.println("Here"+vo1);
return vo1;
}
public String toString(){
return("[ "+symTable+" "+stack+"
]");
}
public boolean chkparamlist(String procname){
Hashtable temp = (Hashtable) symTable.get(procname) ;
String scstring = null ;
if (temp != null){
try{
scstring = (String) temp.get(new Integer(currentScopeID)) ;
}catch
(java.lang.ClassCastException cce){
scstring = null ;
}
}
if (scstring != null){
return false ;
}
return true ;
}
public static void main(String[] args){
JTable tab = new JTable();
tab.insert("xxx","public",false,"class");
tab.enterScope();
tab.insert("x","public",false,"char");
tab.insert("y",null,false,"int");
System.out.println("Outside main:
X is char");
System.out.println(tab.lookup("x"));
tab.insert("main","public",true,"method",null,"constructor");
tab.enterScope();
tab.insert("x",null,false,"int");
System.out.println("Inside main:
X is int");
tab.insert("y",null,false,"int");
System.out.println(tab.lookup("x"));
tab.leaveScope();
System.out.println("Outside main:
X is char");
System.out.println(tab.lookup("x"));
System.out.println("Outside main:
No z");
System.out.println(tab.lookup("z"));
LinkedList arr = new LinkedList()
;
arr.add(new parameternode("int","x"));
arr.add(new parameternode("char","y"));
System.out.println(tab.insert("func","public",false,"int",arr,"simplefunction"));
tab.enterScope();
System.out.println(tab.lookup("func"));
tab.leaveScope();
tab.leaveScope();
}
}