<commit_before>import sys
import ast
import model
import error

class BuiltinMetaType(model.Builtin):
    type = None
    runtime_depends = []
    def __str__(self):
        return 'BuiltinMetaType'
    
BUILTIN_META_TYPE = BuiltinMetaType()
BUILTIN_META_TYPE.type = BUILTIN_META_TYPE

class BuiltinType(model.Builtin):
    def __init__(self, name):
        self.name = name
        self.type = BUILTIN_META_TYPE
        self.runtime_depends = []
        
    def __str__(self):
        return 'BuiltinType(%s)' % self.name

    def execute(self, context):
        return self

class BuiltinFunction(model.Builtin):
    def __init__(self, name, arg_types, return_type, impl, compile_time, context):
        model.Builtin.__init__(self)
        self.name = name
        
        arg_types = [context.resolve_type(ast.Term(at)) for at in arg_types]
        if return_type:
            return_type = context.resolve_type(ast.Term(return_type))
        self.type = model.FuncType(arg_types, return_type)
        
        self.impl = impl
        self.runtime_depends = []
        if compile_time:
            self.call_runtime_depends = []
        else:
            self.call_runtime_depends = [self]

    def execute(self, context):
        return self

    def call(self, context, args):
        arg_values = [arg.value for arg in args]
        ret_value = self.impl(context, arg_values)
        if self.type.return_type:
            return model.Value(ret_value, self.type.return_type, None)

    def __str__(self):
        return 'BuiltinFunction[%s](%s)' % (len(self.runtime_depends), self.name)

class Builtins(model.Context):
    def __init__(self, stdout=sys.stdout):
        model.Context.__init__(self, None)

        def abort(*args):
            raise error.InterpreterError('abort')
        self.add_function('abort', [], None, abort, False)

        bool_type = BuiltinType('Bool')
        self.add_term('Bool', bool_type, None)
        self.add_term('true', model.Value(True, bool_type, None), None)
        self.add_term('false', model.Value(False, bool_type, None), None)
        
        self.add_function('bprint', ['Bool'], None, lambda x, args: stdout.write(str(args[0]) + '\n'), False)
        self.add_function('and', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] and args[1])
        self.add_function('or', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] or args[1])
        self.add_function('xor', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] != args[1])
        self.add_function('not', ['Bool'], 'Bool', lambda x, args: not args[0])
        self.add_function('beq', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] == args[1])
        self.add_function('bneq', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] != args[1])
<commit_msg>builtin functions view fix<commit_after>import sys
import ast
import model
import error

class BuiltinMetaType(model.Builtin):
    type = None
    runtime_depends = []
    def __str__(self):
        return 'BuiltinMetaType'
    
BUILTIN_META_TYPE = BuiltinMetaType()
BUILTIN_META_TYPE.type = BUILTIN_META_TYPE

class BuiltinType(model.Builtin):
    def __init__(self, name):
        self.name = name
        self.type = BUILTIN_META_TYPE
        self.runtime_depends = []
        
    def __str__(self):
        return 'BuiltinType(%s)' % self.name

    def execute(self, context):
        return self

class BuiltinFunction(model.Builtin):
    def __init__(self, name, arg_types, return_type, impl, compile_time, context):
        model.Builtin.__init__(self)
        self.name = name
        
        arg_types = [context.resolve_type(ast.Term(at)) for at in arg_types]
        if return_type:
            return_type = context.resolve_type(ast.Term(return_type))
        self.type = model.FuncType(arg_types, return_type)
        
        self.impl = impl
        self.runtime_depends = []
        if compile_time:
            self.call_runtime_depends = []
        else:
            self.call_runtime_depends = [self]

    def execute(self, context):
        return self

    def call(self, context, args):
        arg_values = [arg.value for arg in args]
        ret_value = self.impl(context, arg_values)
        if self.type.return_type:
            return model.Value(ret_value, self.type.return_type, None)

    def __str__(self):
        return 'BuiltinFunction[%s](%s)' % (len(self.call_runtime_depends), self.name)

class Builtins(model.Context):
    def __init__(self, stdout=sys.stdout):
        model.Context.__init__(self, None)

        def abort(*args):
            raise error.InterpreterError('abort')
        self.add_function('abort', [], None, abort, False)

        bool_type = BuiltinType('Bool')
        self.add_term('Bool', bool_type, None)
        self.add_term('true', model.Value(True, bool_type, None), None)
        self.add_term('false', model.Value(False, bool_type, None), None)
        
        self.add_function('bprint', ['Bool'], None, lambda x, args: stdout.write(str(args[0]) + '\n'), False)
        self.add_function('and', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] and args[1])
        self.add_function('or', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] or args[1])
        self.add_function('xor', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] != args[1])
        self.add_function('not', ['Bool'], 'Bool', lambda x, args: not args[0])
        self.add_function('beq', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] == args[1])
        self.add_function('bneq', ['Bool', 'Bool'], 'Bool', lambda x, args: args[0] != args[1])

{'fraction_non_alphanumeric': 0.14095649047105357, 'fraction_numerical': 0.004314994606256742, 'mean_word_length': 2.928672316384181, 'pattern_counts': {'":': 0, '<': 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': '7604267', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1782, 'n_words': 403}