int, real, bool, char, ptr, enum, cap, file
type color = enum(red, yellow, green, blue)
var i : int, x : real, col : color
const WIDTH = 80
var name : string[20]
var age[20] : int
type entry = rec(height : int; weight : real; next : ptr entry)
# makes a comment to end-of-line
/* C-style comment */
variable := expression
variable++
variable--
variable :=: variable
< <= > >= = !=
and or not
do condition1 ->
statement(s)
[] condition2 ->
statement(s)
[] ... ->
od
fa index_variable := lower to upper ->
statement(s)
af
fa index_variable := upper downto lower by -3 ->
statement(s)
af
do not use a var declaration for index_variable
next, exit
like C continue, break
if condition1 ->
statement(s)
fi
if condition1 ->
statement(s)
[] condition2 ->
statement(s)
[] ... ->
statement(s)
[] else ->
statement(s)
fi
read(a, b, c, ...)
write("a=", a, "b=", b, ...)
writes("a = ", a, ", b = ", b, ..., "\n")
from C: scanf, printf
procedure abc(x : int; y: real; ...)
const ...
var ...
statement(s)
end abc
procedure def(p : int; q: real; ...) returns r : char
const ...
var ...
statement(s)
r := ...
statement(s)
end abc
pass by: val, var,
ref, res
resource name()
type ...
const ...
var ...
procedure ...
const ...
var ...
statement(s)
end ...
statement(s)
end name
sr -o file file.sr
file arg1 arg2 arg3 ...
getarg(i, variable)
numargs()
int(x) real(x) abs(x) max(a,b,c,...) min(a,b,c,...)
low(type) high(type)
lb(x,i) ub(x,i) length(x) maxlength(x)
sqrt(x) log(x,b) exp(x,b) sin(x) ceil(x) floor(x) round(x)
random() random(ub) random(lb,ub)
An elementary one-resource SR example.
Note that write generates a linefeed but that writes
does not.
Note that returns turns a procedure into a function.
SR Program: Compute Factorials.
Shows how to pass an array to a procedure by reference and how to indicate
an upperbound in the procedure's formal parameter that is not known until
the procedure is invoked, at which time the builtin function ub()
can be used to get it.
Note the upper bound of the array nums in the main code
is not known until runtime, i.e., dynamic storage allocation.
A for-all loop with a st (such that) clause is shown;
the :=: (swap) is illustrated.
SR Program: Sort Numbers.
This program illustrates const, strings, a two dimensional
array, how to read to end-of-file (EOF), ++ (post-increment),
and stop.
SR Program: Fiddle with Strings.
Here we see how to initialize variables in their declaration,
the remainder operator %, an else clause in an
if statement, and how to use getarg(i,n).
The latter tries to read the i-th command line argument and put its
value into the variable n.
If there is no i-th command line argument, then the variable n is left
unchanged and getarg returns EOF;
if the i-th command line argument cannot be converted into a value of the
same type as n, then n is left alone and getarg
returns 0.
SR Program: ``Wondrous'' Numbers.
SR allows recursion, as we see here.
Note that the variables N and solutions
are global to the procedures.
A return can be used for explicit procedure return (flowing into
the end keyword is an implicit return).
SR Program: N Queens Problem.
For those budding Java jockeys among you, here is the same N queens program
rewritten in Java, pretty much a direct translation.
Included is an example compile and run, and a timing comparison
to the SR program.
Note that although Java is interpreted, its running time for N queens
is competitive with SR.
Java Program: N Queens Problem.
Another Java example: a sequential program that computes prime numbers
by crossing out multiples (Sieve of Eratosthenes).
If you write and run the corresponding SR program, you will find that
here Java takes about 2.5 times as much CPU time (N = 1,000,000 or 10,000,000).
Java Program: Generating Prime Numbers.
A more detailed example of how to use getarg.
SR Program: Processing Command Line Arguments.
How to do file IO.
SR Program: File Input and Output.
How to read a mixture of strings and integer values in the input more
easily with scanf than with read.
SR Program: Parsing Lines of Names with scanf.
How to do exponentiation, including an error condition.
SR Program: Power Operator **.
SR does not convert reals to integers automatically in an assignment
statement like C does.
SR Program: Converting between Real and Integer.
Yes, we can do mutual recursion by declaring the procedures differently.
SR Program: Mutual Recursion is Allowed.
C hackers know about getopt to process command line arguments.
Well, guess what, SR has its own version.
SR Program: SRgetopt Helps Process Command Line Options.
If you are suffering withdrawal from C while writing SR programs, you can
code some of your program in C and call it from the SR driver code.
But you cannot do this in any of my classes without prior written approval.
User-Written C Language Procedure.
SR Program: Accessing C Language Procedures.
Passing a two dimensional array to a procedure when the lower bound is
not one can lead to problems.
SR Program: Passing Arrays to Procedures.
A string and an arrays of characters can be read and written as a unit,
but an array of integers cannot.
SR Program: Reading and Writing Arrays.
The return value of a function can be an array or a record.
SR Program: A Function Can Return an Array.
A field of a record can be an array but the array bounds must be know at
compile time.
SR Program: A Field of a Record Can Be an Array.
resource object resource driver()
op interface1(... import object
op interface2(... var ocap : cap object
op ... ...
body object(...) ocap := create object(...)
type ... ...
const ... ocap.interface1(...)
var ... ...
ocap.interface2(...)
proc interface1(... ...
const ... end driver
var ...
statement(s)
end interface1
proc interface2(...
const ...
var ...
statement(s)
end interface2
proc ...
end ...
procedure ... # local to object resource
end ...
end name
factorial resource is an object that implements the
fact operation.
Another two-resource SR program.
Shows exit and next (like C's break
and continue).
Uses the quicksort sorting algorithm, which is faster than the exchange sort
used in the earlier example.
SR Program: Two Resource Sort Program.
Shows how to create several resources and pass capabilities to them so
they can access operations in each other.
We also see that resources can be parameterized and can have values
(even if not known until runtime) passed to them when created.
SR Program: Multiple Resources.
The final block can be used for clean-up code.
This example shows how it works and when it is invoked.
SR Program: Resources with final Blocks.
Global variables, constants, and types can be shared by several resources
using a global resource to contain the variables, constants,
and types.
The global resource is created once implicitly and cannot be parameterized.
SR Program: A global Resource.
A resource can be created several times, with different parameters, so we have
several instances of the resource with a capability for each instance.
SR Program: A Parameterized Resource Created Multiple Times.
A linked list example with user-defined types, pointers, an enumeration
type.
A new node in the linked list is created with new and
destroyed with free.
SR Program: User-Defined Types, Records, Pointers, and a Linked List.
A non-obvious technique to get dynamically sized arrays in the public part
of a global resource.
SR Program: Dynamically Sized Arrays in a global Resource.
Here is a much better way, using nested globals,
to get dynamically sized arrays in the public part of a global resource.
This also shows that the assignment operator :=
can be used with arrays.
(The same technique does not work for arrays of semaphores;
we haven't covered semaphores yet.)
SR Program: Dynamically Sized Arrays using Nested globals.
How to work with part of an array, called a slice,
and pass it by var, by val, but not by ref.
SR Program: Arrays Can Be Sliced.
resource main()
type ...
const ...
var ...
process P1
...
end P1
process P2
...
end P2
...
end main
abbreviation: process Pi(i := 1 to N)
An easy way to create many processes running the same code.
SR Program: Multiple Processes in One Resource.
A more elaborate version of the above where the processes nap to simulate
some activity.
SR Program: A Multiple-Process Simulation.
resource worker(...) resource driver()
import worker
process work var wcap[N] : cap worker
... ...
end work fa i := 1 to N ->
wcap[i] := create worker(...)
end worker af
...
end driver
Each process is in a resource and the resource is created multiple times.
This is a template for many of our OS classical problem simulation programs.
Everything you ever wanted to know about final blocks and
resources but were afraid to ask.
SR Program: Resources with Processes and final Blocks.
var machine_name : string[64]
var machine_cap : cap vm
machine_cap := create vm() on machine_name
...
create worker(...) on machine_cap
This shows how to run processes on all the machines in the department
and tie them up.
Just don't say where you learned how to do this.
SR Program: Distributed Multiple Processes on a LAN.
You cannot pass by reference from one virtual machine to another;
unfortunately, the compiler will not scold you.
SR Program: Passing by Reference to a Virtual Machine is an Error.
SR_TRACE environment variable to stdout
or the name of a file will produce some execution tracing output.
Nevertheless, your best debugging friend is still printf.
Shows how the send the output of different processes to different files
rather than seeing it all intermixed in one window.
SR Program: Sending Each Process Output to a Different File.
sr -o simulation simulation.sr
xrdb -merge xtango.res
simulation | animator
Initial XTANGO Animation Window.
Larger PostScript version of above.
Shows how to put XTANGO commands embedded in print statements into a program.
SR Program: Animated Sorting with XTANGO.
File Containing Animation Commands. You should just see the file contents (no external viewer spawned).
Snapshot of the animation.
Follow this link and
the external viewer, XTANGO's animator,
should be spawned if you have followed the
steps above.
XTANGO Animation of Sorting.
SJH shartley@mcs.drexel.edu