<commit_before>import py2js
import py2js.compiler
import ast
import inspect
from py2js.compiler import JSError

class Compiler(py2js.compiler.BaseCompiler):

    def visit_Module(self, node):
        module = []

        for stmt in node.body:
            module.extend(self.visit(stmt))

        return module

    def visit_FunctionDef(self, node):
        is_static = False
        is_javascript = False
        if node.decorator_list:
            if len(node.decorator_list) == 1 and \
                    isinstance(node.decorator_list[0], ast.Name) and \
                    node.decorator_list[0].id == "JavaScript":
                is_javascript = True # this is our own decorator
            elif self._class_name and \
                    len(node.decorator_list) == 1 and \
                    isinstance(node.decorator_list[0], ast.Name) and \
                    node.decorator_list[0].id == "staticmethod":
                is_static = True
            else:
                raise JSError("decorators are not supported")

        # XXX: disable $def for now, because it doesn't work in IE:
        if self._class_name:
        #if 1:
            if node.args.vararg is not None:
                raise JSError("star arguments are not supported")

            if node.args.kwarg is not None:
                raise JSError("keyword arguments are not supported")

            if node.decorator_list and not is_static and not is_javascript:
                raise JSError("decorators are not supported")

            defaults = [None]*(len(node.args.args) - len(node.args.defaults)) + node.args.defaults

            js_args = []
            js_defaults = []
            self._scope = [arg.id for arg in node.args.args]

            for arg, default in zip(node.args.args, defaults):
                if not isinstance(arg, ast.Name):
                    raise JSError("tuples in argument list are not supported")

                js_args.append(arg.id)

                if default is not None:
                    js_defaults.append("%(id)s = typeof(%(id)s) != 'undefined' ? %(id)s : %(def)s;\n" % { 'id': arg.id, 'def': self.visit(default) })

            if self._class_name:
                prep = "%s.prototype.%s = Function(function(" % \
                        (self._class_name, node.name)
                if not is_static:
                    if not (js_args[0] == "self"):
                        raise NotImplementedError("The first argument must be 'self'.")
                    del js_args[0]
            else:
                prep = "Function(function %s(" % node.name
            js = [prep + ", ".join(js_args) + ") {"]

            js.extend(self.indent(js_defaults))

            for stmt in node.body:
                js.extend(self.indent(self.visit(stmt)))

            js.append('});')

            # #If method is static, we also add it directly to the class
            # if is_static:
            #     js.append("%s.%s = %s.prototype.%s;" % \
            #             (self._class_name, node.name, self._class_name, node.name))
            # #Otherwise, we wrap it to take 'self' into account
            # else:
            #     func_name = node.name
            #     js.append("%s.%s = function() {" % (self._class_name, func_name))
            #     js.append("    %s.prototype.%s.apply(arguments[0],Array.slice(arguments,1));"% (self._class_name, func_name))
            #     js.append("}")

            self._scope = []
            return js
        else:
            defaults = [None]*(len(node.args.args) - len(node.args.defaults)) + node.args.defaults

            args = []
            defaults2 = []
            for arg, default in zip(node.args.args, defaults):
                if not isinstance(arg, ast.Name):
                    raise JSError("tuples in argument list are not supported")
                if default:
                    defaults2.append("%s: %s" % (arg.id, self.visit(default)))
                args.append(arg.id)
            defaults = "{" + ", ".join(defaults2) + "}"
            args = ", ".join(args)
            js = ["var %s = Function(function(%s) {" % (node.name, args)]
            self._scope = [arg.id for arg in node.args.args]
            for stmt in node.body:
                js.extend(self.indent(self.visit(stmt)))
            return js + ["});"]

    def visit_ClassDef(self, node):
        js = []
        bases = [self.visit(n) for n in node.bases]
        if not bases:
            bases = ['object']
        assert len(bases) == 1;
        class_name = node.name
        #self._classes remembers all classes defined
        self._classes[class_name] = node
        self._class_names.add(class_name)

        js.append("var %s = __inherit(%s);" % (class_name, bases[0]));

        #~ methods = []
        self._class_name = class_name
        for stmt in node.body:
            if isinstance(stmt, ast.Assign):
                value = self.visit(stmt.value)
                for t in stmt.targets:
                    var = self.visit(t)
                    js.append("%s.%s = %s;" % (class_name, var, value))
            else:
                js.extend(self.visit(stmt))
        self._class_name = None

        #The following is unnecessary: __init__ is inherited from
        #'object'
        #~ methods_names = [m.name for m in methods]
        #~ if not "__init__" in methods_names:
            #~ # if the user didn't define __init__(), we have to add it ourselves
            #~ # because we call it from the constructor above
            #~ js.append("_%s.prototype.__init__ = function() {" % class_name)
            #~ js.append("}")

        return js

    def visit_Return(self, node):
        if node.value is not None:
            return ["return %s;" % self.visit(node.value)]
        else:
            return ["return;"]

    def visit_Delete(self, node):
        return node

    def visit_AssignSimple(self, left, right):
        target = left
        value  = right
        if isinstance(target, (ast.Tuple, ast.List)):
            js = ["var __dummy%d__ = %s;" % (self.dummy_index, value)]

            for i, target in enumerate(target.elts):
                var = self.visit(target)
                declare = ""
                if isinstance(target, ast.Name):
                    if not (var in self._scope):
                        self._scope.append(var)
                        declare = "var "
                js.append("%s%s = __dummy%d__.__getitem__(%d);" % (declare,
                    var, self.dummy_index, i))

            self.dummy_index += 1
        elif isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Index):
            # found index assignment
            js = ["%s.__setitem__(%s, %s);" % (self.visit(target.value),
                self.visit(target.slice), value)]
        elif isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Slice):
            # found slice assignmnet
            js = ["%s.__setslice__(%s, %s, %s);" % (self.visit(target.value),
                self.visit(target.slice.lower), self.visit(target.slice.upper),
                value)]
        else:
            var = self.visit(target)
            if isinstance(target, ast.Name):
                if not (var in self._scope):
                    self._scope.append(var)
                    declare = "var "
                else:
                    declare = ""
                js = ["%s%s = %s;" % (declare, var, value)]
            elif isinstance(target, ast.Attribute):
                js = ["%s.__setattr__(\"%s\", %s);" % (self.visit(target.value), str(target.attr), value)]
            else:
                raise JSError("Unsupported assignment type")
        return js<commit_msg> * Code style cleanups<commit_after>import py2js
