Chapter 7Chapter 7 TopicsIntroductionArithmetic ExpressionsArithmetic Expressions: Design IssuesArithmetic Expressions: OperatorsArithmetic Expressions: Operator Precedence RulesArithmetic Expressions: Operator Associativity RuleExpressions in Ruby and SchemePrecedence & Associativity in PrologSlide 11Slide 12Prolog http://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.htmlSlide 14Arithmetic Expressions: Conditional ExpressionsArithmetic Expressions: Operand Evaluation OrderArithmetic Expressions: Potentials for Side EffectsFunctional Side EffectsReferential TransparencyReferential Transparency (continued)Overloaded OperatorsOverloaded Operators (continued)Type ConversionsType Conversions: Mixed ModeExplicit Type ConversionsFor Some Errors in ExpressionsRelational and Boolean ExpressionsSlide 28Short Circuit EvaluationShort Circuit Evaluation (continued)Assignment StatementsAssignment Statements: Conditional TargetsAssignment Statements: Compound Assignment OperatorsAssignment Statements: Unary Assignment OperatorsAssignment as an ExpressionMultiple AssignmentsAssignment in Functional LanguagesMixed-Mode AssignmentSummaryChapter 7Expressions and Assignment StatementsCopyright © 2012 Addison-Wesley. All rights reserved. 1-2Chapter 7 Topics•Introduction•Arithmetic Expressions•Overloaded Operators•Type Conversions•Relational and Boolean Expressions•Short-Circuit Evaluation (to be evaluated partially when there is no need to evaluate an expression to the end), e.g., if (1 || x) …•Assignment Statements•Mixed-Mode AssignmentCopyright © 2012 Addison-Wesley. All rights reserved. 1-3Introduction•Expressions are the fundamental means of specifying computations in a programming language•To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation•Essence of imperative languages is dominant role of assignment statementsCopyright © 2012 Addison-Wesley. All rights reserved. 1-4Arithmetic Expressions•Arithmetic evaluation was one of the motivations for the development of the first programming languages•Arithmetic expressions consist of operators, operands, parentheses, and function callsCopyright © 2012 Addison-Wesley. All rights reserved. 1-5Arithmetic Expressions: Design Issues•Design issues for arithmetic expressions–Operator precedence rules?–Operator associativity rules?–Order of operand evaluation?–Operand evaluation side effects?–Operator overloading?–Type mixing in expressions?Copyright © 2012 Addison-Wesley. All rights reserved. 1-6Arithmetic Expressions: Operators•A unary operator has one operand•A binary operator has two operands•A ternary operator has three operandsCopyright © 2012 Addison-Wesley. All rights reserved. 1-7Arithmetic Expressions: Operator Precedence Rules•The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated •Typical precedence levels– parentheses– unary operators– ** (if the language supports it)– *, /– +, -Copyright © 2012 Addison-Wesley. All rights reserved. 1-8Arithmetic Expressions: Operator Associativity Rule•The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated•Typical associativity rules–Left to right, except **, which is right to left–Sometimes unary operators associate right to left (e.g., in FORTRAN)•APL is different; all operators have equal precedence and all operators associate right to left•Precedence and associativity rules can be overriden with parenthesesCopyright © 2012 Addison-Wesley. All rights reserved. 1-9Expressions in Ruby and Scheme•Ruby–All arithmetic, relational, and assignment operators, as well as array indexing, shifts, and bit-wise logic operators, are implemented as methods - One result of this is that these operators can all be overriden by application programs•Scheme (and Common LISP)-All arithmetic and logic operations are by explicitly called subprograms-a + b * c is coded as (+ a (* b c))Copyright © 2012 Addison-Wesley. All rights reserved. 1-10Precedence & Associativity in Prologop(+Precedence, +Type, :Name)•Declare Name to be an operator of type Type with precedence Precedence. •Name can also be a list of names, in which case all elements of the list are declared to be identical operators. Precedence is an integer between 0 and 1200. Precedence 0 removes the declaration. •Operator definitions in Prolog look like this: :- op(Precedence, Type, Name).Copyright © 2012 Addison-Wesley. All rights reserved. 1-11Precedence & Associativity in Prolog:- op(Precedence, Type, Name).•Type is one of: xf, yf, xfx, xfy, yfx, fy or fx. •The `f' indicates the position of the functor, while x and y indicate the position of the arguments. •`y' should be interpreted as ``on this position a term with precedence lower or equal to the precedence of the functor should occur''. •For `x' the precedence of the argument must be strictly lower.Copyright © 2012 Addison-Wesley. All rights reserved. 1-12Precedence & Associativity in Prolog:- op(Precedence, Type, Name).•The precedence of a term is 0, unless its principal functor is an operator, in which case the precedence is the precedence of this operator. •A term enclosed in parentheses ( ... ) has precedence 0. •In prolog, for example:- op(500, xf, is_dead).kill(marsellus,zed).is_dead(X) :- kill(_,X).Prolog http://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.html•Other kinds of sequences can be defined by the user. For example, to make left-associative sequences separated by '#', one might use an operator declaration like this ... ?- op(500,yfx,'#').?- (A#B) = 1#2#3#4.A = 1 # 2 # 3B = 4=> 1 # (2 # (3 # 4))•Notice how left-associativity was what determined the bindings in the second goal! •To make right-associative, use xfy.?- op(500,xfy,'#').?- (A#B) = 1#2#3#4.A = 1B = 2 # 3 # 4=> ( ( 1 # 2 ) # 3 ) # 4Copyright © 2012 Addison-Wesley. All rights reserved. 1-14Precedence & Associativity in Prolog:- op(Precedence, Type, Name).•The precedence of a term is 0, unless its principal functor is an operator, in which case the precedence is the precedence of this operator. •A term enclosed in parentheses ( ... ) has precedence 0. •In prolog, for example:- op(500, xf, is_dead).kill(marsellus,zed).is_dead(X) :-
View Full Document