-- Try It Online Occam Emulator
-- https://tio.run/#
-- https://tio.run/#occam-pi
-- INMOS Limited occam 2 Reference Manual (highly recommended)
-- https://www.researchgate.net/publicat...
-- Transputer occam more PROCedures - call()
-- Too much inline code?
-- Missing calls? Shared memory?
-- Here's soemthing for you.
-- PROCedure inside a PROCedure
-- And a FUNCTION to boot!
#INCLUDE "course.module"
PROC callme (CHAN BYTE out)
[19]BYTE string1: -- These variables are defined as far to the left and top as possible and have the widest scope
[13]BYTE string2: -- As close as you are going to get to sorta "global" variables for a PROCedure
INT length:
REAL32 sales.tax.rate:
REAL32 cost1, cost2:
TIMER clock:
INT now, delay:
-- Reference Manual, Section 11, FUNCTIONS, p. 65
-- FUNCTIONS are side effect free, just values passed in and out
INT FUNCTION plus.one (VAL INT r) -- FUNCTION, VAL INT r is the input, the INT in front of FUNCTION indicates the type of RESULT (output)
INT answer:
VALOF
SEQ
answer := r + 1
RESULT answer
: -- end of FUNCTION plus.one
PROC print.string (VAL []BYTE vector, INT size) -- VAL []BYTE vector is the input, INT size is returned,
SEQ -- you have to be very careful about the scope of variables
size := SIZE vector -- VAL doesn't have to be listed first
SEQ i = 0 FOR size -- output characters
out ! vector[i]
: -- end of PROCedure print.string
-- Reference Manual, Section 10, Procedures, p. 61, e.g. next.size examples
-- This process has the side effect of producing a carriage return message
-- You pass it nothing and it returns nothing
PROC crlf () -- PROCedure out ! carriage return, line feed
SEQ
out ! '*c' -- CHANNEL BYTE out
out ! '*n'
: -- end of PROCedure crlf
PROC space () -- out ! space
SEQ
out ! ' '
: -- end of PROCedure space
PROC total.price (VAL REAL32 price, REAL32 total)
SEQ -- REAL32 tends to be X.XX or 1.23E+16 not X or .XX
total := price * (1.0 + sales.tax.rate) -- note that sales.tax.rate is not passed in or a message
: -- end of PROCedure total.price -- it is within the scope of total.price, call.me and total.price have shared common memory
SEQ -- PROC callme
string1 := "Life Is So Peculiar" -- size must match declaration
string2 := "Did a Houdini"
sales.tax.rate := 0.04
delay := 100
print.string(string1, length) -- string1 is output, length = SIZE string1
crlf ()
out.string("size =", 0, out)
space ()
out.int (length, 0, out)
crlf ()
print.string(string2, length) -- PROCedure
crlf ()
out.string("size = ", 0, out)
out.int (length, 0, out)
crlf ()
out.string("5 + 2 = ", 0, out)
length := plus.one(plus.one(5)) -- FUNCTION (recursive)
out.int (length, 0, out)
crlf ()
cost1 := 987.65
out.string ("price = ", 0, out)
out.real32(cost1, 10, 2, out) -- 11 digits before decimal point, 2 digits after decimal point
crlf ()
out.string ("tax = ", 0, out)
out.real32(cost1*sales.tax.rate, 10, 2, out)
crlf ()
out.string ("----------------------------", 0, out)
crlf ()
clock ? now -- Coming Attractions!
clock ? AFTER now PLUS delay
total.price(cost1, cost2) -- note that sales.tax.rate is NOT passed as an argument or sent as a message
out.string ("total price = ", 0, out) -- it is a value within the scope of total.price
out.real32(cost2, 10, 2, out)
crlf () -- exponentials are different
: -- end of PROCedure callme
-- Two PROCesses talking to each other
-- communicationp
#INCLUDE "course.module"
PROC sendit (CHAN OF BYTE in1, commsone, commstwo, out1)
BYTE x:
SEQ
in1 ? x
commsone ! x
commstwo ? x
out1! x
: -- End first PARallel process sendit
-- Begin second PARallel process, returnit
PROC returnit (CHAN BYTE commsone, commstwo)
BYTE x:
SEQ
commsone ? x
x := x + 1
commstwo ! x
: -- End second PARallel process returnit
PROC communicationp (CHAN BYTE in1, out1)
CHAN BYTE commsonea, commstwoa: -- communication channels between Process 1 and Process 2
PAR
sendit(in1, commsonea, commstwoa, out1)
returnit(commsonea, commstwoa)
: -- end of PROC communicationp
Информация по комментариям в разработке