Names and Scope
This outline covers chapter four, and mentions a few additional
things. You are responsible for all of it.
- Legal names.
- Characters: Usually letters and numbers, starting with a letter.
- Others may be allowed
- Underscore is common. COBOL allows a dash.
- Use may be limited.
- Not at the ends.
- Ada allows underscore, but not two in a row.
- Case-sensitivity.
- Special names
- Keywords (e.g. while): Otherwise legal identifiers.
- Predefined words (e.g. printf): May be redefined.
- Keyword-in-context.
- Words that can be identifiers or keywords, depending on placement.
- FORTRAN.
- Markers distinguish variables: $ in perl and php.
- Variables.
- Binding: Name, address, type and scope.
- l-value and r-value.
- Some languages make these uses explicit.
- References.
- Allows a variable to hold the l-value of another variable.
- May (C, C++, most compiled) or may not (Java, many scripting)
require explicit deference.
- Scope.
- The collection of statements which can access that binding, without
qualification.
- Usual delimiters: {, }, begin, end
- Static or dynamic.
- Static: determined by program layout.
- Dynamic: determined by call order. Obsolete.
- Disjoint or nested.
Pascal
Example
Java Example
- Local and non-local references.
- Named scopes
- Modules and classes.
- Ada Integer Stack: Package
Implementation
Driver.
- Java Integer Stack: Class
Driver.
- C++ Integer Stack: Class
Implementation
Driver.
- Allows qualified reference from outside.
- Forward reference.
- Usually, definition must precede reference.
- Java is an exception.
- Declaration before definition.
- Usually an implicit global scope.
- Resolving References.
- Start with current scope and work out through enclosing scopes.
- Names declared in a closer scope may hide outer ones of same name.
- Hidden name is invisible.
- Local declarations may hid parameters.
- Compiler keeps declarations in a symbol table.
- Usually a separate table for each scope.
- Nested scopes make a stack of tables.
- Overloading
- Giving multiple meanings to the same visible symbol.
- Most often functions or operators resolved by argument type.
Usually not return type, except Ada.
- Allowing the user to create overloads is fairly new.
- Overloading function names.
- Overloading operators (C++ and some others).
- Lifetime: The interval during which the variable is allocated some storage.
- Typically determined by scope, but a declaration may override.
- Static: lifetime of the program.
- Global variables.
- Declared so, as static in C.
- static.c
- Local variables: lifetime of their defining scope.
- In a function: entry to exit of the enclosing block.
- In a class.
- Object (usual) variables: Lifetime of the object that contains them.
- Static (class) variables: Lifetime of the class itself.
Often equals the lifetime of the program, but may not.
- Dynamic variables: from creation (new, malloc) to
deletion (delete, free, gc cleanup).
- Closure
- Referencing environment: All the variables you can use.
The sum of your scopes
- Closure: Executable code combined with a referencing environment to
to run it in.
- Lisp lambda, passed function references in some languages.