|
| 1 | +# Specification of Julia Scoping Rule |
| 2 | + |
| 3 | +`S` indicates the current scope. |
| 4 | +Some terminologies(as well as interfaces provided in NameResolution.jl) : |
| 5 | +- `LHS`: left-hide-side |
| 6 | +- `RHS`: right-hide-side |
| 7 | +- `pseudo scope`: a scope but does not introduce free variables in its parent scope. I don't know how to call it actually, `pseudo scope` is a tentative name. |
| 8 | +- `LOCAL`: to mark a symbol as **local**, instead of resolving it automatically |
| 9 | +- `GLOBAL`: to mark a symbol as **global**, instead of resolving it automatically |
| 10 | + |
| 11 | +## For Loop |
| 12 | + |
| 13 | +```julia |
| 14 | +for i = I, j in J |
| 15 | + body |
| 16 | +end |
| 17 | +``` |
| 18 | + |
| 19 | +`i` is LHS in in `S/for1`, `I` is RHS in `S`. |
| 20 | + |
| 21 | +`j` is LHS in `S/for1/for2`, `J` is RHS in `S/for1`. |
| 22 | + |
| 23 | +`body` is RHS in `S/for1/for2`. |
| 24 | + |
| 25 | + |
| 26 | +## Standalone Assignments |
| 27 | + |
| 28 | +```julia |
| 29 | +$f = a |
| 30 | +``` |
| 31 | + |
| 32 | +If `$f` is a function calling expression, it's treated as `function $f; a end` . See [Function Definition](#Function Definition). |
| 33 | + |
| 34 | +Otherwise, all symbols(except heads of expressions) of `$f` are LHS in `S`, `a` is RHS in `S`. |
| 35 | + |
| 36 | +## Functions |
| 37 | + |
| 38 | +```julia |
| 39 | +function f(x::A{T}=default_value) where T |
| 40 | + body |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +- `f` is LHS in `S`(however the function name should be marked for further analysis, e.g., self recursions). |
| 45 | + |
| 46 | +There's a *pseudo* scope `S/mk-type-params`, |
| 47 | + |
| 48 | +- `T` in `where T` is LHS in `S/mk-type-params`. |
| 49 | + |
| 50 | +The requirement of a symbol in argument type annotations is from the scope `S/mk-type-params`. |
| 51 | + |
| 52 | +Say, |
| 53 | +- `T` in `f(x::A{T}=value)` is RHS in `S/mk-type-params`, and |
| 54 | +- `A` is RHS in `S/mk-type-params`(though we know `A` is from `S`, pseudo scope doesn't result in free variables). |
| 55 | + |
| 56 | +The function execution has the scope `S/mk-type-params/inner` and, |
| 57 | +- `x` is LHS in `S/mk-type-params/inner`, |
| 58 | +- `default_value` is RHS in `S/mk-type-params/inner`, |
| 59 | +- `body` is RHS in `S/mk-type-params/inner` |
| 60 | + |
| 61 | + |
| 62 | +Except for |
| 63 | +- `Expr(:function, ::Symbol, Expr(:block, _...))` is a declaration |
| 64 | + |
| 65 | +## Let-Bindings |
| 66 | + |
| 67 | +```julia |
| 68 | +let a = b |
| 69 | + c |
| 70 | +end |
| 71 | +``` |
| 72 | + |
| 73 | +`S/let` is a pseudo scope. |
| 74 | + |
| 75 | +- `b` is RHS in `S` |
| 76 | +- `a` is LHS in `S/let` |
| 77 | +- `c` is RHS in `S/let` |
0 commit comments