Sei sulla pagina 1di 59

The NewtonScript Programming Language

Alpha Draft 1.0


July 17, 1993 PIE Technical Publications Apple Computer, Inc. 1993

Apple Computer, Inc. 1993, Apple Computer, Inc. All rights reserved. No part of this publication or the software described in it may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, mechanical, electronic, photocopying, recording, or otherwise, without prior written permission of Apple Computer, Inc., except in the normal use of the software or to make a backup copy of the software. The same proprietary and copyright notices must be afxed to any permitted copies as were afxed to the original. This exception does not allow copies to be made for others, whether or not sold, but all of the material purchased (with all backup copies) may be sold, given, or loaned to another person. Under the law, copying includes translating into another language or format. You may use the software on any computer owned by you, but extra copies cannot be made for this purpose. Printed in the United States of America. The Apple logo is a registered trademark of Apple Computer, Inc. Use of the keyboard Apple logo (Option-Shift-K) for commercial purposes without the prior written consent of Apple may constitute trademark infringement and unfair competition in violation of federal and state laws. No licenses, express or implied, are granted with respect to any of the technology described in this book. Apple retains all intellectual property rights associated with the technology described in this book. This book is intended to assist application developers to develop applications only for Apple Newton computers. Apple Computer, Inc. 20525 Mariani Avenue Cupertino, CA 95014 408-996-1010 Apple, the Apple logo, APDA, LaserWriter, the light bulb logo, Macintosh, and Newton are trademarks of Apple Computer, Inc., registered in the United States and other countries. Newton ToolKit and QuickDraw are trademarks of Apple Computer, Inc. Simultaneously published in the United States and Canada.

LIMITED WARRANTY ON MEDIA AND REPLACEMENT If you discover physical defects in the manual or in the media on which a software product is distributed, APDA will replace the media or manual at no charge to you provided you return the item to be replaced with proof of purchase to APDA. ALL IMPLIED WARRANTIES ON THIS MANUAL, INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE LIMITED IN DURATION TO NINETY (90) DAYS FROM THE DATE OF THE ORIGINAL RETAIL PURCHASE OF THIS PRODUCT. Even though Apple has reviewed this manual, APPLE MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS MANUAL, ITS QUALITY, ACCURACY, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. AS A RESULT, THIS MANUAL IS SOLD AS IS, AND YOU, THE PURCHASER, ARE ASSUMING THE ENTIRE RISK AS TO ITS QUALITY AND ACCURACY. IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES RESULTING FROM ANY DEFECT OR INACCURACY IN THIS MANUAL, even if advised of the possibility of such damages. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer, agent, or employee is authorized to make any modication, extension, or addition to this warranty. Some states do not allow the exclusion or limitation of implied warranties or liability for incidental or consequential damages, so the above limitation or exclusion may not apply to you. This warranty gives you specic legal rights, and you may also have other rights which vary from state to state.

Draft. Preliminary, Condential. 1993 Apple Computer, Inc. 6/18/93

Contents
Related Publications vii

Chapter 1

Data Types, Operators, and Expressions


Data Types 1-1 Immediate References 1-1 Integers 1-1 Characters 1-2 Booleans 1-2 Pointer References 1-2 Symbols 1-3 Strings 1-3 Real numbers 1-4 Arrays 1-4 Frames 1-4 Operators and Expressions 1-5 Assignments 1-5 Copying NewtonScript values 1-6 Creating Frame Slots 1-7 Arithemetic operators 1-8 Relational operators 1-8 Logical operators 1-9 String Concatenators 1-9 Exists 1-9 Accessing Frame Slots 1-10 Path Expressions 1-10 Accessing Array Elements 1-11 Precedence and evaluation order 1-11

1-1

Chapter 2

Flow of Control
Compound statements Conditional statements Iterators 2-2 Break 2-2 For 2-3 Foreach 2-4 Loop 2-6 While 2-6

2-1 2-1 2-1

iii
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

Repeat

2-6

Chapter 3

Functions

3-1

Dening Functions 3-1 Return statement 3-1 Local statement 3-2 Constant statement 3-2 Parameter Passing 3-2 Scope 3-3 Local variables 3-3 Extent of Local Variables 3-4 Application-Wide Variables 3-5

Chapter 4

Object-Oriented NewtonScript Programming


Inheritance 4-1 Prototype Inheritance 4-1 Creating prototypes 4-2 Prototype inheritance rules 4-2 Parent Inheritance 4-3 Creating parent-child relationships 4-3 Parent inheritance rules 4-4 Combining prototype and parent inheritance Inheritance rules for setting slot values 4-6 Interactions between functions and inheritance An object-oriented example 4-7 Methods and messages 4-9 Self 4-10 Calling a function 4-10

4-1

4-4 4-6

Appendix A

Reserved Words

A-1

Appendix B

Special Character Codes

B-1

Appendix C

NewtonScript Quick Reference Card

C-1

iv
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

Figures and Tables

Chapter 1

Data Types, Operators, and Expressions


Table 1-1 Table 1-2 Figure 1-1 Figure 1-2 Table 1-3

1-1

Characters with special meanings 1-2 Special characters allowed within strings 1-3 Contrasting Assignment Statements in C and NewtonScript How NewtonScript Handles Variable Assignments 1-7 Operator precedence and evaluation order 1-12

1-6

Chapter 4

Object-Oriented NewtonScript Programming


Figure 4-1 Figure 4-2 Figure 4-3 Figure 4-4 Figure 4-6

4-1

Prototype frame 4-2 A prototype chain 4-3 Parent-child relationship 4-4 Prototype and parent inheritance interaction order An inheritance structure 4-8

4-5

AAppendix B

Special Character Codes


Table A-1 Table A-2

B-1 B-2

Character codes sorted by Macintosh character code Character codes sorted by Unicode B-5

v
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

vi
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

P R E FA C E

About This Book


This book, Newton Programmers Guide, is the denitive guide and reference for anyone writing software for the Newton family of personal digital assistants.
Note

The chapters in this book are at different stages of completion. Some are less complete than others. For all chapters, even the most complete ones, keep in mind that the information is preliminary, subject to change, and may not consistently reect the latest technical information available, although every effort is being made to supply you with information that is as accurate as possible. u

Related Publications
The suite of Newton technical documentation includes these other books:
n

A Guide to the Newton Toolkit. This book introduces you to the Newton development environment and teaches you how to develop Newton applications. You should read this book rst if you are a new Newton application developer. The NewtonScript Programming Language.. This book describes the NewtonScript programming language. If you are new to Newton development, you should read this book before the Newton Programmers Guide so that you are familiar with the language.

