Frage

I have a question that I'm unsure about for my HW. Here it goes:

Suppose you are assigned to write a program in some "classic interpreted PL" that has hardware/OS dependencies and is ultimately designed to run in three different hardware/OS environments (say X, Y, and Z). How many different versions of the final code will there be?

If there is more than one, what do you as the user programmer have to do to create the multiple versions? If there is only one, who else has to or had to do something to make the one version work in all three environments?

I would tend to think there would be one because the interpreter starts and process the user program. Would this be correct? Any help would be greatly appreciated.

War es hilfreich?

Lösung

That's one viewpoint, and probably the more correct one. For example, the Python interpreter runs on a huge variety of machines but the machine you target with your code (platform-specific modules notwithstanding) is the Python "virtual" machine so you can run the same code on many disparate platforms.

The person that did all the heavy lifting there was Guido himself or, more generally, the person implementing the interpreter on each platform.


The other viewpoint is that there may be subtle variations in the platforms which will require you to adjust. Let's say the interpreter runs on two platforms, one with a 800x600 screen, the other with a 1920x1024 screen.

So, even though there may be a consistent plot command, the differing resolutions may require some intelligence on your part to adjust to the specific machine.

In that case, you may well want to abstract the screen to a specific resolution so that your virtual screen is 4096x4096 and have that scaled to the actual resolution when plotting.

For example:

# Virtual plot, x and y are both 0-4095
def plot(x,y):
    # Get physical screen dimensions.

    actualWidth = getSysData ("screenwidth")
    actualHeight = getSysData ("screenheight")

    # Convert virtual co-ordinates to physical ones.

    x = x * actualWidth / 4066
    y = y * actualHeight / 4066

    # Plot the physical ones.

    sysplot (x, y)

Andere Tipps

In principle, you will be able to maintain a single code tree for your program for all platforms.

However, parts of your code may still be platform-specific. This is because even languages like Python whose programs are generally considered as cross-platform have platform-specific modules. E.g. the subprocess module has some OS-specific functionality. Moreover there are modules which are entirely OS-specific (e.g. the windows module).

As a programmer you will have to make sure that any platform-specific parts are nicely wrapped in modules with a well-defined API so that can be replaced by code of equivalent functionality on a different platform. You may also want to avoid using functionality that is overly platform-specific (e.g. using the windows module mentioned above will probably make a pain to port your program to a different platform).

Note that you don't necessarily have to use an interpreted language to create a cross-platform program. An interpreted language surely makes it easier, but this is still entirely possible with compiled languages like C. Even though C is commonly thought as a low-level language, it was in fact designed to be cross-platform:

Despite its low-level capabilities, the language was designed to encourage cross-platform programming.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top