+/EE379K
programming paradigms
git clone https://git.julia.blue/+/EE379K
41480ce
master
1import java .util .*; 2 3public class Visitor implements MyParserVisitor { 4private HashSet <String >types ; 5private HashSet <String >globals ; 6private HashSet <String >locals ; 7private boolean inClass =false ; 8private boolean inMethod =false ; 9private boolean inMain =false ; 10private boolean foundMain =false ; 11 12public Visitor () { 13globals =new HashSet (); 14locals =new HashSet (); 15String arr [] = {"int" ,"bool" ,"char" }; 16types =new HashSet (Arrays .asList (arr )); 17 } 18 19public void defaultVisit (SimpleNode node ,Object data ) 20throws InvalidProgramException { 21node .childrenAccept (this ,data ); 22return ; 23 } 24public void visit (SimpleNode node ,Object data ) 25throws InvalidProgramException { 26defaultVisit (node ,data ); 27 } 28public void visit (Program node ,Object data ) 29throws InvalidProgramException { 30defaultVisit (node ,data ); 31if (!foundMain ) { 32throw new InvalidProgramException ("No main function" ); 33 } 34 } 35public void visit (ConstDecl node ,Object data ) 36throws InvalidProgramException { 37String type =node .jjtGetChild (0 ).jjtGetChild (0 ).toString (); 38System .out ."CONSTs of type: " +type ); 39System .out ."\t\twith names: " ); 40for (int i =1 ;i <node .jjtGetNumChildren ();i ++) { 41String current =node .jjtGetChild (i ).toString (); 42if (globals .contains (current )) { 43throw new InvalidProgramException ( 44"Can't define 2 variables with the same name: " +current ); 45 } 46globals .add (current ); 47System .out .current +" " ); 48 } 49System .out .println (); 50 } 51public void visit (EnumDecl node ,Object data ) 52throws InvalidProgramException { 53System .out ."Enum of type: " + ((Ident )node .jjtGetChild (0 )).name ); 54System .out ."\t\twith variants: " ); 55for (int i =1 ;i <node .jjtGetNumChildren ();i ++) { 56System .out .Ident )node .jjtGetChild (i )).name +" " ); 57 } 58System .out .println (); 59 } 60public void visit (VarDecl node ,Object data ) 61throws InvalidProgramException { 62String type =node .jjtGetChild (0 ).jjtGetChild (0 ).toString (); 63System .out ."Vars of type: " +type ); 64System .out ."\t\twith names: " ); 65for (int i =1 ;i <node .jjtGetNumChildren ();i ++) { 66String current =node .jjtGetChild (i ).toString (); 67if (inMethod ) { 68locals .add (current ); 69 }else { 70globals .add (current ); 71 } 72System .out .current +" " ); 73 } 74System .out .println (); 75 } 76public void visit (ClassDecl node ,Object data ) 77throws InvalidProgramException { 78String className = ((Ident )node .jjtGetChild (0 )).name ; 79types .add (className ); 80System .out .println ("Entering " +className ); 81inClass =true ; 82defaultVisit (node ,data ); 83inClass =false ; 84System .out .println ("Leaving " +className ); 85 } 86public void visit (InterfaceDecl node ,Object data ) 87throws InvalidProgramException { 88String interfaceName = ((Ident )node .jjtGetChild (0 )).name ; 89if (types .contains (interfaceName )) { 90throw new InvalidProgramException ( 91"Can't have 2 interfaces named: " +interfaceName ); 92 } 93types .add (interfaceName ); 94System .out .println ("Entering " +interfaceName ); 95inClass =true ; 96defaultVisit (node ,data ); 97inClass =false ; 98System .out .println ("Leaving " +interfaceName ); 99 } 100public void visit (InterfaceMethodDecl node ,Object data ) 101throws InvalidProgramException { 102defaultVisit (node ,data ); 103locals .clear (); 104 } 105public void visit (MethodDecl node ,Object data ) 106throws InvalidProgramException { 107int namePos =0 ; 108if (node .jjtGetChild (0 )instanceof Type ) { 109namePos =1 ; 110 } 111String methodName =node .jjtGetChild (namePos ).toString (); 112globals .add (methodName ); 113if (methodName .equals ("main" ) && !inClass ) { 114foundMain =true ; 115inMain =true ; 116if (namePos ==1 ) { 117throw new InvalidProgramException ("Main function must be void" ); 118 } 119 } 120inMethod =true ; 121System .out .println ("Entering " +methodName ); 122defaultVisit (node ,data ); 123inMethod =false ; 124locals .clear (); 125inMain =false ; 126System .out .println ("Leaving " +methodName ); 127 } 128public void visit (ParamDecl node ,Object data ) 129throws InvalidProgramException { 130System .out ."Params:\t\t\t\t\t" ); 131if (inMain &&node .jjtGetNumChildren () >0 ) { 132throw new InvalidProgramException ( 133"Main function shouldn't have any arguments" ); 134 } 135for (int i =1 ;i <node .jjtGetNumChildren ();i +=2 ) { 136String current =node .jjtGetChild (i ).toString (); 137if (locals .contains (current )) { 138throw new InvalidProgramException ( 139"Can't define 2 variables with the same name: " +current ); 140 } 141locals .add (current ); 142System .out .current +" " ); 143 } 144System .out .println (); 145defaultVisit (node ,data ); 146 } 147public void visit (Statement node ,Object data ) 148throws InvalidProgramException { 149defaultVisit (node ,data ); 150 } 151public void visit (DesignatorStatement node ,Object data ) 152throws InvalidProgramException { 153defaultVisit (node ,data ); 154 } 155public void visit (Condition node ,Object data ) 156throws InvalidProgramException { 157defaultVisit (node ,data ); 158 } 159public void visit (CompareAnd node ,Object data ) 160throws InvalidProgramException { 161defaultVisit (node ,data ); 162 } 163public void visit (Comparison node ,Object data ) 164throws InvalidProgramException { 165defaultVisit (node ,data ); 166 } 167public void visit (Params node ,Object data )throws InvalidProgramException { 168defaultVisit (node ,data ); 169 } 170public void visit (Expr node ,Object data )throws InvalidProgramException { 171defaultVisit (node ,data ); 172 } 173public void visit (Term node ,Object data )throws InvalidProgramException { 174defaultVisit (node ,data ); 175 } 176public void visit (Factor node ,Object data )throws InvalidProgramException { 177defaultVisit (node ,data ); 178 } 179public void visit (Type node ,Object data )throws InvalidProgramException { 180String name =node .jjtGetChild (0 ).toString (); 181if (!types .contains (name )) { 182throw new InvalidProgramException ("Type not declared before use: " + 183name ); 184 } 185 } 186public void visit (Designator node ,Object data ) 187throws InvalidProgramException { 188defaultVisit (node ,data ); 189 } 190public void visit (Ident node ,Object data )throws InvalidProgramException { 191String name =node .name ; 192if (!globals .contains (name ) && !locals .contains (name )) { 193throw new InvalidProgramException ( 194"Variable not declared before use: " +name ); 195 } 196 } 197}