For convenience in running the metacircular evaluator, we provide a read-eval-print loop. It prints a prompt, reads an input expression from a pop-up window, evaluates this expression in the global environment, and prints the result on the next pop-up window.
function read_eval_print_loop(history) {
    const prog = prompt("History:" + history + 
                        "\\n\\n" + "Enter next: ");
    if (prog === null) {
        display("session has ended");
    } else {
        const res = parse_and_eval(prog);
        read_eval_print_loop(history + "\\n" + 
                             stringify(prog) + " ===> " +
The function parse_and_eval transforms a statement string into a tagged-object representation of the statement according to the description in section 4.1.2, a process called parsing and accomplished by the primitive function parse. After that, it applies the function eval_toplevel to the tagged-object representation.
function parse_and_eval(str) {
    return eval_toplevel(parse(str));

[1] Any function defined in the underlying JavaScript can be used as a primitive for the metacircular evaluator. The name of a primitive installed in the evaluator need not be the same as the name of its implementation in the underlying JavaScript; the names are the same here because the metacircular evaluator implements JavaScript itself. Thus, for example, we could put list("first", head) or list("square", x => x * x) in the list of primitive_functions.
[2] JavaScript's apply method of function objects expects arguments in an array. Thus, the argument_list is transformed into an array using a while loop:
function apply_in_underlying_javascript(prim, argument_list) {
   const argument_array = [];
   let i = 0;
   while (!is_null(argument_list)) {
      argument_array[i] = head(argument_list);
      i = i + 1;
      argument_list = tail(argument_list);
   return prim.apply(prim, argument_array);
We have made use of apply_in_underlying_javascript in to define the function apply in section 2.4.3.
4.1.4 Running the Evaluator as a Program