introduction to scripting - university of hawaii · introduction to scripting fa16 –ics 215 ed...

34
Introduction to Scripting FA16 – ICS 215 Ed Meyer

Upload: others

Post on 23-Jan-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Introduction to Scripting

FA16 – ICS 215

Ed Meyer

Page 2: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

What is a scripting language?

2

Page 3: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

What is the difference between a scripting language and a system

programming language?

3

Page 4: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

System Programming Languages

• From scratch

– build data structures

– algorithms

• Strongly typed to manage complexity

4

Page 5: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Scripting Languages

• Designed for gluing

• Automate the execute of tasks

• Typeless to simplify connections among components

5

Page 6: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

System Programming: Beginning

• An alternative to assembly

– Very low-level

– Each statement is a single machine instruction

6

;; To output a CR, LF

crlf macro

push ax

push dx

mov ah, 2

mov dl, 0dh

int 21h

mov dl, 0ah

int 21h

pop dx

pop ax

endm

Page 7: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

System Programming: Beginning

• An alternative to assembly

– Very low-level

– Each statement is a single machine instruction

• Use a compiler to translate higher-level languages to binary instructions

7

Page 8: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

System Programming vs Assembly Language

Page 9: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Higher-Level Languages

• Means many details are handled automatically

– Can write less code to get the same job done

• Such details are

– Register allocation, handled by the compiler

– Procedure calling sequences

– Simple keywords such as while and if for control structures

• 1 system program line ≈ 5 instructions

9

Page 10: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Procedure Calling Sequences

10

Page 11: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

if-else statement

Suppose we want to do something like:if (ax < bx) {

X = -1;

} else {

X = 1;

}

it would look like this:mov ax, 5 ; put the value 5 in register ax

mov bx, 7 ; put the value 7 in register bx

cmp ax, bx

jl axLess ; go to 'axLess' if ax < bx

mov word [X], 1 ; This is the 'else part

jmp Both ; skip the 'then' part

axLess:

mov word [X], -1 ; This is the 'then' part

Both:

11

Page 12: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Read in and Compare 2 Numbers (in C)

12

~18 Lines of code

Page 13: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Read in and Compare 2 Numbers(Assembly)

13

Page 14: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

14

~54 Lines of code

Page 15: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Comparison

18 lines in C : 54 lines in Assembly

1 line in C : 3 lines in Assembly

To run C programs, just install C

Assembly is restricted to machines hardware

Much easier to install software than replace hardware.

15

Page 16: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Typing (System Prog.)

• Need to declare how each piece of information will be usedString output = new String();

• Each variable must be used in ways that are appropriate for that type

16

Page 17: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

String output = "Hello World!" / 5;

17

Page 18: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Typing (System Prog.)

• Need to declare how each piece of information will be usedString output = new String();

• Each variable must be used in ways that are appropriate for that type

• Data and code are segregated

• Objects have well-defined structure– With procedures or methods to manipulate them

– An object of one type cannot be used in place of another

18

Page 19: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Advantages of Typing

• Large programs more manageable

– Clarifies how things are used and treated

• Catch errors before runtime

– Using a string value as a pointer

– Compile error vs Runtime error

• Improves performance

– Mainly via the compiler

19

Page 20: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Scripting Languages

Page 21: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Some Examples

21

Page 22: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Typing (Scripting)

• Actually, computer are fundamentally typeless

– Memory can hold an integer, floating-point, a pointer, or instruction

• Meaning of information is determined by the way it is used

• No declaration of variable type

– Declare a variable and use it!

• Easy to glue components together

22

Page 23: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Typing Example (Scripting)

• In JavaString output = new String();

• In PHP$output = "Hello World!";

$output = 5 + 3;

23

Page 24: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Typing Example (Scripting)

• Creating a button in Tcl

button .b -text Hello! -font {Times

16} -command {puts hello}

24

Page 25: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Typing Example (Scripting)

• Creating a button in C++CFont *fontPtr = new CFont();

fontPtr->CreateFont(16, 0, 0, 0, 700,

0, 0, 0, ANSI_CHARSET,

OUT_DEFAULT_PRECIS,

CLIP_DEFAULT_PRECIS,

DEFAULT_QUALITY,

DEFAULT_PITCH|FF_DONTCARE,

"Times New Roman");

buttonPtr->SetFont(fontPtr);

25

Page 26: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Scripts are Interpreted

• Not compiled

• Rapid turnaround

• Increases productivity

• Generate code on the fly

• Less efficient resource wise

– Not really an issue

26

Page 27: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

27

A comparison of various programming languages based on their level (higher-level languages execute more machine instructions for each language statement) and their degree of typing.

Page 28: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

When should you use…???

28

Page 29: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

A Scripting Language…

• Is the application’s main task to connect preexisting components?

• Will the application manipulate a variety of different things?

• Does the application include a GUI?• Does the application do a lot of string

manipulation?• Will the application’s functions evolve rapidly

over time?• Does the application need to be extensible?

29

Page 30: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

A System Prog. Language…

• Does the application implement complex algorithms or data structures?

• Does the application manipulate large datasets, for example, all the pixels in an image, such that execution speed is critical?

• Are the application’s functions well defined and slow to change?

30

Page 31: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

In Comparison…

Scripting

• Interpreted

• Higher productivity

• Weakly typed

• Higher level than system programming

• Reuse is high

• Machine instructions per statement is high

System Programming

• Best suited for complex systems

• Code is compiled

• Strong typing

31

Page 32: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Trend

• Faster machines

• Better scripting languages

• Growth of Internet

• Expand the applicability of scripting languages

32

Page 33: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

Additional Readings

• On Laulima

– "Scripting: Higher Level Programming for the 21st Century" by John K. Ousterhout, IEEE Computer, 31(3) 1998, pp. 23-30,

– "Are Scripting Languages Any Good? A Validation of Perl, Python, Rexx, and Tcl against C, C++, and Java" by Lutz Prechelt, Advances in Computers, 57 (Ed.: M. Zelkowitz), Academic Press, 2003, pp. 205-270.

33

Page 34: Introduction to Scripting - University of Hawaii · Introduction to Scripting FA16 –ICS 215 Ed Meyer. What is a scripting language? 2. ... •Expand the applicability of scripting

More Recently…

• ACM Blog July 7, 2014

– Python is Now the Most Popular Introductory Teaching Language at Top U.S. Universities

34