Advanced

Names and scopes

A name is not a value. A name is a place in a table, and the table is chosen by where the name is written, which is the whole of scoping.

What you will see

One line with a reason. The decision is written, which is the claim.

About 1 min of reading

Try It

Resolve a shadowed name

1

Write two tables

printf 'outer x=1\ninner x=2\n' > /tmp/s.txt; tail -1 /tmp/s.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
inner x=2. The inner table answers first, which is shadowing.
2

Refuse a missing name

printf 'use y pos=14\n' | awk '{print ($2=="x") ? "found" : "refuse name not in any table"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
refuse name not in any table. The refusal names the place, which is the answer.
3

Write the shadow decision

printf 'shadow allowed reason=inner-wins\n' > /tmp/d.txt; cat /tmp/d.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line with a reason. The decision is written, which is the claim.

Data Flow

A name to a table or a refusal
1 The pass opens a table at each boundary
↓
2 A use walks the parents
↓
3 The first table that has the name answers
↓
4 No table names the use and its place
Why this works
Why the table is built in one pass

The pass walks the tree and opens tables. Opening on the walk is what makes the table match the text.

The one error that must be caught at start

A name used before it is declared in the same table. The pass sees the order, and the order is the reason it is caught.

Examples

A scope is a table with a parent

Looking up walks the parents. Walking parents is what makes an inner name win over an outer one.

A name that finds no table is refused

The refusal names the use and the place. The place is where the name was written, which is the answer.

Shadowing is a decision

The inner name hides the outer. Hiding is a decision that is written rather than remembered.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Passes as a pipeline
Previous Lesson
Passes as a pipeline
Next Lesson
Generating code instead of answering
Generating code instead of answering →