Being the Interpreter
13 Jul 2015General Rules and Setup for Interpreting a Program
First, mark down a box labeled “current line”. Every step you take, make a note of what line you’re on.
You’ll start at the first line of the program and, unless some rule specifies otherwise, go to the next line of code after you’re finished with each line.
Also make a special section labled “output”, which you’ll use every time something is written to the console by the program.
If a line of code is an expression only, evaluate the expression as normal then throw away the return value of the expression.
Variable declaration
Look at your program. For all of the instances you see of var name
or var name = expression
(that isn’t in the body of a function (and if you haven’t seen functions yet, don’t worry)), make a table that looks like
name1 | name2 | name3 | name4 | … |
It should have one column for each variable name.
You don’t actually fill anything in to start, instead if there’s a = expression
portion of the variable declaration you wait until the line in question is reached before filling in the entry in the table according to the rules of the assignment expression.
Expressions
If an expression is the only thing on the line, evaluate the expression according to the appropriate rules for that expression.
Arithmetic
Numbers evaluate to themselves. Arithmetic operations evaluate exactly according to their them to: +
is addition, -
is subtraction, etc.
Strings
Strings evaluate to themselves. The +
operator “concatenates” two strings together.
Booleans
true
evaluates to true
, false
evaluates to false
.
The boolean operator !
takes an expression. Evaluate ! exp
by first evaluating the expression exp
. If it returns a truthy value, then return false
. If it returns a falsy value, then return true
.
The short-circuiting operators &&
and ||
have special rules. exp1 && exp2
is evaluated by first evaluating exp1
, if it is truthy then evaluate exp2
and return its value. If it is falsy, then return the value of exp1
.
exp1 || exp2
is evaluated by first evaluating exp1
. If it is truthy then return the value of exp1
. If it is falsy then evaluate exp2
and return its value.
As a reminder, falsy values are NaN
, null
, undefined
, 0
, "", and false
. Everything else is truthy.
Assignment
Assignment is always of the form name = expression
. First, you evaluate the expression based on the kind of expression it is, then fill whatever value it returns into the appropriate entry in the table.
The value you wrote into the table is also the value returned by the expression.
Output to console
For purposes of “being the interpreter”, we’re going to treat the function console.log
as a special operation. When you see an expression of the form console.log(exp)
, evaluate the expresion that is the argument, then write the value in the output column you’ve set aside. As an expression, console.log
returns undefined
.
typeof
The typeof
operator takes an expression as an argument. Evaluate this expression is and return, as a string, the type of the value returned according to the following rules
- numbers return “number”
- this includes
NaN
andInfinity
- this includes
- strings return “string”
- undefined returns “undefined”
- objects return “object”
- booleans return “boolean”
For loops
A basic for loop has the form
for (initialization; condition_for_continuing; next_step){
statement1;
statement2;
statement3;
...
}
It’s not strictly required, but you should make the “initialization” code only be of the form var name = exp
or name = exp
. The condition for continuing the loop should be an expression that returns a boolean. The next step slot should be an assignment expression that modifies the variable named in the initialization.
The rule is that you
- execute the code in the “initialization” slot
- evaluate the condition for continuing
- if it is truthy, go to step (3)
- if it falsey, jump to the line of code after the end of the for loop
- execute the statements in the for loop
- execute the code in the “next step” part of the for loop
- go to step (2)
While loops
A while loop has the form
while (condition){
statement1;
statement2;
statement3;
...
}
The rule is that you
- evaluate the condition
- if it is truthy, go to step (2)
- if it is falsey, jump to the line of code after the end of the while loop
- execute the statements in the while loop
- go to step (1)
If statements
If statements have the basic form
if (condition){
statement1;
statement2;
...
}
else {
morestatement1;
morestatement2;
morestatement3;
}
The rule for them is that you
- evaluate the condition
- if it is truthy, perform the statements listed between the braces of the “if”
- if it is falsy, perform the statements listed between the braces of the “else”
The other form of if-statement is to leave out the else
branch. In this case, our rule reads
- evaluate the condition
- if it is truthy, perform the statements listed between the braces of the “if”
- if it is falsy, do nothing