コード例 #1
0
  public void handleAssignment(ASTAssignment node) {
    connector.startPar(); // STARTPAR
    Random r = new Random();
    int q = r.nextInt(100);

    boolean gotAQuestion = q < QUESTION_FREQUENCY; // q < QUESTION_FREQUENCY;//HACK FOR NOW FIXME
    String name = node.getName();
    if (((ASTVar) node.jjtGetChild(0)).isArg()) {
      name += "_";
    }
    Integer value = (Integer) node.jjtGetChild(1).jjtAccept(this, null);
    int index = 0;

    Variable v = Global.getCurrentSymbolTable().getVariable(name);
    if (v instanceof ByNameVariable) {
      if (gotAQuestion) {
        if (v.getIsArray()) {
          gotAQuestion = false;
        } else {
          assignmentQuestion = questionFactory.getByNameQuestion(node.getLineNumber(), name);
        }
      }
      v.setValue(value);
      index = ((ByNameVariable) v).getIndex();
    } else if (v.getIsArray()) {
      index = (Integer) node.jjtGetChild(0).jjtGetChild(0).jjtAccept(this, null);

      try {
        v.setValue(value, index);
      } catch (VizIndexOutOfBoundsException e) {

        System.out.println(e);
      }
      if (gotAQuestion) {
        assignmentQuestion =
            questionFactory.getAssignmentQuestion(node.getLineNumber(), name, index);
      }
    } else {
      if (gotAQuestion) {
        assignmentQuestion = questionFactory.getAssignmentQuestion(node.getLineNumber(), name);
      }
      try {
        v.setValue(value);
      } catch (Exception e) {
        System.out.println(e);
      }
    }
    System.out.println(assignmentQuestion);
    if (gotAQuestion) {
      int i = -257;
      if (assignmentQuestion.getIndex() != -1) {
        if (assignmentQuestion.aboutArg) {
          try {
            i =
                Global.getFunction("main")
                    .getSymbolTable()
                    .get(assignmentQuestion.getVariable(), assignmentQuestion.getIndex());
          } catch (Exception e) {
            System.out.println(e);
          }
        } else {
          try {
            i = Global.getCurrentSymbolTable().get(name, assignmentQuestion.getIndex());
          } catch (Exception e) {
            System.out.println(e);
          }
        }
      } else {
        if (assignmentQuestion.aboutArg || v instanceof ByNameVariable) {
          System.out.println("Getting " + name);
          try {
            i = Global.getFunction("main").getSymbolTable().get(assignmentQuestion.getVariable());
          } catch (Exception e) {
            System.out.println(e);
          }
        }
        {
          try {
            i = Global.getCurrentSymbolTable().get(name);
          } catch (Exception e) {
            System.out.println(e);
          }
        }
      }
      if (gotAQuestion) {
        setAssignmentQuestionAnswer(i);
        connector.addQuestion(assignmentQuestion);
      }
    }

    if (v instanceof ByNameVariable) {

      connector.greyScope("foo");
      System.out.println("Greying scope");
      connector.highlightScopeByName("main");

      if (v.getIsArray()) {
        connector.highlightVarByName(((ByNameVariable) v).getVariable(), index);
        connector.modifyVarByName(((ByNameVariable) v).getVariable(), index, value);
      } else {
        connector.highlightVarByName(((ByNameVariable) v).getVariable());
        connector.modifyVarByName(((ByNameVariable) v).getVariable(), value);
      }
    } else {
      if (v.getIsArray()) {
        connector.modifyVarByName(v, index, value);
      } else {
        connector.modifyVarByName(v, value);
      }
    }
    connector.endPar(); // ENDPAR
    update(node.getLineNumber(), UPDATE_REASON_ASSIGNMENT);
  }
コード例 #2
0
  public Integer handleVar(ASTVar node) {
    System.out.println(node.jjtGetParent());
    Integer value = -256;
    int index = -10000;
    String name = node.getName();
    if (node.isArg()) {
      name += "_";
    }

    if (JustCalling) {
      return value;
    }
    Variable v = Global.getCurrentSymbolTable().getVariable(name);
    if (v instanceof ByNameVariable) {
      v = Global.getCurrentSymbolTable().getCacheVariable(name);
      System.out.println(v + " should be null the first time");
      if (v != null) {
        int ret = 0;
        try {
          ret = v.getValue();
        } catch (VizIndexOutOfBoundsException e) {
          System.out.println(e);
        }
        return ret;
      }
    } else {
      v = Global.getCurrentSymbolTable().getVariable(name);
    }

    boolean cached = false;
    if (v == null) {

      v = Global.getCurrentSymbolTable().getVariable(name);
      System.out.println(v);
      int val = 0;
      try {
        val = v.getValue();

        Global.getCurrentSymbolTable().addCacheVariable(name, val);

      } catch (VizIndexOutOfBoundsException e) {
        System.out.println(e);
      }
      if (node.getIsArray()) {
        name += "[" + ((SimpleNode) node.jjtGetChild(0)).getCode() + "]";
      }
      connector.addVariableToCache(name, val, "foo");
    } else {
      cached = true;
    }

    if (v instanceof ByNameVariable) {
      try {
        value = v.getValue();
      } catch (VizIndexOutOfBoundsException e) {
        System.out.println(e);
      }
    } else {
      if (node.getIsArray()) {
        index = (Integer) node.jjtGetChild(0).jjtAccept(this, null);
        node.setIndex(index);
        try {
          value = v.getValue(index);

        } catch (VizIndexOutOfBoundsException e) {
          System.out.println(e);
          ASTExpression exp = (ASTExpression) node.jjtGetChild(0);
          ASTNum num = new ASTNum(JJTNUM);
          Random r = new Random();
          num.setValue(r.nextInt(6));
          exp.jjtAddChild(num, 0);
          try {
            index = (Integer) exp.jjtAccept(this, null);
            value = v.getValue(index);
          } catch (VizIndexOutOfBoundsException f) {
            System.out.println("oops...");
          }
          program.codeBuilt = false;
          Global.lineNumber = 1;
          program.buildCode();
        }
      } else {
        try {
          value = v.getValue();
        } catch (VizIndexOutOfBoundsException f) {
          System.out.println(f);
        }
      }
      System.out.println("F*****g " + value + v + cached);
    }
    if (v instanceof ByNameVariable) {
      if (cached) {
        connector.highlightVarByName(v);
      } else {

        connector.greyScope("foo");

        connector.highlightScopeByName("main");
        if (((ByNameVariable) v).getVariable().getIsArray()) {
          connector.highlightVarByName(
              ((ByNameVariable) v).getVariable(), ((ByNameVariable) v).getIndex());
        } else {
          connector.highlightVarByName(((ByNameVariable) v).getVariable());
        }
      }
    } else {

      if (node.getIsArray()) {
        // connector.highlightVarByName(v, index);
      } else {
        // connector.highlightVarByName(v);
      }
    }
    return value;
  }