vii
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

P R E FA C E

viii
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

C H A P T ER

Data Types, Operators, and Expressions

In NewtonScript, every statement in a script evaluates to a value. Therefore, everything in NewtonScript programming is an expression. This chapter discusses the basic components of the language, its data types, its operators, and some of the expressions you can make by combining the two. Many of the concepts here are common to other programming languages.

Data Types
NewtonScript uses 32-bit references for data representation. There are two kinds of references: immediate references, in which the reference contains the data, and pointer references, in which the reference contains a pointer to a heap object

Immediate References
An immediate reference can represent one of three types of data:
n n n

integers characters Booleans, TRUE and NIL

The data is stored in 30 bits; the remaining two bits are ag bits.

Integers
Integers have no decimal place. They can be either signed or unsigned and are interpreted as decimal numbers unless prexed with 0x (zero and the letter x) to signify a hexadecimal value. Here are some examples of integers:

Data Types
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

1-1

CHAPTER

Data Types, Operators, and Expressions

13475 -86 0x56a Note that there is a 30 bit limit on the size of integers. Behaviour when that limit is exceeded is undened.

Characters
The character set in Newton is stored as Unicode, in two bytes, to facilitate international conversions. By design, the rst 128 characters match the rst 128 characters of the Macintosh standard character set. You must use Unicode character codes to specify characters higher than the rst 128 in the character set (that is, special characters.) Characters in the standard character set are specied in your code by the dollar sign. For example, $a or $7 represent the characters "a" and "7", respectively. Special characters like "" must be specied as Unicode (16-bit) characters by using the four-digit hex character code preceded by $\u. The Unicode equivalent of "" is: $\uF7FF See Appendix C, "Special Character Codes" for a list of the special characters and their Unicode equivalents. In addition to special printed characters there are other characters that have special meanings. They are shown in Table 1-1.

Table 1-1

Characters with special meanings

Character code

Meaning

$x $\n $\t $\\ $\xx $\uxxxx

the character x newline tab backslash hexadecimal Unicode; see Appendix C, "Special Character Codes"

Booleans
Newton denes only two Boolean constants, TRUE and NIL. In conditional statements, anything that is not NIL is considered to be TRUE.

Pointer References
A pointer reference represents one of several kinds of heap objects,including:
n n n n

Symbolstags used to identify any kind of value Stringsquoted constants Real numbersimmutable oating point numbers Arraysa numbered collection of references

1-2

Data Types
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER 1

Data Types, Operators, and Expressions

Framesa collection of named references

Symbols
NewtonScript symbols are like identiers in other computer languages. A symbol is a sequence of as many as 254 characters, including letters, trailing numbers, and the underscore character '_'. A symbol
n n

cannot begin with a number can include capital and lower-case letters; (NewtonScript is case insensitive, though it preserves case) can break the above rules (except the 254 character limit) if the symbol is enclosed in vertical bars (|). For instance, |Walter Smith!| is a valid symbol, even though it contains spaces and special characters

