170 likes | 352 Views
Variables. Names Binding Types and Type Checking Scope and Lifetime. Variable Names. Syntactic restrictions Maximum (character) length Valid characters Alphanumeric, “connectors”, etc. Case-sensitivity Reserved words. Variables. What is a variable?
E N D
Variables • Names • Binding • Types and Type Checking • Scope and Lifetime
Variable Names • Syntactic restrictions • Maximum (character) length • Valid characters • Alphanumeric, “connectors”, etc. • Case-sensitivity • Reserved words
Variables • What is a variable? • Abstraction of computer memory cell or collection of cells • Assembly Language = Machine Language + names • Tuple of attributes: • Name • Address • Value • Type • Lifetime • Scope
Variable Address • Not necessarily exclusive • Many variables can refer to same address(aliasing) • Not necessarily constant • Variables often refer to different addresses in lifetime of program (e.g., recursive function) • L-value
Variable Type • Range of values a variable can have • Can be dictated by machine limitations(e.g., C short) • Often enforced by compiler/interpreter • Strong typing vs. latent (or dynamic) typing
Variable Value • Contents of (possibly abstract) memory cell addressed by variable • R-value
Variable Binding • Binding – the association between variable and value • Binding time – when the association takes place • Language-definition time (e.g., + in C) • Compiler-design time (e.g., int in C) • Compile-time (e.g., int count = 5) • Run-time (e.g., count = count + 5)
Binding Attributes • Static vs. dynamic • Can binding change over lifetime of program? • Type • Can be set explicitly or implicitly • Consider: FORTRAN, BASIC, Perl • Dynamic type binding: Scheme, LISP, APL • Type Inference: ML, Miranda, Haskell
Variable Binding Lifetime • For how long (in program’s execution) does binding persist? • Categories: • Static • Stack-dynamic • Explicit heap-dynamic • Implicit heap-dynamic
Variable Binding Lifetime (2) • Static Binding • Binding made “before” program execution, remain bound until program termination. • Type, address are static • Efficiency: memory reference can be calculated at compile-time • Cannot be used with recursion
Variable Binding Lifetime (3) • Stack-dynamic • Type is static, address is dynamic • Directly supports recursion • Efficiently supported by run-time stacks • Not necessarily as efficient as static variable bindings
Variable Binding Lifetime (4) • Explicit Heap-Dynamic • Storage explicitly allocated/deallocated out of memory pool at run-time • Useful for dynamically-sized structures (e.g. linked-lists, stacks) • Programming complexity to maintain integrity(e.g., dangling references, array-bounds checking)
Variable Binding Lifetime (5) • Implicit Heap-Dynamic • Memory storage allocated when value is assigned • Very flexible • Reduced programmer complexity • Run-time overhead (array bounds checking, garbage collection)
Variable Scope • Range of program statements in which variable is visible. • Static scoping (lexical scoping)Scope can be determined at compile time • Dynamic scopingScope depends on execution Lifetime “When” Scope “Where”
Lexical vs. Dynamic Scoping (define a 12) (define add-a (let ((a 45)) (lambda (n) (+ n a))) (let ((a 12)) (add-a 15)) Lexical: 60 Dynamic: 27
Namespaces: Functions vs. Data • Many languages (e.g. C++, Common Lisp) maintain separate namespaces for functions and data names • Often have different scoping and lifetime rules too
Garbage Collection • Practically essential for systems with implicit heap-dynamic object • Garbage: anything not “reachable” from known roots. • Roots: Current stack + current variable bindings