Starting from:

$30

Rust-eze CMPT 331 

Rust-eze
CMPT 331 
1 Introduction
Rust-eze is modern, object-oriented, type-safe programming language. It inherits the best parts of Rust and
Java to provide a fast and memory-safe language for the object-oriented paradigm, while also bringing in
some syntactic influence from Pascal. Rust-eze does, however, differ from its parent languages in the following
ways:
1. Rust-eze is an object-oriented language, which, like Java, means there are no structs and only classes/objects.
2. Like Java, but unlike Rust, Rust-eze requires that all variables be explicitly defined with their respective
types.
3. Similar to Rust, but unlike Java, Rust-eze uses a system of borrowing and ownership so only one
variable can point to a given place in memory at a time. This prevents the need for a garbage collector
as variables are automatically dropped and the memory is freed when they go out of scope.
4. Unlike Java, but like Rust, Rust-eze is compiled into the native binary, so there is no need for a JVM
or an intermediate bytecode representation of Rust-eze programs.
5. Similar to Rust, all instance variables must be initialized within the constructor.
6. Similar to both parent languages, all models (classes) belong to a garage (Java package). However,
more similar to Rust, the garage is inferred based on the relative file location and does not have to be
explicitly defined within the file.
2
1.1 Genealogy
3
1.2 Hello World
1 model HelloWorld
2 s t a r t
3 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
4 s t a r t
5 p r i n t l n ( " H ell o ␣world ! " ) ;
6 f i n i s h main
7 f i n i s h model
Listing 1: HelloWorld.rez
1.3 Program Structure
The key organizational concepts in Rust-eze are as follows:
1. Every file contains a single model (equivalent to a class), which should be the same name as the file
minus the .rez file extension.
2. All instance members default to being in the interior (private) unless they are declared with the ext
keyword to note their exterior (public) presence.
3. Instance variables are declared within the specs block and must be initialized within the constructor,
which is a function that is the same name as the model.
4. Local variables are immutable by default unless they are declared with the mut keyword.
5. The entry point for all Rust-eze programs is the main method that is located in one of the models of
the project.
The program below defines a new model called Lightning that contains 3 instance variables: an exterior
String called name, an exterior 32-bit signed integer called age, and an interior 32-bit signed integer called
miles. Each of these instance variables are initialized within the constructor. Each Lightning has 2 member
functions: drive and say_it. drive is an exterior method as it is defined with the ext keyword. Since it
modifies an instance variable, it must take in a mutable reference to the object in addition to the number of
miles being driven. Inside of the method, a new local variable called new_mileage is declared without the
mut keyword, meaning that it is immutable and is a constant with the value it is initialized with. The other
instance method, say_it, is also public and does not modify the instance variables, which is why it needs an
immutable reference to self.
1 model Li g h t ni n g
2 s t a r t
3 s p e c s
4 s t a r t
5 e x t S t ri n g name ;
6 e x t i 3 2 age ;
7 i 3 2 mil e s ;
8 f i n i s h s p e c s
9
10 e x t f n Li g h t ni n g ( S t ri n g my_name, i 3 2 my_age )
11 s t a r t
12 s e l f . name := my_name;
13 s e l f . age := my_age ;
14 s e l f . mil e s := 0 ;
15 f i n i s h Li g h t ni n g
16
17 e x t f n d ri v e (&mut s e l f , i 3 2 num_miles ) −> v oid
18 s t a r t
19 i 3 2 new_mileage := s e l f . mil e s + num_miles ;
20 s e l f . mil e s := new_mileage ;
4
21 f i n i s h d ri v e
22
23 e x t f n s ay_i t (& s e l f ) −> v oid
24 s t a r t
25 p r i n t l n ( "Kachow ! " ) ;
26 f i n i s h s ay_i t
27 f i n i s h model
Listing 2: Lightning.rez
Next, the Lightning model is imported to a new file called Main.rez and is initialized within the main method.
Since the mut keyword is used, we can use the mutable method of drive on the new object as well as directly
modify its exterior specs. After the_lightning’s name is printed, we call its say_it method and then call the
drive method with an input of 42 miles to increase the object’s mileage.
1 import g a r a ge . Li g h t ni n g ;
2
3 model Main
4 s t a r t
5 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
6 s t a r t
7 mut Li g h t ni n g t h e_li g h t ni n g := new Li g h t ni n g ( "McQueen" , 1 7 ) ;
8 p r i n t l n ( t h e_li g h t ni n g . name ) ;
9
10 t h e_li g h t ni n g . s ay_i t ( ) ;
11 t h e_li g h t ni n g . d ri v e ( 4 2 ) ;
12 f i n i s h main
13 f i n i s h model
Listing 3: Main.rez
1.4 Types and Variables
There are two kinds of variables in Rust-eze: value types and reference types. Variables of value types
directly contain their data, while variables of reference types store references to their data or objects in
memory. Due to the ownership system, only one variable of a reference type can point to a particular place
in memory at a given time. See Section 3 for details.
1.5 Visibility
In Rust-eze, visibility of methods and instance variables is defined as either interior (private) or exterior
(public). Everything is part of the interior unless explicitly stated to be in the exterior. Once a variable or
method is public, it may be accessed outside of the model in which it is defined.
1.6 Statements Differing from Rust and Java
Statement Example
Assignment statement
mut i 3 2 x := 5 ;
x := 3 ;
i 3 2 y := x + 2 ;
5
If statement
i 3 2 x := 7 ;
i f x > 3 && x < 9
s t a r t
p r i n t l n ( " H ell o ␣ t h e r e " ) ;
e l s e
p r i n t l n ( "Kachow" ) ;
f i n i s h i f
For loop
f o r mut i 3 2 i i n r an ge ( 0 , 1 0 , 1 )
s t a r t
p r i n t l n ( i ) ;
f i n i s h f o r
While loop
mut i 3 2 i := 0 ;
w hil e i < 10
s t a r t
p r i n t l n ( i ) ;
i := i + 1 ;
f i n i s h w hil e
2 Lexical Structure
2.1 Programs
A Rust-eze program consists of one or more source files. A source file is an ordered sequence of (probably)
Unicode characters.
Conceptually speaking, a program is compiled using five steps:
1. Transformation, which converts a file from a particular character repertoire and encoding scheme into
a sequence of Unicode characters.
2. Lexical analysis, which translates a stream of Unicode input characters into a stream of tokens.
3. Syntactic analysis (parsing), which translates the stream of tokens into a concrete syntax tree (CST).
4. Semantic analysis, which converts the CST into an abstract syntax tree (AST) and is passed to the
semantic analyzer for scope and type checking and the borrow checker to make sure all variables
referenced are active owners of data.
5. Code generation, which converts the AST into executable code for the target platform and CPU architecture.
2.2 Grammars
This specification presents the syntax of the Rust-eze programming language where it differs from Rust and
Java.
6
2.2.1 Lexical grammar (tokens) where different from Rust and Java
<assignment operator> → :=
<block begin> → start
<block end> → finish
<print> → println
<visibility modifier> → ext | ϵ
<mutability modifier> → mut | ϵ
2.2.2 Syntactic ("parse") grammar where different from Rust and Java
<model definition> → model <model name> <parent declaration> <block begin> <statements>
<block end> model
<parent declaration> → extends <parent model name> | ϵ
<specs definition> → specs <block begin> <spec definition> <block end> specs
<spec definition> → <visibility modifier> <type> <spec name>; <spec definition> | ϵ
<variable declaration> → <mutability modifier> <type> <variable name> <assignment operator>
<expression>;
<if statement> → if <condition> <block begin> <statements> else <statements> <block end> if
<for loop> → for mut <type> <var name> in <expr> <block begin> <statements> <block end> for
<while loop> → while <condition> <block begin> <statements> <block end> while
<function definition> → <visibility modifier> fn <function name> (<parameter list>) -> <return type>
<block begin> <statements> <block end> <function name>
<parameter list> → <type> <parameter name>, <parameter list> | ϵ
2.3 Lexical Analysis
2.3.1 Comments
Rust-eze supports two forms of comments: single-line and multi-line comments. Single-line comments start
with the characters // and extend to the end of the line in the source file. Multi-line comments begin with
/* and end with */ and may span multiple lines. Comments do not nest.
2.4 Tokens
There are several kinds of tokens: identifiers, keywords, literals, operators, and punctuators. White space
and comments are not tokens, though they act as separators for tokens where needed.
Tokens:
• identifier
• keyword
• integer literal
• real literal
• character literal
• string literal
• operator or punctuator
7
2.4.1 Keywords Different from Rust and Java
New keywords: println, range, model, specs, start, finish, ext, Tuple
Removed keywords: use, class, match, do, private, byte, short, str, boolean, static, public, double, long,
float, int, as, pub, impl, struct
3 Type System
Rust-eze uses a strong static type system. This means that the Rust-eze compiler will catch type mismatch
errors at compile time through early binding compile-time type checking.
3.1 Type Rules
The type rules for Rust-eze are as follows:
S ⊢ e1 : T
S ⊢ e2 : T
T is a primitive type
—————————
S ⊢ e1 := e2 : T
S ⊢ e1 : T
S ⊢ e2 : T
T is a primitive type
—————————
S ⊢ e1 == e2 : bool
S ⊢ e1 : T
S ⊢ e2 : T
T is a primitive type
—————————
S ⊢ e1 != e2 : bool
S ⊢ e1 : T
S ⊢ e2 : T
T is a numeric primitive type
—————————————
S ⊢ e1 > e2 : bool
S ⊢ e1 : T
S ⊢ e2 : T
T is a numeric primitive type
—————————————
S ⊢ e1 < e2 : bool
S ⊢ e1 : T
S ⊢ e2 : T
T is a numeric primitive type
—————————————
S ⊢ e1 + e2 : T
8
S ⊢ e1 : String
S ⊢ e2 : T
T is either a primitive, String, or a reference type that contains a to_string method
—————————————————————————————————————–
S ⊢ e1 + e2 : String
3.2 Value Types
Data Type Description
i8, i16, i32, i64 Signed integers that store up to X bits, where X
is the number after "i" in the data type.
u8, u16, u32, u64 Unsigned integers that store up to X bits, where
X is the number after "u" in the data type.
f32, f64 Floating point numbers that store up to X bits,
where X is the number after "f" in the data type.
bool Boolean value that can be either false or true.
char Character value that stores the data for a single
character.
3.3 Reference Types
Data Type Description
String A sequence of characters.
Example:
S t ri n g x := " h e l l o " ;
Vec<T> A dynamic, homogeneous array of values of type
T, which can be any type.
Example:
Vec<i 3 2> nums1 := [ 0 , 0 , 7 ] ;
mut Vec<i 3 2> nums2 := new Vec<i 3 2 >(2) ;
nums2 [ 0 ] := 4 2;
nums2 [ 1 ] := 9 9;
Tuple A collection of values of different types that is fixed
in size.
Example:
Tuple<i 3 2 , bool , i 3 2> my_tuple := ( 7 ,
f a l s e , 3 ) ;
mut Tuple<S t ri n g , i 3 2> the_ tuple :=
new Tuple<S t ri n g , i 3 2 >() ;
the_ tuple . 0 := "jOSh" ;
the_ tuple . 1 := 2 ;
9
4 Example Programs
4.1 Caesar Cipher Encrypt
1 // Model d e f i n i t i o n f o r the Caesar ci p h e r
2 model C ae sa rCiphe r
3 s t a r t
4 // Encrypt a s t r i n g based on the s h i f t amount
5 e x t f n e n c r y p t (& s e l f , &S t ri n g in_ s t r , i 3 2 shi f t_am t ) −> S t ri n g
6 s t a r t
7 // Get the r e a l s h i f t amount and c r e a t e a new v e c t o r o f c h a r a c t e r s
8 i 3 2 r e a l _ s h i f t := shi f t_amt % 2 6;
9 Vec<char> out_vec := new Vec<char >() ;
10
11 // Loop through the e n t i r e s t r i n g
12 f o r mut i 3 2 i i n r an ge ( 0 , i n_ s t r . l e n ( ) , 1 )
13 s t a r t
14
15 // Get the c h a r a c t e r a t the gi v e n inde x a s an i n t e g e r
16 // ( u8 ) c a s t s the e x p r e s si o n on the r i g h t o f i t t o be an u8
17 mut u8 cur_char := ( u8 ) i n_ s t r . char_at ( i ) ;
18
19 i f cur_char >= 97 && cur_char <= 122
20 s t a r t
21 // Convert the l o w e r c a s e l e t t e r s t o u p p e r c a s e l e t t e r s
22 cur_char := cur_char − 3 2;
23 f i n i s h i f
24
25 // Only modi fy u p p e r c a s e l e t t e r s
26 i f cur_char >= 65 && cur_char <= 90
27 s t a r t
28 // Per form the s h i f t
29 cur_char := cur_char + r e a l _ s h i f t ;
30
31 // This i s the d i f f e r e n c e f o r wraparound f o r Z
32 mut i 3 2 d i f f := cur_char − 9 0;
33 i f d i f f > 0
34 s t a r t
35 // Per form the Z wraparound
36 cur_char := 65 + d i f f − 1 ;
37 e l s e
38 // Now compute the A wraparound i f t h e r e was no Z wraparound
39 d i f f := 65 − cur_char ;
40
41 i f d i f f > 0
42 s t a r t
43 cur_char := 90 − d i f f + 1 ;
44 f i n i s h i f
45 f i n i s h i f
46 f i n i s h i f
47
48 // Get the f i n a l c h a r a c t e r a s the a p p r o p ri a t e type
49 ch a r fi n al_ c h a r := ( ch a r ) cur_char ;
50
51 // Push i t t o the end o f the v e c t o r ( v e c t o r w i l l r e s i z e a s needed )
52 out_vec . push ( fi n al_ c h a r ) ;
53 f i n i s h f o r
54
55 // J oin the el em e n t s o f the v e c t o r t o g e t h e r t o form a s t r i n g
56 // by u si n g the s t r i n g r e p r e s e n t a t i o n o f each o f the el em e n t s
57 // This onl y works i f the s t r i n g c o n c a t e n a ti o n type r u l e i s v ali d ,
58 // which i t i s i n t h i s s c e n a r i o bec au se ch a r i s a p r i m i t i v e type
59 r e t u r n out_vec . j o i n ( "" ) ;
60 f i n i s h e n c r y p t
10
61
62 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
63 s t a r t
64 Cae sa rCiphe r ci p h e r := new C ae sa rCiphe r ( ) ;
65
66 // C re a te a new s t r i n g
67 S t ri n g x := "Kachow" ;
68
69 // Per form e n c r y p t and p a s s an immutable r e f e r e n c e t o x
70 S t ri n g y := ci p h e r . e n c r y p t (&x , 9 5 ) ;
71
72 // Should p ri n t Kachow
73 p r i n t l n ( x ) ;
74 // Should p ri n t BRTYFN
75 p r i n t l n ( y ) ;
76 f i n i s h main
77 f i n i s h model
Listing 4: CaesarCipher.rez
4.2 Caesar Cipher Decrypt
1 model C ae sa rCiphe r
2 s t a r t
3 // Returns a dec r yp ted s t r i n g based on the s h i f t amount
4 e x t f n d e c r y p t (& s e l f , &S t ri n g in_ s t r , i 3 2 shi f t_am t ) −> S t ri n g
5 s t a r t
6 // Decrypt i s the same a s e n c r y p t with n e g a ti v e s h i f t amount
7 // Encrypt i s d e fi n e d i n S e c ti o n 4. 1
8 r e t u r n s e l f . e n c r y p t ( in_ s t r , −shi ft_am t ) ;
9 f i n i s h e n c r y p t
10
11 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
12 s t a r t
13 Cae sa rCiphe r ci p h e r := new C ae sa rCiphe r ( ) ;
14
15 S t ri n g x := "Kachow" ;
16 S t ri n g y := ci p h e r . e n c r y p t (&x , 9 5 ) ;
17 S t ri n g z := ci p h e r . d e c r y p t (&y , 9 5 ) ;
18 // Kachow
19 p r i n t l n ( x ) ;
20 // BRTYFN
21 p r i n t l n ( y ) ;
22 // KACHOW
23 p r i n t l n ( z ) ;
24 f i n i s h main
25 f i n i s h model
Listing 5: CaesarCipher.rez (enhanced)
4.3 Factorial
1 // D e fi n e a model f o r the f a c t o r i a l program
2 model F ac t o ri alP r o g r am
3 s t a r t
4 // Return the r e s u l t o f f a c t o r i a l (num)
5 e x t f n f a c t o r i a l (& s e l f , i 3 2 num) −> i 3 2
6 s t a r t
7 i f num <= 0
8 s t a r t
9 // f a c t o r i a l ( 0 ) = 1
11
10 // and any thin g < 0 i s i n v ali d , s o r e t u r n 1
11 r e t u r n 1 ;
12 e l s e
13 // f a c t o r i a l (n) = n ∗ f a c t o r i a l (n − 1 )
14 r e t u r n num ∗ s e l f . f a c t o r i a l (num − 1 ) ;
15 f i n i s h i f
16 f i n i s h f a c t o r i a l
17
18 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
19 s t a r t
20 F ac t o ri alP r o g r am f p := new F ac t o ri alP r o g r am ( ) ;
21 // Should be 120
22 p r i n t l n ( f p . f a c t o r i a l ( 5 ) ) ;
23
24 // Both sh ould be 1
25 p r i n t l n ( f p . f a c t o r i a l ( 0 ) ) ;
26 p r i n t l n ( f p . f a c t o r i a l (−1) ) ;
27 f i n i s h main
28 f i n i s h model
Listing 6: FatorialProgram.rez
4.4 Quicksort
1 // Import the Random model f o r u se l a t e r
2 import s t d . u t i l . Random ;
3
4 // D e fi n e a model f o r the c l a s s i c S o r t sA n d S h u f fl e s f i l e from al g o ri t hm s
5 model S o r t sA n d S h u f fl e s
6 s t a r t
7 // This i s the wrapper f u n c ti o n f o r the q u i c k s o r t implemen t a ti on
8 e x t f n q u i c k s o r t (& s e l f , &mut Vec<i 3 2> data ) −> v oid
9 s t a r t
10 // C all q u i c k s o r t on the e n t i r e v e c t o r
11 s e l f . q ui c k s o r t_wi t h_i n di c e s ( data , 0 , data . l e n ( ) − 1 ) ;
12 f i n i s h q u i c k s o r t
13
14 // Helpe r q u i c k s o r t f u n c ti o n
15 // P ri v a t e bec au se onl y a c c e s s i b l e wi t hi n the model
16 f n q ui c k s o r t_wi t h_i n di c e s (& s e l f , &mut Vec<i 3 2> data , i 3 2 s t a r t_inde x , i 3 2 end_index ) −>
v oid
17 s t a r t
18 // Recu r si on b a se c a s e t o end i f we have an a r r a y o f s i z e 0 o r 1
19 i f s t a r t_i n d e x >= end_index
20 s t a r t
21 r e t u r n ;
22 f i n i s h i f
23
24 // D e cl a r e a pi v o t inde x t h a t w i l l be changed i n a few l i n e s
25 mut i 3 2 piv o t_index := 0 ;
26
27 i f end_index − s t a r t_i n d e x < 3
28 s t a r t
29 // Piv o t inde x i s the s t a r t inde x bec au se w i l l need one more l e v e l
30 // o f r e c u r s i o n r e g a r d l e s s o f the pi v o t
31 piv o t_index := s t a r t_i n d e x ;
32 e l s e
33 // O the rwi se d e c l a r e a new random o b j e c t
34 Random ran := new Random ( ) ;
35
36 // Get the f i r s t pi v o t o p ti o n
37 i 3 2 pivot_choice_1 := ran . r a n d I n t ( s t a r t_inde x , end_index + 1 ) ;
38
12
39 // Get the sec ond pi v o t op ti on , but onl y u se i t i f d i f f e r e n t from
40 // the f i r s t
41 mut i 3 2 pivot_choice_2 := ran . r a n d I n t ( s t a r t_inde x , end_index + 1 ) ;
42 w hil e pivot_choice_2 == pivot_choice_1
43 s t a r t
44 pivot_choice_2 := ran . r a n d I n t ( s t a r t_inde x , end_index + 1 ) ;
45 f i n i s h w hil e
46
47 // Get the t hi r d pi v o t o p ti o n but onl y u se i t i f d i f f e r e n t from
48 // both o f the o t h e r o p ti o n s
49 mut pivot_choice_3 := ran . r a n d I n t ( s t a r t_inde x , end_index + 1 ) ;
50 w hil e pivot_choice_3 == pivot_choice_1 | | pivot_choice_3 == pivot_choice_2
51 s t a r t
52 pivot_choice_3 := ran . r a n d I n t ( s t a r t_inde x , end_index + 1 ) ;
53 f i n i s h w hil e
54
55 // Get the median o f the pi v o t s and s e t the a p p r o p ri a t e pi v o t inde x
56 i f data [ pivot_choice_1 ] <= data [ pivot_choice_2 ] && data [ pivot_choice_1 ] >= data [
pivot_choice_3 ]
57 s t a r t
58 piv o t_index := pivot_choice_1 ;
59 e l s e i f data [ pivot_choice_1 ] <= data [ pivot_choice_3 ] && data [ pivot_choice_1 ] >=
data [ pivot_choice_2 ]
60 piv o t_index := pivot_choice_1 ;
61 e l s e i f data [ pivot_choice_2 ] <= data [ pivot_choice_1 ] && data [ pivot_choice_2 ] >=
data [ pivot_choice_3 ]
62 piv o t_index := pivot_choice_2 ;
63 e l s e i f data [ pivot_choice_2 ] <= data [ pivot_choice_3 ] && data [ pivot_choice_2 ] >=
data [ pivot_choice_1 ]
64 piv o t_index := pivot_choice_2 ;
65 e l s e
66 piv o t_index := pivot_choice_3 ;
67 f i n i s h i f
68 f i n i s h i f
69
70 // Per form the p a r t i t i o n
71 i 3 2 p a r ti ti o n_ o u t := s e l f . p a r t i t i o n ( data , s t a r t_inde x , end_index , piv o t_index ) ;
72
73 // Per form q u i c k s o r t on the 2 s i d e s o f the p a r t i t i o n
74 s e l f . q ui c k s o r t_wi t h_i n di c e s ( data , s t a r t_inde x , p a r ti ti o n_ o u t − 1 ) ;
75 s e l f . q ui c k s o r t_wi t h_i n di c e s ( data , p a r ti ti o n_ o u t + 1 , end_index ) ;
76 f i n i s h q ui c k s o r t_wi t h_i n di c e s
77
78 // D e cl a r e a p r i v a t e f u n c ti o n f o r s o r t i n g the a r r a y t h a t r e t u r n s the inde x
79 // f o r the p a r t i t i o n
80 f n p a r t i t i o n (& s e l f , &mut Vec<i 3 2> data , i 3 2 s t a r t_inde x , i 3 2 end_index , i 3 2 piv o t_index )
−> i 3 2
81 s t a r t
82 // Move the pi v o t t o the end o f the a r r a y
83 i 3 2 pi v o t := data [ piv o t_index ] ;
84 data [ piv o t_index ] := data [ end ] ;
85 data [ end ] := pi v o t ;
86
87 // Keep t r a c k o f where the low p a r t i t i o n s t a r t s
88 mut i 3 2 l a s t_l ow_ p a r ti ti o n_i n d e x := s t a r t_i n d e x − 1 ;
89
90 // Go through the sub a r ray , e x cl u di n g the l a s t inde x bec au se the pi v o t
91 // v al u e i s i n the end inde x
92 f o r mut i i n r an ge ( s t a r t_inde x , end_index , 1 )
93 s t a r t
94 i f data [ i ] < pi v o t
95 s t a r t
96 // Make sp ac e f o r the low v al u e
97 l a s t_l ow_ p a r ti ti o n_i n d e x := l a s t_l ow_ p a r ti ti o n_i n d e x + 1 ;
98
13
99 // Move i t t o i t s new s p o t i n the a r r a y
100 i 3 2 temp := data [ i ] ;
101 data [ i ] := data [ l a s t_l ow_ p a r ti ti o n_i n d e x ] ;
102 data [ l a s t_l ow_ p a r ti ti o n_i n d e x ] := temp ;
103 f i n i s h i f
104 f i n i s h f o r
105
106 // Move the pi v o t t o the a p p r o p ri a t e l o c a t i o n
107 data [ end ] := data [ l a s t_l ow_ p a r ti ti o n_i n d e x + 1 ] ;
108 data [ l a s t_l ow_ p a r ti ti o n_i n d e x + 1 ] := pi v o t ;
109
110 // Return the l o c a t i o n o f the pi v o t t o d i s t i n g u i s h the 2 p a r t i t i o n s
111 r e t u r n l a s t_l ow_ p a r ti ti o n_i n d e x + 1 ;
112 f i n i s h p a r t i t i o n
113
114 // Knuth s h u f f l e
115 e x t f n k n u t h_ s h u f fl e (& s e l f , &mut Vec<i 3 2> data ) −> v oid
116 s t a r t
117 // C re a te the random o b j e c t
118 Random ran := new Random ( ) ;
119
120 // I t e r a t e through the e n t i r e a r r a y
121 f o r mut i 3 2 i i n r an ge ( 0 , data . l e n ( ) , 1 )
122 s t a r t
123 // Get a random inde x
124 i 3 2 swap_index := ran . r a n d I n t ( 0 , data . l e n ( ) ) ) ;
125
126 // Swap the 2 el em e n t s
127 i 3 2 temp := data [ i ] ;
128 data [ i ] := data [ swap_index ] ;
129 data [ swap_index ] := temp ;
130 f i n i s h f o r
131 f i n i s h k n u t h_ s h u f fl e
132
133 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
134 s t a r t
135 // I n i t i a l i z e a v e c t o r with i n i t i a l c a p a ci t y f o r 10 el em e n t s
136 mut Vec<i 3 2> my_arr := new Vec<i 3 2 >(10) ;
137 // F i l l the v e c t o r with v al u e s 0 − 9
138 f o r mut i 3 2 i i n r an ge ( 0 , 1 0 , 1 )
139 s t a r t
140 my_arr [ i ] := i ;
141 f i n i s h f o r
142
143 S o r t sA n d S h u f fl e s s a s := new S o r t s A n d S h u f fl e s ( ) ;
144
145 // S h u f f l e the v e c t o r
146 s a s . k n u t h_ s h u f fl e (&mut my_arr ) ;
147 // Can a l s o do p r i n t l n (my_arr ) , but adding the t o_ s t ri n g c a l l makes the code c l e a r e r
148 p r i n t l n (my_arr . t o_ s t ri n g ( ) ) ;
149
150 // S o r t the v e c t o r
151 s a s . q u i c k s o r t (&mut my_arr ) ;
152 p r i n t l n (my_arr . t o_ s t ri n g ( ) ) ;
153 f i n i s h main
154 f i n i s h model
Listing 7: SortsAndShuffles.rez
4.5 Selection Sort
1 // Import random f o r the Knuth s h u f f l e d e fi n e d i n S e c ti o n 4. 4
2 import s t d . u t i l . Random ;
14
3
4 model S o r t sA n d S h u f fl e s
5 s t a r t
6 // C re a te a p u bli c f u n c ti o n f o r d oin g a s e l e c t i o n s o r t
7 e x t f n s e l e c t i o n _ s o r t (& s e l f , &mut Vec<i 3 2> data ) −> v oid
8 // Loop through a l l but the l a s t elemen t bec au se an a r r a y o f l e n g t h 1
9 // i s al r e a d y s o r t e d
10 f o r mut i 3 2 i i n r an ge ( 0 , data . l e n ( ) − 1 , 1 )
11 s t a r t
12 // Assume f i r s t elemen t i s s m a l l e s t
13 // Can d i r e c t l y a s s i g n h e r e bec au se i 3 2 i s a v al u e type , s o the v al u e
14 // g e t s s t o r e d r a t h e r than the a c t u al r e f e r e n c e
15 mut i 3 2 sm all e s t_i n d e x := i ;
16
17 // Go through the r e s t o f the a r r a y
18 f o r mut i 3 2 j i n r an ge ( i + 1 , data . l e n ( ) , 1 )
19 s t a r t
20 // I f we have a sm all e r element , s a ve the new l ow e r inde x
21 i f data [ sm all e s t_i n d e x ] > data [ j ]
22 s t a r t
23 sm all e s t_i n d e x := j ;
24 f i n i s h i f
25 f i n i s h f o r
26
27 // Move the s m a l l e s t elemen t i n pl a c e
28 i 3 2 temp := data [ i ] ;
29 data [ i ] := data [ sm all e s t_i n d e x ] ;
30 data [ sm all e s t_i n d e x ] := temp ;
31 f i n i s h f o r
32 f i n i s h s e l e c t i o n _ s o r t
33
34 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
35 s t a r t
36 // From S e c ti o n 4. 4
37 mut Vec<i 3 2> my_arr := new Vec<i 3 2 >(10) ;
38 f o r mut i 3 2 i i n r an ge ( 0 , 1 0 , 1 )
39 s t a r t
40 my_arr [ i ] := i ;
41 f i n i s h f o r
42
43 S o r t sA n d S h u f fl e s s a s := new S o r t s A n d S h u f fl e s ( ) ;
44
45 // S h u f f l e ( from S e c ti o n 4 . 4 )
46 s a s . k n u t h_ s h u f fl e (&mut my_arr ) ;
47 p r i n t l n (my_arr . t o_ s t ri n g ( ) ) ;
48
49 // S o r t
50 s a s . s e l e c t i o n _ s o r t (&mut my_arr ) ;
51 p r i n t l n (my_arr . t o_ s t ri n g ( ) ) ;
52 f i n i s h main
53 f i n i s h model
Listing 8: SortsAndShuffles.rez (enhanced)
4.6 Object-Oriented Programming With Transformers
1 model Owner
2 s t a r t
3 s p e c s
4 s t a r t
5 // Every owner has a name
6 e x t S t ri n g name ;
7 f i n i s h s p e c s
15
8
9 // D e fi n e a new owner with the gi v e n name
10 e x t f n Owner ( S t ri n g my_name)
11 s t a r t
12 s e l f . name := my_name;
13 f i n i s h Owner
14 f i n i s h model
Listing 9: Owner.rez
1 import g a r a ge . Owner ;
2
3 model T rans fo rme r
4 s t a r t
5 s p e c s
6 s t a r t
7 e x t S t ri n g name ;
8 e x t S t ri n g car_name ;
9 i 3 2 power ;
10 &T rans fo rme r l e a d e r ;
11 &Owner owner ;
12 f i n i s h s p e c s
13
14 // D e fi n e a c o n s t r u c t o r f o r the T ran s fo rme r
15 e x t f n T rans fo rme r ( S t ri n g my_name, S t ri n g my_car_name , i 3 2 power )
16 s t a r t
17 s e l f . name := my_name;
18 s e l f . car_name := my_car_name ;
19 s e l f . power := power ;
20
21 // Leader and owner have t o be s e t by s e t t e r s , s o n u l l a t f i r s t
22 // Also f o l l o w the r u l e t h a t a l l s p e c s a r e i n i t i a l i z e d i n the c o n s t r u c t o r
23 s e l f . l e a d e r := n u l l ;
24 s e l f . owner := n u l l ;
25 f i n i s h T ran s fo rme r
26
27 // Returns the d i f f e r e n c e i n power when a t t a c ki n g an o the r t r a n s f o rm e r
28 // > 0 means win
29 // < 0 means l o s s
30 // == 0 means draw
31 e x t f n a t t a c k (& s e l f , &T rans fo rme r opponent ) −> i 3 2
32 s t a r t
33 mut i 3 2 p owe r_di f f := s e l f . power − opponent . get_power ( ) ;
34
35 &T rans fo rme r opponent_leader := opponent . g e t_l e a d e r ( ) ;
36 i f opponent_leade r != n u l l
37 s t a r t
38 // We w i l l say the l e a d e r h el p s out i f t h e i r comrade i s b ei n g a t t a c k e d
39 p owe r_di f f := p owe r_di f f − opponent_leader . get_power ( ) ;
40 f i n i s h i f
41
42 r e t u r n p owe r_di f f ;
43 f i n i s h a t t a c k
44
45 // I n c r e a s e s the power o f the t r a n s f o rm e r by the f a c t o r
46 e x t f n s u p e r c h a r g e (&mut s e l f , i 3 2 f a c t o r )
47 s t a r t
48 s e l f . power := s e l f . power ∗ f a c t o r ;
49 f i n i s h s u p e r c h a r g e
50
51 // D e fi n e a g e t t e r f o r the power s p e c
52 e x t f n get_power(& s e l f ) −> i 3 2
53 s t a r t
54 r e t u r n s e l f . power ;
55 f i n i s h get_power
16
56
57 // D e fi n e a g e t t e r f o r the l e a d e r s p e c
58 e x t f n g e t_l e a d e r (& s e l f ) −> &T rans fo rme r
59 s t a r t
60 r e t u r n s e l f . l e a d e r ;
61 f i n i s h g e t_l e a d e r
62
63 // D e fi n e a s e t t e r f o r the l e a d e r
64 e x t f n s e t_l e a d e r (&mut s e l f , &T rans fo rme r new_leader )
65 s t a r t
66 s e l f . l e a d e r := new_leader ;
67 f i n i s h s e t_l e a d e r
68
69 // D e fi n e a s e t t e r f o r the owner
70 e x t f n set_owner(&mut s e l f , &Owner new_owner )
71 s t a r t
72 s e l f . owner := new_owner ;
73 f i n i s h set_owner
74 f i n i s h model
Listing 10: Transformer.rez
1 import g a r a ge . T rans fo rme r ;
2
3 // Autobot i s a s u b c l a s s /model o f T rans fo rme r
4 model Autobot e x t e n d s T rans fo rme r
5 s t a r t
6 e x t f n Autobot ( S t ri n g my_name, S t ri n g my_car_name , i 3 2 power )
7 s t a r t
8 // All s p e c s a r e i n i t i a l i z e d i n the supe r c o n s t r u c t o r , s o n o thin g
9 // need s t o be e x p l i c i t l y i n i t i a l i z e d h e r e
10 supe r (my_name, my_car_name , power ) ;
11 f i n i s h Autobot
12
13 // Function t o " r o l l out "
14 e x t f n r oll_ o u t (& s e l f )
15 s t a r t
16 p r i n t l n ( " R oll ␣ out : ␣" + s e l f . name ) ;
17 f i n i s h r oll_ o u t
18 f i n i s h model
Listing 11: Autobot.rez
1 import g a r a ge . Owner ;
2 import g a r a ge . T rans fo rme r ;
3 import g a r a ge . Autobot ;
4
5 model MainTrans formers
6 s t a r t
7 e x t f n main (Vec<S t ri n g > a r g s ) −> v oid
8 s t a r t
9 // C re a te a new au t ob o t f o r Bumblebee
10 mut Autobot bumblebee := new Autobot ( "Bumblebee" , "Chevy␣Camaro" , 5 0 0 ) ;
11
12 // Give Bumblebee an owner
13 Owner sam := new Owner ( "Sam␣Witwicky" ) ;
14 // This method i s a c c e s s i b l e bec au se Autobot e x t e n d s T rans fo rme r
15 bumblebee . set_owner(&sam ) ;
16
17 Autobot optimus_prime := new Autobot ( "Optimus␣Prime" , "Truck" , 1 0 0 0 ) ;
18 // Give a r e f e r e n c e t o Optimus Prime f o r Bumblebee ’ s l e a d e r
19 bumblebee . s e t_l e a d e r (&optimus_prime ) ;
20
21 // C re a te Megatron
17
22 T rans fo rme r megatron := new T rans fo rme r ( "Megatron" , "Tank" , 2 0 0 0 ) ;
23
24 // This w i l l be 500 bec au se Bumblebee g e t s a s s i s t a n c e from Optimus Prime
25 // but t h e i r combined power i s t o o low
26 p r i n t l n ( megatron . a t t a c k (&bumblebee ) ) ;
27
28 // Should be −1000 bec au se the d i f f e r e n c e i n t h e i r power i s 1000 and
29 // Megatron has no l e a d e r
30 p r i n t l n ( optimus_prime . a t t a c k (&megatron ) ) ;
31
32 // M ul ti pl y Bumblebee ’ s power by a f a c t o r o f 3
33 bumblebee . s u p e r c h a r g e ( 3 ) ;
34
35 // This w i l l be −500 bec au se Bumblebee i s now a t 1500 power
36 // pl u s Optimus Prime ’ s 1000 power
37 p r i n t l n ( megatron . a t t a c k (&bumblebee ) ) ;
38
39 // Optimus Prime has the f u n c ti o n bec au se he i s an Autobot
40 // Megatron d oe s not bec au se i t i s onl y d e fi n e d i n the Autobot model
41 optimus_prime . r oll_ o u t ( ) ;
42 f i n i s h main
43 f i n i s h model
Listing 12: MainTransformers.rez
18

More products