import py2js.compiler
import ast
import inspect
from py2js.compiler import JSError

class Compiler(py2js.compiler.BaseCompiler):

    def visit_Module(self, node):
        module = []

        for stmt in node.body:
            module.extend(self.visit(stmt))

        return module

    def visit_FunctionDef(self, node):
        is_static = False
        is_javascript = False
        if node.decorator_list:
            if len(node.decorator_list) == 1 and \
                    isinstance(node.decorator_list[0], ast.Name) and \
                    node.decorator_list[0].id == "JavaScript":
                is_javascript = True # this is our own decorator
            elif self._class_name and \
                    len(node.decorator_list) == 1 and \
                    isinstance(node.decorator_list[0], ast.Name) and \
                    node.decorator_list[0].id == "staticmethod":
                is_static = True
            else:
                raise JSError("decorators are not supported")

        # XXX: disable $def for now, because it doesn't work in IE:
        if self._class_name:
        #if 1:
            if node.args.vararg is not None:
                raise JSError("star arguments are not supported")

            if node.args.kwarg is not None:
                raise JSError("keyword arguments are not supported")

            if node.decorator_list and not is_static and not is_javascript:
                raise JSError("decorators are not supported")

            defaults = [None]*(len(node.args.args) - len(node.args.defaults)) + node.args.defaults

            js_args = []
            js_defaults = []
            self._scope = [arg.id for arg in node.args.args]

            for arg, default in zip(node.args.args, defaults):
                if not isinstance(arg, ast.Name):
                    raise JSError("tuples in argument list are not supported")

                js_args.append(arg.id)

                if default is not None:
                    js_defaults.append("%(id)s = typeof(%(id)s) != 'undefined' ? %(id)s : %(def)s;\n" % { 'id': arg.id, 'def': self.visit(default) })

            if self._class_name:
                prep = "%s.prototype.%s = Function(function(" % (self._class_name, node.name)
                if not is_static:
                    if not (js_args[0] == "self"):
                        raise NotImplementedError("The first argument must be 'self'.")
                    del js_args[0]
            else:
                prep = "Function(function %s(" % node.name

            js = [prep + ", ".join(js_args) + ") {"]

            js.extend(self.indent(js_defaults))

            for stmt in node.body:
                js.extend(self.indent(self.visit(stmt)))

            js.append('});')

            # #If method is static, we also add it directly to the class
            # if is_static:
            #     js.append("%s.%s = %s.prototype.%s;" % \
            #             (self._class_name, node.name, self._class_name, node.name))
            # #Otherwise, we wrap it to take 'self' into account
            # else:
            #     func_name = node.name
            #     js.append("%s.%s = function() {" % (self._class_name, func_name))
            #     js.append("    %s.prototype.%s.apply(arguments[0],Array.slice(arguments,1));"% (self._class_name, func_name))
            #     js.append("}")

            self._scope = []
            return js
        else:
            defaults = [None]*(len(node.args.args) - len(node.args.defaults)) + node.args.defaults

            args = []
            defaults2 = []
            for arg, default in zip(node.args.args, defaults):
                if not isinstance(arg, ast.Name):
                    raise JSError("tuples in argument list are not supported")
                if default:
                    defaults2.append("%s: %s" % (arg.id, self.visit(default)))
                args.append(arg.id)
            defaults = "{" + ", ".join(defaults2) + "}"
            args = ", ".join(args)
            js = ["var %s = Function(function(%s) {" % (node.name, args)]
            self._scope = [arg.id for arg in node.args.args]
            for stmt in node.body:
                js.extend(self.indent(self.visit(stmt)))
            return js + ["});"]

    def visit_ClassDef(self, node):
        js = []
        bases = [self.visit(n) for n in node.bases]
        if not bases:
            bases = ['object']
        assert len(bases) == 1;
        class_name = node.name
        #self._classes remembers all classes defined
        self._classes[class_name] = node
        self._class_names.add(class_name)

        js.append("var %s = __inherit(%s);" % (class_name, bases[0]));

        #~ methods = []
        self._class_name = class_name
        for stmt in node.body:
            if isinstance(stmt, ast.Assign):
                value = self.visit(stmt.value)
                for t in stmt.targets:
                    var = self.visit(t)
                    js.append("%s.%s = %s;" % (class_name, var, value))
            else:
                js.extend(self.visit(stmt))
        self._class_name = None

        #The following is unnecessary: __init__ is inherited from
        #'object'
        #~ methods_names = [m.name for m in methods]
        #~ if not "__init__" in methods_names:
            #~ # if the user didn't define __init__(), we have to add it ourselves
            #~ # because we call it from the constructor above
            #~ js.append("_%s.prototype.__init__ = function() {" % class_name)
            #~ js.append("}")

        return js

    def visit_Return(self, node):
        if node.value is not None:
            return ["return %s;" % self.visit(node.value)]
        else:
            return ["return;"]

    def visit_Delete(self, node):
        return node

    def visit_AssignSimple(self, left, right):
        target = left
        value  = right
        if isinstance(target, (ast.Tuple, ast.List)):
            js = ["var __dummy%d__ = %s;" % (self.dummy_index, value)]

            for i, target in enumerate(target.elts):
                var = self.visit(target)
                declare = ""
                if isinstance(target, ast.Name):
                    if not (var in self._scope):
                        self._scope.append(var)
                        declare = "var "
                js.append("%s%s = __dummy%d__.__getitem__(%d);" % (declare,
                    var, self.dummy_index, i))

            self.dummy_index += 1
        elif isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Index):
            # found index assignment
            js = ["%s.__setitem__(%s, %s);" % (self.visit(target.value), self.visit(target.slice), value)]
        elif isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Slice):
            # found slice assignmnet
            js = ["%s.__setslice__(%s, %s, %s);" % (self.visit(target.value), self.visit(target.slice.lower), self.visit(target.slice.upper), value)]
        else:
            var = self.visit(target)
            if isinstance(target, ast.Name):
                if not (var in self._scope):
                    self._scope.append(var)
                    declare = "var "
                else:
                    declare = ""
                js = ["%s%s = %s;" % (declare, var, value)]
            elif isinstance(target, ast.Attribute):
                js = ["%s.__setattr__(\"%s\", %s);" % (self.visit(target.value), str(target.attr), value)]
            else:
                raise JSError("Unsupported assignment type")
        return js
{'fraction_non_alphanumeric': 0.12334286456979464, 'fraction_numerical': 0.002729399532102937, 'mean_word_length': 1.6332991101984942, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '3677589', 'n_tokens_mistral': 4443, 'n_tokens_neox': 4241, 'n_words': 1086}