# rule concat: a def passes itself a parameter grown at the end, `p ++ x` or # `List.append(.., p, ys)` (String.append(p, ..) too), as the argument in p's # own position, the slot it carries; p grown into another slot is not a # finding. The argument is that append written in place, inside any number # of parentheses (`(p ++ x)`), or a lone name read from a let: the nearest # `q = ..` or `+q = ..` of that name before the call in its block or an # enclosing one, whose right side is such an append (a later let of the name # shadows it; other binders, a typed or destructuring let and a do-bind do # not count). A String and a List are cons lists, so appending copies all of # p: the loop is quadratic, with right output (a text step that cost three # times the render; a JSON stringify). Prepend (`x <> acc`) # and reverse once at the end, build with `h <> go(t)`, or gather pieces and # join them once. # # The same append passed on through a continuation is the same loop one hop # removed (`cont`): an argument of a call of a function-typed parameter # (`rest(file, List.append(&2, U32, acc, b <> t))`, where rest is # `fl => a => slurp(f, fl, a)`) that appends onto any parameter of the def, # read the same way (in place, in parentheses, or a lone name through its # let). A `+` parameter the body reads more than once is left alone: it # builds a value it also uses elsewhere (`path ++ "/" ++ name`), not an # accumulator. Prepend each chunk and reverse once at the end; the # accumulator's type changes along the chain (ez's slurp, native: 14.75 s -> # 0.36 s at 16 MB). import Base import ../../src.bend as Src import ../../finding.bend as F import ../../syntax/lex.bend as Lex import ../../syntax/tree.bend as Tree import ../calls.bend as Calls import ../../lazy/lazy.bend as Lazy # a leaf that is a lowercase name: its token. The rule matches a leaf's kind # here and in bound.is_eq only; the rest reads kinds through these def lone.name(n: Tree.Node) -> Maybe<&2, Lex.Tok>: match n: case Tree.Leaf{Lex.Tok{Lex.TName{}, t, l, c}}: Some{Lex.Tok{Lex.TName{}, t, l, c}} case other: None{} # a chain that is exactly one lowercase name: its token def lone(n: Tree.Node) -> Maybe<&2, Lex.Tok>: match n: case Tree.NCons{h, Tree.NNil{}}: lone.name(h) case other: None{} # the list a call appends onto: List.append's first list, String.append's first def appended(+t: String, as: List<&2, Tree.Node>) -> Maybe<&2, Lex.Tok>: +list = String.eq(t, "List.append") +str = String.eq(t, "String.append") Bool.pick(Maybe<&2, Lex.Tok>, Bool.or(list, str), lone(Calls.arg(as, Bool.pick(Nat, list, 2n, 0n))), None{}) # a leaf, then a group: the call's append def onto.call(h: Tree.Node, kids: Tree.Node) -> Maybe<&2, Lex.Tok>: match h: case Tree.Leaf{Lex.Tok{k, +t, l, c}}: appended(t, Calls.args(kids)) case other: None{} # a first cell, then the second: a name then `++`, or a call def onto.next(+h: Tree.Node, h2: Tree.Node) -> Maybe<&2, Lex.Tok>: match h2: case Tree.Leaf{Lex.Tok{k, +o, _l, _c}}: Bool.pick(Maybe<&2, Lex.Tok>, Bool.and(Calls.kind.oper(k), String.eq(o, "++")), lone.name(h), None{}) case Tree.Group{open, kids, close}: onto.call(h, kids) case other: None{} # the name an argument appends onto, `p ++ ..` or an append call, inside any # number of parentheses def onto(a: Tree.Node) -> Maybe<&2, Lex.Tok>: match a: case Tree.NCons{Tree.Group{Lex.Tok{_, +o, _, _}, kids, _}, Tree.NNil{}}: Lazy.stop(Maybe<&2, Lex.Tok>, Bool.not(String.eq(o, "(")), None{}, _u => onto(kids)) case Tree.NCons{h, Tree.NCons{h2, rest}}: onto.next(h, h2) case other: None{} # a let of one plain name in scope: the name, and what its right side # appends onto type Let is Data: Let{name: String, onto: Maybe<&2, Lex.Tok>} # what the nearest let of the name appends onto; nothing when no let binds it def find(lets: List<&2, Let>, +name: String) -> Maybe<&2, Lex.Tok>: match lets: case Nil{}: None{} case Con{Let{+nm, m}, rest}: Lazy.stop(Maybe<&2, Lex.Tok>, String.eq(nm, name), m, _u => find(rest, name)) # what the let of a lone name appends onto def via.of(m: Maybe<&2, Lex.Tok>, +lets: List<&2, Let>) -> Maybe<&2, Lex.Tok>: match m: case None{}: None{} case Some{Lex.Tok{k, +t, l, c}}: find(lets, t) # the name an argument appends onto, when it is a lone name read from a let def via(a: Tree.Node, +lets: List<&2, Let>) -> Maybe<&2, Lex.Tok>: via.of(lone(a), lets) # the first maybe when it holds a token, else the second def either(a: Maybe<&2, Lex.Tok>, b: Maybe<&2, Lex.Tok>) -> Maybe<&2, Lex.Tok>: match a: case None{}: b case Some{tok}: Some{tok} # the name an argument appends onto, in place or through a let def seen(+a: Tree.Node, +lets: List<&2, Let>) -> Maybe<&2, Lex.Tok>: either(onto(a), via(a, lets)) # an `=` def bound.eq(k: Lex.TokKind) -> Bool: match k: case Lex.TEq{}: True{} case other: False{} # a name, then `=`, then the right side: the let def bound.of(m: Maybe<&2, Lex.Tok>, +h2: Tree.Node, rhs: Tree.Node) -> Maybe<&2, Let>: match m: case None{}: None{} case Some{Lex.Tok{k, +t, l, c}}: Lazy.stop(Maybe<&2, Let>, Bool.not(Calls.kind.leaf(~bound.eq, h2)), None{}, _u => Some{Let{t, onto(rhs)}}) # a plain name, then `=`, then the right side: the let def bound.named(+h: Tree.Node, +h2: Tree.Node, rhs: Tree.Node) -> Maybe<&2, Let>: bound.of(lone.name(h), h2, rhs) # `+`, then a plain name, `=` and the right side: the let def bound.plus(+h1: Tree.Node, +h2: Tree.Node, rest: Tree.Node) -> Maybe<&2, Let>: match rest: case Tree.NCons{h3, rhs}: Lazy.stop(Maybe<&2, Let>, Bool.not(String.eq(Calls.leaf.text(h1), "+")), None{}, _u => bound.named(h2, h3, rhs)) case other: None{} # the let a statement's own tokens make, when it binds one plain name with # `=`, bare or `+` def bound(kids: Tree.Node) -> Maybe<&2, Let>: match kids: case Tree.NCons{+h1, Tree.NCons{+h2, +rest}}: Lazy.either(Maybe<&2, Let>, Calls.kind.leaf(~Calls.kind.oper, h1), _u => bound.plus(h1, h2, rest), _v => bound.named(h1, h2, rest)) case other: None{} # the lets in scope after one more, when there is one def push(m: Maybe<&2, Let>, +lets: List<&2, Let>) -> List<&2, Let>: match m: case None{}: lets case Some{lt}: lt <> lets # a finding when the name is the parameter of the argument's own slot def hit( m: Maybe<&2, Lex.Tok>, +slot: String, +path: String, +acc: List<&2, F.Finding> ) -> List<&2, F.Finding>: match m: case None{}: acc case Some{Lex.Tok{k, +t, l, c}}: Bool.pick(List<&2, F.Finding>, String.eq(t, slot), F.Finding{path, l, c, U32.from_nat(String.length(t)), "concat", t ++ " is appended to on every step, which copies it each time and makes the loop quadratic; prepend and reverse once at the end, or build with <>."} <> acc, acc) # the findings on a self-call's arguments, each against the parameter in its # position (params, from the argument's own slot on) def hits( as: List<&2, Tree.Node>, +params: List<&2, String>, +lets: List<&2, Let>, +path: String, acc: List<&2, F.Finding> ) -> List<&2, F.Finding>: match as: case Nil{}: List.reverse(&2, F.Finding, acc) case Con{+a, rest}: +slot = Maybe.default(&2, String, List.head(&2, String, params), "") was = seen(a, lets) hits(rest, List.tail(&2, String, params), lets, path, hit(was, slot, path, acc)) # the lets a statement leaves in scope for the statements after it def after(kind: Tree.StmtKind, +kids: Tree.Node, +lets: List<&2, Let>) -> List<&2, Let>: match kind: case Tree.SLet{}: push(bound(kids), lets) case other: lets # every self-call's arguments, with the lets in scope def walk( n: Tree.Node, +name: String, +params: List<&2, String>, +lets: List<&2, Let>, +path: String ) -> List<&2, F.Finding>: match n: case Tree.NCons{Tree.Leaf{Lex.Tok{k, +t, l, c}}, Tree.NCons{Tree.Group{Lex.Tok{_, +o, _, _}, +kids, _}, rest}}: +me = Bool.and(String.eq(o, "("), String.eq(t, name)) List.concat(&2, F.Finding, [ Lazy.stop(List<&2, F.Finding>, Bool.not(me), [], _u => hits(Calls.args(kids), params, lets, path, [])), walk(kids, name, params, lets, path), walk(rest, name, params, lets, path)]) case Tree.NCons{Tree.Group{open, kids, close}, rest}: List.concat(&2, F.Finding, [walk(kids, name, params, lets, path), walk(rest, name, params, lets, path)]) case Tree.NCons{Tree.Stmt{kind, +kids, body}, rest}: List.concat(&2, F.Finding, [walk(kids, name, params, lets, path), walk(body, name, params, lets, path), walk(rest, name, params, after(kind, kids, lets), path)]) case Tree.NCons{h, rest}: walk(rest, name, params, lets, path) case other: Nil{} def check.go(ds: List<&2, Calls.Def>, +path: String, acc: List<&2, List<&2, F.Finding>>) -> List<&2, F.Finding>: match ds: case Nil{}: List.concat(&2, F.Finding, List.reverse(&2, List<&2, F.Finding>, acc)) case Con{Calls.Def{+name, +sig, body}, rest}: check.go(rest, path, Lazy.stop(List<&2, F.Finding>, Calls.exempt(path, sig), [], _u => walk(body, name, Calls.names(sig), [], path)) <> acc) # the rule def check(s: Src.Src) -> List<&2, F.Finding>: Src.Src{path, text, toks, tree, bound, items} = s check.go(Calls.defs(tree), path, []) # --------------------------------------------------------------------------- # through a continuation: `rest(file, List.append(.., acc, ..))`, where rest # is a function-typed parameter (`fl => a => slurp(f, fl, a)`), grows acc the # same way a self-call does, one hop removed # does a parameter's chain open with `+`? def plus(p: Tree.Node) -> Bool: match p: case Tree.NCons{+h, rest}: Bool.and(Calls.kind.leaf(~Calls.kind.oper, h), String.eq(Calls.leaf.text(h), "+")) case other: False{} # how many times a plain name is read under the node def uses(nn: Tree.Node, +name: String) -> Nat: match nn: case Tree.NCons{h, rest}: Nat.add(uses(h, name), uses(rest, name)) case Tree.Group{open, kids, close}: uses(kids, name) case Tree.Stmt{kind, kids, body}: Nat.add(uses(kids, name), uses(body, name)) case Tree.Leaf{Lex.Tok{Lex.TName{}, +t, l, c}}: Bool.pick(Nat, String.eq(t, name), 1n, 0n) case other: 0n # past the first read: a second one drops a `+` parameter def keep.again(+isplus: Bool, more: Nat) -> Bool: match more: case 0n: True{} case 1n+m: Bool.not(isplus) # may an append onto the parameter be reported? Any parameter but a `+` one # the body reads more than once: that one builds a value it also uses # elsewhere (`path ++ "/" ++ name`), not an accumulator def keep(+isplus: Bool, count: Nat) -> Bool: match count: case 0n: True{} case 1n+more: keep.again(isplus, more) def accs.go(ps: List<&2, Tree.Node>, +body: Tree.Node, +acc: List<&2, String>) -> List<&2, String>: match ps: case Nil{}: acc case Con{+p, rest}: +nm = Calls.param_name(p) accs.go(rest, body, Bool.pick(List<&2, String>, keep(plus(p), uses(body, nm)), nm <> acc, acc)) # the parameters an append onto may be reported def accs(sig: Tree.Node, +body: Tree.Node) -> List<&2, String>: accs.go(Calls.params(sig), body, []) def conts.go(ps: List<&2, Tree.Node>, +acc: List<&2, String>) -> List<&2, String>: match ps: case Nil{}: acc case Con{+p, rest}: conts.go(rest, Bool.pick(List<&2, String>, Calls.has_arrow(p), Calls.param_name(p) <> acc, acc)) # the function-typed parameters, `rest: File -> List<&2, U32> -> IO(..)`: the # continuations a call may pass an accumulator through def conts(sig: Tree.Node) -> List<&2, String>: conts.go(Calls.params(sig), []) # a finding when the name is one of the reportable parameters def hit.cont( m: Maybe<&2, Lex.Tok>, +accs: List<&2, String>, +path: String, +acc: List<&2, F.Finding> ) -> List<&2, F.Finding>: match m: case None{}: acc case Some{Lex.Tok{k, +t, l, c}}: Bool.pick(List<&2, F.Finding>, List.contains(~String, ~String.eq, accs, t), F.Finding{path, l, c, U32.from_nat(String.length(t)), "concat", t ++ " is appended to and passed on through a continuation, which copies it on every step and makes the loop quadratic; prepend each chunk and reverse once at the end."} <> acc, acc) # the findings on a continuation call's arguments def hits.cont( as: List<&2, Tree.Node>, +accs: List<&2, String>, +lets: List<&2, Let>, +path: String, acc: List<&2, F.Finding> ) -> List<&2, F.Finding>: match as: case Nil{}: List.reverse(&2, F.Finding, acc) case Con{+a, rest}: hits.cont(rest, accs, lets, path, hit.cont(seen(a, lets), accs, path, acc)) # every continuation call's arguments, with the lets in scope def cwalk( n: Tree.Node, +conts: List<&2, String>, +accs: List<&2, String>, +lets: List<&2, Let>, +path: String ) -> List<&2, F.Finding>: match n: case Tree.NCons{Tree.Leaf{Lex.Tok{k, +t, l, c}}, Tree.NCons{Tree.Group{Lex.Tok{_, +o, _, _}, +kids, _}, rest}}: +me = Bool.and(String.eq(o, "("), List.contains(~String, ~String.eq, conts, t)) List.concat(&2, F.Finding, [ Lazy.stop(List<&2, F.Finding>, Bool.not(me), [], _u => hits.cont(Calls.args(kids), accs, lets, path, [])), cwalk(kids, conts, accs, lets, path), cwalk(rest, conts, accs, lets, path)]) case Tree.NCons{Tree.Group{open, kids, close}, rest}: List.concat(&2, F.Finding, [cwalk(kids, conts, accs, lets, path), cwalk(rest, conts, accs, lets, path)]) case Tree.NCons{Tree.Stmt{kind, +kids, body}, rest}: List.concat(&2, F.Finding, [cwalk(kids, conts, accs, lets, path), cwalk(body, conts, accs, lets, path), cwalk(rest, conts, accs, after(kind, kids, lets), path)]) case Tree.NCons{h, rest}: cwalk(rest, conts, accs, lets, path) case other: Nil{} def cont.go(ds: List<&2, Calls.Def>, +path: String, acc: List<&2, List<&2, F.Finding>>) -> List<&2, F.Finding>: match ds: case Nil{}: List.concat(&2, F.Finding, List.reverse(&2, List<&2, F.Finding>, acc)) case Con{Calls.Def{name, +sig, +body}, rest}: cont.go(rest, path, Lazy.stop(List<&2, F.Finding>, Calls.exempt(path, sig), [], _u => cwalk(body, conts(sig), accs(sig, body), [], path)) <> acc) # the rule's other half: appends passed on through a continuation def cont(s: Src.Src) -> List<&2, F.Finding>: Src.Src{path, text, toks, tree, bound, items} = s cont.go(Calls.defs(tree), path, [])