cs 330 organization of programming languages

16
CS 330 Organization CS 330 Organization of Programming of Programming Languages Languages Soundararajan Ezekiel Soundararajan Ezekiel Department of Computer Science Ohio Northern University ADA, Ohio 45810 e-mail: [email protected] http://www.onu.edu/user/FS/ sezekiel

Upload: anneliese-panagos

Post on 31-Dec-2015

43 views

Category:

Documents


0 download

DESCRIPTION

CS 330 Organization of Programming Languages. Soundararajan Ezekiel Department of Computer Science Ohio Northern University ADA, Ohio 45810 e-mail: [email protected] http://www.onu.edu/user/FS/sezekiel. Implementing Subprograms. The General Semantics of Calls and Returns - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 330 Organization of Programming Languages

CS 330 Organization of CS 330 Organization of Programming Languages Programming Languages CS 330 Organization of CS 330 Organization of

Programming Languages Programming Languages

Soundararajan EzekielSoundararajan Ezekiel

Department of Computer Science

Ohio Northern University

ADA, Ohio 45810

e-mail: [email protected]://www.onu.edu/user/FS/sezekiel

Page 2: CS 330 Organization of Programming Languages

04/19/23 2

Implementing SubprogramsImplementing SubprogramsImplementing SubprogramsImplementing Subprograms The General Semantics of Calls and ReturnsThe General Semantics of Calls and Returns Implementing FORTRAN 77 SubprogramsImplementing FORTRAN 77 Subprograms

Page 3: CS 330 Organization of Programming Languages

04/19/23 3

1.The General Semantics of Calls and Returns1.The General Semantics of Calls and Returns1.The General Semantics of Calls and Returns1.The General Semantics of Calls and Returns the subprogram call and return operations of a language are the subprogram call and return operations of a language are

together called its subprogram linkagetogether called its subprogram linkage subprogram call has numerous actionsubprogram call has numerous action

– 1. Mechanism for parameter passing 1. Mechanism for parameter passing – 2. If local variables are not static-- the call must cause storage2. If local variables are not static-- the call must cause storage– 3. Save the execution status of calling program unit3. Save the execution status of calling program unit– 4. Arrange to transfer control-- make sure that control return to 4. Arrange to transfer control-- make sure that control return to

the proper placethe proper place– 5. Provide some mechanism to access non local variables5. Provide some mechanism to access non local variables

Page 4: CS 330 Organization of Programming Languages

04/19/23 4

Required action of subprogram return also Required action of subprogram return also complicatedcomplicated– move the local values of the associated formal move the local values of the associated formal

parameters to the actual parametersparameters to the actual parameters– deallocate the storage used for local variables and deallocate the storage used for local variables and

restore the execution status of the calling program unitrestore the execution status of the calling program unit– control must be returned to the calling program unitcontrol must be returned to the calling program unit

Page 5: CS 330 Organization of Programming Languages

04/19/23 5

2. Implementing FORTRAN 77 2. Implementing FORTRAN 77 subprogramssubprograms

we will discuss simple situation in FORTRAN 77 we will discuss simple situation in FORTRAN 77 subprogramssubprograms

subprogram call requires the following actionssubprogram call requires the following actions– 1. Save the execution status of the current program 1. Save the execution status of the current program

unitunit– 2. Carry out the parameter passing process2. Carry out the parameter passing process– 3. Pass the return address to the callee3. Pass the return address to the callee– 4. Transfer control to the callee4. Transfer control to the callee

Page 6: CS 330 Organization of Programming Languages

04/19/23 6

The semantics of a FORTRAN 77 subprogram return requires the following actionsThe semantics of a FORTRAN 77 subprogram return requires the following actions 1. If pass-by-value-result parameters are used, the current values of those 1. If pass-by-value-result parameters are used, the current values of those

parameters are moved to the corresponding actual parametersparameters are moved to the corresponding actual parameters 2. If the subprogram is a function, the functional value is moved to a place 2. If the subprogram is a function, the functional value is moved to a place

accessible to the calleraccessible to the caller 3. The execution status of the caller is restored3. The execution status of the caller is restored 4. Control is transferred back to the caller4. Control is transferred back to the caller the call and return action require storage for the followingthe call and return action require storage for the following

– 1. Status information about the caller1. Status information about the caller– 2. Parameters2. Parameters– 3. Return address3. Return address– 4. Functional value for function subprogram4. Functional value for function subprogram

Page 7: CS 330 Organization of Programming Languages

04/19/23 7

Functional Programming LanguagesFunctional Programming Languages IntroductionIntroduction Mathematical FunctionsMathematical Functions Fundamentals of Functional Programming LanguagesFundamentals of Functional Programming Languages First Functional Programming Language: LISPFirst Functional Programming Language: LISP An Introduction to SchemeAn Introduction to Scheme COMMON LISPCOMMON LISP MLML HaskellHaskell Applications of Functional LanguagesApplications of Functional Languages Comparison with other languageComparison with other language

Page 8: CS 330 Organization of Programming Languages

04/19/23 8

