CptS 355 Programming Language Design - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CptS 355 Programming Language Design

Description:

They consist of operators, operands, parentheses, and function calls ... Boolean operators also vary. FORTRAN 77 FORTRAN 90 C Ada .AND. and && and .OR. or || or. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 10
Provided by: roge2
Category:

less

Transcript and Presenter's Notes

Title: CptS 355 Programming Language Design


1
CptS 355Programming Language Design
Values, Expressions, Conversions
  • Roger Ray
  • WSU Vancouver, Spring 2001

values.ppt
2
Arithmetic Expressions
  • Their evaluation was one of the motivations for
    the development of the first programming
    languages
  • They consist of operators, operands, parentheses,
    and function calls
  • Design choices are surprisingly varied!

?Advice
3
Operator Syntax
  • Terminology
  • A unary operator has one operand
  • A binary operator has two operands
  • A ternary operator has three operands
  • The operator precedence rules define the order in
    which adjacent operators of different precedence
    levels are evaluated. Typical levels
  • function calls
  • parentheses
  • unary operators
  • (if the language supports it)
  • , /
  • , -
  • gt, lt, ...
  • The operator associativity rules define the order
    in which adjacent operators with the same
    precedence level are evaluated.
  • What are some common rules?

C, C, and Java have over 50 operators and 17
different levels of precedence
?Advice
4
Side Effects
  • When a function referenced in an
    expressionalters another operand of the
    expression
  • Solution 1 Write the language definition to
    disallow functional side effects
  • No two-way parameters in functions
  • No nonlocal references in functions
  • Solution 2 Write the language definition to
    specify operand evaluation order
  • Eval operands left-to-right

a 10 b a fun(a) / Assume that fun
returns 1, but alters a to 2 /
Discussion
?Advice
Discussion
5
Unusual Expressions
  • Conditional (If-Then-Else) expressions
  • Sequence (next value) expressions

average (count 0)? 0 sum / count
C/Java
average (printf ("here we are"), sum/count)
C/Java
?Advice
6
Operator Overloading
  • Some is common
  • for int and float
  • Some is surprising
  • for numbers and text (Java)
  • Some is potential trouble
  • / for int and float (C, Java)
  • Some is extensible
  • C and Ada allow user-defined overloads

Discussion
?Advice
7
Type Conversions
  • A mixed-mode expression has operands of different
    types
  • A narrowing conversion may lose information
  • A widening conversion preserves information (at
    least approximately)
  • An implicit type conversion is often called a
    coercion
  • An explicit conversion is often called a cast

Examples
Discussion
?Advice
8
Boolean Expressions
  • Comparison symbols used vary among languages
  • "not equal" !, /, .NE., ltgt,
  • Boolean operators also vary
  • FORTRAN 77 FORTRAN 90 C Ada
  • .AND. and and
  • .OR. or or
  • .NOT. not ! not
  • What does "short circuit" mean?
  • C/Java yes
  • Pascal no
  • Ada explicit choice
  • C/Java Oddities
  • C has no boolean type
  • a lt b lt c is not what you might expect
  • similar bit operations

Discussion
?Advice
9
Assignment Oddities
  • Dual meaning
  • Multiple targets
  • Conditional targets
  • Compound assignment operators
  • Unary assignment operators
  • Statement or operator?
  • Implicit conversions

x y z
PL/1
x y z
Algol
a, b 10 x, y y, x
PL/1 published
(a b) ? x y 0
C
C
sum next
i
C
while ((c getchar()) ! EOF)
C
i x
C
?Advice
Write a Comment
User Comments (0)
About PowerShow.com