Interpreters, Scripting
Traditionally:
Source Code ---> compiler ---> machine code
The machine code(or assembly language) runs very quickly, with no overhead.
It is targetted to a particular CPU (e.g intel for pcs) and to a particular Op.Sys
(e.g Windows - it uses OS facilities to access files, keyboard etc).
What a compiler does
-
splits source code lines into separate strings(e.g numbers, keywords, variable names...)
-
checks syntax
-
for each encountered variable - has it been declared?
-
for each procedure call - check the type of parameters...
-
generates machine-code equivalent for the source code (many to one)
What an Interpreter does
-it depends - there are different approaches (but in every case, machine code is not created)
In general, more work is done at run-time (e.g parameter checking)
Often, the source code is obeyed as it is checked:
-
no machine code
-
much easier to write an interpreter than a compiler (machine-code is very intricate to generate)
-
can run on any cpu
-
run-time overhead - may involve checks, deciding what to do, then doing it.
The speed issue is diminishing over the years.
Moore's law:
processor power (speed, memory) doubles every 18 months approx.
Scripting
-
not a clear definition - many PL's CAN be used to script,
but it is much easier in certain ones.
Perl, Tcl, Python
-
main area: PL as 'glue' for pre-written components:
(libraries, GUI's, programs, other scripts, O.S commands....)
-
Other areas can also be described as 'scripting':
-
VB Script, JavaScript - in web pages
-
VBA - vb for applications:
-
we can write vb code within Word, excel,...
-
in Word, every menu, button, area of text .. has a vba equivalent (is an object).
( rather like javascript, where the elements of the pages can be programmed as objects)
-
keystrokes can be recorded - they generate MODIFIABLE vb code.
Some features of scripting languages
String manipulation
eg. - split a string of text into separate words:
input: the cat sat on the mat.
output: an array of 6 strings - one word per element.
In vb:
dim words () as string
line=textbox1.text ' or get it from a web form...
words=Split(line, " ")
all scripting languages can do stuff like the above.
Regular expressions
used to check chars within strings against a pattern
e.g check if a room number is of the form:
A12 , A90, b21
(e.g letter, digit, digit
in perl:
if room= ~/^[A-Za-z][0-9][0-9]
print "ok"
Running other programs
in vb:
Dim result As Integer
result = Shell("notepad.exe", 1) '2nd param: focus option
Examples-
do: View source in IExplorer - it runs notepad.
-
Japa - behind the scenes, it runs the java compiler, and pretty printer.
-
VB forms can incorporate Word docs, excel sheets.
Summary- scripting
-
on increase
-
academic studies lagging (see your booklet for good article)
-
a range of meanings
To Script or not?
The following do not apply in every situation, but
yes if
-
gui
-
lots of complex string work
-
connecting existing components
no if
-
complex algorithms (e.g playing mp3, zipping up a file)
-
speed essential (games, video editing...