programming languages - ece2893users.ece.gatech.edu/riley/ece2893/handouts/languages-handout.pdf ·...

26
Programming Languages ECE2893 Lecture 26 ECE2893 Programming Languages Spring 2011 1 / 26

Upload: phammien

Post on 25-Mar-2018

242 views

Category:

Documents


4 download

TRANSCRIPT

Programming LanguagesECE2893

Lecture 26

ECE2893 Programming Languages Spring 2011 1 / 26

Programming Languages1 We have use the C and C++ language in this class.2 There are of course many programming languages that can be

used to solve a particular problem.3 All of the languages have strenths and weaknesses, and often the

choice of which language to use is dependent on the application.4 We will discuss briefly a number of the other languages and

highlight when they might be appropriate or not.

ECE2893 Programming Languages Spring 2011 2 / 26

Binary Programming1 The very first programs were written in binary.2 The programmer wrote out, in 1’s and 0’s the machine code to

execute a particular program.3 This is obviously very tedious and error prone, but it worked.

ECE2893 Programming Languages Spring 2011 3 / 26

Assembly Language1 The complexity and difficulty of programming in binary led to the

development of the first assembler.2 The assembler allowed the programmer to enter the instructions

not in binary form, but using instruction mnemonics and refer toaddresses by name (such as Loop:.

3 The first assembler was, of course, written in binary.4 Later assemblers were written in assembly language.5 Entire operating systems were written in assembly language.

# Program to execute euc l id ’ s a lgo r i t hm# George F . Ri ley , F a l l 2008

LDI R0,0 # Set R0 to zeroLDM R1,m # Get value f o r mLDM R2, n # Get value f o r n

Loop : MOD R3, R1,R2 # Remainder o f m / nBEQ R3, R0, Done # Done , s to re r e s u l tLD R1,R2 # Set m to nLD R2,R3 # Set n to rBEQ R0, R0, Loop # Return to step E1

Done : STM R2, gcd # Store r e s u l tHLT

ECE2893 Programming Languages Spring 2011 4 / 26

Fortran1 The earlist uses for mainframe computers was for scientific

processing, such as calculating earth orbits given initial velocityand acceleration.

2 Also used for nuclear physics, weather prediction, and oilexploration (just to name a few).

3 The Fortran (FORmula TRANslation) language was designedspecifically for implementing scientific analysis applications.

4 Fortran has seen several upgrades over time with differentspecifications.

I Fortran I written 1954–1957I Fortran II – 1958I Fortran IV – 1962

Actually written in Fortran IVI Fortran 66 Standard in 1966 (ANSI)I Fortran 77 (1978)I Fortran 90 (1991)I Fortran 95 (1995)I Fortran 2003 (2003)

ECE2893 Programming Languages Spring 2011 5 / 26

Early Fortran Drawbacks1 Early Fortran (Through Fortran IV) did not have the notion of a

stack.No recursion!

2 Did not have structures3 Did not have pointers4 Did not have variable declarations

I A variable was automatically created when referenced:I = 2 Integer variable I automatically created

I If the variable name started with I − M, it was automaticallydetermined to be an integer, otherwise a float (or double).

5 Extensive use of line numbers6 Extensive use of the goto statement.7 Input/Output specifications built into the language.8 Spaces Completely Ignored

ECE2893 Programming Languages Spring 2011 6 / 26

Fortran with Punched Cards1 Most early Fortran programs were implemented using punched

cards

ECE2893 Programming Languages Spring 2011 7 / 26

Fortran GOTO1 The Fortran language makes extensive use of the GOTO

statement.2 This often resulted in completely unmaintainable spaghetti code.

ECE2893 Programming Languages Spring 2011 8 / 26

Fortran ExampleC AREA OF A TRIANGLE − HERON ’S FORMULAC INPUT − CARD READER UNIT 5 , INTEGER INPUT , NO BLANK CARD FOR END OF DATAC OUTPUT − LINE PRINTER UNIT 6 , REAL OUTPUTC INPUT ERROR DISPAYS ERROR MESSAGE ON OUTPUT

501 FORMAT(3 I5 )601 FORMAT( " A= " , I5 , " B= " , I5 , " C= " , I5 , " AREA= " , F10 . 2 , "SQUARE UNITS " )602 FORMAT( "NORMAL END" )603 FORMAT( " INPUT ERROR OR ZERO VALUE ERROR" )

INTEGER A,B,C10 READ(5 ,501 ,END=50 ,ERR=90) A,B,C

IF (A=0 .OR. B=0 .OR. C=0) GO TO 90S = (A + B + C) / 2.0AREA = SQRT( S ∗ (S − A) ∗ (S − B) ∗ (S − C) )WRITE(6 ,601) A,B,C,AREAGO TO 10

50 WRITE(6 ,602)STOP

90 WRITE(6 ,603)STOPEND

ECE2893 Programming Languages Spring 2011 9 / 26

Fortran Summary1 Fortran is still used today

I Millions of lines of legacy code that is still in useI Try Googling “written in FORTRAN 77” (1.9M hits).

2 The languate syntax is uglyI Built for speed decades agoI Still in use in the High Performance Computing and Scientific

Computational community3 Language has improved over the years with several new

standards, but backwards compatibility always an issue.

ECE2893 Programming Languages Spring 2011 10 / 26

Basic1 Beginner’s All-purpose Symbolic Instruction Code2 Invented in 1964, but became popular with the advent of personal

computers in the early 1980’s.3 Heavy use of line numbers and GOTO statements.4 Built–in string data type.5 Many implementations were interpreted rather than compiled.

ECE2893 Programming Languages Spring 2011 11 / 26

Basic Example10 INPUT "What i s your name : " , U$20 PRINT " He l lo " ; U$30 INPUT "How many s ta r s do you want : " , N40 S$ = " "50 FOR I = 1 TO N60 S$ = S$ + "∗ "70 NEXT I80 PRINT S$90 INPUT "Do you want more s ta r s ? " , A$100 IF LEN(A$) = 0 THEN 90110 A$ = LEFT$(A$ , 1)120 IF A$ = "Y" OR A$ = " y " THEN 30130 PRINT " Goodbye " ; U$140 END

ECE2893 Programming Languages Spring 2011 12 / 26

Basic Integrated Development Environment

ECE2893 Programming Languages Spring 2011 13 / 26

Pascal1 Named after Blaise Pascal, a French mathematician, who

designed an “Arithmetical Machine” in 1641.2 Language designed by Niklaus Wirth in 1970.3 Goals were:

I Make a language suitable for teaching programmingI Make a language that could easily be compiled and executed on

computing platforms of that day.4 Support for:

I records (what C calls structures)I Local variables and scopingI recursionI pointers

5 Again, the micro–computer revolution boosted the popularity ofthe language.

6 Turbo Pascal by Borland was one of the more popularimplemenations.

I Efficient in–memory compilationI A true Integrated Developement Environment

ECE2893 Programming Languages Spring 2011 14 / 26

Pascal ExampleType

Employee_type = ( Hourly , Salary , SalaryExempt ) ;InputRec = RECORD

emp_name : packed array [ 1 . . 3 0 ] of char ;s o c i a l : packed array [ 1 . . 9 ] of char ;sa la ry : rea l ;emp_type : Employee_type ;end ;

Varindex : integer ;r a t i o : rea l ;found : boolean ;i n p f : f i l e of InputRec ;

ECE2893 Programming Languages Spring 2011 15 / 26

Pascal ExampleProgram TowersOfHanoi ( input , ou tput ) ;

Vard isks : integer ;

Procedure Hanoi ( source , temp , d e s t i n a t i o n : char ; n : integer ) ;

begini f n > 0 then

beginHanoi ( source , des t i na t i on , temp , n − 1 ) ;wri te ln ( ’Move d isk ’ ,n : 1 , ’ from peg ’ , source , ’ to peg ’ , d e s t i n a t i o n ) ;Hanoi ( temp , source , des t i na t i on , n − 1 ) ;end ;

end ;

beginwri te ( ’ Enter the number o f d isks : ’ ) ;readln ( d isks ) ;wri te ln ( ’ So lu t i on : ’ ) ;Hanoi ( ’A ’ , ’B ’ , ’C ’ , d isks ) ;end .

ECE2893 Programming Languages Spring 2011 16 / 26

Turbo Pascal IDE

ECE2893 Programming Languages Spring 2011 17 / 26

LISP1 List Processing Language2 Developed in 1958

Predates all modern lanugages except Fortran.3 Build in support for linked lists and list management4 Became popular in Artificial Intelligence community5 Much of the popular Emacs text editor written in Lisp.6 Alternate acronyms:

I Lost in Stupid ParenthesesI Lots of Irritating Superfluous Parentheses

ECE2893 Programming Languages Spring 2011 18 / 26

Lisp Example; ; Options Menu Se t t i ngs; ; =====================(cond

( ( and ( str ing−match "XEmacs" emacs−version )(boundp ’ emacs−major−version )( or (and

(= emacs−major−version 19)(>= emacs−minor−version 14) )

(= emacs−major−version 20) )( fboundp ’ l oad−opt ions− f i l e ) )

( load−opt ions− f i l e " ~ / . xemacs−options " ) ) ); ; ============================; ; End of Options Menu Se t t i ngs

ECE2893 Programming Languages Spring 2011 19 / 26

Another Lisp Example; ; Customized C/C++ mode s t u f f( defconst my−c−style

’ ( ( c−tab−always− indent . t )( c−auto−newline . t )( c−comment−only− l ine−offset . 2)( c−hanging−braces−alist . ( ( substatement−open a f t e r )

( brace− l ist−open ) ) )( c−hanging−colons−al ist . ( ( member− in i t− intro before )

( i nhe r− i n t r o )( case− label a f t e r )( l a b e l a f t e r )( access− label a f t e r ) ) )

( c−cleanup− l is t . ( scope−operatorempty−defun−bracesdefun−close−semi ) )

( c−o f f se t s−a l i s t . ( ( a r g l i s t−c l o s e . c− l i neup−arg l i s t )( substatement−open . 0)( case− label

. 2)( block−open

. 0)( knr−argdec l− in t ro . − ) ) )

( c−echo−syntact ic− information−p . t ))

"My C Programming Sty le " )

; ; o f f s e t cus tomiza t ions not i n my−c−style( setq c−o f f se t s−a l i s t ’ ( ( member− in i t− intro . ++ ) ) )

ECE2893 Programming Languages Spring 2011 20 / 26

APL1 A Programming Language2 Invented in 19573 A number of built-in operators that worked on arrays, rather than

individual variables.4 Used a number of special keystroke characters for various

operations.I Up and down arrowsI Greek charactersI Set union and intersection

5 Language is known for it’s terseness, and (in general) lack ofunderstandability.

ECE2893 Programming Languages Spring 2011 21 / 26

APL Examples

ECE2893 Programming Languages Spring 2011 22 / 26

TCL1 Tool Command Language2 Pronounced tickle3 Invented in 1988.4 Interpreted Language5 No data typing!

I Duck TypingI The type of each variable is determined at run–time by the

interpreter.I Variables can change type by assigning a new value

6 Popular for creating simple applications7 Combined with the Tk toolkit makes easy and simple graphical

applications.

ECE2893 Programming Languages Spring 2011 23 / 26

TCL Exampleset dirname [ l index $argv 0 ]set f i lename [ l index $argv 1 ]set f i l e s [ glob $dirname / Job∗ ]foreach t h i s $ f i l e s {

# Process each Job∗ f i l eset f [ open $ t h i s r ]puts " Processing $ t h i s "set ncount 0while { ! [ eof $f ] } {

gets $f l i n ei f { [ str ing f i r s t " node count " $ l i n e ] >= 0 } {

set nodecount [ l index $ l i n e 5 ]lappend n c l i s t $nodecount

}i f { [ str ing f i r s t " Prog 19 " $ l i n e ] >= 0 } {

set memuse [ l index $ l i n e 3 ]set mb [ str ing f i r s t "MB" $memuse ]set memuse [ str ing range $memuse 0 [ expr $mb − 1 ] ]set nodeary ( $nodecount ) $memuse

}}close $f

}

