DEV Community

Bilal Niaz
Bilal Niaz

Posted on • Updated on

What is the scope and compiler?

Scope:
A scope is a section of the programme where variables can be declared, and there are three places where variables can be declared in general:

  • Inside a function or a block which is called local variables,

  • In the definition of function parameters which is called formal parameters.

  • Outside of all functions which is called global variables.

Without such a concept, a program could perform some tasks, but they would be extremely limited and not terribly interesting.
We'll approach learning about scope by thinking of it as a dialogue.

  1. Engine: responsible for start-to-finish compilation and execution of our JavaScript program.

  2. Compiler: one of Engine's friends; handles all the dirty work of parsing and code-generation (see previous section).

  3. Scope: another friend of Engine; collects and maintains a look-up list of all the declared identifiers (variables), and enforces a strict set of rules as to how these are accessible to currently executing code.

Compiler:
A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses.
In a traditional compiled-language process, a chunk of source code, your program, will undergo typically three steps before it is executed, roughly called "compilation":
Tokenizing/Lexing:
Breaking up a string of characters into meaningful (to the language) chunks, called tokens.For instance, consider the program:var a = 2;This program would likely be broken up into the following tokens:var, a, =, 2, and ;. Whitespace may or may not be persisted as a token, depending on whether it's meaningful or not.
The difference between tokenizing and lexing is complex and technical distinction.
Parsing:
Converting a stream (array) of tokens into a tree of nested elements that collectively indicate the program's grammatical structure This type of tree is known as a "AST" (Abstract Syntax Tree).
The tree for var a = 2; might start with a top-level node called VariableDeclaration, with a child node called Identifier (whose value is a), and another child called AssignmentExpression which itself has a child called NumericLiteral (whose value is 2).
Code-Generation:
The procedure for converting an AST into executable code. This component varies a lot based on the language, the platform it's aimed towards, and other factors.

We need a little bit more compiler terminology to proceed further with understanding.

When Engine executes the code that Compiler produced for step (2), it has to look-up the variable a to see if it has been declared, and this look-up is consulting Scope. But the type of look-up Engine performs affects the outcome of the look-up.

In our case, it is said that Engine would be performing an "LHS" look-up for the variable a. The other type of look-up is called "RHS".

I bet you can guess what the "L" and "R" mean. These terms stand for "Left-hand Side" and "Right-hand Side".
let give a example:
console.log( a );
The reference to a is an RHS reference, because nothing is being assigned to a here. Instead, we're looking-up to retrieve the value of a, so that the value can be passed to console.log(..).
By contrast:
a = 2;
The reference to a here is an LHS reference, because we don't actually care what the current value is, we simply want to find the variable as a target for the = 2 assignment operation.

Top comments (0)