Imperative programming Data elements and bindings • Variables and their values • Expression (value) evaluation • Binding of variables and types – static and dynamic binding • Lifetime of variables • Scopes of variables Lambdas in C++11 – static and dynamic scoping TUT Pervasive Computing 1 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Memory von Neumann (instructions and data) architecture Instructions and data Results of operations CPU Input and Arithmetic Control output and logic unit devices unit • Imperative • von Neumannin programming architecture – variable – memory cell – assignment – transfer (CPU-memory) TUT Pervasive Computing 2 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Data items (variables) • Creating and handling data – is based on changing the state (memory content) of the computer • Essential operation of the program – memory location = space for data item – content of the location = value of the data item • Pointer/reference type – value of a data item is a memory address or a reference to a memory location – in most languages identifies a variable • equal pointers = same variables • A named variable can be either a data item or a reference to it TUT Pervasive Computing 3 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
memory location for a variable vs. Variables value of a variable • Characteristics: – name X := X + 1; – address (L-value) Pascal: var X: Integer; – value (R-value) subroutine SUM ( I, J, K ) Fortran: – type integer I, J, K I = J + K – lifetime return – scope end call SUM ( 1, 2, 3 ) • Aliasing C: ( f ( a ) + 3 ) -> b [ c ] = 2; TUT Pervasive Computing 4 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Value vs. reference semantics • In some languages (C, C++,…) variables contain the data • In other languages (Java, Python, …) variables (=names) are references to objects that contain the data • Affects how variables/objects work (e.g. lifetime, memory allocation) • A major source of confusion when changing to another language TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Value semantics • Variables: named data items • (Nameless variables also possible) • Assigning to a variable changes the data • Pointers/references: variables that refer to another variable p 3 a TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
a Reference semantics 3 • Objects: nameless data items • Variables/references/names (term depend on language): named references to objects • (Nameless references also possible) • Assigning to a variable changes the reference (refer now to another object) • Objects and variables separate, variables cannot refer to other variables, only to objects TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Assignment • Changes the state of data L := R • Type checking ensures that the memory location referenced by L has room for the value of expression R 0x0804a008 0x0804a865 ptr • Indirect referencing 0x0804a008 2148 – e.g. through pointer dereference – usually what we want – makes compiler optimization harder ptr == 0x0804a008 *ptr == 2148 TUT Pervasive Computing 8 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Variations of assignments • Multiple assignment a, b = b, a – e.g. Python Triple = proc ( ) returns ( int , int , int ) – function may return ( 1, 2, 3 ) return several values (e.g Clu) end Triple; ... a, b, c: int := Triple ( ) • Compound operations a.b [ i + j ] = a.b [ i + j ] + 2; – e.g. C ( += , -= ) a.b [ i + j ] += 2; • Increment and decrement operations a [ i ++ ] *= 10; TUT Pervasive Computing 9 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
if ( x = y ) if ( x == y ) Expression language a = b = 1 • Statement has a value a = ( b = 1 ) • C: assignment has a value – enables multiple assignments while ( ( ch = getchar ( ) ) != EOF ) ... • Algol68: begin a := if b < c then d else e; a := begin f ( b ); g ( c ) end ; g ( d ); 2 + 3 end TUT Pervasive Computing 10 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Expressions • Expressions consist of – operands • e.g. variables, constants, function calls – operators • e.g. operator symbols, functions a - 5 + f ( 1, 2 ) * 3 TUT Pervasive Computing 11 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Operators (1) • Different amount of operands: – unary (signs, negation, C’s ++ and &) – binary (arithmetic operators, comparison operators, assignment, ->) – ternary (in C) a ? b : c • Different layout: – prefix (many unary operators, e.g. -1, !a, ++i) • operators in Scheme – postfix (e.g. i++) – infix (many binary operators) TUT Pervasive Computing 12 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Operators (2) • Precedence – described as precedence levels • e.g. binary *-operator usually has a higher precedence (level) than binary +-operator • Association – applied to operators of the same precedence level – operators associated from left to right: • e.g. arithmetic operators, C’s <<, -> – operators associated from right to left: • e.g. power raising, C’s +=, = TUT Pervasive Computing 13 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Operator precedences Fortran Pascal C Ada ** *, /, div , mod ++, -- **, abs *, / +, - (all) +, - (unary) *, /, mod , rem +, - (all) *, /, % +, - (unary) +, - (binary) +, - (binary) C-code: s != 1 << n + 1; Obs! Parentheses are recommended A [ i ] = i = x; a = b << c = d; TUT Pervasive Computing (Does not work) 14 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
( a + b ) * ( c – d ) Expression evaluation (1) * • Evaluation can be based on an intermediate + - presentation – syntax tree a b c d • passed in post-order (left subtree, right subtree, root) – postfix notation a b + c d - * • evaluation can be based on stack – three-address-code • common sub-expressions can be used in optimization • memory space needed for temporary variables t1 = a + b t2 = c – d t3 = t1 * t2 TUT Pervasive Computing 15 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Expression evaluation (2) • Precedence levels and associations do not determine the evaluation order of the whole syntax tree + * * 3 * ( y – 1 ) + 2 * ( x + 3 ) 3 - 2 + • Obs: side effects y 1 x 3 a = 10; TUT Pervasive Computing b = a + fun ( a ); 16 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Expression evaluation (3) A or B • Evaluation of logic expressions can be based on short- short-circuit evaluation A and B – Modula-2: IF ( i > 0 ) AND ( i < 11 ) AND ( a [ i ] = b ) THEN ... – C: if ( p && ( p->item == x ) ) ... – Ada: short-circuit operators: andthen , orelse • Example of lazy evaluation which means that – expression is evaluated only when its value is needed • e.g. passing an expression as a parameter does not require evaluation of the expression TUT Pervasive Computing 17 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Bindings • Binding – relation or association • e.g. between an object and property/feature • e.g. between an operation and its symbol • Binding time – time, when a binding occurs • Static and dynamic binding TUT Pervasive Computing 18 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Example bindings C-code: • Set of possible types for count – bound at language design time int count; • Type of count ... – bound at compile time count = count * 5; • Set of possible values of count – bound at compiler design time • Value of count – bound at execution time with this statement • Set of possible meanings for the operator symbol * – bound at language design time • Meaning of the operator symbol * in this statement – bound at compile time • Internal representation of the literal 5 – bound at compiler design time TUT Pervasive Computing 19 Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Binding of types • Static (compile-time) / dynamic (run-time)? • Objects only / objects + references? • Can the type of an object change? • Can an object have several types at the same time: inheritance (polymorphism) • Explicit/implicit typing • Type deduction TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen
Recommend
More recommend