<commit_before>! Copyright (C) 2008, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: fry locals accessors quotations kernel sequences namespaces
assocs words arrays vectors hints combinators continuations
effects compiler.tree
stack-checker
stack-checker.state
stack-checker.errors
stack-checker.visitor
stack-checker.backend
stack-checker.recursive-state ;
IN: compiler.tree.builder

<PRIVATE

GENERIC: (build-tree) ( quot -- )

M: callable (build-tree) infer-quot-here ;

: check-no-compile ( word -- )
    dup "no-compile" word-prop [ do-not-compile ] [ drop ] if ;

: check-effect ( word effect -- )
    swap required-stack-effect 2dup effect<=
    [ 2drop ] [ effect-error ] if ;

: inline-recursive? ( word -- ? )
    [ "inline" word-prop ] [ "recursive" word-prop ] bi and ;

: word-body ( word -- quot )
    dup inline-recursive? [ 1quotation ] [ specialized-def ] if ;

M: word (build-tree)
    [ check-no-compile ]
    [ word-body infer-quot-here ]
    [ current-effect check-effect ] tri ;

: build-tree-with ( in-stack word/quot -- nodes )
    [
        <recursive-state> recursive-state set
        V{ } clone stack-visitor set
        [ [ >vector \ meta-d set ] [ length d-in set ] bi ]
        [ (build-tree) ]
        bi*
    ] with-infer nip ;

PRIVATE>

: build-tree ( word/quot -- nodes )
    [ f ] dip build-tree-with ;

:: build-sub-tree ( #call word/quot -- nodes/f )
    #! We don't want methods on mixins to have a declaration for that mixin.
    #! This slows down compiler.tree.propagation.inlining since then every
    #! inlined usage of a method has an inline-dependency on the mixin, and
    #! not the more specific type at the call site.
    specialize-method? off
    [
        #call in-d>> word/quot build-tree-with unclip-last in-d>> :> in-d
        {
            { [ dup not ] [ ] }
            { [ dup ends-with-terminate? ] [ #call out-d>> [ f swap #push ] map append ] }
            [ in-d #call out-d>> #copy suffix ]
        } cond
    ] [ dup inference-error? [ drop f ] [ rethrow ] if ] recover ;

: contains-breakpoints? ( word -- ? )
    def>> [ word? ] filter [ "break?" word-prop ] any? ;
<commit_msg>compiler.tree.builder: Fix scoping of a variable by hints vocab<commit_after>! Copyright (C) 2008, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: fry locals accessors quotations kernel sequences namespaces
assocs words arrays vectors hints combinators continuations
effects compiler.tree
stack-checker
stack-checker.state
stack-checker.errors
stack-checker.visitor
stack-checker.backend
stack-checker.recursive-state ;
IN: compiler.tree.builder

<PRIVATE

GENERIC: (build-tree) ( quot -- )

M: callable (build-tree) infer-quot-here ;

: check-no-compile ( word -- )
    dup "no-compile" word-prop [ do-not-compile ] [ drop ] if ;

: check-effect ( word effect -- )
    swap required-stack-effect 2dup effect<=
    [ 2drop ] [ effect-error ] if ;

: inline-recursive? ( word -- ? )
    [ "inline" word-prop ] [ "recursive" word-prop ] bi and ;

: word-body ( word -- quot )
    dup inline-recursive? [ 1quotation ] [ specialized-def ] if ;

M: word (build-tree)
    [ check-no-compile ]
    [ word-body infer-quot-here ]
    [ current-effect check-effect ] tri ;

: build-tree-with ( in-stack word/quot -- nodes )
    [
        <recursive-state> recursive-state set
        V{ } clone stack-visitor set
        [ [ >vector \ meta-d set ] [ length d-in set ] bi ]
        [ (build-tree) ]
        bi*
    ] with-infer nip ;

PRIVATE>

: build-tree ( word/quot -- nodes )
    [ f ] dip build-tree-with ;

:: build-sub-tree ( #call word/quot -- nodes/f )
    #! We don't want methods on mixins to have a declaration for that mixin.
    #! This slows down compiler.tree.propagation.inlining since then every
    #! inlined usage of a method has an inline-dependency on the mixin, and
    #! not the more specific type at the call site.
    f specialize-method? [
        [
            #call in-d>> word/quot build-tree-with unclip-last in-d>> :> in-d
            {
                { [ dup not ] [ ] }
                { [ dup ends-with-terminate? ] [ #call out-d>> [ f swap #push ] map append ] }
                [ in-d #call out-d>> #copy suffix ]
            } cond
        ] [ dup inference-error? [ drop f ] [ rethrow ] if ] recover
    ] with-variable ;

: contains-breakpoints? ( word -- ? )
    def>> [ word? ] filter [ "break?" word-prop ] any? ;

{'fraction_non_alphanumeric': 0.13285457809694792, 'fraction_numerical': 0.004937163375224416, 'mean_word_length': 2.4470224284609436, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, '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': '401166', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1430, 'n_words': 413}