001 package com.croftsoft.apps.compiler.mini.node; 002 003 import java.util.*; 004 005 /********************************************************************* 006 * Parse tree node for the Mini programming language. 007 * 008 * @see 009 * MiniNode 010 * 011 * @author 012 * <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A> 013 * @version 014 * 1999-04-27 015 *********************************************************************/ 016 017 public class DeclarationSequenceMiniNode extends AbstractMiniNode 018 ////////////////////////////////////////////////////////////////////// 019 ////////////////////////////////////////////////////////////////////// 020 { 021 022 protected List declarationMiniNodeList; 023 024 ////////////////////////////////////////////////////////////////////// 025 ////////////////////////////////////////////////////////////////////// 026 027 /********************************************************************* 028 * @throws SemanticErrorException 029 * If there duplicate names are declared. 030 *********************************************************************/ 031 public DeclarationSequenceMiniNode ( 032 DeclarationSequenceMiniNode declarationSequenceMiniNode, 033 DeclarationMiniNode declarationMiniNode ) 034 throws SemanticErrorException 035 ////////////////////////////////////////////////////////////////////// 036 { 037 if ( declarationSequenceMiniNode != null ) 038 { 039 declarationMiniNodeList 040 = declarationSequenceMiniNode.declarationMiniNodeList; 041 } 042 else 043 { 044 declarationMiniNodeList = new LinkedList ( ); 045 } 046 047 declarationMiniNodeList.add ( declarationMiniNode ); 048 049 checkDuplicates ( ); 050 051 } 052 053 /********************************************************************* 054 * @throws SemanticErrorException 055 * If there duplicate names are declared. 056 *********************************************************************/ 057 public DeclarationSequenceMiniNode ( 058 DeclarationMiniNode declarationMiniNode ) 059 throws SemanticErrorException 060 ////////////////////////////////////////////////////////////////////// 061 { 062 this ( null, declarationMiniNode ); 063 } 064 065 /********************************************************************* 066 * @throws SemanticErrorException 067 * If there duplicate names are declared. 068 *********************************************************************/ 069 protected void checkDuplicates ( ) throws SemanticErrorException 070 ////////////////////////////////////////////////////////////////////// 071 { 072 boolean hasDuplicates = false; 073 074 Set nameSet = new HashSet ( ); 075 076 Iterator i = declarationMiniNodeList.iterator ( ); 077 while ( i.hasNext ( ) ) 078 { 079 NameSequenceMiniNode nameSequenceMiniNode 080 = ( ( DeclarationMiniNode ) i.next ( ) 081 ).getNameSequenceMiniNode ( ); 082 083 Iterator j 084 = nameSequenceMiniNode.getNameMiniNodeList ( ).iterator ( ); 085 086 while ( j.hasNext ( ) ) 087 { 088 NameMiniNode nameMiniNode = ( NameMiniNode ) j.next ( ); 089 090 String name = nameMiniNode.getName ( ); 091 092 if ( nameSet.contains ( name ) ) 093 { 094 hasDuplicates = true; 095 break; 096 } 097 else 098 { 099 nameSet.add ( name ); 100 } 101 } 102 } 103 104 if ( hasDuplicates ) 105 { 106 throw new IllegalArgumentException ( 107 "duplicate names declared" ); 108 } 109 } 110 111 ////////////////////////////////////////////////////////////////////// 112 // Access methods 113 ////////////////////////////////////////////////////////////////////// 114 115 public List getDeclarationMiniNodeList ( ) 116 ////////////////////////////////////////////////////////////////////// 117 { 118 return declarationMiniNodeList; 119 } 120 121 public boolean declares ( NameMiniNode nameMiniNode ) 122 ////////////////////////////////////////////////////////////////////// 123 { 124 Iterator i = declarationMiniNodeList.iterator ( ); 125 126 while ( i.hasNext ( ) ) 127 { 128 DeclarationMiniNode declarationMiniNode 129 = ( DeclarationMiniNode ) i.next ( ); 130 131 Iterator j = declarationMiniNode.getNameSequenceMiniNode ( 132 ).getNameMiniNodeList ( ).iterator ( ); 133 134 while ( j.hasNext ( ) ) 135 { 136 if ( nameMiniNode.equals ( j.next ( ) ) ) 137 { 138 return true; 139 } 140 } 141 } 142 143 return false; 144 } 145 146 ////////////////////////////////////////////////////////////////////// 147 // MiniNode interface methods 148 ////////////////////////////////////////////////////////////////////// 149 150 public void generate ( MiniNodeCodeVisitor miniNodeCodeVisitor ) 151 ////////////////////////////////////////////////////////////////////// 152 { 153 miniNodeCodeVisitor.generateDeclarationSequence ( this ); 154 } 155 156 public void checkSemantics ( Stack parentMiniNodeStack ) 157 throws SemanticErrorException 158 ////////////////////////////////////////////////////////////////////// 159 { 160 parentMiniNodeStack.push ( this ); 161 162 MiniNodeLib.checkSemantics ( 163 declarationMiniNodeList, parentMiniNodeStack ); 164 165 parentMiniNodeStack.pop ( ); 166 } 167 168 ////////////////////////////////////////////////////////////////////// 169 ////////////////////////////////////////////////////////////////////// 170 }