Example #1
0
    public void messageReceived(Message message) {
      MessageType type = message.getType();

      switch (type) {
        case PARSER_SUMMARY:
          {
            Number body[] = (Number[]) message.getBody();
            int statementCount = (Integer) body[0];
            int syntaxErrors = (Integer) body[1];
            float elapsedTime = (Float) body[2];

            System.out.printf(PARSER_SUMMARY_FORMAT, statementCount, syntaxErrors, elapsedTime);
            break;
          }

        case SYNTAX_ERROR:
          {
            Object body[] = (Object[]) message.getBody();
            int lineNumber = (Integer) body[0];
            int position = (Integer) body[1];
            String tokenText = (String) body[2];
            String errorMessage = (String) body[3];

            int spaceCount = PREFIX_WIDTH + position;
            StringBuilder flagBuffer = new StringBuilder();

            // Spaces up to the error position
            for (int i = 1; i < spaceCount; ++i) flagBuffer.append(' ');

            // A pointer to the error followed by the error message
            flagBuffer.append("^\n*** ").append(errorMessage);

            // Text, if any, of the bad token
            if (tokenText != null) flagBuffer.append(" [at \"").append(tokenText).append("\"]");
            System.out.println(flagBuffer.toString());
            break;
          }
      }
    }
Example #2
0
    /**
     * Called by the source whenever it produces a message.
     *
     * @param message the message.
     */
    public void messageReceived(Message message) {
      MessageType type = message.getType();
      Object body[] = (Object[]) message.getBody();

      switch (type) {
        case SOURCE_LINE:
          {
            int lineNumber = (Integer) body[0];
            String lineText = (String) body[1];

            System.out.println(String.format(SOURCE_LINE_FORMAT, lineNumber, lineText));
            break;
          }
      }
    }
Example #3
0
    /**
     * Called by the back end whenever it produces a message.
     *
     * @param message the message.
     */
    public void messageReceived(Message message) {
      MessageType type = message.getType();

      switch (type) {
        case SOURCE_LINE:
          {
            if (lines) {
              int lineNumber = (Integer) message.getBody();
              System.out.printf(LINE_FORMAT, lineNumber);
            }
            break;
          }

        case ASSIGN:
          {
            if (assign) {
              Object body[] = (Object[]) message.getBody();
              int lineNumber = (Integer) body[0];
              String variableName = (String) body[1];
              Object value = body[2];
              System.out.printf(ASSIGN_FORMAT, lineNumber, variableName, value);
            }
            break;
          }

        case FETCH:
          {
            if (fetch) {
              Object body[] = (Object[]) message.getBody();
              int lineNumber = (Integer) body[0];
              String variableName = (String) body[1];
              Object value = body[2];

              System.out.printf(FETCH_FORMAT, lineNumber, variableName, value);
            }
            break;
          }

        case CALL:
          {
            if (call) {
              Object body[] = (Object[]) message.getBody();
              int lineNumber = (Integer) body[0];
              String routineName = (String) body[1];

              System.out.printf(CALL_FORMAT, lineNumber, routineName);
            }
            break;
          }

        case RETURN:
          {
            if (returnn) {
              Object body[] = (Object[]) message.getBody();
              int lineNumber = (Integer) body[0];
              String routineName = (String) body[1];

              System.out.printf(RETURN_FORMAT, lineNumber, routineName);
            }
            break;
          }

        case RUNTIME_ERROR:
          {
            Object body[] = (Object[]) message.getBody();
            String errorMessage = (String) body[0];
            Integer lineNumber = (Integer) body[1];

            System.out.print("*** RUNTIME ERROR");
            if (lineNumber != null)
              System.out.print(" AT LINE " + String.format("%03d", lineNumber));
            System.out.println(": " + errorMessage);
            break;
          }

        case INTERPRETER_SUMMARY:
          {
            Number body[] = (Number[]) message.getBody();
            int executionCount = (Integer) body[0];
            int runtimeErrors = (Integer) body[1];
            float elapsedTime = (Float) body[2];

            System.out.printf(
                INTERPRETER_SUMMARY_FORMAT, executionCount, runtimeErrors, elapsedTime);
            break;
          }

        case COMPILER_SUMMARY:
          {
            Number body[] = (Number[]) message.getBody();
            int instructionCount = (Integer) body[0];
            float elapsedTime = (Float) body[1];

            System.out.printf(COMPILER_SUMMARY_FORMAT, instructionCount, elapsedTime);
            break;
          }
      }
    }