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-25 015 *********************************************************************/ 016 017 public class NameMiniNode 018 extends AbstractMiniNode implements ElementMiniNode 019 ////////////////////////////////////////////////////////////////////// 020 ////////////////////////////////////////////////////////////////////// 021 { 022 023 protected String name; 024 025 ////////////////////////////////////////////////////////////////////// 026 ////////////////////////////////////////////////////////////////////// 027 028 public NameMiniNode ( String name ) 029 ////////////////////////////////////////////////////////////////////// 030 { 031 this.name = name; 032 } 033 034 ////////////////////////////////////////////////////////////////////// 035 ////////////////////////////////////////////////////////////////////// 036 037 public String getName ( ) { return name; } 038 039 ////////////////////////////////////////////////////////////////////// 040 // MiniNode interface methods 041 ////////////////////////////////////////////////////////////////////// 042 043 public void generate ( MiniNodeCodeVisitor miniNodeCodeVisitor ) 044 ////////////////////////////////////////////////////////////////////// 045 { 046 miniNodeCodeVisitor.generateName ( this ); 047 } 048 049 public void checkSemantics ( Stack parentMiniNodeStack ) 050 throws SemanticErrorException 051 ////////////////////////////////////////////////////////////////////// 052 { 053 int size = parentMiniNodeStack.size ( ); 054 055 boolean wasDeclared = false; 056 057 for ( int i = size - 1; i >= 0; i-- ) 058 { 059 MiniNode miniNode = ( MiniNode ) parentMiniNodeStack.get ( i ); 060 061 if ( miniNode instanceof BlockMiniNode ) 062 { 063 DeclarationSequenceMiniNode declarationSequenceMiniNode 064 = ( ( BlockMiniNode ) miniNode 065 ).getDeclarationSequenceMiniNode ( ); 066 067 if ( ( declarationSequenceMiniNode != null ) 068 && declarationSequenceMiniNode.declares ( this ) ) 069 { 070 wasDeclared = true; 071 break; 072 } 073 } 074 else if ( miniNode instanceof ProcedureDeclarationMiniNode ) 075 { 076 ParameterSequenceMiniNode parameterSequenceMiniNode 077 = ( ( ProcedureDeclarationMiniNode ) miniNode 078 ).getParameterSequenceMiniNode ( ); 079 080 if ( ( parameterSequenceMiniNode != null ) 081 && parameterSequenceMiniNode.declares ( this ) ) 082 { 083 wasDeclared = true; 084 break; 085 } 086 } 087 } 088 089 if ( !wasDeclared ) 090 { 091 throw new SemanticErrorException ( "undeclared name" ); 092 } 093 } 094 095 ////////////////////////////////////////////////////////////////////// 096 ////////////////////////////////////////////////////////////////////// 097 098 public boolean equals ( Object other ) 099 ////////////////////////////////////////////////////////////////////// 100 { 101 if ( other == null ) return false; 102 103 if ( !getClass ( ).equals ( other.getClass ( ) ) ) return false; 104 105 return name.equals ( ( ( NameMiniNode ) other ).name ); 106 } 107 108 public int hashCode ( ) 109 ////////////////////////////////////////////////////////////////////// 110 { 111 return name.hashCode ( ); 112 } 113 114 ////////////////////////////////////////////////////////////////////// 115 ////////////////////////////////////////////////////////////////////// 116 }