Symbols normally evaluate to the value of their contents. You can prevent this by preceding the symbol name with a single quote ('). In this case the symbol evaluates to the symbol itself.

Strings
A string constant is zero or more characters enclosed in double-quotation marks. Examples include "pqr" "Now is the time" ""

You can include characters that are not in the standard character set in strings by inserting the escape code, \u, to toggle on the Unicode hex mode. Follow the \u with the four-number code specifying the special character. If you need to return to the regular character set, add another \u to toggle the Unicode hex mode off; however, you are not required to turn toggle the hex mode off. (See Appendix C, "Special Character Codes" for a list of the special characters.) For example, you could specify the French phrase, "Garon, le billet, sil vous plat!", by embedding Unicode in the string to specify the special characters. You accomplish this as follows: "Gar\u00e7\uon, le billet, sil vous pla\u00ee\ut!" Other characters you can include in strings are summarized in Table 1-2.

Table 1-2

Special characters allowed within strings

Character

Meaning

\n \t \u \\

new line tab toggles Unicode on and off backslash

Data Types
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

1-3

CHAPTER

Data Types, Operators, and Expressions

Real numbers
Real numbers must contain a decimal point, which can appear any place within the number. Hence they are known as "oating point" real numbers. They are represented internally in double precision, that is, in 64 bits. They have 15 digits of precision. Some examples of real numbers include: -0.587 123.9 3.141592653589 You can also use exponential notation to represent real numbers. Simply follow the real number with an "e" and a signed integer exponent, in the range of -308...+308, as shown in these examples: 763.112e4 87.3789e-45 -34.2e6 69.e-5 Real numbers are stored as binary objects and have the implicit class Real.

Arrays
Arrays are numbered lists of zero or more references, called array elements that are seperated by commas, and enclosed in brackets. The syntax of an array is: [className1: element1 ,element2 ,elementn, ] Here is a simple example of an array: [1,2,3] Because this array does not include an explicit class name, NewtonScript assigns it the implicit class Array. The Array designation simply gives the array a type; it does not give it any special properties. You can specify a class name for your array by preceding the rst array element with a class identier and a trailing colon, as shown here: [RandomData: [1,2,3], {check:"121", amount:9.00}, "Last element"] Array elements may consist of any mixture of the data types that NewtonScript supports. Note that the RandomData array in this example contains another array as its rst element, a frame as the second element (this data type is discussed in the section Frames,) and a string as the third element. NewtonScript numbers the elements in an array by beginning with zero for the rst element. Therefore, element 0 in the RandomData array is the array [1,2,3].

Frames
A frame is the basic data object in a NewtonScript program. A frame, like an array, is a list of references separated by commas. However, the references in frames are named rather than numbered. A frame is enclosed in curly braces and is composed of zero or more slots, which are name: value pairs. The syntax of a frame is:

1-4

Data Types
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER 1

Data Types, Operators, and Expressions

{slotName1:value1 , slotName2:value2 , slotNameN:valueN , } The slot name can consist of any valid symbol that refers to any valid NewtonScipt data type. A frame can consist of any number of slots. Here is a simple example of a frame: {name:"Joe Bob", phone:"4-5678", employee:12345} Slots can contain references to objects, including other frames and functions. Here is an example of a frame containing another frame and a function: { Jupiter:{size:491, distance:8110,speed:34}, position: func(foo) speed*foo/3.1416 } You may specify an optional class for a frame. This is stored in a slot that you give the name class. Just as for array classes, the class of a frame gives it a type, not special properties. You can also specify references to other frames by using the slot names _proto and _parent. These set up hierarchical and prototypical behaviors which you can use to construct object-oriented programs. Chapter 3, "Inheritance," describes these concepts and behaviors.

Operators and Expressions


NewtonScript operators include assignment operators, arithmetic operators, logical and relational operators, messaging operators, array and frame element accessors, and bitwise operators.

Assignments
Data type declarations are unnecessary in NewtonScript; assignment statements serve the purpose of initializing variables. A simple assignment statement takes the form a := b; The trailing semicolon is an optional statement separator. Essentially, an assignment statement looks like this: lvalue := expression where lvalue is a variable, array or frame access expression. Since all statements have a value, you could also construct the following assignment statement: x := if a > b then a else b; Here is an example of the assignment of a frame reference to a symbol: myFrame := {name: "", phone: "123-4567"} Once the frame has been created you can assign values to slots like this: myFrame.name := "Julia" If the frame slot does not already exist it will be created.

Operators and Expressions


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

1-5

CHAPTER

Data Types, Operators, and Expressions

You can construct arrays and assign values to them in a similar manner. The second line of this code fragment changes the value 789 to 987. myArray := [123, 456, 789, "a string"] myArray [2] := 987
s Note

If you accidentally write an assignment statement as = rather than :=, your statement becomes a simple relational expression. For example, the statement x = 5 returns TRUE or NIL and leaves the value of x unchanged. s

Copying NewtonScript values


NewtonScript, unlike C, does not copy the contents of a memory location when it makes an assignment. Instead, since most NewtonScript values are pointer references rather than immediate references, NewtonScript merely reuses the reference rather than copying the actual object. Figure 1-1 shows this difference graphically.

Figure 1-1

Contrasting Assignment Statements in C and NewtonScript

C x=foo; foo x NewtonScript x:=foo; foo x

18E3 2E 2E

memory address contents contents memory address contents contents

18E3 2E 18E3

You can see how these differences manifest when you compare these code snippet examples from the C programming language and NewtonScript. In the C example we have the struct named foo that contains an integer member x. You then make two instances of the foo struct named a and b. You assign the integer 1 to the member x in the a structure. You then save the value of the a struct to the b struct and change the value of the variable a.x to 2. As you expect, the value of the member b.x is 1 at this point in the execution of the C code. C: struct foo { int x; };

1-6

Operators and Expressions


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER 1

Data Types, Operators, and Expressions

foo a; foo b; a.x = 1; b = a; a.x = 2; // at this point b.x = 1 The equivalent code in NewtonScript local a := {x: 1}; local b := a; a.x := 2; // at this point b.x = 2 gives a different outcome. The rst assignment statement essentially creates the slot a.x and stores the integer 1 in it. In memory, NewtonScript creates an address for the slot a.x that points to a memory location where the integer 1 is stored. When the frame a is assigned to the local variable b, NewtonScript copies the address referring to the location of frame a, so that they share the identical contents. Both of the variables, a and b, refer to the same memory location. When that memory location is changed for one variable it is automatically changed for both. See Figure 1-2 for a picture of the process. Since the two variables share the same memory reference, the slot b.x is assigned the value of 2, by default, when the slot a.x is assigned the value of 2.

Figure 1-2

How NewtonScript Handles Variable Assignments

a:= {x: 1}; b:= a;

a.x b.x a.x b.x

18E3 18E3

18E3

memory address contents memory address contents

a.x:= 2;

18E3 18E3

18E3

Creating Frame Slots


To create a new slot in a frame, simply assign a value to it. If the slot does not exist, it is created. As shown in the discussion above, the statement aFrame:= {x: 1}; dynamically creates the frame, aFrame, with the slot, aFrame.x, and puts the value 1 into it.

Operators and Expressions


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

1-7

CHAPTER

Data Types, Operators, and Expressions

Note

The NewtonScript inheritance rules affect the ultimate behavior of assignment statements in frames. For more information about inheritance and setting slot values see "Inheritance rules for setting slot values,"in Chapter 4.

Arithemetic operators
NewtonScript provides the standard set of binary arithmetic operators. They include, in order of precedence: * / div mod + See Table 1-3 for a summary of operator precedence.

Relational operators
The relational operators are less than (<), less than or equal to (<=), greater than (>), and greater than or equal to (>=). The equality operators are equal (=) and not equal (<>). The relational and equality operators work the way you would expect them to work on immediate reference data types. For referenced values, strings, arrays, and frames, the contents are compared only with the relational operators, <,<=, >, >=. However, the equality operators actually compares pointers, not contents. For instance, if you type [1,2] = [1,2] into the Newton ToolKit listener and press Return, it evaluates to NIL because the two arrays are not the same object. However, if you use the statement, x := [1,2] to assign a reference to an array to the symbol x, then assign the same reference to the variable y, and nally compare their equality using the statements, y := x x = y NewtonScript returns TRUE. You can see that the assignment statement copied the pointer values rather than the evaluating the pointers and copying the values stored at the pointer address. If you try the same exercise when an immediate value is stored in a variable, the result is what you would expect using a language like Pascal. NewtonScript returns the value TRUE in response to the following lines of code: a := 1 b := a a = b

1-8

Operators and Expressions


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER 1

Data Types, Operators, and Expressions

Logical operators
The logical operators include the three reserved words: not and or Like their counterparts, || and &&, in the C programming language, expressions involving the logical operators and or

stop evaluation as soon as the conclusion of the verity of the expression has been determined. For instance, if the value of the rst half of the AND conditional expression in the if statement if x < length(someArray) AND someArray[x] = 42 then doSomething; is NIL, then NewtonScript does not process the second part of the AND conditional statement. Similarly, the OR statement stops executing as soon as one non-NIL value is determined since an OR logical operation only requires one TRUE value to make the whole statement TRUE.

String Concatenators
The two string concatenators, && and &, concatenate strings by creating a new string from the two strings you specify. You can also use these operators to concatenate objects that are not strings. For instance, the statement "foo" & 17 returns the string "foo17" The single & operator also creates copies of two strings you specify and concatenates them by tacking one onto the end of the other with no space in between. In contrast, the && operator concatenates the two strings you specify, by placing a space at the end of the copy of the rst string and then placing a copy of the second string after the space.

Exists
Exists is a special postx operator that is used to check for the existence of variables or slots. When you apply the exists operator to an identier, such as a variable, it returns TRUE if the variable is dened in the current context and NIL otherwise. (See"Scope" in Chapter 2 for more informtion about the scope of variables.) Here is an example of a simple IfThen structure that uses the exists operator:

Operators and Expressions


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

1-9

CHAPTER

Data Types, Operators, and Expressions

if myVar exists then myVar else 0 When you apply exists to a frame slot access expression (see the following section), exists returns TRUE if the slot exists in the frame or any of its prototype frames, and NIL otherwise. For more information about frames and inheritance, see Chapter 3 "Inheritance." Here are some examples (IfThen expressions are described below in the section Condition Expressions): if myFrame.aSlot exists then if not hasSlot(myFrame, 'aSlot) then print("'aSlot slot is in a prototype of myFrame") The built-in function hasSlot provides similar functionality to the exists operator. These functions differ in the type of inheritance that is used to search for slots. Refer to Chapter 4, "Functions," to nd out more about built-in NewtonScript functions.

Accessing Frame Slots


Slots in a particular frame can be accessed by using a frame identier followed by a dot. For example, the expression myFrame.name returns the contents of the name slot from the frame named myFrame. If the slot does not exist, NewtonScript returns NIL. If the slot is not found in the specied frame when you use the dot operator to access a frame slot, prototype inheritance is used to search prototype frames. However, the search does NOT continue up to the parent frame and its prototypes. The search uses only prototype inheritance, not parent inheritance. Three built-in functions, GetVar, GetVariable, and GetSlot, provide similar kinds of slot access, but with different inheritance behavior. Refer to the Newton Programmers Guide for details.

Path Expressions
Alternatively, you can access frame data by using a path expression, like this: myFrame.(expression) where the path expression is enclosed in parentheses within a slot access expression. Here, the value of expression is used as the slot name. For example: myFrame := {Name: "Joe", Title: "Cat"} slotPath := 'Title myFrame.(slotPath) The value of myFrame.(slotPath) is "Cat". In this script, myFrame.(slotPath) evaluates to the Title slot, and the value Cat is returned. The symbol 'Title represents the path to the slot named Title, of the frame myFrame. Longer path expressions are also allowed:

1-10

Operators and Expressions


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER 1

Data Types, Operators, and Expressions

> myFrame:={Kind:"Cat",Type:{Hair:"long",Color:"black"}} > slotPath := 'Type.color > myFrame.(slotPath) The value of myFrame.(slotPath) is "black". A path expression can also be expressed as an array with the class "pathExpr", where each array element is a slot in the path. For example, here is the previous example using array syntax for the path expression: myFrame:={Kind:"Cat",Type:{Hair:"long",Color:"black"}} slotPath := '[pathExpr: Type, color] myFrame.(slotPath) The value of myFrame.(slotPath) is "black". Note this syntax: [pathExpr: Type, color]. This creates an array with the class "pathExpr".

Accessing Array Elements


Array elements are accessed with square brackets, for example: myArray := [123, 456, "Alice's Restaurant"] myArray[2] The value of myArray[2] is "Alice's Restaurant". This expression returns the contents of the third element in the array myArray. Note that array indexes begin at zero. Like frame slots, array elements can also be accessed using path expressions. The path to an array element is simply the array index of the element. Here is an example: aframe:={name:"Matt",info:{grade:"B",tests:[87,96,73]}} mypath:='[pathExpr: info,tests,1] myframe.(mypath) The value of myFrame.(mypath) is 96.

Precedence and evaluation order


The precedence of operators is a set of ratings that tells the compiler which operator to evaluate rst, in case it is not inherently apparent in the ststement. Table 1-3 lists the order of precedence of all the NewtonScript operators in order from top to bottom. Note that operators grouped together in the table are of equal precedence. Evaluation order is simply the order in which the symbols in a line of code are processed. Most operators in NewtonScript are processed and evaluated from left to

Precedence and evaluation order


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

1-11

CHAPTER

Data Types, Operators, and Expressions

right. The one notable exception is the assignment statement, which is evaluated from right to left.

Table 1-3

Operator precedence and evaluation order

Operator

Action

Evaluation Order

. : :? [] << >> * / div mod + & && exists < <= > >= = <> not and or :=

slot access message send conditional message send array dereferencer unary minus left shift right shift multiply oat division integer division remainder add subtract concatenate (string replicant of exprs) concatenate with 1 space between variable & slot existence less than less than or equal greater than greater than or equal equal not equal logical not logical and logical or assignment

left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right

left-to-right left-to-right

none left-to-right

left-to-right left-to-right right-to-left

1-12

Precedence and evaluation order


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Flow of Control

Flow of control in a NewtonScript program is determined by


n n n

Compound statements Conditional statements Iterators

This chapter discusses the syntax and function of these constructs.

Compound statements
The BeginEnd construct is used to group statements. You can use these groups anywhere the syntax calls for a single expression or statement. For example, you may want to execute more than one action or expression in the then part of a conditional statement. In that case you simply use the Begin and End statements to group the statements as shown here: if x=length(myArray) then begin result := :read(x); print(result) end The return value of a BeginEnd block is the value of either the last expression in the block or the value of an executed Return statement. Note that the optional trailing semi-colon doesnt affect the return value of the last statement.

Conditional statements
Conditional expressions have the following syntax: if testExpression then firstStatement else alternateStatement

2-1
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Flow of Control

If testExpression evaluates to anything other than NIL, it is considered TRUE. As is standard in other programming languages, the statement following the testExpression is executed if testExpression is not NIL (true) and its value is returned. Note that not all statements return a value. For instance, if x > 12 then Print(x); returns NIL. If you have a test expression that evaluates to NIL, the optional alternate statement of the else clause is evaluated. If there is no else statement, NewtonScript returns NIL.

Iterators
Like all other NewtonScript expressions, iterative statements also return a value. The iterators normally return a NIL value unless a Break statement, which can be used to stop execution of a loop, is executed. As in other programming languages, loops can be nested. NewtonScript includes these iterators:
n n n n n

For Foreach Loop While Repeat


s Warning

Loops are not valid at the top level, that is, outside of a function. This shouldt be a problem in actual coding because you wouldnt put a loop at the top level, except when testing or debugging. For more information about function specications see s

Break
While not an iterator itself, the Break statement interrupts the execution of any of the iterative structures. You must use the Break statement to stop the simple Loop structure, which has no built-in constructs to stop it. See the section Loop in this chapter to nd out more about that structure. The syntax of the Break statement is: break expression; The Break statement optionally takes an argument that is an expression, which is evaluated and returned as the value of the loop. If you use the Break statement with no expression, the loop returns the value NIL.

2-2
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Flow of Control

For
You use a For loop to perform an action repeatedly, until a loop counter variable reaches a certain value or a break statement is reached. The syntax of a For loop is: for counter := initialExpression to limitExpression by incrExpression do statement; When a For loop starts, the counter variable is set to the value of the initial expression variable. The values of the initial expression and the limit expression variables are set just once. If you do not specify a value for the incremental expression, NewtonScript uses a default value of 1. The incremental expression must be an integer. A value of zero is an error. After each repetition of the loop, the counter is incremented by the value of the incremental expression. If the incrExpression variable is positive, the loop test is counter <= limitExpression If the result of this test is not NIL, the statement in the loop is executed; otherwise it is skipped. If the value of the incremental expression is negative, the loop test is counter >= limitExpression If the value of the intial expression is less than the value of the limit expression then the statement in the loop is executed and the counter variable is incremented by either the default value of 1 (if no incremental expression is specied) or by the value of the incremental expression. After each repetition of the loop, the counter is incremented by the value of the incremental expression, which of course can be negative, or by the default value of 1. Again, if the incremental expression is zero, an error is generated at runtime. Here is an example: for x:=1 to 10 by 2 do print(x); 1 3 5 7 9 The value of counter is undened on loop exit. It is an error to change the value of counter from within the loop body. If you do so, the loop behavior is undened. As usual, you can use a group of statements enclosed in a BeginEnd block, instead of a single statement, inside a For loop.

2-3
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Flow of Control

Foreach
Looping with Foreach allows you to iterate an action over each element of an array or frame. The syntax of the Foreach loop is as follows: foreach slot, element in frameOrArray do|collect statement; This loop executes once for each element in an array or frame. On each iteration, the slot variable takes the value of the next array index or frame slot name, and the element variable takes the value of the next array element or frame slot, beginning with the rst element in the array or frame. Using both slot and element variables is optional, and when you specify just one variable, it is assigned the element value. Note that the values of slot and element are undened on loop exit. Here is an example using the Foreach loop to print slot names and values for a frame: myFrame := {name:"Carol", office:"San Diego", phone:"123-4567"}; foreach slot, value in myFrame do begin print(slot); print(value) end The output it returns is: name "Carol" office "San Diego" phone "123-4567" Here is an example for an array: myArray := ["John", "Mary", "Christine", "Mark"]; foreach index, value in myArray do begin print(index); print(value) end 0 "John" 1 "Mary" 2 "Christine" 3

2-4
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Flow of Control

"Mark" The alternative form of the Foreach loop uses the keyword "collect". In this variation, the results of the iteration are collected into an array which is returned. For example, the code snippet myFrame := {name:"Carol", office:"San Diego",phone:"123-4567"}; results := foreach value in myFrame collect value; print(results); gives you an easy way to collect data and print it out, producing the output: ["Carol", "San Diego", "123-4567"] Heres an example of some code that collects and prints both the slot names and values: results := foreach slot,value in myFrame collect [slot,value]; print(results); This example produces this output: [[name, "Carol"], [office, "San Diego"], [phone, "123-4567"]]
s Warning

Do not add or remove slots to a frame when youre within a loop that iterates over that frame; the results may be unpredictable! (Though it is not recommended, you can remove the current slot in an array.) s Setting values from within a loop If you are setting the value of several slots to a constant string by using a single statement in a loop to iterate over the slots, all the slots will contain the same pointer. (See Copying NewtonScript values, in Chapter 1 for a discussion of this behavior.) When the value of one slot is changed, all the slots in the group receive the same value. Consider this example: a := [f1, f2] local f; foreach f in a do begin SetValue(f, 'text, "") end The text slot in both frames f1 and f2 will contain a pointer to the same string value, which is probably not what you want. If you want to initialize empty strings like this, its best to set the value to NIL, not to the empty string. Another solution is to use the Clone function to clone the empty string as in the statement SetValue(f, 'text, Clone("")) See the NewtonScript Programmers Guide for details.

2-5
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Flow of Control

Loop
The syntax of the Loop statement is as follows: loop statement; This is a simple looping mechanism that simply repeats the statements within the loop until a Break statement is encountered. This example prints the value of the variable x until it reaches the value of 0 and the break statement is executed. x:=4 loop if x = 0 then break else begin print(x); x:=x-1 end 4 3 2 1

While
The syntax of the While loop is as follows: while expression do statement; The While loop works by evaluating the test expression rst. If the test evaluates to non-NIL, then the statement after the keyword do executes. This sequence repeats until the test expression evaluates to NIL and the loop ends.

Repeat
The syntax of the Repeat loop is as follows: repeat action until expression; The Repeat loop executes the action specied by the statements inside the loop rst and then evaluates the test expression. If the expression at the end of the loop evaluates to NIL, the statement actions repeat and the test expression is evaluated again, and so on until the expression evaluates to non-NIL. At this point the loop ends.

2-6
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Functions

This chapter describes NewtonScript functions.

Dening Functions
The general syntax of a NewtonScript function is: functionName: func(param1, param2, paramN) statements You may specify zero or more parameters in the parameter list of your function. The parentheses must appear. The body of the function is typically a group of statements enclosed by the Begin and End keywords, although it could be a single statement. A function returns a value, typically the value of the last statement enclosed in a BeginEnd block. This sample function diff returns the value of the difference between its two parameters: diff: func(n1, n2) begin if n1 > n2 then return n1 - n2 else return n2 - n1; end

Return statement
The Return statement sets a functions return value. The syntax of the return statement is return expression; The expression is optional. If it appears, the value of the expression is returned as the value of the function. If no expression follows the keyword Return, NewtonScript returns NIL as the function value.

Dening Functions
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

3-1

CHAPTER2

Functions

Local statement
You dene variables that are local to a function by using the Local statement, which has the syntax: local var1, var2, varN, varName := value; If you dont assign an initial value for a local variable, its assigned the default value NIL. For example, the statement local x, y:= 2, z; creates three local variables, x, y, and z, and initalizes them to the values of NIL , 2, and NIL, respecitvely.

Constant statement
You dene constants by using the Constant statement. Like local variables, constants may be dened and initialized in one statement. The syntax for a constant is: constant name1 ,name2, nameN , constantName := value; These sample Constant statements dene and initialize constants of various data types. constant thisConstant := 32; constant thatConstant := "Mike"; constant anotherOne := _symbol ;

Parameter Passing
NewtonScript functions pass parameters using the call-by-value mechanism. If you alter the value of a parameter, as in the function func foo(x) x := 1; the change is not permanent and is not detected outside the function. In other words, when an argument is an immediate reference data type, as in this example, the function cannot affect the argument passed to it. If the argument is a pointer reference to a structured data typefor example, if its an array, frame, or stringthen any changes your function makes to the internal values of that argument are persistent. Consider the following code fragments that make persistant changes: func foo(x) x[0] := 1; // sets 1st element of array arguments func foo(x) x.slot1 := 1; // sets slot1 of frame

3-2

Parameter Passing
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Functions

Scope
The part of a program within which a variable can be used is called the scope of the variable. Normally a variable is available within the function or frame where it is dened, although a frame variable (slot) can also be inherited from prototype and parent frames. (See Chapter 3, "Object-Oriented NewtonScript Programming," for more information about inheritance.) Variables that are accessible only in a certain parts of a program are called local variables.

Local variables
You can ensure that a variable is local to a function, and not inherited from the caller or other context (due to dynamic scoping), by using the Local statement to declare it before initializing it. For example: myFunc: func (x) begin local myVar, counter; ... end This example declares the variables myVar and counter as local variables. Each time the function is executed, they are created as separate variables, unique to the function myFunc. Local variables can also be initialized when they are declared, like this: local x:=3, y:=2+2; The declaration of the local variable is processed at compile time; the values are assigned at runtime, when the statement is encountered. For example, the statements x := 10; local x, y := 20; result in a value of 10 for x and a value of 20 for y. This works because y has been declared as a local variable and initialized to the value NIL at compile time. By contrast, the statements x := y + 10; local x, y := 20; result in a runtime error. Because local variables are not initialized until runtime, the rst statement is an attempt to add NIL to 10, which produces an error.

Scope
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

3-3

CHAPTER2

Functions

Attempting to access the value of an undened variable results in a runtime error. Attempting to assign a value to an undened variable creates a local variable of the specied name. When looking up a variable, NewtonScript rst searches the local variables, then the global variables, and then the inherited variables (through the proto and parent chains, as described in the previous chapter). For purposes of scope, BeginEnd blocks do not isolate local variables. For example: f := {foo: 10, bar: func(x) begin if foo then Print ("hello") ; if x > 0 then begin local foo; foo := 42; end; return foo; end; } For any value of x, nothing is printed. The variable foo has an initial value of NIL. Although the compiler has created a local variable foo, the variable is not initialized until the If statement that assigns it the value 42 executes. The return value of f:bar( 10 ) is 42. The return value of f:bar( -5 ) is undened.

Extent of Local Variables


NewtonScript doesnt necessarily free up the storage associated with a data structure when control exits the function block. Instead, it waits until a data structure is no longer referenced anywhere in executing code. Therefore, storage allocated for data structures is available until all references to the structures are deleted. Consider this example:

3-4

Scope
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER2

Functions

func () begin local myArray := [1,2,3,4]; return myArray; end The storage asscociated with myArray isnt freed up when control exits the function block. Therefore, if youre allocating large data structures you should make sure not to leave any references to them after youre done using them. If you do, NewtonScript will not reclaim the associated memory. By contrast, consider this buggy C code: int* fn (void) { int myArray[] = {1,2,3,4}; return myArray; } The problem is that when control exits the function block, the storage for myArray is deallocated, and pointer returned by the function merely points to some location in the stack.

Application-Wide Variables
Unlike local variables, slots that are created in the top-level (highest parent) frame of your application are available throughout. (For information on parent inheritance in NewtonScript, see Chapter 3, "Object-Oriented NewtonScript Programming." For example: myApp := { // Here are the globals myErrorBase: 5000, appName: "SampleApp" player: nil, . . . } Because these slots are dened and initialized at the top level, they are inherited by all child views of the application.

Scope
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

3-5

CHAPTER2

Functions

3-6

Scope
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Object-Oriented NewtonScript Programming

NewtonScript supports several object-oriented features and concepts through its inheritance system. NewtonScript uses frames as the basic data structure and inheritance is set up through the _parent and _proto slots in a frame. Methods are implemented as slots containing functions, and messages can be set using a special message sending syntax. This chapter describes parent and prototype inheritance as well as
n n n n

how to set up frames with these relationships the rules associated with parent and prototype inheritance how inheritance affects setting slot values the uses of parent and prototype inheritance

Inheritance
There are two kinds of inheritance in NewtonScript: prototype inheritance and parent inheritance.

Prototype Inheritance
A frame can have a prototype, which is simply another frame it references in a special way. A frame inherits slots from its prototype if it does not contain them in itself. If a frame contains a slot with the same name as a slot in the prototype, it overrides the prototype slot. You use inheritance from prototype frames (abbreviated as protos) for
n

object renementin the Newton system there are many system prototypes you can use whenever you need them

Inheritance
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

4-1

CHAPTER

Object-Oriented NewtonScript Programming

persistent storage of data this must be something that does not change

Creating prototypes
You create a prototype relationship between frames by using a special slot named: _proto whose value references the frame you want to use as your prototype frame. For example, to use a frame called pageTemplate as a prototype for a frame called myPage, you include a _proto slot whose value is a pointer to the pageTemplate frame. This is illustrated in Figure 4-1.

Figure 4-1

Prototype frame

myPage {_proto: pageTemplate leftMargin: 0.75 }

pageTemplate {topMargin: 1 bottomMargin: 2 leftMargin: 1.5 rightMargin: 1.5 }

This frame serves as a prototype

Prototype inheritance rules


If during runtime a function in the frame myPage references the slot named topMargin, as shown in Figure 4-1, the interpreter looks rst at the frame myPage. It doesnt nd the slot named topMargin so it follows the _proto reference to its prototype frame, pageTemplate, where it nds the slot and gets the value 1. In this case, the frame myPage inherits that slot. However, if a function in the frame myPage, references the slot named leftMargin, that slot is found in the current frame and evaluates to the value 0.75. In this case, the current frame overrides the value of the prototype. Note that since frame slots can contain functions as well as simple expressions, functions can also be inherited and overridden. The system obtains values during runtime by following the prototype inheritance rules for looking up slot references. NewtonScript looks rst in the current frame for a slot name. If the slot is not found, it looks at the prototype frame, and if the slot is still not found, it looks at that frames prototype frame, and so on, through all the prototypes in the chain.

4-2

Inheritance
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Object-Oriented NewtonScript Programming

An example of a prototype chain is shown in Figure 2-2. In this gure, the inheritance chain starts with the current frame, myPage, and follows the arrows to its prototypes frames on the right.

Figure 4-2
myPage {_proto: . . }

A prototype chain
pageTemplate {_proto: . . } someFrame {_proto: . . } someOtherFrame {_proto: . . }

Parent Inheritance
Besides prototypical relationships between frames, you can set up hierarchical parent-child relationships. Inheritance from parent frames is used for
n n

sharing information between objects behavior and data objects, for example creating hierarchies, like the view hierarchy of Newton

Creating parent-child relationships


The parent-child link between frames exists by way of a slot named _parent, which exists in the child frame. You can set this slot directly in your code or you can use the drawing tools in the Newton Toolkit to create view hierarchies. See Getting Started with the Newton Toolkit for more information about how to do this. Figure 4-3 shows an example of a parent-child relationship between two frames.

Inheritance
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

4-3

CHAPTER

Object-Oriented NewtonScript Programming

Figure 4-3

Parent-child relationship

myDoc {. . . }

Parent

myPage {_parent: myDoc . . }

Child

Frames that serve as parents can themselves be children of other frames, thereby forming an inheritance chain extending upwards.

Parent inheritance rules


When you create parent-child hierarchies between frames, NewtonScript uses an inheritance mechanism that works similarly to prototype inheritance. As in prototype inheritance a child frame inherits slots from its parent that it does not itself contain. However, if a child frame contains a slot name that is the same as one in a parent frame, the child slot overrides the parent.

Combining prototype and parent inheritance


In practice, most frames have prototypes and parents. When a slot is referenced during runtime, the parent inheritance mechanism interacts with the prototype inheritance mechanism. The rules for inheritance order are really very simple: 1. NewtonScript looks rst in the current frame for a referenced slot. (Unless you specically reference a different frame with a dot operator expression like myDoc.header. In that case, the search begins in the referenced frame, myDoc, and

4-4

Inheritance
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Object-Oriented NewtonScript Programming

moves forward to include only prototype frames of myDocparent inheritance does not apply. For details on using the dot operator to access frame slots see the section Accessing Frame Slots.) 2. If the slot is not found, the prototype chain of the current frame is searched. 3. If the slot is still not found, the search moves up one level to the parent frame. The parent and its prototype chain are searched in order. The search then moves up another level (to the parents parent) and continues in the same way until the slot is found. The numbers in Figure 4-4 indicate the order that frames are searched for a slot reference that is made from a function in the current frame.

Figure 4-4
myDoc {_proto: baseDoc header: nil footer: 2 }

Prototype and parent inheritance interaction order


baseDoc {_proto: docTemplate footer: 1 } docTemplate {paperSize: standard . . }

myChap {_parent: myDoc _proto: chapTemplate sides: double chapNum: 2 }

chapTemplate {chapNum: nil header: true . }

myPage {_parent: myChap _proto: pageTemplate leftMargin: 0.75 }

pageTemplate {_proto: sectionTemp topMargin: 1 bottomMargin: 2 leftMargin: 1.5 rightMargin: 1.5 sides: single }

sectionTemp {sides: nil . . }

1
Current Frame

Basically, prototype inheritance takes precedence over parent inheritance; all prototype frames on one level are searched before moving up to search a parent frame and its prototypes on another level.

Inheritance
Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

4-5

CHAPTER

Object-Oriented NewtonScript Programming

s Note

You can use the Inherited keyword when executing a function, to force an inherited version of the function to be executed rather than the version in the current frame. This only works with prototype inheritance. s

Inheritance rules for setting slot values


Inheritance rules apply not only when a slot is referenced, but also when its value is set. However, the rules are different for setting a slot. The basic difference is that slot values are only changed in parent frames during runtime. They are never changed in prototype frames during runtime. One reason for this is that prototype frames often exist in ROM and therefore, cannot be changed. (Of course, when you are rst creating the prototype frames their slots can be set, but not when the application is running.) When setting a slot, the inheritance search is the same as for slot reference, except that the slot is not always set where it is found. These are the rules for where a slot is set: 1. If a slot exists in the current frame, its value is set there. 2. If the slot exists the prototype chain of the current frame, it is copied into the current frame and its value is set there. 3. If the slot exists in the parent of the current frame, its value is set in that parent frame. 4. If the slot exists in the prototype chain of the parent, it is copied into the parent frame at the same level where it was found, and its value is set in that parent frame. Note that if you create the slot from within a function, it is created as a local variable within the scope of the function only. If you want to make sure the slot is created as a global in the current frame, you must use the special frame specier Self. Self always refers to the frame in which execution is occurring. If you want to explicitly set the value of a slot in the parent of the current frame, you can use the expression Self._parent.theSlot to force the slot to be created there.

Interactions between functions and inheritance


NewtonScript saves information about the context of a NewtonScript function (unlike function pointers that are references to blocks of code in C.) In NewtonScript, a function object is created when the keyword func compiles. Consider the simple function:

4-6

Interactions between functions and inheritance


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Object-Oriented NewtonScript Programming

simpleFunction := func(x) x+y; Along with the code, NewtonScript saves information about the frame in which it was dened (its lexical environment) and about what frame received the message (its receiver environment.) An example of the frame object is shown in Figure 4-5.

Figure 4-5

Functions in NewtonScript contain information about their lexical and receiver environment.

Code Receiver environment Lexical environment

func (x) x+y self Defining code block

When the function is called with x as a parameter, NewtonScript looks for the free variable y, rst in the frame where the function is dened. If it doesnt nd a value there it continues looking in the inheritance chain.

An object-oriented example
The inheritance structure shown in Figure 2-6 demonstrates some of the essentials of sending messages.

Interactions between functions and inheritance


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

4-7

CHAPTER

Object-Oriented NewtonScript Programming

Figure 4-6
frame1

An inheritance structure

{ slot1: "slot1 frame1" slot6: 99 }

frame2 { _parent: frame1 slot1: "slot1 frame2" slot2: "slot2 frame2" }

frame4 { _parent: frame2 _proto: frame3 msg1: func() }

frame3 { slot3: "slot3 frame3" slot5: 42 }

You construct this using the code: frame1 := { slot1: "slot1 from frame1", slot6: 99; }; frame2 := { _parent: frame1, slot1: "slot1 from frame2" slot2: "slot2 from frame2",};

frame3 := { slot3: "slot3 from frame3", slot5: 42,};

4-8

Interactions between functions and inheritance


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

CHAPTER

Object-Oriented NewtonScript Programming

frame4 := { _parent: frame2, _proto: frame3, msg1: func() begin //show slot from parent inheritance Print(slot1); //show slot from proto inheritance Print(slot3); //show slot from parent inheritance - again //but doesn't work cause // self.slot1 only searches proto chain Print(self.slot1); //show slot from proto inheritance - again Print(self.slot3); end

Methods and messages


Methods in NewtonScript are nothing more than functions dened in a frame slot. As in other object-oriented languages, you can send methods as messages to objects (frames) that exist in your code. The syntax you use to send a message is: frameExpression : messageName (args) Using this syntax, you are actually sending the message, messageName along with any arguments, to the frame (that is the value of the frame expression) on the left side of the colon. In the example, if you type the command line > frame4:msg1() you are sending msg1 to frame4. This produces the output: "slot1 from frame2" "slot3 from frame3" NIL "slot3 from frame3" #2 NIL

Interactions between functions and inheritance


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

4-9

CHAPTER

Object-Oriented NewtonScript Programming

Self
As mentioned earlier, there is a special variable, named Self, that you can use in your script code. It always evaluates to the receiver; that is, the last frame which received a message. This is almost always the frame in which execution is currently occurring.

Calling a function
You rarely call functions using NewtonScript. There is, however, a syntax for calling a function. It is: call functionExpression with (arg) Note that another way to call a function is to use the built-in Newton function apply(functionExpression,[args]) The second syntax may actually be preferable to the rst, since it allows you to dynamically set the arguments. It is important to be aware that when you call a function, the system variable is set to the frame in which the function is called.

4-10

Interactions between functions and inheritance


Alpha Draft, Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Reserved Words
The following words are reserved in NewtonScript. You may not use any of these words as symbols unless you enclose the word in vertical bars, like this: |self|.

and begin break by call collect do div else end exists for foreach from func global if in inherited local loop mod not onexception or repeat return

A-1
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/19/93

APPENDIX

Reserved Words

self send set then to try until while with

A-2
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/19/93

APPENDIX

Special Character Codes


This appendix contains a character code table that has both Macintosh and Unicode (16-bit) character codes for the high 128 characters in the Newton character set (characters 128 through 254). When specifying character constants or strings that contain characters from the high 128 characters, you must use unicode character codes. The Macintosh character codes are provided for convenience if you are used to using them.

B-1
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Table A-1

Character codes sorted by Macintosh character code

Mac

Unicode

Char

80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2

00C4 00C5 00C7 00C9 00D1 00D6 00DC 00E1 00E0 00E2 00E4 00E3 00E5 00E7 00E9 00E8 00EA 00EB 00ED 00EC 00EE 00EF 00F1 00F3 00F2 00F4 00F6 00F5 00FA 00F9 00FB 00FC 2020 00B0 00A2

B-2
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6

00A3 00A7 2022 00B6 00DF 00AE 00A9 2122 00B4 00A8 2260 00C6 00D8 221E 00B1 2264 2265 00A5 00B5 2202 2211 220F 03C0 222B 00AA 00BA 2126 00E6 00F8 00BF 00A1 00AC 221A 0192 2248 2206

B-3
Alpha Draft. Preliminary 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA

00AB 00BB 2026 00A0 00C0 00C3 00D5 0152 0153 2013 2014 201C 201D 2018 2019 00F7 25CA 00FF 0178 2044 00A4 2039 203A FB01 FB02 2021 00B7 201A 201E 2030 00C2 00CA 00C1 00CB 00C8 00CD

B-4
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF

00CE 00CF 00CC 00D3 00D4 F7FF 00D2 00DA 00DB 00D9 0131 02C6 02DC 00AF 02D8 02D9 02DA 00B8 02DD 02DB 02C7

Table A-2

Character codes sorted by Unicode

Mac

Unicode

Char

CA C1 A2 A3 DB B4 A4 AC

00A0 00A1 00A2 00A3 00A4 00A5 00A7 00A8

B-5
Alpha Draft. Preliminary 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

A9 BB C7 C2 A8 F8 A1 B1 AB B5 A6 E1 FC BC C8 C0 CB E7 E5 CC 80 81 AE 82 E9 83 E6 E8 ED EA EB EC 84 F1 EE EF

00A9 00AA 00AB 00AC 00AE 00AF 00B0 00B1 00B4 00B5 00B6 00B7 00B8 00BA 00BB 00BF 00C0 00C1 00C2 00C3 00C4 00C5 00C6 00C7 00C8 00C9 00CA 00CB 00CC 00CD 00CE 00CF 00D1 00D2 00D3 00D4

B-6
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

CD 85 AF F4 F2 F3 86 A7 88 87 89 8B 8A 8C BE 8D 8F 8E 90 91 93 92 94 95 96 98 97 99 9B 9A D6 BF 9D 9C 9E 9F

00D5 00D6 00D8 00D9 00DA 00DB 00DC 00DF 00E0 00E1 00E2 00E3 00E4 00E5 00E6 00E7 00E8 00E9 00EA 00EB 00EC 00ED 00EE 00EF 00F1 00F2 00F3 00F4 00F5 00F6 00F7 00F8 00F9 00FA 00FB 00FC

B-7
Alpha Draft. Preliminary 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

D8 F5 CE CF D9 C4 F6 FF F9 FA FB FE F7 FD B9 D0 D1 D4 D5 E2 D2 D3 E3 A0 E0 A5 C9 E4 DC DD DA AA BD B6 C6 B8

00FF 0131 0152 0153 0178 0192 02C6 02C7 02D8 02D9 02DA 02DB 02DC 02DD 03C0 2013 2014 2018 2019 201A 201C 201D 201E 2020 2021 2022 2026 2030 2039 203A 2044 2122 2126 2202 2206 220F

B-8
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

Mac

Unicode

Char

B7 C3 B0 BA C5 AD B2 B3 D7 F0 DE DF

2211 221A 221E 222B 2248 2260 2264 2265 25CA F7FF FB01 FB02

B-9
Alpha Draft. Preliminary 1993 Apple Computer, Inc. 7/17/93

APPENDIX

Special Character Codes

B-10
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

NewtonScript Quick Reference Card


The third and fourth pages of this appendix contains a quick reference card for the NewtonScript programming language.

C-1
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

NewtonScript Quick Reference Card

C-2
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

APPENDIX

NewtonScript Quick Reference Card

C-3
Alpha Draft. Preliminary 1993 Apple Computer, Inc. 7/17/93

APPENDIX

NewtonScript Quick Reference Card

C-4
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

Index
A
Arrays 1-4

N
Numbers 1-1

B
BREAK statement 2-2

O
Operators 1-5

C
Characters 1-2 special codes 1-3

R
Real numbers 1-4 REPEAT loop 2-6

F
FOREACH loop 2-4 to 2-5 FOR loop 2-3 Frames 1-4 setting class 1-5 Functions dening 3-1

S
Scope 3-3 Strings 1-3 Symbols 1-3 special characters in 1-3

U G
Global variables 3-5 Unicode 1-2

V I
Inheritance 4-1 to ?? Integers 1-1 Variables global 3-5 local 3-3

L
Local variables 3-3 LOOP statement 2-6

W
WHILE loop 2-6

IN-1
Alpha Draft. Preliminary. 1993 Apple Computer, Inc. 7/17/93

Potrebbero piacerti anche