set n c l i s t [ l so r t −integer $ n c l i s t ]set f [ open $f i lename w]foreach t $ n c l i s t {

i f [ info exists nodeary ( $ t ) ] {puts $f " NCount $ t mem $nodeary ( $ t ) "

}}close $f

ECE2893 Programming Languages Spring 2011 24 / 26

Java1 Developed in 1996 by Sun Microsystems2 Object oriented language

I Classes, objectsI Class inheritanceI Class member functions and virtual methods

3 Automatic garbage collection for unused memory4 Write Once, Run Anywhere

I Java compiler (javac) does not create platform–specific machinecode.

I Rather, generates a sequence of platform–indepenent byte codes.I The byte codes are then interpreted by the java run–time interpreter

(java).I Does not always use native representation for integers or floats.I Immensely popular for web–based applications.

ECE2893 Programming Languages Spring 2011 25 / 26

Java Example/ / OddEven . javaimport javax . swing . JOptionPane ;

public class OddEven {private i n t i npu t ; / / a whole number ( " i n t " means i n t e g e r )public OddEven ( ) { }public s t a t i c void main ( S t r i n g [ ] args ) {

OddEven number = new OddEven ( ) ;number . showDialog ( ) ;

}

public void showDialog ( ) {t ry {

i npu t = In tege r . pa rse In t ( JOptionPane . showInputDialog (" Please Enter A Number " ) ) ;

c a l c u l a t e ( ) ;} catch ( NumberFormatException e ) {

System . e r r . p r i n t l n ( "ERROR: I n v a l i d i npu t . Please type i n a numer ica l value . " ) ;}

}

private void c a l c u l a t e ( ) {i f ( i n pu t % 2 == 0) {

System . out . p r i n t l n ( " Even " ) ;} else {

System . out . p r i n t l n ( "Odd" ) ;}

}}

ECE2893 Programming Languages Spring 2011 26 / 26