IntroductionIntroduction So far we talked about imperative languages and Object oriented So far we talked about imperative languages and Object oriented

language( Small talk) language( Small talk) The Object Oriented Programming Languages -- similar to The Object Oriented Programming Languages -- similar to

Imperative languagesImperative languages The reason for similarity -- von Neumann architecutureThe reason for similarity -- von Neumann architecuture The functional programming paradigm, which is based on The functional programming paradigm, which is based on

mathematical functions, is the design for one of the most important mathematical functions, is the design for one of the most important nonimperative style of languages.nonimperative style of languages.

LISP began as purely functional language but soon acquired some LISP began as purely functional language but soon acquired some important imperative features that increased its execution efficiencyimportant imperative features that increased its execution efficiency

Page 9: CS 330 Organization of Programming Languages

04/19/23 9

COMMON LISP extension of LISPCOMMON LISP extension of LISP ML is a strongly typed functional languages with more ML is a strongly typed functional languages with more

conventional syntax than LISP conventional syntax than LISP Haskell is partially based on ML but it is purely functional Haskell is partially based on ML but it is purely functional

languagelanguage OBJECTIVE:-OBJECTIVE:-

– Introduce the concept Introduce the concept – Mathematics behindMathematics behind– It is difficult to acquire an actual feel for functional programming without It is difficult to acquire an actual feel for functional programming without

some actual programming experience-- so we will do some simple some actual programming experience-- so we will do some simple programming in COMMON LISPprogramming in COMMON LISP

Page 10: CS 330 Organization of Programming Languages

04/19/23 10

Mathematical FunctionsMathematical Functions Mathematical function is a mapping of one set , called Mathematical function is a mapping of one set , called domain domain

set , to another set called the set , to another set called the rangerange set set the function definitions specifies the domain and range sets, the function definitions specifies the domain and range sets,

either either explicitly explicitly or or implicitlyimplicitly along with the mapping along with the mapping One of the fundamental characteristics of mathematical One of the fundamental characteristics of mathematical

functions is that the evaluation order of their mapping functions is that the evaluation order of their mapping expressions is controlled by expressions is controlled by recursionrecursion and and conditional conditional expressionexpression rather than by the rather than by the sequencingsequencing and and iterative iterative repetitionsrepetitions that are common in imperative programming that are common in imperative programming languages languages

Page 11: CS 330 Organization of Programming Languages

04/19/23 11

Another important characteristics of mathematical functions Another important characteristics of mathematical functions is that because they have no side effect s, they always define is that because they have no side effect s, they always define the same value given the same set of arguments.the same value given the same set of arguments.

Side effect in programming languages connected to variables Side effect in programming languages connected to variables that model memory locationthat model memory location

mathematical functions defines a value, rather than mathematical functions defines a value, rather than specifying a sequence of operations on values in memory to specifying a sequence of operations on values in memory to produce a valueproduce a value

There are no variables in the sense of imperative There are no variables in the sense of imperative languages-- so no side effectslanguages-- so no side effects

Page 12: CS 330 Organization of Programming Languages

04/19/23 12

Simple functionsSimple functions cube(x)cube(x) x*x*x, where x is a real number x*x*x, where x is a real number name, domain and range are real numbers , the symbol name, domain and range are real numbers , the symbol meaning “ meaning “

is defined as”is defined as” when x= 2 it gives the value 8when x= 2 it gives the value 8 Alonzo Church Alonzo Church Born: 14 June 1903 in Washington, D.C., USA Died: 11 Aug 1995 in Hudson, Ohio, USA

Page 13: CS 330 Organization of Programming Languages

04/19/23 13

Lambda expression

Lambda(x) x*x*x it can more than one parameter (Lambda(x) x*x*x)(2) the answer will be 8

Page 14: CS 330 Organization of Programming Languages

04/19/23 14

Functional Forms A higher order function or functional forms, is one that either takes

functions as parameters or yields a function as its result or both One common kind of functional form is functional composition--

which has two functional parameters and yields a function whose value is the first actual parameter function applied to the result of the second

Notation o as an operator h f o g f o g f(x) f(x) x+2, g(x) x+2, g(x) 3*x h(x) 3*x h(x) f(g(x) f(g(x) (3*x)+2 (3*x)+2

Page 15: CS 330 Organization of Programming Languages

04/19/23 15

Construction Construction is a functional form that takes a list of

functions as parameter When applied to an argument, a construction applies each

of its functional parameters to that argument and collects the results in a list or sequence

Notation put all the function in [] like [f,g] example g(x) x*x h(x) 2*x i(x) x/2 then [g,h,i](4) (16, 8,2)

Page 16: CS 330 Organization of Programming Languages

04/19/23 16

Apply-to-all apply-to-all is a functional form that takes a single function as a

parameter if applied to a list of arguments, apply-to-all applies its functional

parameter to each of the values in the list argument and collects the result in a list or sequence

Notation alpha h(x) x*xx*x alpha(h, (2,3,4)) yields ( 4,9,16)alpha(h, (2,3,4)) yields ( 4,9,16)

There are many other functions -- This will be enough for usThere are many other functions -- This will be enough for us