variables_resolution.md
July 22, 2021 ยท View on GitHub
Goals
- "Global" variables resolution. This means that it's desirable that variable defined in one file should be linked to usage of it in other file. Example: go to declaration, find all usages, auto-completion.
- From the other side we don't want that single-file scripts suffer from contamination by variables defined in other unrelated files.
Obviously, the two goals above conflict with each other. We need to come up with good heuristics to handle both cases acceptably good and/or provide a way to structure a code in a way to hint the intellij-awk for desired behavior.
Considerations
- variable usages starting capital-case:
Var. Usually this denotes global variable.- resolution let's not rely on this to be more universal
Declaration-like cases
Var = ...split("",Arr)- awk idiomatic way to define empty arrayArr["key"] = ...Arr["key"]
Initializing context
- variable occurrences in
BEGINblocks - variable occurrences in
init*()functions
Let's implement next heuristic (in order of attempt):
RESOLVE-ARGresolve current function argumentRESOLVE-CUR-INIT-DECLresolve first of Declaration-like cases in current file in Initializing contextRESOLVE-ALL-INIT-DECLresolve first of Declaration-like cases in all project files in Initializing contextRESOLVE-CUR-INIT-VARresolve first variable occurrence in current file in Initializing context- cases like
function process() { print Line } BEGIN { while(getline Line) process() }
- cases like
RESOLVE-CUR-DECLresolve first of Declaration-like cases in current fileRESOLVE-CUR-VARresolve first variable occurrence in current file- why this is backward-compatible
Bottom line: we don't try to resolve global var in other files unless it looks like declaration in initialization context.
TODO
- Global resolution should exclude variables that resolve to function arguments
- handle case
function a(group) { print group } function b() { match(s,/s/,group) }- should not resolve to arg
- handle case
- Handle
match(STR, /ERE/, group)case - idea resolve
Option["opt_name"]toOption["opt_name"] = ...