<commit_before>/*
 * Copyright 2013 University of Chicago and Argonne National Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package exm.stc.frontend.tree;

import exm.stc.ast.SwiftAST;
import exm.stc.ast.antlr.ExMParser;
import exm.stc.common.exceptions.InvalidSyntaxException;
import exm.stc.common.exceptions.STCRuntimeError;
import exm.stc.frontend.Context;
import exm.stc.frontend.LogHelper;

public class Literals {

  /**
   *
   * @param context
   * @param tree
   * @return null if tree isn't a literal, the value otherwise
   */
  public static Long extractIntLit(Context context, SwiftAST tree) {
    // Literals are either represented as a plain non-negative literal,
    // or the unary negation operator applied to a literal
    if (tree.getType() == ExMParser.INT_LITERAL) {
      return parseIntToken(tree.child(0));
    } else if (tree.getType() == ExMParser.OPERATOR
        && tree.getChildCount() == 2
        && tree.child(1).getType() == ExMParser.INT_LITERAL) {
      long posValue = parseIntToken(tree.child(1).child(0));
      return -posValue;
    } else {
      return null;
    }
  }
  
  /**
   * Parse token with correct radix, etc
   * @param tree
   * @return
   */
  public static long parseIntToken(SwiftAST tree) {
    assert(tree.getType() == ExMParser.INTEGER) :
        "Bad Token:" + LogHelper.tokName(tree.getType());
    return Long.parseLong(tree.getText(), 10);
  }

  public static String extractBoolLit(Context context, SwiftAST tree) {
    assert(tree.getType() == ExMParser.BOOL_LITERAL);
    assert(tree.getChildCount() == 1);
    return tree.child(0).getText();
  }

  public static Double extractFloatLit(Context context, SwiftAST tree) {
    // Literals are either represented as a plain non-negative literal,
    // or the unary negation operator applied to a literal
    SwiftAST litTree;
    boolean negate;
    if (tree.getType() == ExMParser.FLOAT_LITERAL) {
      litTree = tree.child(0);
      negate = false;
    } else if (tree.getType() == ExMParser.OPERATOR
        && tree.getChildCount() == 2
        && tree.child(0).getType() == ExMParser.NEGATE
        && tree.child(1).getType() == ExMParser.FLOAT_LITERAL) {
      litTree = tree.child(1).child(0);
      negate = true;
    } else {
      return null;
    }
    double num;
    if (litTree.getType() == ExMParser.NOTANUMBER) {
      num = Double.NaN;
    } else if (litTree.getType() == ExMParser.INFINITY) {
      num = Double.POSITIVE_INFINITY;
    } else if (litTree.getType() == ExMParser.SCI_DECIMAL) {
      // Parse as decimal scientific notation.  Rules match
      num = Double.parseDouble(litTree.getText());
    } else {
      assert(litTree.getType() == ExMParser.DECIMAL);
      // Parse as decimal
      num = Double.parseDouble(litTree.getText());
    }
    return negate ? -1.0 * num : num;
  }

  public static String extractStringLit(Context context, SwiftAST tree) throws InvalidSyntaxException {
    assert(tree.getType() == ExMParser.STRING_LITERAL);
    assert(tree.getChildCount() == 1);
    // E.g. "hello world\n" with plain escape codes and quotes
   
    String result = extractLiteralString(context, tree.child(0));
    LogHelper.trace(context, "Unescaped string '" + tree.child(0).getText() + 
              "', resulting in '" + result + "'");
    return result;
  }
  


  /**
   * Interpret an integer literal as a float literal, warning
   * if this would result in loss of precision
   * @param value an integer literal string
   * @return
   */
  public static double interpretIntAsFloat(Context context, long longval) {
     // Casts from long to double can lost precision
     double floatval = (double)longval;
     if (longval !=  (long)(floatval)) {
       LogHelper.warn(context, 
             "Conversion of 64-bit integer constant " + longval
           + " to double precision floating point resulted in a loss of"
           + "  precision with result: " + (long)(floatval));
     }
    return floatval;
  }
  
  public static String extractLiteralString(Context context, 
                                              SwiftAST stringLiteral) 
                                          throws InvalidSyntaxException {
    System.out.println(stringLiteral.getText());
    System.out.println(stringLiteral.getType());
    int type = stringLiteral.getType();
    assert(type == ExMParser.STRING || 
           type == ExMParser.STRING_MULTI_LINE_1 || 
           type == ExMParser.STRING_MULTI_LINE_2);
    
    return unescapeString(context, 
                          unquote(stringLiteral.getText(), type));
  }
