Advertisement
KillianMills

TypeCheckVisitor.java

Dec 13th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.41 KB | None | 0 0
  1. // Name:TypeCheckVisitor.java
  2. // Author: David Sinclair      Date: 29 Aug 2012
  3. //
  4. // Visitor for basic type checking expressions in an abstract syntax tree in the ExprLang language
  5. //
  6.  
  7. import java.util.*;
  8.  
  9. public class TypeCheckVisitor implements ParserVisitor
  10. {
  11.  
  12.     private static HashMap<String, HashMap<String, STC>> ST = new HashMap<>();
  13.  
  14.     private static HashSet<String> declaredFuncts = new HashSet<String>();
  15.     private static HashSet<String> calledFuncts = new HashSet<String>();
  16.  
  17.     private static String currentScope = "";
  18.  
  19.     public Object visit(SimpleNode node, Object data)
  20.     {
  21.         throw new RuntimeException("Visit SimpleNode");
  22.     }
  23.  
  24.     public Object visit(ASTProgram node, Object data)
  25.     {
  26.         currentScope = "global";
  27.         HashMap<String, HashMap<String, STC>> ST = (HashMap<String, HashMap<String, STC>>) data;
  28.         //System.out.println(ST);
  29.         node.jjtGetChild(0).jjtAccept(this, data);
  30.         node.jjtGetChild(0); // var decl
  31.         node.jjtGetChild(1); // const decl
  32.         node.jjtGetChild(2); // function
  33.         node.jjtGetChild(3); // main prog
  34.  
  35.         node.jjtGetChild(0).jjtAccept(this, data);
  36.         node.jjtGetChild(1).jjtAccept(this, data);
  37.         node.jjtGetChild(2).jjtAccept(this, data); // This makes function get called
  38.         node.jjtGetChild(3).jjtAccept(this, data);
  39.  
  40.         for(Map.Entry<String, HashMap<String, STC>> entry : ST.entrySet()) {
  41.  
  42.             String tempScope = entry.getKey();
  43.             if (!tempScope.equals("main") && !tempScope.equals("global")){
  44.                 calledFuncts.add(tempScope);
  45.             }
  46.         }
  47.  
  48.         if(calledFuncts.size() == declaredFuncts.size()){
  49.             System.out.println("All declared functions were called");
  50.         }
  51.         else{
  52.             System.out.println("Not all declared functions were called");
  53.         }
  54.         return DataType.Program;
  55.     }
  56.  
  57.     public Object visit(ASTVarDecl node, Object data)
  58.     {
  59.         node.jjtGetChild(0).jjtAccept(this, data);
  60.         return DataType.VarDeclaration;
  61.     }
  62.  
  63.     public Object visit(ASTConstDecl node, Object data)
  64.     {
  65.         node.jjtGetChild(0).jjtAccept(this, data);
  66.         return DataType.ConstDeclaration;
  67.     }
  68.  
  69.     public Object visit(ASTEqual node, Object data)
  70.     {
  71.  
  72.         return DataType.Equal;
  73.     }
  74.  
  75.     public Object visit(ASTFunction node, Object data)
  76.     {
  77.         currentScope = "is_prime";
  78.         node.jjtGetChild(0).jjtAccept(this, data); // type
  79.         node.jjtGetChild(1).jjtAccept(this, data); // ID
  80.         System.out.println(node.jjtGetChild(3).jjtAccept(this, data));
  81.         System.out.println(node.jjtGetChild(4).jjtAccept(this, data));
  82.         //System.out.println("^_^ Function ^_^");
  83.         //System.out.println(node.value);
  84.         //System.out.println();
  85.         return DataType.Function;
  86.     }
  87.  
  88.     public Object visit(ASTParamList node, Object data)
  89.     {
  90.         node.jjtGetChild(0).jjtAccept(this, data);
  91.         return DataType.ParamList;
  92.     }
  93.  
  94.     public Object visit(ASTType node, Object data)
  95.     {
  96.         //node.jjtGetChild(0).jjtAccept(this, data);
  97.         return DataType.Int;
  98.     }
  99.  
  100.     public Object visit(ASTMainProg node, Object data)
  101.     {
  102.         node.jjtGetChild(0).jjtAccept(this, data);
  103.         currentScope = "main";
  104.         return DataType.MainProg;
  105.     }
  106.  
  107.     public Object visit(ASTAssign node, Object data)
  108.     {
  109.         node.jjtGetChild(0).jjtAccept(this, data);
  110.         return DataType.Assign;
  111.     }
  112.  
  113.     public Object visit(ASTMathSign node, Object data)
  114.     {
  115.         node.jjtGetChild(0).jjtAccept(this, data);
  116.         return DataType.Plus;
  117.         //return DataType.Minus;
  118.         //return DataType.Div;
  119.         //return DataType.Mult;
  120.     }
  121.  
  122.     public Object visit(ASTCondition node, Object data)
  123.     {
  124.         System.out.println(node.jjtGetChild(0).jjtAccept(this, data));
  125.         System.out.println(node.jjtGetChild(1).jjtAccept(this, data));
  126.         System.out.println(node.jjtGetChild(2).jjtAccept(this, data));
  127.         //System.out.println(node.jjtGetChild(3).jjtAccept(this, data));
  128.         node.jjtGetChild(0).jjtAccept(this, data);
  129.         node.jjtGetChild(1).jjtAccept(this, data);
  130.         node.jjtGetChild(2).jjtAccept(this, data); // This makes function get called
  131.         //node.jjtGetChild(3).jjtAccept(this, data);
  132.  
  133.         return DataType.Condition;
  134.     }
  135.  
  136.     public Object visit(ASTSigns node, Object data)
  137.     {
  138.         node.jjtGetChild(0).jjtAccept(this, data);
  139.         return DataType.Equal;
  140.  
  141.         //return DataType.NotEqual;
  142.         //return DataType.LessThan;
  143.         //return DataType.GreaterThan;
  144.         //return DataType.LTE;
  145.         //return DataType.GTE;
  146.  
  147.     }
  148.  
  149.     public Object visit(ASTIdentList node, Object data)
  150.     {
  151.         node.jjtGetChild(0).jjtAccept(this, data);
  152.         return DataType.IdentList;
  153.     }
  154.  
  155.     public Object visit(ASTArgList node, Object data)
  156.     {
  157.         node.jjtGetChild(0).jjtAccept(this, data);
  158.         return DataType.ArgList;
  159.     }
  160.  
  161.     public Object visit(ASTNum node, Object data)
  162.     {
  163.  
  164.         System.out.println("INTEGER CITY");
  165.         return DataType.TypeInteger;
  166.     }
  167.  
  168.     public Object visit(ASTBool node, Object data)
  169.     {
  170.         node.jjtGetChild(0).jjtAccept(this, data);
  171.         return DataType.TypeBoolean;
  172.     }
  173.  
  174.     public Object visit(ASTId node, Object data)
  175.     {
  176.         //Hashtable ST = (Hashtable) data;
  177.         HashMap<String, HashMap<String, STC>> ST = (HashMap<String, HashMap<String, STC>>) data;
  178.         HashMap<String, STC> innerMap = new HashMap();
  179.  
  180.         if(node.value.equals(currentScope) && !node.value.equals("main")){ // function
  181.  
  182.             declaredFuncts.add((String) node.value);
  183.             return DataType.Function;
  184.         }
  185.         /*else if(node.value == "main"){ // main
  186.  
  187.  
  188.         }
  189.         else if(){ // global */
  190.         else {
  191.             innerMap = ST.get(currentScope);
  192.             /*System.out.println(ST);
  193.             System.out.println(currentScope);
  194.             System.out.println(innerMap);
  195.             System.out.println(node.value);*/
  196.             STC mapEntry = innerMap.get(node.value);
  197.             if (mapEntry.type == "Int") {
  198.                 return DataType.TypeInteger;
  199.             } else if (mapEntry.type == "Bool") {
  200.                 return DataType.TypeBoolean;
  201.             } else {
  202.                 return DataType.TypeUnknown;
  203.             }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement