5

Why is pure interpretation more common for scripting languages compared to programming languages?

I mean, why are programs written in a scripting language not converted to machine language and then executed? From what I have read, one of the reasons is speed; for scripting purposes speed is not so important and since interpretation is slower it doesn't matter for scripting languages.

Are there more reasons for using interpretation in scripting?

David Richerby
  • 82,470
  • 26
  • 145
  • 239
geek123
  • 51
  • 2

2 Answers2

7

Scripting languages are a class of programming languages. This class is rather fuzzily defined.

One possible definition is that they're languages that are designed to automate small tasks, rather than to write large, complex programs. Because they are small tasks, the author tends to spend some time tweaking them, so it makes sense to make the path from source code to executable as simple as possible, and it doesn't get simpler than making the source code itself executable. Because they are simple tasks, the end-user might want to tweak them, too, so it makes sense to distribute them as source code. Because the tasks are relatively small and simple, there's little to be gained by static checking and optimization, so there's little value in using a compiler.

Another possible definition is that a scripting language is one in which the source code is executable. In other words, a scripting language is pretty much defined as one that's intended to be interpreted.

Few scripting languages are purely interpreted though. Most at least parse the whole file before starting execution. Many even compile to some form of bytecode that can be saved to disk.

Gilles 'SO- stop being evil'
  • 44,159
  • 8
  • 120
  • 184
2

Interpreted languages are used because of their simplicity.

A compiler will assemble all the parts and link them together as an executable. If there is a bug in any one of the parts the build will fail and there will not be any executable to run (missing symbols, missing libraries, incompatible libraries, etc). Also compiled programs often need to manage memory allocation themselves, and thus are prone to bug like memory leaks, buffer overruns, null pointers, etc.

Many interpreters will get right to it and run the program with limited requirements (syntax mostly). If there is a bug in the code the program may run until it gets to that bug then stop. New code can be loaded on the fly. Memory is usually managed for you, so you don't need to worry about deleting allocated memory.

Also interpreters can often be run as a command prompt and commands can be typed in, to test different parts of the program or to interact with your program in different ways.

Interpreters are also used in programs to allow you to modify the behavior of the program as it runs, e.g. MS Word macros.

You don't have to stop the program and recompile the code run it again to make a change.

Compiled programs usually run faster, and have access to lower level operations of the internal workings of the computers (registers, memory locations, permissions, etc).

It can be a blurry line, as some interpreted languages need to be compiled to microcode to run (e.g. Java), and some compiled languages have memory management features commonly found in interpreters (e.g. C#).

peeldog
  • 91
  • 1
  • 2