<commit_before>import structs/[ArrayList]
import Type, Declaration, Expression, Visitor, TypeDecl, VariableAccess,
       Node, ClassDecl, FunctionCall, Argument
import tinker/[Response, Resolver, Trail]

VariableDecl: class extends Declaration {

    name: String
    type: Type
    expr: Expression
    owner: TypeDecl

    isArg := false
    isGlobal := false
    isConst := false
    isStatic := false
    externName: String = null

    init: func ~vDecl (.type, .name, .token) {
        this(type, name, null, token)
    }

    init: func ~vDeclWithAtom (=type, =name, =expr, .token) {
        super(token)
    }

    accept: func (visitor: Visitor) {
        visitor visitVariableDecl(this)
    }

    setType: func(=type) {}
    getType: func -> Type { type }

    getName: func -> String { name }

    toString: func -> String {
        "%s : %s%s" format(
            name,
            type ? type toString() : "<unknown type>",
            expr ? " = " + expr toString() : ""
        )
    }

    setOwner: func (=owner) {}

    setExpr: func (=expr) {}
    getExpr: func -> Expression { expr }
    
    isStatic: func -> Bool { isStatic }
    setStatic: func (=isStatic) {}
    
    isGlobal: func -> Bool { isGlobal }
    setGlobal: func (=isGlobal) {}
    
    isArg: func -> Bool { isArg }

    getExternName: func -> String { externName }
    setExternName: func (=externName) {}
    isExtern: func -> Bool { externName != null }
    isExternWithName: func -> Bool {
        (externName != null) && !(externName isEmpty())
    }

    resolveAccess: func (access: VariableAccess) {
        if(name == access name) {
            access suggest(this)
        }
    }

    resolve: func (trail: Trail, res: Resolver) -> Response {

        trail push(this)

        //printf("Resolving variable decl %s\n", toString());

        if(expr) {
            response := expr resolve(trail, res)
            f(!response ok()) {
                trail pop(this)
                return response
            }
        }

        if(type == null && expr != null) {
            // infer the type
            type = expr getType()
            if(type == null) {
                trail pop(this)
                res wholeAgain(this, "must determine type of %s\n" format(toString()))
                return Responses OK
            }
        }

        if(type != null) {
            response := type resolve(trail, res)
            f(!response ok()) {
                trail pop(this)
                return response
            }
        }

        trail pop(this)

        parent := trail peek()
        {
            if(!parent isScope() && !parent instanceOf(TypeDecl)) {
                //println("uh oh the parent of " + toString() + " isn't a scope but a " + parent class name)
                idx := trail findScope()
                result := trail get(idx) addBefore(trail get(idx + 1), this)
                trail peek() replace(this, VariableAccess new(this, token))<commit_msg>Uncomprehensible dumb errors in VariableDecl..<commit_after>import structs/[ArrayList]
import Type, Declaration, Expression, Visitor, TypeDecl, VariableAccess,
       Node, ClassDecl, FunctionCall, Argument
import tinker/[Response, Resolver, Trail]

VariableDecl: class extends Declaration {

    name: String
    type: Type
    expr: Expression
    owner: TypeDecl

    isArg := false
    isGlobal := false
    isConst := false
    isStatic := false
    externName: String = null

    init: func ~vDecl (.type, .name, .token) {
        this(type, name, null, token)
    }

    init: func ~vDeclWithAtom (=type, =name, =expr, .token) {
        super(token)
    }

    accept: func (visitor: Visitor) {
        visitor visitVariableDecl(this)
    }

    setType: func(=type) {}
    getType: func -> Type { type }

    getName: func -> String { name }

    toString: func -> String {
        "%s : %s%s" format(
            name,
            type ? type toString() : "<unknown type>",
            expr ? " = " + expr toString() : ""
        )
    }

    setOwner: func (=owner) {}

    setExpr: func (=expr) {}
    getExpr: func -> Expression { expr }
    
    isStatic: func -> Bool { isStatic }
    setStatic: func (=isStatic) {}
    
    isGlobal: func -> Bool { isGlobal }
    setGlobal: func (=isGlobal) {}
    
    isArg: func -> Bool { isArg }

    getExternName: func -> String { externName }
    setExternName: func (=externName) {}
    isExtern: func -> Bool { externName != null }
    isExternWithName: func -> Bool {
        (externName != null) && !(externName isEmpty())
    }

    resolveAccess: func (access: VariableAccess) {
        if(name == access name) {
            access suggest(this)
        }
    }

    resolve: func (trail: Trail, res: Resolver) -> Response {

        trail push(this)

        //printf("Resolving variable decl %s\n", toString());

        if(expr) {
            response := expr resolve(trail, res)
            if(!response ok()) {
                trail pop(this)
                return response
            }
        }

        if(type == null && expr != null) {
            // infer the type
            type = expr getType()
            if(type == null) {
                trail pop(this)
                res wholeAgain(this, "must determine type of %s\n" format(toString()))
                return Responses OK
            }
        }

        if(type != null) {
            response := type resolve(trail, res)
            if(!response ok()) {
                trail pop(this)
                return response
            }
        }

        trail pop(this)

        parent := trail peek()
        {
            if(!parent isScope() && !parent instanceOf(TypeDecl)) {
                //println("uh oh the parent of " + toString() + " isn't a scope but a " + parent class name)
                idx := trail findScope()
                result := trail get(idx) addBefore(trail get(idx + 1), this)
                trail peek() replace(this, VariableAccess new(this, token))
{'fraction_non_alphanumeric': 0.12193903048475763, 'fraction_numerical': 0.0003331667499583542, 'mean_word_length': 1.8016798880074663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '5272770', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1667, 'n_words': 495}