let pop_scope env =
        match env.locals with
        | [] ->
                        (match env.globals.parent with
                                | Some old_globals -> {
                                                        globals = old_globals;
                                                        num_globals = env.num_globals;
                                                        locals = env.locals;
                                                        num_locals = env.num_locals;
                                                        sdepth = env.sdepth;
                                                        max_depth = env.max_depth;
                                                        errors = env.errors;
                                                        warnings = env.warnings;
                                                        unique_id = env.unique_id;
                                                        names = env.names;
                                                        varprops = env.varprops;
                                                        imported = env.imported;
                                                        templates = env.templates;
                                                        constants = env.constants;
                                                }
                                | None -> raise (RuntimeError.InternalError "popping a top level scope"))
        | local:: tl ->
                        match local.parent with
                        | Some old_parent -> {
                                                globals = env.globals;
                                                num_globals = env.num_globals;
                                                locals = old_parent:: tl;
                                                num_locals = env.num_locals; (* preserve, we are still in the same stack frame *)
                                                sdepth = env.sdepth;
                                                max_depth = env.max_depth;
                                                errors = env.errors;
                                                warnings = env.warnings;
                                                unique_id = env.unique_id;
                                                names = env.names;
                                                varprops = env.varprops;
                                                imported = env.imported;
                                                templates = env.templates;
                                                constants = env.constants;
                                        }
                        | None -> {
                                                globals = env.globals;
                                                num_globals = env.num_globals;
                                                locals = tl;
                                                num_locals = List.tl env.num_locals; (* exiting stack frame, restore old number of locals *)
                                                sdepth = env.sdepth - 1;
                                                max_depth = env.max_depth;
                                                errors = env.errors;
                                                warnings = env.warnings;
                                                unique_id = env.unique_id;
                                                names = env.names;
                                                varprops = env.varprops;
                                                imported = env.imported;
                                                templates = env.templates;
                                                constants = env.constants;
                                        }