问题
I can't seem to find a direct answer on this. Using several search engines gives me very vague results, or only answers half the question.
So, to elaborate, when you write a program and it's compiled/interpreted, does the computer read each line sequentially in chronological order or does it skip around by default?
Example: Using C-style source because it was the first thing that came to mind.
printf("I'm the first line of your program! Yay!");
printf("I'm the second, woo!");
printf("And I'm the third, yay!");
Above you can see there are three lines of code, and when run, they will be run in order and outputted to the console in said order.
I am assuming that, unless told to jump to a specific part of code within the source file, it will read each line of code in order. So is this how a program is interpreted in a computer, and do all programming languages get compiled/interpreted this way? Is how a program read language specific?
I know this is a basic question about programming languages and computer science, but I'd like some clarification and there aren't any concrete answers out there.
I probably worded the question incorrectly with my searches - that might be why I didn't find anything. Thanks!
回答1:
The answer is highly dependent on some preconditions.
For a start, there exist so-called "scripting" programming languages. The name itself pretty much sums the approach an interpreter of such language takes when excuting programs written in that language: since they are literally scripts, they are read in order and executed as the interpreter reads out complete statements.
To put it differently, since a script is supposed to be read from top to bottom, the definitions of various stuff must come earlier than the places that stuff is used at.
Contrary to those, compiled languages usually grant more freedom to the programmer. For instance, in modern languages it's quite often possible to declare types textually "below" the places they are used to declare/define variables of those types or other types making use of those types.
But supposedly there exist a more deep distinction between "scripting" and compiled languages: for the latter, the files as a medium are more declarative rather than imperative.
For instance, take Go. All the source code of a Go program is organized into "packages". A package consists of all the source files located in a specific directory and having a common package declaration. Now any piece of the code located in the files of a particular package is able to use any other symbol (a package-level variable, a type or a function) of that package — no matter in which exactly file, and where exactly in that file that symbol is defined.
So that's how this works "at large". If you're curious about how compilers precisely work with the source files, then I think these days they tend to "slurp" then wholesale into memory or read them in a streamed manner — parsing their contents as they go.
This simply does not matter because the compilers do not somehow directly convert what's written in the source code files but rather build the so-called abstract syntax trees out of these contents and then convert those ASTs into the target machine (or VM) code, and this, in itself, usually happens in several stages — all using their own convenient intermediate representations.
TL;DR
The question of how a compiler reads a text file simply does not matter: it's an uninteresting miniscule implementation detail. ;-)
回答2:
For the most part, unless Multi-threading is used, code statements will run in the "main" thread and be executed in order, and will not go to the next statement until the previous statement has completed.
If you use multi-threading there is no guarantee of the order of statements that occur in separate threads.
But within the same thread, statements are always executed in order.
Some high-level languages still support the "goto" statement, which allows you to jump around, but that is frowned upon because it creates "spaghetti" code, using loop constructs and calling methods is the preferred approach.
回答3:
Almost all programming language implementations in wide use today read the entire source file into memory and then convert into one or more higher-level representations before they compile or execute any of it.
For example, if you run this Python program:
print "first line"
print print "<- error on this line"
Then the interpreter outputs:
File "temp.py", line 2
print print "<- error on this line"
^
SyntaxError: invalid syntax
Notice that it does not print "first line" before reporting that error. It detected the syntax error in the second line before beginning to execute even the first line.
Back in the early days of computing when memory was scarce, some languages did parse and execute each line one at a time. But these days, that style is quite rare.
来源:https://stackoverflow.com/questions/46304522/are-all-programming-languages-read-in-sequential-order