-1

By scopes I mean function scopes, classes scopes, libraries, DLLs and so on.

If at the end all code is translated to series of instructions(by compiler), how does the high-level scopes in an high-level language have any effect on the code at the lowest level?

Apeee
  • 75
  • 4
  • 2
    I'm not sure what you mean. Are you asking why do we have scopes? Or how they're implemented internally by the language? Scopes are just a convenience for the programmer; they are enforced as part of the programming language, but you're right, in the end it's all just machine instructions (or byte code running on a VM that enforces scope rules). – Robert Harvey Oct 04 '12 at 13:41
  • So what use do they have in the design and programming process of a program? – Apeee Oct 04 '12 at 13:43
  • 2
    They keep things orderly, like shelves in a bookcase. A book only knows what shelf it's on, and about other books on the same shelf, not about books on other shelves. Imagine if we just threw all the books into a huge pile. It would be pretty hard to manage them, wouldn't it? – Robert Harvey Oct 04 '12 at 13:43
  • Okay, thank you! What did you mean by "why scopes"? and I'd like to know how they are implemented? does the IDE implement it? – Apeee Oct 04 '12 at 13:45
  • 1
    The programming language implements the scoping rules. – Robert Harvey Oct 04 '12 at 13:48
  • 1
    The IDE has nothing to do with it, it's just an editor. Scopes existed long before IDEs – alexis Oct 04 '12 at 13:54
  • Extremely broad question, it's a fundamental concept in most modern languages. When and where a variable is and isn't accessible within the code. – Orbling Oct 04 '12 at 14:25

3 Answers3

1

That's the whole point - one of them is high-level, the other is low-level. The CPU instructions don't know anything about scopes. It's a matter of memory management.

"Scopes" are part of the high-level programming environment - the compiler checks for you (according to rules of the programming language) that you can access the stuff you're trying to access (for example, you can't access variable x that's local to another function). One might say that this is a means of reducing bugs.

The "real" code being executed doesn't know about scopes.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

Each variable name, or function name, etc., corresponds to a location in memory where the corresponding information or code is stored. (That's a slight simplification but you get the idea). A scope represents a mapping from names to locations. Think of it as a dictionary or similar data structure in your favorite language.

Nested scopes work as a sort of stack. When the language syntax introduces a new scope, a new mapping is pushed to the stack. When you use a variable name, say i, the compiler or interpreter looks for it at the top-level mapping, then (if it's not found there) upwards according to the rules of the language. When a scope ends, the relevant mapping is popped off the stack and the previous mapping comes back in effect.

The ugly details of how this works could vary: In the simplest case, a compiler could generate code that refers to memory locations directly. More realistically: a C compiler produces executable objects that include a "symbol table" which maps variable and function names to locations in memory. The linker resolves references between modules, by looking up references from one module in the symbol table of another. And names are not mapped to absolute locations, but to offsets from some reference point. This allows libraries to be "relocatable code", which means they could be loaded to any location in memory during execution and still work properly. Languages that compile to a virtual machine may cut some corners, but the principles are the same.

alexis
  • 48,685
  • 16
  • 101
  • 161
0

Scope is a high level language construct and mechanism. The low level CPU instructions are not concerned with scope.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415