<commit_msg>Drop debugging output<commit_after>/*
 * Copyright 2013 University of Chicago and Argonne National Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package exm.stc.frontend.tree;

import exm.stc.ast.SwiftAST;
import exm.stc.ast.antlr.ExMParser;
import exm.stc.common.exceptions.InvalidSyntaxException;
import exm.stc.common.exceptions.STCRuntimeError;
import exm.stc.frontend.Context;
import exm.stc.frontend.LogHelper;

public class Literals {

  /**
   *
   * @param context
   * @param tree
   * @return null if tree isn't a literal, the value otherwise
   */
  public static Long extractIntLit(Context context, SwiftAST tree) {
    // Literals are either represented as a plain non-negative literal,
    // or the unary negation operator applied to a literal
    if (tree.getType() == ExMParser.INT_LITERAL) {
      return parseIntToken(tree.child(0));
    } else if (tree.getType() == ExMParser.OPERATOR
        && tree.getChildCount() == 2
        && tree.child(1).getType() == ExMParser.INT_LITERAL) {
      long posValue = parseIntToken(tree.child(1).child(0));
      return -posValue;
    } else {
      return null;
    }
  }
  
  /**
   * Parse token with correct radix, etc
   * @param tree
   * @return
   */
  public static long parseIntToken(SwiftAST tree) {
    assert(tree.getType() == ExMParser.INTEGER) :
        "Bad Token:" + LogHelper.tokName(tree.getType());
    return Long.parseLong(tree.getText(), 10);
  }

  public static String extractBoolLit(Context context, SwiftAST tree) {
    assert(tree.getType() == ExMParser.BOOL_LITERAL);
    assert(tree.getChildCount() == 1);
    return tree.child(0).getText();
  }

  public static Double extractFloatLit(Context context, SwiftAST tree) {
    // Literals are either represented as a plain non-negative literal,
    // or the unary negation operator applied to a literal
    SwiftAST litTree;
    boolean negate;
    if (tree.getType() == ExMParser.FLOAT_LITERAL) {
      litTree = tree.child(0);
      negate = false;
    } else if (tree.getType() == ExMParser.OPERATOR
        && tree.getChildCount() == 2
        && tree.child(0).getType() == ExMParser.NEGATE
        && tree.child(1).getType() == ExMParser.FLOAT_LITERAL) {
      litTree = tree.child(1).child(0);
      negate = true;
    } else {
      return null;
    }
    double num;
    if (litTree.getType() == ExMParser.NOTANUMBER) {
      num = Double.NaN;
    } else if (litTree.getType() == ExMParser.INFINITY) {
      num = Double.POSITIVE_INFINITY;
    } else if (litTree.getType() == ExMParser.SCI_DECIMAL) {
      // Parse as decimal scientific notation.  Rules match
      num = Double.parseDouble(litTree.getText());
    } else {
      assert(litTree.getType() == ExMParser.DECIMAL);
      // Parse as decimal
      num = Double.parseDouble(litTree.getText());
    }
    return negate ? -1.0 * num : num;
  }

  public static String extractStringLit(Context context, SwiftAST tree) throws InvalidSyntaxException {
    assert(tree.getType() == ExMParser.STRING_LITERAL);
    assert(tree.getChildCount() == 1);
    // E.g. "hello world\n" with plain escape codes and quotes
   
    String result = extractLiteralString(context, tree.child(0));
    LogHelper.trace(context, "Unescaped string '" + tree.child(0).getText() + 
              "', resulting in '" + result + "'");
    return result;
  }
  


  /**
   * Interpret an integer literal as a float literal, warning
   * if this would result in loss of precision
   * @param value an integer literal string
   * @return
   */
  public static double interpretIntAsFloat(Context context, long longval) {
     // Casts from long to double can lost precision
     double floatval = (double)longval;
     if (longval !=  (long)(floatval)) {
       LogHelper.warn(context, 
             "Conversion of 64-bit integer constant " + longval
           + " to double precision floating point resulted in a loss of"
           + "  precision with result: " + (long)(floatval));
     }
    return floatval;
  }
  
  public static String extractLiteralString(Context context, 
                                              SwiftAST stringLiteral) 
                                          throws InvalidSyntaxException {
    int type = stringLiteral.getType();
    assert(type == ExMParser.STRING || 
           type == ExMParser.STRING_MULTI_LINE_1 || 
           type == ExMParser.STRING_MULTI_LINE_2);
    
    return unescapeString(context, 
                          unquote(stringLiteral.getText(), type));
  }

{'fraction_non_alphanumeric': 0.10869994081672914, 'fraction_numerical': 0.006312882225290984, 'mean_word_length': 2.821711270260083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6335340', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2937, 'n_words': 838}