---
# System prepended metadata

title: Untitled

---



**Question 1**
There is a difference in the handling of function declarations between JavaScript on the one hand and the Source Academy and the MCE on the other hand: In JavaScript, the function declarations that appear anywhere in a statement sequence of a block are automatically moved to the beginning of the block. In JavaScript, the following program will produce the 
value 42:
```javascript=
{
const x = f(8);
function f(y) {
    return y + 34;
}
x;
}
```
because any JavaScript system will move the function declaration to the beginning of the
block:
```javascript=

function f(y) {
    return y + 34;
}
const x = f(8);
x;
```

before the program is evaluated. Verify that this is not the case in the Source Academy or in the MCE. Modify the evaluator MCE 5 of Brief B11 such that it behaves like JavaScript implementations in this respect.

```javascript =

: is_sequence(component)
           ? eval_sequence(push_fns_front(sequence_statements(component)), env)

function push_fns_front(stmts){
    const fns =  filter(x => is_function_declaration(x), stmts);
    const rest = filter(x => !is_function_declaration(x), stmts);
    return append(fns, rest);
}



// Modified parse_and_evaluate to check for function declaration and move all function declaration to the top
// Added is_not_function_declaration to filter out non function declarations
function parse_and_evaluate(input) {
    let program = parse(input);
    if (is_sequence(program)) {
        const fun_decl = filter(is_function_declaration, head(tail(program)));
        if (!is_null(fun_decl)) {
            const not_fun_decl = filter(is_not_function_declaration, head(tail(program)));
            set_tail(program, list(append(fun_decl, not_fun_decl)));
        }
    }
    const implicit_top_level_block = make_block(program);
    return evaluate(implicit_top_level_block,
                    the_global_environment);
}
```

### Question 2

The evaluator MCE 5 of Brief B11 does not detect undeclared names. Therefore, the follow-
ing program runs without error in the MCE:
```javascript=
false ? abracadabra(simsalabim) : 42;
```
The Source Academy, on the other hand, gives nice error messages for any name that is not
declared. Modify the evaluator such that any undeclared name is detected and reported to
the user as an error.
Hint: The function scan_out_declarations of the given MCE might come in handy.

```javascript=
function scan_out_application(component) {
    return is_sequence(component)
           ? accumulate(append,
                        null,
                        map(scan_out_application,
                            sequence_statements(component)))
           : is_conditional(component)
           ? accumulate(append, 
                        null, 
                        map(scan_out_application, 
                            tail(component)))
           : is_application(component)
           ? accumulate(append, 
                        null, 
                        map(scan_out_application, 
                            tail(component)))
           : is_list(head(component))
           ? accumulate(append, 
                        null, 
                        map(scan_out_application, 
                            component))
           : is_name(component)
           ? list(symbol_of_name(component))
           : is_declaration(component)
           ? list(declaration_symbol(component))
           : null;
}


function eval_block(component, env) {
    const body = block_body(component);
    const locals = scan_out_declarations(body);
    const applications = scan_out_application(body);
    const unassigneds = list_of_unassigned(locals);
    for (let i = 0; i < length(applications); i = i + 1) {
        display("hi");
        const curr = list_ref(applications, i);
        if (is_null(member(curr, locals))) {
            return error(curr, "unbound name");
        }
    }
    return evaluate(body, extend_environment(locals,
                                             unassigneds, 
                                             env));
}

function parse_and_evaluate(input) {
    const program = parse(input);
    const implicit_top_level_block = make_block(program);
    scan_out_application(program)
    return evaluate(implicit_top_level_block,
                    the_global_environment);
}

```
