-Language = require './language'
+﻿_ = 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'
 escodegen = require 'escodegen'
 esprima = require 'esprima'
+
+Language = require './language'
 
 module.exports = class Python extends Language
   name: 'Python'


  # Using a third-party parser, produce an AST in the standardized Mozilla format.
  parse: (code, aether) ->
    ast = parser.parse code, {locations: true, ranges: true}

    # 'this' is not a keyword in Python, so it does not parse to a ThisExpression
    # Instead, we expect the variable 'self', and map it to a ThisExpression here.
    ast.body[0].body.body.unshift {"type": "VariableDeclaration","declarations": [{ "type": "VariableDeclarator", "id": {"type": "Identifier", "name": "self" },"init": {"type": "ThisExpression"} }],"kind": "var"}

    ast

  runtimeGlobals:
    __pythonRuntime: parser.pythonRuntime

  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

  # Wrap the user code in a function. Store @wrappedCodePrefix and @wrappedCodeSuffix.
  wrap: (rawCode, aether) ->
