1 Introducing ASML Methods Values Constraints Constants Variables

1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP 201

2 I. Methods • Methods are named operations that may be invoked in various contexts • The definition of a method may include parameters that will be associated with particular values each time the method is invoked • Methods may also specify a return value that is available after the operation has been performed

The general form for a method definition the name of method The type of the return value, if there is one name (parameter 1 as Type, … parameter n as Type) as Type the values sent to the method, if there any 3

4 Method Is. Word() • An expression in the form Is. Word() is an application of the name Is. Word to argument i, which is a long integer. Is. Word ( i as Integer ) as Boolean return (0<= i and i <= 0 x. FFFF) Main() step if Is. Word ( 123 ) then Writelone (“ 123 is a word of memory. ”) • Is. Word evaluates an argument i and returns the value true if i 0 and i 0 x. FFFF (hexadecimal number). • If i doesn’t fit these criteria, Is. Word returns the value false.

Functions and update procedures • Functions have no effect on state variables • Functions must return a value Is. Word ( i as Integer ) as Boolean return (0<= i and i <= 0 x. FFFF) • Update procedures configure the values of state variables for the next sequential step of the machine • Update procedures may optionally return a value 5

Update procedure power() var x as Integer = 3 var y as Integer = 0 var index as Integer = 3 power (x as Integer, n as Integer) as Integer var result as Integer = 1 var i as Integer = 1 Output: step while i<=n result: =result*x 5 cubed = 125 i: =i+1 step return result 3 cubed = 27 Main() x = 81 step y : = power(5, 3) step Write. Line (“ 5 cubed =” +y) Write. Line(“ 3 cubed =” + power(3, index)) x: = power(x, power(2, 2)) step Write. Line (“x = ” +x) 6

7 Local names for values Statement in the form identifier = expression introduce identifier as a local name for the value given on the right hand side of the equals sign (“ = ”) by expression. Main() my. Set = {1, 2, 3} step Write. Line(“my. Set has” + as. String(size(my. Set))+ “ elements. ”) Local names may be introduced within any block of statement. Local names could appear after the “then” keyword of an if … then … else statement

Local names in sequences of steps • A local name can be referenced anywhere in its statement block. • In a sequence of steps, local names introduced within a step block are available within any following step block. Main() step my. Set 1 = {1, 2, 3} Write. Line (“my. Set has” + as. String(size(my. Set 1))+ “ elements. ”) step my. Set 2 = my. Set 1 union {4 , 5, 6} Write. Line(“my. Set has” + as. String(size(my. Set 2))+ “ elements. ”) 8

9 Method overloading allows you: • to use the same name in different methods • in each instance to have the compiler choose the correct instance Must be a clear way for the compiler to decide which method should be called for any particular instance

Method overloading example 10 S = { 1, 8, 2, 13, 6} Max (i as Integer, j as Integer) as Integer return if i > j then i else j Max (s as Set of Integer) as Integer return any m | m in S where not (exists n in S where n>m ) run() step writeln(“The largest number in the set is ” + ( Max(S) ) ) step writeln(“The largest of the two integers is ” + ( Max(2, 3) ) ) A series of methods with the same name but differentiated by their parameter lists are overloaded methods

II. Values What are values? 11 The term value has the same meaning as in mathematical sets A value is an immutable element that supports two operations: equality testing and set membership. If x and y are values, then we can ask whether x is equal to y If the S is a set, then we can ask whether x is an element of S

12 Structured values • Some values are composed of other values ordered pair • Some structured values are built into Asm. L; others may be defined by you structure Location base as Integer offset as Integer Location (1, 4) is a value of the user-defined structure type Location

13 Built in value types Data Type Meaning Boolean Byte A type that can be assigned true and false 8 -bit unsigned integer type Short 16 -bit signed integer type Integer 32 -bit signed integer type Long 64 -bit signed integer type Float Single-precision 32 -bit floating-point format type as specified in IEEE 754 Double-precision 64 -bit floating-point format type as specified in IEEE 754 Double

14 Built in value types Data Type Meaning Char Unicode character type String Unicode string type Seq of A A sequence of elements of type A Set of A A set containing elements of type A Map of A to B A table whose entries map of elements of type A to type B

15 Built in value types Data Type Meaning A tuple consisting of elements (A 1, A 2, …) of type A 1, A 2, … Tuple values are written in the same form For example, (1, 2) is of a value of the built-in type (Integer, Integer) Types in the form t? includes all of the values of type t plus A? the special value undef. For example, a variable declared as Boolean? could contain either of the Boolean value true or false or the value undef.

III. Constraints Assertions • Assertions require and ensure statements document constraints placed upon the model • Violating a constraint at runtime is called an assertion failure All. Tickets = {1. . 1024} Is. Ticket. ID (i as Integer) as Boolean require i > 0 return ( i in All. Tickets) Choose. Any. Ticket() as Integer ensure Is. Ticket. ID(result) return any t | t in All. Tickets • The keyword result denotes the value of the return statement 16

17 Type constraints • Types of constants, variables and method parameters may be specified using the syntax : name as type • Types may be explicitly checked using the “ is” operator : value is type an expression will be either true or false, depending on whether the value given is an element of the type • Types are not themselves values. For example, you can’t pass a type as an argument

18 IV. Constants • Constants are fixed, named values • Constants can either be global or local • Global constants are declared in the beginning of the program • Local constants are declared within a statement block • The general form of declaring and initialising a constant is: Name [as Type] = value Max. Integer = 100 run() Min. Integer = 0

19 V. Variables • Variables are names given to memory locations. • Updating variables is the only way to change a state of a machine. • Variables can be complex types such as structures or maps as well as simple types. • Here is the general form for declaring and initialising variables: var name [as Type] = value

20 Types of variables • There are three types of variables in Asm. L – Global variables – Local variables – Instance-based variables are variables that exist for each instance of a class var x as Integer = 10 // shows explicit typing var x = 10 // shows implicit typing var greeting as String = “Hello” // shows a // string variable var y = [1. . 6] // shows a sequence of // integers from 1 to 6 var menu = {“ham”, “cheese”, “bit”} // shows a
- Slides: 20