<commit_before>﻿_ = window?._ ? self?._ ? global?._ ? require 'lodash'  # rely on lodash existing, since it busts CodeCombat to browserify it--TODO

parser = require 'filbert'
parser_loose = require 'filbert/filbert_loose'
estraverse = require 'estraverse'

Language = require './language'

module.exports = class Python extends Language
  name: 'Python'
  id: 'python'
  parserID: 'filbert'
  runtimeGlobals:
    __pythonRuntime: parser.pythonRuntime
  thisValue: 'self'
  thisValueAccess: 'self.'
  wrappedCodeIndentLen: 4

  constructor: ->
    @indent = Array(@wrappedCodeIndentLen + 1).join ' '

  hasChangedASTs: (a, b) ->
    try
      [aAST, bAST] = [null, null]
      options = {locations: false, ranges: false}
      aAST = parser_loose.parse_dammit a, options
      bAST = parser_loose.parse_dammit b, options
      unless aAST and bAST
        return true
      return not _.isEqual(aAST, bAST)
    catch error
      return true

  # Replace 'loop:' with 'while True:'
  replaceLoops: (rawCode, aether) ->
    # rawCode is pre-wrap
    return [rawCode, []] if rawCode.indexOf('loop:') is -1
    convertedCode = ""
    replacedLoops = []
    rangeIndex = 0
    lines = rawCode.split '\n'
    for line, lineNumber in lines
      rangeIndex += @wrappedCodeIndentLen # + 4 for future wrapped indent
      if line.replace(/^\s+/g, "").indexOf('loop') is 0
        start = line.indexOf 'loop'
        end = start + 4
        end++ while (line[end] != ':' and end < line.length)
        if end < line.length
          a = line.split("")
          a[start..end] = 'while True:'.split ""
          line = a.join("")
          replacedLoops.push rangeIndex + start
      convertedCode += line
      convertedCode += '\n' unless lineNumber is lines.length - 1
      rangeIndex += line.length + 1 # + 1 for newline
    [convertedCode, replacedLoops]

  # Wrap the user code in a function. Store @wrappedCodePrefix and @wrappedCodeSuffix.<commit_msg>Removed unnecessary parameter in replaceLoops()<commit_after>﻿_ = window?._ ? self?._ ? global?._ ? require 'lodash'  # rely on lodash existing, since it busts CodeCombat to browserify it--TODO

parser = require 'filbert'
parser_loose = require 'filbert/filbert_loose'
estraverse = require 'estraverse'

Language = require './language'

module.exports = class Python extends Language
  name: 'Python'
  id: 'python'
  parserID: 'filbert'
  runtimeGlobals:
    __pythonRuntime: parser.pythonRuntime
  thisValue: 'self'
  thisValueAccess: 'self.'
  wrappedCodeIndentLen: 4

  constructor: ->
    @indent = Array(@wrappedCodeIndentLen + 1).join ' '

  hasChangedASTs: (a, b) ->
    try
      [aAST, bAST] = [null, null]
      options = {locations: false, ranges: false}
      aAST = parser_loose.parse_dammit a, options
      bAST = parser_loose.parse_dammit b, options
      unless aAST and bAST
        return true
      return not _.isEqual(aAST, bAST)
    catch error
      return true

  # Replace 'loop:' with 'while True:'
  replaceLoops: (rawCode) ->
    # rawCode is pre-wrap
    return [rawCode, []] if rawCode.indexOf('loop:') is -1
    convertedCode = ""
    replacedLoops = []
    rangeIndex = 0
    lines = rawCode.split '\n'
    for line, lineNumber in lines
      rangeIndex += @wrappedCodeIndentLen # + 4 for future wrapped indent
      if line.replace(/^\s+/g, "").indexOf('loop') is 0
        start = line.indexOf 'loop'
        end = start + 4
        end++ while (line[end] != ':' and end < line.length)
        if end < line.length
          a = line.split("")
          a[start..end] = 'while True:'.split ""
          line = a.join("")
          replacedLoops.push rangeIndex + start
      convertedCode += line
      convertedCode += '\n' unless lineNumber is lines.length - 1
      rangeIndex += line.length + 1 # + 1 for newline
    [convertedCode, replacedLoops]

  # Wrap the user code in a function. Store @wrappedCodePrefix and @wrappedCodeSuffix.
{'fraction_non_alphanumeric': 0.11646279306829765, 'fraction_numerical': 0.0050968399592252805, 'mean_word_length': 2.840508806262231, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7220089', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1230, 'n_words': 366}