You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.7 KiB

Names

Naming is a kind of abstraction. It is the process of attaching a label to a "thing". Common "things" described by labels in a programming language are literal values, constants, variables, procedures, functions, modules, types, etc.

In a programming language, names are organized into contexts known as namespaces. In a namespace, each name has one and only one value. The structure of name spaces in a program is known as the environment. Thus, the environment is the structure responsible for binding or associating a name with its value.

When we think about names in a program, we need to consider two essential qualities with respect to a name, its scope and its extent.

Scope is the context in which a name has a particular meaning.

Extent is the lifetime during which the value associated with the name may be accessed.

Extent occurs in a temporal process, in a program one measure of time is the sequence of function activations that occur while the program is running. An activation is created when a function or method is called.

An activation is a data structure that describes the instantaneous state of a running program. An activation implicity must know the following things:

structure activation
	return address
	return value (if a function)
	previous activation
	environment
		temporary variable scope
		parameter scope
		outer scope
	(In Javascript there is also an execution context known as, 'this'.)

Activations are mostly allocated on a last allocated, first released basis. For performance reasons, activations are often allocated on a stack. This is only allowed if temporary variables and parameters have an extent that is the same as the duration of the function or method. Note that in some languages this is not possible because the environment may be reached even after the function has returned!

To resolve a name into a value, a program needs to find the scope in which that name is defined. There are two kinds of scoping found in programming languages, lexical and dynamic.

A lexical scope is a scope in which the binding of a name may be determined by reading the source code.

A dynamic scope is a scope in which the binding of a name is determined by following the chain of activations until a name binding is found.

All sane languages are lexically bound. It is very difficult to reason about dynamic scope and languages that have them, like LISP, need to be handled with great care. Thankfully, there is a lexically scoped version of LISP called Scheme that we will be using later in the course.

A type in a formal language is a set of values. Nothing more, nothing less. However, when we define programming languages, it is useful to define these sets as sets of meaningfully related values. For example, in Java we say the set {true, false} defines the type boolean. We also say the set of all 32 bit 2s-complement signed integers are the type int.

Programming language type systems may be analyzed in two orthogonal dimensions, strong versus weak typing and static versus dynamic typing.

A strongly typed programming language does not allow type casting, coercion, or conversion..

A weakly typed programming language allows type casting, coercion, or conversion.

A statically typed language requires that variables, parameters, and function/method return values be explicitly typed. That is, a statically typed language types the reference.

A dynamically typed languages store all type information in the value itself. That is, a dynamically typed langage types the value.

A first class object is a value that may be assigned to a variable, passed as an argument, or returned from a function.